Posts Tips to write great code

Tips to write great code

Greate code is one which will be appreciated by others. You should write a code such that, others have the idea of what the code does just by looking at the code. It should be properly formated, properly aligned, with proper naming conventions etc.

Some tips to write great code,

– Follow your Framework

Use the controls, base classes within your framework.

1) You should use base classes provided by your framework. You should know you framework for that and understand which class is to be used when.

2) Database Abstraction: Make sure you provide proper abstraction for you database, as it contains very critical informations.

3) Follow the best practices: Each programming language have some best practices, so make sure you follow them. You can even have your own practices with which you are comfortable. But you need to make sure they are consistently used.

– Keep your columns narrow

Keeping your code columns narrow helps to easily read the code.

1) Often you would see that the statements are long to fit in a page. You should avoid such statements. You can break long statements, so that they fit into the page without getting the horizontal scrollbar.

2) Long statements are mostly Queries so you need to make sure you break them down such that it fits into the page.

– Break your code into logical blocks

Divide your code into small blocks of code.

1) Functions: Break your code into functions. But that does not mean you break everything in functions. You should write a code block in function when you are going to use that code repeatedly, even if its few lines of code. Also make sure you give logical name to the function. That will help other to understand what the code block does, just by looking at it.

2) Every code you write needs to have some purpose. Make sure the code block that you write severs some purpose.

3) Also make sure one block of code should serve only one thing. So that you can reuse that code block for other things as well.

NOTE : Every code you write serves some purpose. So think before writing any code.

– Comments

Make sure you comment everything. That will help you and even other to understand the code.

1) One comment for each code block. The comment should not be very big. Remember to name the function such that it explains its purpose.

2) Multiple line of comments is acceptable only when the code block is complex. The comment should explain the purpose of the complicated code block.

– Reduce amount of code

Make sure you write only what you want. Try to make your work done with minimum line of code.

1) Remove code which confuses the user. Make sure you write understandable code. Not only others but you ll also have to understand the code later.

2) Also make sure you do not write any unnecessary code blocks. Even remove the unwanted code blocks or code lines which you have commented once you are sure they are not goin to be used. They make the code look shabby.

3) Try to follow naming conventions. Avoid lengthy variable and function names. Also you need to make sure the name should explains the purpose of block.

– Indentation

To make your code look neat and clean you must follow the indentation. This may be very boring part but it makes you code look better.

These are some tips, after following which you can write code that you and even others will love to read. Make sure to have a good presentation by commenting and indentation. Others will love to read such neat and clean code. Also make sure you do not have any unwanted block of code or any unwanted commented code.

This post is licensed under CC BY 4.0 by the author.