February 8, 2016

Mafia Games Monday (MGM) – Code Consistency

So over the years of being a developer and a game owner I have seen A LOT of different code. I know, rather shocking. I have seen code in several different languages, different architectures, and much more.

However, when I begin a project, there is one thing that I almost always see: a lack of code consistency.

What is this, you ask?

Shurg Question

Well, take for example this small block of PHP code to say “Hello ” for each user:

$con = mysqli_connect('host','db_user', 'db_pass', 'db_name') or die("Error ".mysqli_error($con));
$userq = mysqli_query($con, "SELECT * FROM users");
while($useri = mysqli_fetch_array($userq)) {
echo "Hello :" . $useri['username'] . "! <br />";
}

Now that looks pretty simple, doesn’t it? Now, take this example:
$con = mysqli_connect('host','db_user', 'db_pass', 'db_name') or die("Error ".mysqli_error($con));
$uq = mysqli_query($con, "SELECT * FROM users JOIN u_other ON users.uid = u_other.uID JOIN u_inv ON u_other.uID = u_inv.uinv_ID");
while($useri = mysqli_fetch_row($uq)) {
echo "Hello :" . $useri[1] . "! <br />";
}

Now, by looking at that code, do you know what all it’s doing? You might (so kudos to you if you do), but in actuality it is doing the exact same thing as the previous example. Now from a consistency standpoint, there’s nothing wrong with that if these were two separate applications, but if you had both of these in the same application, that wouldn’t make much sense now, would it?

That is what I mean by code consistency – ensure your structure and methods are similar (or the same) throughout the entire application.

Let’s take one more example for good measure. It will be similar code, but look at the differences between this block:

$con = mysqli_connect('host','db_user', 'db_pass', 'db_name') or die("Error ".mysqli_error($con));
$userq = mysqli_query($con, "SELECT * FROM users");
while($useri = mysqli_fetch_array($userq)) {
  if($useri['red'] <> 0) {
    $redq = mysqli_query($con, "SELECT * FROM red_stuff WHERE ruid = {$useri['uid']}");
    if(mysqli_num_rows($redq) > 0) {
      $redu = mysqli_fetch_array($redq);
      echo $useri['username'] . "'s red value is: " . $redu['redval'] . " <br /> ";
    }
    else {
      echo "Error running query for user: " . $useri['username'] . " <br />";
    }
  }
  else {
    echo "No red value for: " . $useri['username'] . " <br />";
  }
}

and this block:

$con = mysqli_connect('host','db_user', 'db_pass', 'db_name') or die("Error ".mysqli_error($con));
$userq = mysqli_query($con, "SELECT * FROM users");
while($useri = mysqli_fetch_array($userq)) {
if($useri['red'] <> 0) {
$redq = mysqli_query($con, "SELECT * FROM red_stuff WHERE ruid = {$useri['uid']}");
if(mysqli_num_rows($redq) > 0) {
$redu = mysqli_fetch_array($redq);
echo $useri['username'] . "'s red value is: " . $redu['redval'] . " <br /> ";
}
else {
echo "Error running query for user: " . $useri['username'] . " <br />";
}
}
else {
echo "No red value for: " . $useri['username'] . " <br />";
}
}

What are the differences that you saw?

Well, if you looked at it very closely, the code itself is exactly the same; however, isn’t the first example a little easier to follow? Keep this in mind when you are writing any sort of code – you want anyone that sees your code to be able to read it.

But I’m getting off-track here. What does this have to do with consistency?

Well again, if you have both of these blocks of code in your application, that’s not only a problem with duplication of code, but also with consistency. It’s difficult to read when the way that they are written are not the same!

Ok, I’m sold that consistency is important. What could I do about it? Where do I start?

Well, the best place to start is to go through the last thing that you have personally written and see what you did there. As a developer, I can tell you that my most recent work is better than my first. That’s true with any industry, really. So anyways, start from there and structure your code based off of that for the rest of your application. If you indent your code between the braces one tab length, then do that throughout – that kind of thing.

Remember – the more consistent your code base is in your application, the easier it will be to change and/or fix!

That’s all for now, until next time…keep that tech gear ticking.

~Goliath7470