The Best Programming Habits to Have

The 8 Best Programming Habits All Coders Should Have

“You might not think that programmers are artists, but programming is an extremely creative profession. It’s logic-based creativity. ” — John Romero

One of my favorite shows in recent years has been, Silicon Valley, a comedy-drama involving the journey of a few coders that start a lossless-compression company in Palo Alto. If you’ve watched the series, then you’re aware of the tabs-versus-spaces controversy. Or should I call it a running joke? Richard Hendrix, the founder of the company, is quite serious about implementing tabs as the proper programming habit, ensuring that all his employees follow along as well.

If you’re a seasoned coder, then you get this controversy. Of course, the reasoning behind all this is that a single tab is more efficient, requires less keystrokes and uses less bits, making it nominally better than using spaces. For an engineer like Hendrix, who’s all about compression, saving time and storage space, this makes sense. But tabs don’t look the same across all platforms. The alternative is to use two spaces, which is my preferred method.

But, this parody highlights something really important when it comes to coding. When developing code, ensuring that you’re implementing the right programming habits will not only save you time and confusion down the road, when and if others come into the fray and immerse themselves in your code, it will also be cleaner, more efficient and easier to work with for yourself when revisiting it weeks, months or even years later.


What Are The Best Programming Habits?

I’ve been a coder for nearly two decades. It appeals to the introvert in me. There’s just something about programming and building software that I have always loved. But after nearly two decades of writing code, I’ve definitely identified some of the best habits that help to create an efficient coding environment and produce clean code that’s easily amendable. If you’re a coder yourself, especially if you’re a newcomer, you might want to take note of some of these best-use practices.


#1 — Plan and organize your code before you begin

Rushing into a coding project might seem exciting at the outset. However, that excitement might make you lose sight of the bigger picture and help to convolute things. Code needs to be planned and organized before you begin. What structure will you implement? How will you separate your stylesheets from your Javascript? How will you structure your pages and folders? What are the overall goals that you’re implementing?

There’s a lot to think about before heading into a coding project, especially if it’s a major one. But all good coding habits help to foster higher-quality end products. When you fail to plan, you end up with a subpar product that’s confusing to come back to later on and work on. That confusion can create bugs and headache that you’d rather try to avoid down the road. Here are some things that you should keep in mind:

  • Setup an organized folder structure for the code by creating a separate folder for images, JS files, CSS files, and so on.
  • Implement a system for cross-platform scripting on mobile, tablet and desktop devices using Twitter’s Bootstrap.
  • Create an includes folder for all code that will be reused throughout the project such as headers, footers and menus.


#2 — Implement functions and classes

One really good programming habit to have is to ensure that you create functions or classes for code that will be reused on occasion. You should never find yourself rewriting code that’s being used repeatedly as it adds to code-bloat, making it unnecessarily larger in size and scale. Programming is all about efficiency in the underlying code that makes everything run smoothly and function properly.

Build a dedicated file specifically for functions that will be reused over and over. For example, database calls such as opening a database connection, selecting data, inserting data, updating data, deleting data and closing connections should all be rolled into functions. This will also make your job much easier by not having to rewrite redundant lines of code. All you need to do is call the function. Simple. Clean. And easy.

For example, the following is a example of a PHP function to insert a record into a MySQL database, a task that’s commonly performed on PHP-driven websites:

// MySQL Database Insert Function
// Insert and Accept Key => Val Array
// Then return new Record ID function 
function insert($data, $table, $SHOWQUERY="") 
  global $conn;
  foreach($data as $key => $name) 
    $attribs[] = $key;
    $name = stripslashes($name);
    $values[] = "'" . $conn->real_escape_string($name) . "'";
  $attribs = implode(",", $attribs);
  $values = implode(",", $values);
  $query = "insert into $table ($attribs) values ($values)";
  // pass any variable such as 1 or true to show the query 
  // this will display the query instead of executing it
    echo $query;
    return $conn->insert_id; 
    $last_inserted_id = $conn->insert_id;
    $mysqlError = $conn->error;
    $getErrorFile = debug_backtrace();
    echo 'Error: File Path: '.$getErrorFile[0]["file"].'\n';
    echo 'On Line '.$getErrorFile[0]["line"].'\n';
    echo 'Query: '.$query.' \n';
    echo 'Mysql Error: '.$mysqlError;


#3 — Use easy-to-read naming conventions throughout your code

Naming conventions are important no matter what type of code you’re developing in. The easier you make the variable names, function names, class names, and any other programmatic names, the easier it will be to develop that code and reference different parts. Since code isn’t all created in one day, it’s important to be able to look back and reference code that you developed, in a simple and easy way.

For example, instead of writing this:

// set variable for days passed since last visit
$dplastvisit = 5; 


You should write this:

// set variable for days passed since last visit
$days_passed_last_visit = 5; 


#4 — Comment all of your code, even if it seems obvious

No matter what happens, you should comment all of your code. This is definitely one of the best coding habits that you could have, because while you’re working on your code, it’s easy to manage. However, when you come back to that code, it can become very confusing to, especially if there are a ton of nested elements within that code. It’s also good practice to comment the closing brackets for blocks of code like this.

// this function is used to echo variables

// create a function that accepts two variables as arguments
function the_printer($variable1 = '', $variable2 = '') { 
  if ($variable1=='red') { 
    echo "the red variable was entered";
  else { 
    echo "the first variable: $variable1";
  } // end if statement for variable 1 red condition

  if ($variable2=='blue') { 
    echo "the blue variable was entered";
  else { 
    echo "the first variable: $variable1";
  } // end if statement for variable 2 blue condition
} // end function here


#5 — Optimize your code for efficiency through arrays, loops, etc.

Okay, if you’re a seasoned programmer, then you know that one of the best habits to get into is in using loops and arrays to iterate through result sets, or to compute some code without having to write out each line separately. This helps to optimize code and also makes for more efficient programming with less code-bloat. When you have less code-bloat, your leaner code will run quicker and take up less space.

In the beginning, when you start coding, you might not think about efficiency as much. However, it’s a really good programming habit to get into early on because not only will the code take up less space, but it will execute faster because the leaner sized-down code allows it to take up less space and memory to execute.

For example, consider the following code that will produce the years for an expiration date on a credit card number:

// get the current year and store in variable
$current_year = date('Y');

//echo out the next 10 years for expiration years
echo "<option value='".$current_year+1."'>".$current_year+1."</option>\n";
echo "<option value='".$current_year+2."'>".$current_year+2."</option>\n";
echo "<option value='".$current_year+3."'>".$current_year+3."</option>\n";
echo "<option value='".$current_year+4."'>".$current_year+4."</option>\n";
echo "<option value='".$current_year+5."'>".$current_year+5."</option>\n";
echo "<option value='".$current_year+6."'>".$current_year+6."</option>\n";
echo "<option value='".$current_year+7."'>".$current_year+7."</option>\n";
echo "<option value='".$current_year+8."'>".$current_year+8."</option>\n";
echo "<option value='".$current_year+9."'>".$current_year+9."</option>\n";
echo "<option value='".$current_year+10."'>".$current_year+10."</option>\n";


You should do this:

// get the current year and store in variable
$current_year = date('Y');
$last_year = $current_year+10;

//echo out the next 10 years for expiration years
for ($i=$current_year;$i<$last_year;$i++) { 
  echo "<option value='".$i."'>".substr($i,2)."</option>\n";


#6 — Test and debug your code as you build

You don’t want to get yourself into a situation where you write a whole lot of code, then have to debug multiple sections of it because something went wrong along the way and you suffer what us programmers like to call the white-screen-of-death. Not only do you need to test and debug the code as you build it, but you also need to ensure that you have all your error reporting turned on so that you can actually see your errors as you go along.

Every time you create a block of code, you should test and debug it to ensure that it’s working properly. When it comes to PHP, you’ll also need to ensure that these settings are turned on in your php.ini file or your user.ini file, which is usually located in your root directory. Debugging as you go along is also a great habit when programming because it will save you precious hours when you’re trying to debug and don’t have to sift through hundreds or thousands of lines of code just to find the error.

At the top of your code you should use:

ini_set('display_errors', 1);

In your php.ini or user.ini file you should modify the following statement:

display_errors = on;


#7 — Implement a system for version controls

Version controlling is an important aspect of programming. When you’re building a simple piece of software, you might not be thinking about version control at the outset. However, over time, you’ll want to improve that code, no matter what type of code it is. And, as you improve, you’ll want to track your versions. Keep in mind that programming isn’t just about writing lines of code, you have to be able to duly organize that code and track your work.

It’s also good to keep versions so that you can check back every now and then to see what you did in a prior version, or maybe to bring code back that you deleted in a prior versions but now want to reuse portions of. It’s a great habit to get into. And in order to do this, you need a system for tracking versions such as GitHub’s version control system.


#8 — Educate and expand your coding skill set

As a programmer, you need to get into the habit of learning and educating yourself on a variety of different technologies. Not only should you continuously work on mastering the current language that you’re proficient in, but you should also work to pick up skills in a variety of other languages. All you need to do is spend 30 to 45 minutes per day learning something new when it comes to programming.

If you’re a web-based developer, you might want to pick up some extra knowledge on Javascript and jQuery. Or, maybe you want to truly master HTML5 and CSS3. Or, you might just possibly want to make the leap into iOS or Android programming so that you can build apps and link your web-based code to apps that can interface with your work. Whatever it is, spend the time trying to learn something new and make this a daily habit and ritual.

Learn Web Development

Want to become a seasoned web developer? Over 16 hours of painstakingly-detailed tutorials will take you from a complete beginner to an advanced web developer.

View the Course!