Home » Articles

6 tips to make your fellow coders love you

paan 30 July 2008 Articles 2,917 views 9 CommentsPrint This Post Print This Post Email This Post Email This Post

6 tips to make your fellow coders love you

Code readability is paramount in an environment where you are not the only one reading and writing the code. And even if you are the only developer of your system, I know more than a few coders that read a code that they wrote last week and thought “What the hell was I trying to do with this code?!” So, give your fellow coders a break and follow these 6 tips to make your code readable and make your fellow coders love you.

1.    Consistency

Consistency is the number one rule when writing readable code. It doesn’t matter if you but your braces in the same like as the if statement like this:

1
2
3
if (x==0){
    //do stuff
}

or if it’s on the next line, like this :

1
2
3
4
if (x==0)
{
    //do stuff
}

Just pick one and stick with it. Follow the C++ coding standards or follow the gnu coding standards or follow the java coding standards or make your own standards, it’s up to you, but chooses one and use it everywhere. It’s very annoying to see different kind of coding style in the same project, or worse, the same source file. Create a “Coding Standards.txt” and put it on the top directory of your source code so that all the coders can see that you follow, and expect them to follow, a coding guideline.

2.    Indenting

Indenting is one of the most important aspect of code readability. Indenting allows readers to have a general overview of the flow of the code. And it also provides a visible cue the code’s structure. For me personally, proper(and more importantly consistent) indenting is one of the biggest factor that contributes to code readability. The general rule is that all logic block should be indented, and any logic that branch from the same point should be indented at the same level.

3.    Comments

Many people argue that a lot of comments makes a code readable. But for me the comments should be used sparingly. A well written code should tell its readers what it is doing just by looking at it. Comments should be used however to indicate why a certain part of the code does what it does. For example:

1
2
3
4
5
6
7
8
9
10
11
12
//example 1
i=0;//initialize i
while (i<1000000){//while i is less then 100
	i++;//increment i
}
 
//example 2
//this is part of out test suite to stress the CPU 
i=0;
while (i<1000000){//an arbitaryly long number
	i++;
}

The first example while having more comments is pretty much useless since we already know it is initializing the variable and we already know that it is comparing against a million and we already know that is is incrementing the variable from the code. So the comments add nothing to the understanding of the code. We want to know why it is doing the loop.

The second example solves this by providing a reason for the code to be there and also provide useful information like why the number 1 million is chosen. If you see the code in a production environment then we KNOW that we can remove it because we know what it does. But if we see the first example we might be hesitant to remove it, why is the code here? Does the program require time to finish its task before running the next line and the previous developer never heard of sleep? What is the significant of the number 1 million. Is that the time to live for some unknown cache that we are waiting to expire?

Let the code tells most of the story and what the code can’t tell, you comment.

4.    Variable names

Following my stand that code should obvious and tell its readers what it is doing I believe in descriptive variable naming. I really don’t mind a long variable but numberOfCommentsFromUsersThatAreRegistered is really tiresome to type and read and more likely than not will cause horizontal scrolling on the part of the reader. The basic rule is to be as descriptive as possible but to drop grammatical articles from the variable name. So numberOfCommentsFromUsersThatAreRegistered would become something like registeredUsersCommnetNum. Short forms should be avoided unless it is a really common convention like the prefix is for any function that checks if something is a certain type/condition etc. or the numOf prefix. In strongly typed language like java or C it is common to prefix the name of the variable with the data type. For example intCommentCount is an integer and strVehicleName is a string and so on. Other stuff like underscore use and capitalization shouldn’t matter much but, as per item 1 in this list, pick a style and stick with it.

5.    Minimize horizontal scrolling

When writing code make some effort to minimize horizontal scrolling on your code. The typical screen size is 80 columns and you should try to make sure your code fits this. Horizontal scrolling make it harder to read because of the effort required to actually even see the code. On modern IDE the person reading the code would need to move the mouse to the bottom of the screen and click on the scrollbar. Which is bad enough but then if he needs to read something above or below the code he probably needs to scroll back to see it. On emacs it’s just plain annoying to scroll horizontally. I know there is word wrapping but that always make the code looks bad and ‘wrong’. Most codes can be broken into multiple lines or multiple instructions while keeping the same behaviour.

6.    Follow the gnu convention of doing one thing and doing it right

This apply to writing functions. I always cringe when I see a function called “process” or something like that. Never mind that is it is an ambiguous name to start with, it also implies that the function will do a lot things. For example it might filter the input, then validate it, then store it in the database. It would be much better to create one function to filter the data, another to validate it and another to store it into the database. It is easier to debug that way since we can isolate the processes in the function and also easier to extend because you can, for example, change how the program validate the data without messing with the database code. And more importantly and to the point of this article it is easier to read smaller chunks of code that you know is doing a single thing that to plow through a whole page of code that could be doing either of the three things mention.

The End

Now go write your code and make your fellow coders love you.

Digg!
Rate this:
3.5 (2 people)

9 Comments »

  1. Good points! Consistency is the really rule #1 and it is also necessary to create coding conventions before starting a project.

    Rate this:
    2.5
  2. Some verify good points here. But seriously “The typical screen size is 80 columns”? Typical for what ? 1994 ?

    We could agree to go to 120 characters today and not inconvienience anybody looking at code.

    Rate this:
    2.5
  3. Good read; consistency is the key to good programming :)

    Rate this:
    2.5
  4. Good list, if only more programmers wanted actually did this. Most of the time I gotta fight my way through some hacky stuff

    Rate this:
    2.5
  5. About //Paul Loveridge’s comments:
    There is a limit to what the eye can catch, both horizontaly and vertically, I believe that the author recommendation of limitting line length to 80 is very pertinent. He’s talking of readability for fellow developers here. Even for oneself it will be easier to the eyes.

    Rate this:
    2.5
  6. Screen size now is not as scarce commodity as it were maybe 2 years ago. Now a lot of people regularly run at the resolution of 1280×1024 and upwards so 120 columns a reasonable value. But I still regularly use 1024*768 on most of my machine and some really old machine I worked on is still running at 800*600 and at these resolution 80 columns fits just nicely.

    Btw did you notice how most readme are still line broken at 80 columns wide, But of course it all boils down to preference, so I think that 80 is the acceptable standards with 120 gaining popularity.

    Rate this:
    3.4
  7. That’s a nice post!

    Though #1 is very well agreeable, i give special comments to #3 and #4. They have much values here out of all i believe.

    I feel happy that i had been of this kind since my graduation and tend to follow the coding conventions as much as I can.

    Cheers!

    Raghavans last blog post..Men always have better friends

    Rate this:
    2.5 (1 person)
  8. Thanks Raghavans.
    I think If one code only follow one rule from above I would like them to follow #1 and be consistent about the bad standards to follow. The second best thing is that I hoope they follow #3 and comment what they do.

    I’ve also noticed that there are a lot of little gems of code even in code with poor readability. This suggest that that are some coders that are good and yet write code that is very poor in readability. But the really great codes are usually written very nicely and are easily read.

    I’m not sure if the code being great is a function of it being readable, meaning that we feel the code is great because we can read it better, or if it’s just that great coders understand this shit and that is why thery are great.

    Rate this:
    3.4
  9. 89 Ways for You to Become the Coolest Programmer in the World…

    Since there are dozens of posts on becoming a better developer, but no single post with all the advice you need, perhaps, you’ll find this short guide useful.
    1. Learn the Skills You Need

    Learn the programming basics

    “The goal of this guide is to b…

Have your say!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>