Remove Unnecessary Code by Rethinking Your Default Setting
Over the past few years, I've spent a lot of time revamping my old code. I've been doing what I can to simplify, simply, simplify. While going through the code, I started noticing a pattern when it came to setting defaults. Let's take a look at a quick example and see how I was able to remove the unnecessary code.
Background
One page that I found displays staff presentations from a MySQL table like the following:
id | title | presenter | date | location |
---|---|---|---|---|
1 | Web Development 101 | John Smith | 2014-01-01 | Austin, TX |
2 | Responsive Design Pros and Cons | Smithy Peterson | 2014-01-16 | Boulder, CO |
3 | Wi-Fi for the New Age | Jake Bible | 2014-02-17 | Asheville, NC |
When you initially visit the page, it displays a list of presentation titles. Clicking on any of those titles brings you to the same page, but the page now shows the details associated with the presentation. Here is a simplified version of what the code looked like:
<?php
//CONNECT WITH ONLINE DATABASE
require "{$_SERVER['DOCUMENT_ROOT']}/../connect.html";
//INITIALIZE VARIABLES
$presentations = array();
$showList = false;
$showDetails = false;
//GET PRESENTATION LIST
$sql = "SELECT id, title FROM presentations ORDER BY date DESC";
if($result = $mysqli->query($sql)) {
while($row = $result->fetch_assoc()) {
$presentations[$row['id']] = $row['title'];
}
}
//IF SHOWING A PRESENTATION
if(isset($_GET['show'])) {
//IF PASSED ID IS VALID
if(in_array($_GET['show'], array_keys($presentations))) {
//GET PRESENTATION
$sql = "SELECT title, presenter, date, location FROM presentations WHERE id={$_GET['show']} LIMIT 1";
if($result = $mysqli->query($sql)) {
if($row = $result->fetch_assoc()) {
$showDetails = true;
//ELSE...PRESENTATION ENTRY NOT FOUND
} else {
$showList = true;
}
//ELSE...QUERY FAILED
} else {
$showList = true;
}
//ELSE...INVALID ID
} else {
$showList = true;
}
//ELSE...SHOWING THE LIST
} else {
$showList = true;
}
//IF SHOWING A PRESENTATION
if($showDetails) {
print "<div>Title: {$row['title']}</div>";
print "<div>Presenter: {$row['presenter']}</div>";
print "<div>Date: {$row['date']}</div>";
print "<div>Location: {$row['location']}</div>";
//ELSE...IF SHOWING THE LIST
} elseif($showList) {
print '<ul>';
foreach($presentations as $currID=>$currTitle) {
print "<li><a href='?show=$currID'>$currTitle</a></li>";
}
print '</ul>';
}
?>
Change the Default
You may have noticed that $showList flag is set to true in quite a few spots. The code could be made a lot simpler by just changing the default value for $showList.
<?php
//CONNECT WITH ONLINE DATABASE
require "{$_SERVER['DOCUMENT_ROOT']}/../connect.html";
//INITIALIZE VARIABLES
$presentations = array();
$showList = true;
$showDetails = false;
//GET PRESENTATION LIST
$sql = "SELECT id, title FROM presentations ORDER BY date DESC";
if($result = $mysqli->query($sql)) {
while($row = $result->fetch_assoc()) {
$presentations[$row['id']] = $row['title'];
}
}
//IF SHOWING A PRESENTATION
if(isset($_GET['show'])) {
//IF PASSED ID IS VALID
if(in_array($_GET['show'], array_keys($presentations))) {
//GET PRESENTATION
$sql = "SELECT title, presenter, date, location FROM presentations WHERE id={$_GET['show']} LIMIT 1";
if($result = $mysqli->query($sql)) {
if($row = $result->fetch_assoc()) {
$showDetails = true;
}
}
}
}
//IF SHOWING A PRESENTATION
if($showDetails) {
print "<div>Title: {$row['title']}</div>";
print "<div>Presenter: {$row['presenter']}</div>";
print "<div>Date: {$row['date']}</div>";
print "<div>Location: {$row['location']}</div>";
//ELSE...IF SHOWING THE LIST
} elseif($showList) {
print '<ul>';
foreach($presentations as $currID=>$currTitle) {
print "<li><a href='?show=$currID'>$currTitle</a></li>";
}
print '</ul>';
}
?>
Use Else Instead
And in this particular case, the $showList flag could be removed altogether. Since we're always displaying the list of presentation titles unless one of the links is clicked, we could just use an else statement.
<?php
//CONNECT WITH ONLINE DATABASE
require "{$_SERVER['DOCUMENT_ROOT']}/../connect.html";
//INITIALIZE VARIABLES
$presentations = array();
$showDetails = false;
//GET PRESENTATION LIST
$sql = "SELECT id, title FROM presentations ORDER BY date DESC";
if($result = $mysqli->query($sql)) {
while($row = $result->fetch_assoc()) {
$presentations[$row['id']] = $row['title'];
}
}
//IF SHOWING A PRESENTATION
if(isset($_GET['show'])) {
//IF PASSED ID IS VALID
if(in_array($_GET['show'], array_keys($presentations))) {
//GET PRESENTATION
$sql = "SELECT title, presenter, date, location FROM presentations WHERE id={$_GET['show']} LIMIT 1";
if($result = $mysqli->query($sql)) {
if($row = $result->fetch_assoc()) {
$showDetails = true;
}
}
}
}
//IF SHOWING A PRESENTATION
if($showDetails) {
print "<div>Title: {$row['title']}</div>";
print "<div>Presenter: {$row['presenter']}</div>";
print "<div>Date: {$row['date']}</div>";
print "<div>Location: {$row['location']}</div>";
//ELSE...SHOW THE LIST
} else {
print '<ul>';
foreach($presentations as $currID=>$currTitle) {
print "<li><a href='?show=$currID'>$currTitle</a></li>";
}
print '</ul>';
}
?>
Conclusion
While all three methods provide the same result, it can take a lot less code by just putting more thought into the default settings.
0 Comments
There are currently no comments.
Leave a Comment