Ujjwal Chadha
Ujjwal Chadha

@ujjwalscript

7 Tweets 4 reads Sep 11, 2022
These 4 simple programming practices will drastically improve your code readability ๐Ÿงต๐Ÿ‘‡
1. Do not nest statements when not needed
โŒโŒ
if (x == 0)
if (y == 0)
if (z == 0)
count++;
โœ…โœ…
if (x == 0 && y == 0 && z == 0)
count++;
2. Use early returns
โŒโŒ
if (x != 0) {
// Do processing
}
โœ…โœ…
if (x == 0)
return;
// Do processing
3. Return Boolean expressions directly
โŒโŒ
if (a < 0 && b > 0 && c == 0) {
return true;
} else {
return false;
โœ…โœ…
return a < 0 && b > 0 && c == 0;
4. Do not put unnecessary comments
โŒโŒ
// If a is less than 0 then we execute this
if (a < 0)
...
โœ…โœ…
if (a < 0)
...
Note that these considered good practices and are not hard rules. You would generally end up with a more maintainable and readable code if you follow the dos and improve the quality of your overall code.
That's all for this thread. If you find this useful:
1. Retweet and leave a like on the first tweet - it encourages me to write more of similar content.
2. Follow me @ujjwalscript for more useful tips and threads ๐Ÿ™‚

Loading suggestions...