Being able to write clean code and using best practices can be challenging when it comes to creating applications. Here are some of the improvements that can be made to achieve that goal with the help of the a static code analysers (here I use Embold), to code and learn best practices along the way!
Let’s go through a series of examples of bad practice, followed by how we can avoid them.
This post focuses on writing clean code with examples written in Java, but it can be applicable to any language.
- Thread🧵
- Catch Multiple Exception in a Single Catch!
- Naming Conventions (boolean vs. int) 📋
- JDBC
- Dead code🧟
- Sanitize 🩹
Thread 🧵
Simple Example of Creating a thread by using Runnable interface.
public class MainServer implements Runnable { @Override public void run() { System.out.println("Hello World"); } public static void main(String[] args) { MainServer ex = new MainServer(); Thread t1= new Thread(ex); t1.run(); } }
In the above code, t1.run()
is called instead of using start()
method to start a thread. It seems like a normal thing, Right? Nope, To achieve the multi-threading. We have to execute the thread-like separate execution, not like the regular method.
start()
– Creates a Thread and then execute run() method.run()
– Calls run method as normal method!
– No thread creation
Now, if we modify our code to achieve multi-threading by replacing the run() method with start().
t1.start()
However, if we need multiple invocations of the thread, we can’t use start() every time. So, we have to use run() in that case!
Catch it!
Instead of Vertical Scaling the catch
block, which increases in complexity and increases lines of code. We can perform Horizontal Scaling of Multiple Exception in the single catch
block.
Vertical Scaled Catch block!
public class MainServer { public static void main(String[] args) { try { // do something } catch (IllegalArgumentException e) { throw e; } catch (IllegalStateException e) { throw e; } } }
Now, Horizontal Scaled Catch Block!
public class MainServer { public static void main(String\[\] args) { try { // do something }catch (IllegalArgumentException | IllegalStateException e) { throw e; } // catch block is better now! } }
Naming Variables 📋
Someone told this someday and it now becomes a meme!
Coding is about 99% naming the variables and 1% thinking about logic.
~unknown.
Naming Variables have to be taken into account when writing a code since the meaningful names to the variables help other developers to find the value out of your code.
int isValid; int hasVowels; Boolean isokay;
In the above code, int
variables are declared like
(True or False). This reduces readability and maintainability issues when working with large-scale applications. boolean
int setName() { return name; }
In the above code, It is looking like a setter method but it returns something.
void intToInteger(){}
Indicates that it converts int to object Integer, it should return the Integer Object.
It recommended to use,
When declaring
- Class
– use Uppercase followed by lowercase (ie) Student - Method
– use camelCase (ie) toIntArray() - Variable
– use underscore when variable isfinal
(ie) PIN_NUMBER
– avoid underscore when variable is normal (ie) age
Keep in mind, the above conventions when declaring variables, class, and methods.
JDBC
PreparedStatement
Use PreparedStatement when using DB Connection. Since it helps in preventing SQL Injection Attacks.
Close Resource
Ensure that, the Resource after usage of it is closed to use the resource efficiently. (ie) Close the Resource like Connection, ResultSet, Statement objects after their usage to achieve efficient resource Utilization.
Dead Code 🧟
Dead Code is part of the code that’s never used/ called in the program. The dead code increases Lines of Code and takes up memory too. It can be avoided by getting rid of the method/variable which is not used/called from any part of the program.
Sanitize🩹
No matter what, Sanitize the input before providing the access to the system.
These are some of the Techniques to get started with writing clean code. We can’t remember all this in our muscle memory all the time when coding. So, Free static code analyser tools like Embold are very useful to catch problems you may have missed! Over time, you’ll make lesser errors and be able to do your best coding work.
Learn while you Do!
Comments are closed.