Friday, March 13, 2020

Common mistakes to avoid while coding in Java

During my development experience I have gone through multiple stages of exceptions and errors. I will list down all the mistakes I have done in my development experience from the beginning of my career. I think it will be very helpful to all of those who faced same kind of issues.

1. Use of integer, long, floats: While calculating some result with integer array and trying to get the sum, often it might happen that the result sum exceeded the integer.max_val. So in this case I recommend to use long.

we know if we do float operation with integer variable we might loose the decimal point value. hence we need to be very careful while doing such kind of computation.

2. NULL pointer exception: Handling null pointer exception is mandatory in 99% cases BUT we might miss some critical bugs in future if we use if-else blocks instead of assert statement. So recommended is to use assert statement if you don't want the value to be null.

3. Trying to overuse !StringUtils.isEmpty() kind of syntax. Its always better to use StringUtils.isNotEmpty() since it gives better readability. We often spend our time figuring out issues in code where we missed "!" before StringUtils.isEmpty().


No comments:

Post a Comment