Programming Best Practices: Do you follow them?

Published On: 22 December 2012.By .
  • Digital Engineering

So you’re a programmer and have you ever wondered that the code you write might make the next guy working on your code pull his hair out? Not really? Well you most certainly did. I’m not trying to be hard on programmers here but fellas, you gotta accept that the job might get tough sometimes and pushing deadlines can cause you to forget about the best solution and just go with the ugly solution that can be easily implemented. Its not a crime to do that but writing clean code might add a star badge in your profile. The difference between an average and a premium programmer is the code quality that one produces. So here we go with some pointers to help you guys step up your code quality a little bit:

 

Comments: Alright lets face it, either we’r too lazy to comment our code for documenting purpose or we assume everybody who’s going to read the code obviously knows what the code is talking about. Comments not only helps the other guy working on the code but also helps you to recall in future that why the hell you coded stuff that way. For example, It’s easy to write short comments when you’re declaring a function saying what the function does and what parameters it accepts actually means. But you gotta avoid obvious comments like:

 

<code>

if ($country_code == ‘US’) {

// display the form input for state

echo form_input_state();

}

</code>

 

Clean Indentation: So does your code look like a bunch of ants dipped in ink walked on the screen? Is it so hard to read and match braces that even your IDE gives up? Have some mercy on the guy who ends up maintaining your code and try and write code with good handwriting. Make use of tabs and try to break long lines into short lines for easy readability. Don’t overcrowd your code with lotta comments, place comments wherever necessary and in orderly fashion. Most IDEs assist you to write clean indented code already, all you gotta do is follow. When you’re done with code have a nice look and make sure you see an beauty which could turn-on a programmer.

 

Consistent naming scheme: Variable and object naming is a status symbol of your code. Don’t just go with var1, var2, temp1, $x etc., try and be a bit descriptive like country_name, company_id, get_user_old_status() and so on. I know in cases where you need to hurry you might end up using such sloppy variable names but you gotta correct those before you push your code to final. These names don’t convey a thing about what’s their purpose and in the end gives a hard time to the programmer trying to wrap his head around the code section. Also when writing SQL queries, capitalize all SQL keywords. It looks more clean and very much readable.

 

There are a bunch of more stuff to consider, but anyways, following above 3 rules would definitely give you a start to be a premium coding guy. To get to know more about best code writing practices, refer to Open source CMS like Joomla, WordPress, Magento and so on. These codes are well documented and commented, structured and laid out in a very programmer friendly way. It will also give you an insight in optimizing your code writing skills and implementing best of the many possible solutions.

– Saurav Pandey

 

Related content

That’s all for this blog