Avoid Overly Complicated Code by Stopping Scripts Early

The if construct is commonly used within PHP. It lets us execute blocks of code based on certain condition(s) being met. We can even layer if constructs within if constructs to perform operations that are very specific to the task at hand. While this gives us greater control over how the overall script executes, there may be cases where piling on the if constructs can lead to unnecessary complications.

Background

At some point, most of us have been told to indent code as needed. Indenting code improves readability and helps show how each code block relates to the others. For example, if we display someone's name whenever the variable holding the name is set, our code might look like the following:

<?php
if(isset($name)) {
     print $name;
}
?>

Note how the print statement is indented. The indent shows that the print statement is dependent on the if construct. Multiple levels of indentation may be needed depending on the situation. For example, we may have an if construct within a foreach loop.

<?php
//INITIALIZE VARIABLES
$names = array('John Smith', 'Jake Bible', '', 'Big Timber', 'Gordon Shumway');
 
//DISPLAY NAMES
print '<ul>';
foreach($names as $currName) {
     if($currName != '') {
          print "<li>$currName</li>";
     }
}
print '</ul>';
?>

The Problem

While indenting code improves readability, there's a risk of over complicating scripts. For example, I developed a PHP class to handle the log-in process for a website. To prevent visitors who aren't logged in from viewing a page, the class just needs to be included at the top of the script that generates the page.

Since the class handles everything regarding the log-in process, an if test is used to determine whether the page's script should run or not. If the visitor is logged in the script executes. Otherwise, no further processing is needed.

<?php
//LOAD CLASS TO PROCESS LOGIN
require "{$_SERVER['DOCUMENT_ROOT']}/../class_loginProcess.php";
$login = new login();
 
//IF VISITOR IS LOGGED IN, EXECUTE EVERYTHING ELSE
if($login->loggedIn) {
     //...
}
?>

This may look fairly straight forward, but it's more complicated once the code for everything else is added. Some pages being protected have hundreds, if not thousands, of lines of code—all within the overarching if construct. Of course, the PHP processor has no issues with the code. It's more than happy to skip past all the code when a visitor isn't logged in.

The problem is that I need to remember what each level of indentation represents. This is especially difficult when re-familiarizing myself with a script that I haven't looked at for a long period of time. It also doesn't help that when I'm elbows deep in code, the number of distractions seems to increase. Someone wants to know the status of a project, something needs to be posted online, the fire alarm goes off, etc.

The Solution

Getting rid of this extra level of complexity is simple. Since the rest of the script shouldn't run when the visitor isn't logged in, we could turn to exit.

<?php
//LOAD CLASS TO PROCESS LOGIN
require "{$_SERVER['DOCUMENT_ROOT']}/../class_loginProcess.php";
$login = new login();
 
//IF VISITOR ISN'T LOGGED IN, STOP SCRIPT
if(!$login->loggedIn) {
     exit;
}
 
//EXECUTE EVERYTHING ELSE

?>

Conclusion

Using exit lets us end the script where ever we want. If the visitor isn't logged in, there's no reason for the script to continue. Instead of enclosing the entire page's script with an if construct, we can just use exit. If the visitor is logged in, the rest of the script executes as normal.

0 Comments

There are currently no comments.

Leave a Comment