Tips for a clean programming code

→ Are you a new visitor? Please visit the page guidance for new visitors ←

Tips for a clean programming code

Everyone of us hates when we have to work on another person’s project and the code is written bad. When you start a project you need to have in mind that, maybe, someone will work on it too, so if you will write bad code, that person will make you get some hiccups later on.

In this article I will write some examples of good versus bad code, and the strategy that you have to adopt when writting code (not only in php, but in every language).

Here are some tips to improve your code:

1) Always document your code!

If you write a certain function and call it in your application, and that function calls another one and so on, and you will not write something about that, the next person after you, will have a headache. Adding a quick comment above sections of code it is good, and also documenting classes and functions.

2) Indenting code

No one likes to search for an opened bracket a half an hour because you write everything on a single line. Use 4 or 8 spaces to indent your code, and when you go on another line, increase that with 4 or 8 spaces again. Let’s see an example:

A BAD way of writting code:

A GOOD way of writting the same code:

Above is a good example of code indentation. Code is clean and readable.

3) Give variables and methods a suggestive name.

What if you have to debug a function that is called cfdc(); You will need to see his entire body to find out what is good for. You can as an alternative call that method check_for_database_connection();  Or you can use camel-case writting method and call it checkForDatabaseConnection(); Better and readable huh ?

4) Do not delete, always comment out.

If the website that you are developing is not ready to go into production mode, and you still have code to write you should definitely use comments.

A comment in php for example, for a single line is like this (notice the two slashes):

and for multiple lines is like this:

Imagine if you have written two ways of solving one problem. You will have to stick with one of them. You erase one block of code, and stick with the another one, but after that you find out that this is causing other bugs in your application. So you want to go back, but you cannot because you deleted the old solution. This is why you want to use comments instead of deleting blocks of code.

For all stated above, there are several websites that help you to achieve them. You can use php code cleaner for example. It helps you format your code and teach you how to organise it until you get used with this concept.

So next time when you write an application keep all these things in mind, and apply them.

Update 15 Oct. 2014

A few things pointed out by Nicolás Gabriel Kivatinetz on linkedin:

1. The methods should aim to contain no more than 5 lines insides. If you follow this rule you can be sure that your method is doing just what you want it to do and nothing else. If your method contains more than 5 lines it is because the method is doing many things so you should consider on creating another method with that function.

2. Never use a new explicitely. Suppose that I have a class person with a constructor person(skinColor);
It is always clearer calling a static method rather than to the constructor. I will create a static method person:blonde(); which will return a new person(‘blonde’).
May be this is not the best example but it is just a simple example that shows what I am referring to.

Thank you!

 

Request an article ←