Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,869,276 members, 6,886,361 topics. Date: Sunday, 29 May 2022 at 06:08 AM

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

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

Can You Write Codes With A Tablet Instead Of A Laptop? / Need Help With My 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)

Looking For A Tech Company In Lagos Island In Need Of An Android Developer / Is Udemy Video Streaming Much Slower Than Youtube / React Native Swiper Slider Layout Example

(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 - 2022 Oluwaseun Osewa. All rights reserved. See How To Advertise. 44
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.