Ujjwal Chadha
Ujjwal Chadha

@ujjwalscript

7 Tweets 1 reads Aug 19, 2022
Improve your coding style with these 4 dos and don'ts ๐Ÿงต๐Ÿ‘‡
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, retweet and leave a like on the first tweet - it encourages me to write more of similar content.
If you are new to the account, follow me @ujjwalscript for more useful tips and threads ๐Ÿ™‚

Loading suggestions...