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++;
โโ
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
โโ
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;
โโ
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)
...
โโ
// 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 ๐
If you are new to the account, follow me @ujjwalscript for more useful tips and threads ๐
Loading suggestions...