Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,159,191 members, 7,839,050 topics. Date: Friday, 24 May 2024 at 12:59 PM

6 Steps On How To Write Clean Codes - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / 6 Steps On How To Write Clean Codes (770 Views)

Reading Other People's Codes/github Open Repo Projects / Xmas Codes / Free Udemy Courses 100% Free [coupon Codes Applied] (2) (3) (4)

(1) (Reply)

6 Steps On How To Write Clean Codes by ijecks(f): 2:54pm On Nov 15, 2018
How to Write Clean code - Abuchi Mmaduabuchi

[Abuchi Mmaduabuchi is a software engineer at larisoft ltd with years of experience in product management, software development and software testing. He can be reached at abuchi@larisoft.com]

Writing clean code is so important in software development that not only does it help teams progress faster on projects, it is also a very good metric for determining who is and who isn't an experienced developer.

In this article we shall examine what factors constitute a clean code.

1. Variables and functions should be named meaningfully:
A well written function should say what it does in the name in the clearest manner possible. Even in situations where a meaningful name will be too long, it is still better to use hyphens or camelcase to convey what the function does.

There is no greater sin in coding than having variable and function names which convey no meaning whatsoever.

2. Functions should be short in length:
A well written function should not be longer than 7 lines. If its longer than 7 lines, you should seriously consider moving parts of the function to a different function. If after considering moving the parts and you still think there’s no way to reduce it; think again.

This is simply to enable other programmers understand your code without striving to remember what they read 20 lines ago.

3. Avoid convoluted Braces and code blocks
Code parts that involve braces should be not be 3 lines deep. You know you are writing bad code when you punch something like this into the keyboard

if(foo==bar){

if(arr == ss){

if(noo== no){

for(i = 0; i < 1; i++){

while(i < j){

}
}
}

}
}

Again, in pursuit of code readability, programmers who write code like this should be fired, or you know; shot and then fired.

4. Be careful with Comments:
Comments are a mixed bag which should be used very sparingly if ever at all. A properly written code doesn't usually need comments except in rare circumstances where the programmer is convinced that; shorten the length of the function as much as he can, name the variable as clearly and as long as he can, and eliminate braces all he can, what the code does is still not immediately clear to another programmer. Only then should comments be introduced.

Also, while making comments in code, you should favor explaining the meaning and type/return type of a variable or a function, instead of explaining what the function/variable does. This eases deconstructing your code.

5. Avoid Unnecessary, but fancy algorithms
An algorithm is simply the series of steps the computer takes to accomplish a task. These steps are usually set by you; the programmer. Now, because you usually think you are awesome, its very easy to start introducing recursions where a loop would do; or you know, invent and implement your own sorting algorithm while there are already several to choose from.

Your primary objective should be to write as very little code as possible that is as clear to read as possible, and which meets the specifications at play. Your fancy algorithms really usually deviate from these primary objectives.


6. Remove all dormant variables, functions
While programming, it is very common to introduce variables and functions solely for testing purposes (You should be using unit tests by the way, not doing this). But these variables should be removed as soon as they’ve served their purpose.

Sloppy programmers tend to leave this as part of their code. Especially debug statements which go on to slow down the system when deployed to production.


In summary, software development projects are a very complex activity which is prone to failure. Writing clean code greatly reduces this complexity and increases chances of success. Writing clean code of course encompasses much more than is written in this article. But the points above are good starting points.


Keep coding!

3 Likes

(1) (Reply)

Event PS (a Web Application) Developed By A Participant During Our Last Bootcamp / Free Css Masterclass For Beginners / Introducing Google Hash Code Team-based Programming Competition 2019 For Univers

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 13
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.