Technology
programming
Software Development
Debugging
Code Optimization
Code Quality
Coding Best Practices
Start writing better code, here (with examples):
Do not nest statements when not needed. Low nesting makes your code more readable and more debuggable.
❌❌
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++;
Use early returns. Eliminating invalid cases early makes your code less error prone and easy to debug.
❌❌
if (x != 0) {
// Do processing
}
✅✅
if (x == 0)
return;
// Do processing
❌❌
if (x != 0) {
// Do processing
}
✅✅
if (x == 0)
return;
// Do processing
Return Boolean expressions directly. It is a lot easier to read and much less cluttered.
❌❌
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;
Do not put unnecessary comments. Your code should be self-explanatory.
❌❌
// If speed of car x is less than 20, execute this
if (x < 20)
...
✅✅
if (carSpeed < 20)
...
❌❌
// If speed of car x is less than 20, execute this
if (x < 20)
...
✅✅
if (carSpeed < 20)
...
Delegate smaller unit of tasks outside the scope to functions
❌❌
int min;
if (x < y)
min = x
...
else
min = y
// Use min to do some processing
✅✅
function minimum(x, y, z) {
...
}
int min = minimum(x, y, z)
// use min to do processing
❌❌
int min;
if (x < y)
min = x
...
else
min = y
// Use min to do some processing
✅✅
function minimum(x, y, z) {
...
}
int min = minimum(x, y, z)
// use min to do processing
Type to interfaces and assign to concrete classes (for languages that support interfaces and types). This makes your code flexible if the implementation (and thus the concrete class) changes.
❌❌
HashMap map = new HashMap()
✅✅
Map map = new HashMap()
❌❌
HashMap map = new HashMap()
✅✅
Map map = new HashMap()
Catch, and rethrow exceptions with appropriate messages when needed. This makes debugging way easier.
✅✅
function processFile(filePath) {
try {
text = FileApi.read(filePath)
} catch (IOException e) {
throw new InvalidArgumentException('path invalid')
}
✅✅
function processFile(filePath) {
try {
text = FileApi.read(filePath)
} catch (IOException e) {
throw new InvalidArgumentException('path invalid')
}
Try to keep your data immutable. Objects that don't have state are much less error prone.
This is a big topic to explain here, but if you don't know what immutability is, read the answers here: stackoverflow.com
This is a big topic to explain here, but if you don't know what immutability is, read the answers here: stackoverflow.com
Don't have large objects hanging in static variables. Static variables live for the entirety of your program and thus if you keep adding to them, they hog RAM and make your program slow.
Rather, have multiple short-lived objects (Unless there's huge perf impact)
Rather, have multiple short-lived objects (Unless there's huge perf impact)
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 🙂
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...