Thursday, 14 November 2013

Thursday, November 14, 2013
2
If you are a programmer or someone who is in the process of learning, then this article will help you to write good code.

Now, what do I mean by writing good code. In simple terms, when you program something then you should write the programing code in such a manner that it’s easily understandable to anyone who reads it.

This is something you might have heard while programming.


Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.


Let’s take a look and find out the best ways to write “Good Code” which is readable by anyone.

1. Indentation

It can be said that you already know that you should indent your code. But do you maintain a consistent style of indentation? If not then you should.
Here are some examples of indentation.

Method 1.



function method1(){
                if (bca) {
                        do_something();
                        oncemore();
  } 
  } else {  
          finish();  
      }  
}

Method 2.



function method2()
{ 
        if(checking)
        {
               do_something();
               oncemore();
         }
         else
         { 
               finish();
}

Method 3



 function method3()
{       if(checking)
         {   do_something();
              oncemore();
          }
          else
          {    finish();
           }
}

For my personal use I have been coding in "method 1" since the beginning. It's basically a matter of preference. Whichever method you are comfortable in, you should choose that one and stick to that particular method only, throughout the entire programming process.


2. Avoiding unnecessary comments

If you comment your code that you are doing great and its something that every
programmer should do.

But do you comment in places where there is none required?

Let's take an example to understand what I mean to say.


// get the username
$username = get_username(..........);  
 // if username is "ajindia"
if ($username == 'ajindia') {  
     // display the results
    echo results();  
}

I guess you get the picture of what I am trying to say here. Comments in such obvious situations is best avoided. But then what should you do?


//get username for ajindia
$username = get_username(..........);  
if ($username == 'ajindia') {  
    echo results();  
}


3. Avoid Multiple Nesting

How much do you nest your code? While nesting is sometimes necessary but too much nesting is also not a good practice. Why? This makes it harder to read the code.


function get_user_details() {  
  // ...  
      if (some condition) {  
          if some condition)) {  
              if (some condition) {  
                  if (some condition) {  
                     // ...  
                  } else {  
                    return false;  
                }  
            } else {  
                return false;  
            }  
        } else {  
            return false;  
        }  
    } else {  
        return false;  
    }  
} 


4. SQL special words should be capitalised.

Whenever you are writing some code in SQL make sure you capitalize the special SQL words. Why should you do this?
Well this makes them more readable and as because they are case insensitive you will not be in any problem. I do this to make code look much more readable. It’s a small detail which you need to look into and the impact is huge.

Here is an example, take a look.


SELECT id, username FROM user;  
 
UPDATE user SET last_login = NOW()  
WHERE id = '777'  
  
SELECT id, username FROM user u  
LEFT JOIN user_address ua ON(u.id = ua.user_id)  
WHERE ua.state = 'KOL'  
GROUP BY u.id  
ORDER BY u.username  
LIMIT 0,10


5. Documentation

Well we have already covered how we can use commenting feature in different ways, now let’s see another important aspect about it.
Documentation is a big part in programming. Remember when you are coding a web application or any other application with different modules you need to make sure you create a documentation explaining each one.
The primary reason being that your code will might be later read by another team or programmer and if that person can’t understand what a particular section of the code is for, then you will be in a big problem. It is also certainly possible that you, yourself will forget what that particular code was meant for.


6. Group your code

Whenever you come forward to see some group of code performing a similar task and its possible for a particular function or module, it’s best to group those blocks of code.
// load the templates  
load_template('header');  
load_template('user_list',$users);  
load_template('footer');  


2 comments: