For the beginners – Creating quality code – Part 2

Welcome back to the series of articles, where we work our way from the scratch to creating an application with quality code. You will learn how to quickly improve your coding habits and style to write easy to understand and manageable code. If you missed the first part of this series, you can use the link below to read it now:

For the beginners – Creating quality code – Part 1

Overview

In the second step, we created a very basic design flow in our minds of the ticket management system. I will copy that here so that we can continue from that part again.

  1. Display Login Screen
  2. Authenticate login request from user or coder. If login is successful, determine login type and show appropriate screens for startup
  3. Allow ticket creation/update functionality if a user has logged in
  4. Allow ticket update/close functionality to coders

As explained in the last article, this application design is in fact more like a ‘declaration of intent’, because it allows us to see on the application level what functionality our code is going to provide (You can keep your own design flow that you made, if you like, before proceeding. Your code will only need minor changes from ours to work correctly).

Let’s start converting our design into real code. Fire up your localhost webserver and create a folder in the document root called ‘tickets’. Create a single file called index.php and open it in your favorite IDE/editor.

Step 3 – Coding the application layer

With index.php open in our editor, we are going to convert our application design flow into php code. The key point to remember here is that once you are done writing the application layer code, you can compare it directly to the design flow you created earlier. The only difference you will notice in the two is that the earlier is written in English, while the latter is in PHP. If that is not the case, then you went wrong somewhere in the way. Review your code and fix it until you achieve the desired code output. Here is how i will code my index.php, based on my design flow.

<?php
  $page = '';
  session_start();
  if (! isset($_SESSION['userid']) )
  {
     if ( isset($_POST['username'] && isset($_POST['password']) &&
        is_valid_user($_POST['username'], $_POST['password']) )
     {
        $userid = get_user_id($_POST['username']);
        $usertype = get_user_type('$_POST['username']);
        $_SESSION['userid'] = $userid;
        $_SESSION['usertype'] = $usertype;
        $page = 'welcome.php';
     }
     else
        $page = 'login.php';
  }
  else
  {
     if ($_SESSION['usertype'] == 'coder')
        $page = 'view_tickets.php';
     else
        $page = 'add_ticket.php';
  }

  include($page);
?>

Ok, that was easy. I have written down my application layer code based on my design flow. I have assumed a few user related function names that I will create later on, and I have checked that my php code and my application design flow in English are both trying to ‘say’ the same thing.

Good to go to next step? Not really. Here’s why:

One of the fundamental flaw in the above code for the application layer is that it’s not really the ‘application layer’ code. You can see it is directly manipulating user login status, which should not be part of the application layer.

Application layer should only show the flow of user’s navigation through the system, and not HOW it would happen.

Based on this observation, I figure I made a mistake while creating my application flow. So I take a step back and try to redesign my application flow, this time keeping in mind that it should look like a real life process and nothing to do with code.

Here’s my second attempt:

  1. Decide which type of user is logged into system
  2. Send coders to their respective page for updating tickets
  3. Send users to their respective page for adding tickets
  4. Send everyone else to login page

Ok, now that I see it, this looks like a better application flow. It does not say how or when the user will be logged in to the system. Now let’s code this application design flow into PHP and see what comes up:

<?php
  $page = '';
  session_start();

  $usertype = get_user_type();
  switch($usertype)
  {
     case 'coder':
        $page = 'view_tickets.php';
        break;
     case 'user':
        $page = 'add_ticket.php';
        break;
     default:
        $page = 'login.php';
  }

  include($page);
?>

This should give you an idea of how to write a good application layer code. You can see there are not a lot of if/else checks, the code is very concise (only 10-20 lines) and looking at it it’s clear what it is the application doing. So to sum it up, when writing the application layer code, remember these points:

  • Do not include any internal process handling code.
  • Make the code as short as possible, so that anyone who reads it can understand the ‘flow’ of the application
  • Avoid if/else checks as much as possible, because the more conditional the code, the more ambiguous is the flow of the application. Use switch statements if necessary to define more than a single path of processing.

This wraps up the second part of this series. We will move onto creating the rest of the application code in the next article. As usual, you are welcome to ask any questions in the comments section about this tutorial.

  • Oakley vault

    The eyewear brand companies Oakley vault sunglasses mainly for women with a couple of designs in the actual mens category. Known because of its colorful and unique sets, Oakley vault are easily recognizable using the embossed logo on the actual temples. The exquisite design is from the frames range from Oakley sunglasses outlet to classic aviator tones, with pastel and earthy colored lenses. The variety of colors the brand offers could be compared with the mood from the Cheap Oakley vault.