Golden rules for programmers to write good code

Why we need to code properly because a well written code not only does its job well but is also easy to extend, maintain and debug. So here are some key points:

 1. Follow a consistent coding standard:

There are a lot of coding standards, follow one of them. Like in php we have PSR-0, PSR-1, PSR-2, PSR-3 , PSR-4. Make it easy for everyone in the team to read and understand the code.

 2. Name things properly:

Name variables, functions and files properly, long names are allowed but don’t use such kind of names that are not relevant to the context. Use names that describe what that file or function stands for.

For example:

A variable for fetching data from database for a particular location can be like   $locationDetailsFromDB .

 3. Avoid creating long functions and god objects:

Never use long function in your code or god object (that know too much or do too much). Keep the methods, functions and files small. There could be a large number of files but never lengthen a file too much. A long file can be messy that can cause errors later and it may happen that you will be unable to read or understand the code written by you only.

 4. Use MVC framework:

Use MVC framework while coding. In this way your code will be compact and clean, in properly written framework code it is easy to understand what is written where and how things are working. Separate your view files from the database queries. See, things will still function when you write the backend code in frontend files but it is not a good practice for coding. In future that may cause error and can make your code messy and bulky so try to separate things in model, controllers and view files, and you will find that everyone can understand and read your code without taking your help.

 5. Be expressive:

First understand the concept and logic that how are you going to tackle the problem in the code and then starting coding. Your code should speak for itself to explain what is written in the code and what it is doing.

 6. Use try catch for exceptions:

Always try to use the try catch statement in the code where you think that your code can throw exception. In this way you can reasonably expect and handle an error that is not an exceptional condition.

 7. Indent your code properly:

Always indent your code during coding, don’t keep this kind of attitude that you will do it later on or it is not required. Indentation of code is one of the golden rules of clean and maintainable coding.

 8. Avoid inline comments:

Always avoid inline comments in your code. That means never use variable names or code in the comments.

// print_r($someVariable); exit;                 //wrong way

// print the variable/array                        //right way

 9. Do not stick to arrays or maps:

Never stick to just arrays or maps for long data sets, try to use classes and structures for complex data structures. Objects are wonderful to work with. You will never like to go back to arrays or maps once you start using objects.

 10. Reuse methods and classes:

Do not repeat the same code in different files. Just separate out that code and put it in a method and call that method wherever required.

 11. Take time while coding:

Take time while coding but never write messy code. Though it’s necessary to keep an eye on time too but writing code in hurry could make it hard to maintain.

Always remember that “you are responsible for the quality of your code” so write clean and easy to understand code so that it will be easier to maintain that code in future by you or anyone else.

Leave a Reply

Your email address will not be published. Required fields are marked *