5 Most common Programming mistakes to avoid


It’s easy to makes mistakes when you do something the first time. Initially you may never recognize mistakes that you make. All you notice is that it takes a lot longer to write and debug your programs. Even then, once the programs are deployed they still seem to have errors that should have been found during testing.

A life spent making mistakes is not only more honorable, but more useful than a life spent doing nothing.
George Bernard Shaw

We make mistakes because we are human, and by realizing our mistakes, we become wiser and better people. We learn how to fall and pick ourselves up.

So, in the end, it’s more important to ask ourselves, how to rise up afterwards, than to grumble about the fall itself.

Save your sanity by avoiding the most common mistakes while coding and save time and trouble as well.

1. Poor formatting of code


The code should be understandable. Code maintenance is time consuming and expensive. Frankly, it’s not at all fun. It’s much better to be able to spend productive time adding capabilities than to spend weeks digging through old code, trying to figure out what you were trying to do.

Think about the articles you read online. Some are nicely formatted, with a line between each paragraph, and everything is consistent. Some articles, though, have everything arranged in one Binary Large Object (BLOB), and they’re impossible to read.

Every programmer tends to have a programming style. It’s not as critical what your style is, as long as it is consistent. You need to let the code format help guide you.

For example, in my code, I insist on writing comments on the top of my program, using indentation and leaving one line space between two functions.

As you move forward in your code, look into whether your organization has a coding style. Consider defining a coding style for all your programmers, and stick to one that’s clear and fosters maintainability.

Strict code formatting rules are useful when several developers work on the same code using a version control system. Merging can be a pain if different developers have different formatting rules as the same code would look different for the merging tool. One should use it all the time to ensure that you use a consistent styling in all your source files. This will also save you a lot of time which you would usually spent trying to adjust the formatting manually. In that case you should agree to a common base and once and for all define formatter rules for this particular code styling. Then everybody can just hit the format-shortcut and everything fits the agreed styling.

The following patterns balance the forces affecting code formatting:

* Quick readability. The reader should be able to understand the gross structure of the code in a glance. The shapes made by blocks of text help you communicate overall structure.

* Detailed readability. The reader must be able to read lots of code in depth.

* Length. The number of lines must be minimized so that browsers can be as small as possible.

* Width. The width of code must be minimized. Code must not ordinarily spill across the right margin, requiring horizontal scrolling or destroying the shape of the text with line wrapping.

2. Poor testing and error checking

error checking

To handle your users, the best way is testing and error checking. Error checking is the practice of checking the result of every operation in your code. Make sure it’s either what you expect, or that your code can handle the unexpected result.

Testing doesn’t just imply running the code yourself. Testing means letting real users, people who may behave in unpredictable ways, run your code. You will find it infinitely informative.

Error handling takes two forms: structured exception handling and functional error checking. Structured exception handling is always preferred as it is easier to cover 100% of code. On the other hand it is very hard to cover 100% of all errors in languages that do not have exceptions, such as PHP 4. Code that covers 100% of errors is extraordinarily verbose and difficult to read, and can contain subtle bugs and errors in the error handling code itself.

Applications should always fail safe. If an application fails to an unknown state, it is likely that an attacker may be able to exploit this indeterminate state to access unauthorized functionality, or worse create, modify or destroy data.

When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks.

We will show different error handling methods:

  • Simple “die()” statements
  • Custom errors and error triggers
  • Error reporting

3. Poor commenting practice


In computer programming, a comment is a programmer-readable annotation in the source code of a computer program, but it is not executed by the computer. They are added with the purpose of making the source code easier to understand in the form of notes, and are generally ignored by compilers and interpreters.

Comments are very important when you are working with a team. You may move on in your career, and someone else may need to come in and understand your code. At that point, a comment written in the code will help.

4. Poor variable naming

Let’s consider an example. Suppose you want to calculate the speed of a vehicle when distance covered was 10 kms and time taken was 20 mins.

It’s a simple example, let’s say you encounter the line a = b/c. What does a mean? What are b and c? How do they relate to the rest of your code? Ten minutes after you write the routine, you’re going to forget. Never mind if someone else has to come in and make fixes or write an update.

Be sure to name your variables in a way that represents their function. You will save a lot of time and reduce a lot of headaches.

It becomes really frustrating to work on the code when many of the variable names are short and not descriptive. If you are new and the only developer left on the project, and there isn’t documentation as to what most of the variables do, you have to spend extra time tracking down what they represent.

The problem with the code is not the short names, but rather the lack of a comment which would explain the abbreviations, or point to some helpful materials about the formulas from which the variables are derived.

5. Not choosing the right data structures

data structure

Data Structure is a way to organized data in such a way that it can be used efficiently.

Data structures are generally based on the ability of a computer to fetch and store data at any place in its memory, specified by a pointer—a bit string, representing a memory address, that can be itself stored in memory and manipulated by the program. Thus, the array and record data structures are based on computing the addresses of data items with arithmetic operations; while the linked data structures are based on storing addresses of data items within the structure itself. Many data structures use both principles, sometimes combined in non-trivial ways.

Many of you have heard terms like linked list, tree, tagged union, record and array. Each of these are logical representations of data that correspond to some architectural structure of what you’re trying to represent.

One of the most common mistakes I see programmers do – is paying too little attention to data structure choice. Since almost all your code builds on your choice of data representation method, choosing the wrong data structure can have costly implications down the line.