Some programming techniques will make all the difference in your code. Developers will neglect those techniques and as a consequence will have a bad time chasing and fixing stressful bugs. To help you build easy-to-understand software and get rid of bugs more easily, check out the following tips!
1 – Code Naming
YES, names are EXTREMELY important.
With a self-describing name, it is very easy to find out what your code is doing.
For this reason, do not use the first name that pops into your head. Think about the best possible name that describes exactly for what your package, class, method, variable is responsible.
Avoid ambiguous names! If a bug occurs in this functionality, it will be very difficult to find it.
Examples of ambiguous names:
Double clientValue; // Value of the client, value of what? void hide() // What does that hide? class Historic {} // Historic of what? Historic for everything? //Where is the cohesion?
Specific names help A LOT!
2 – Make Your Code Cohesive and Low Coupled
I still see many experienced developers writing god classes or methods that have so many responsibilities that end up making the code very difficult to maintain. The basic rule of making classes and methods responsible for only one thing makes the whole difference when designing your code.
So, if you create a method called: saveCustomer, make sure this method is doing only that, don’t overcomplicate it. Good code is the code you read and it does pretty much what it says. Bad code is the code you read and when you use it, it does something else you weren’t expecting.
Your code should not be highly coupled (highly dependent with other code), because the more dependent is your code, more difficult it is to change the code. If your method receives 10 instances in the constructor, for sure there is something wrong with the design of the class. So, make sure your classes are focused enough to avoid too many dependencies so you avoid high coupling.
3 – Get to know the shortcuts of your IDE
I am going to use Eclipse’s example. It’s very important to use shortcuts so you can code two or three times faster.
Control + 3 = “Eclipse’s Google” – write “new class” for example.
Control + shift + F = format your code.
Control + 1 = do various actions. For example, invoke a method that does not exist yet and use the shortcut.
Alt + up = moves a line of code to the above line.
Control + shift + R = search any archive you want on your project.
Control + H = search any word you want on your project.
Alt + Shift + R = rename any package, class, method, or variable and update all the references.
Configure Eclipse’s format. Change it from 80 characters per line to 120. Configure save actions too so you can have actions when you save your code on Eclipse. For example, we can use the command “organize imports” after each code save.
There are many other shortcuts, but remember, you need to use them until they get automatic for you. If you forgot to use the shortcut, undo what you did without using the shortcut and then, do the same thing using the shortcut, so you’ll never forget it. For example, if you are going to delete a code line and you forget to use the shortcut, just undo it and use the shortcut control + D.
More sources:
Eclipse Shortcuts
Netbeans Shortcuts
IntelliJ Shortcuts
4 – Encapsulate methods for what you need
Have you noticed a lot of code in your application that does the same thing?
Why not encapsulate a method for this?
For example:
Calendar c = Calendar.getInstance(); c.setTime(new Date()); c.add(Calendar.DATE, +1); Date userDate = c.getTime();
It would be a lot easier and much clearer to encapsulate all this code in a separate class and method:
Date nextDay = DateUtils.getNextDay();
Consider if need to keep the state of your object, if so, instantiate it!
Remember, the most important thing here is to encapsulate your solution!
5 – Create a culture of Code Review in your company
Even if the company you work for doesn’t use a Code Review tool, ask your friend to take a look at your code.
Each developer has his own experiences. We can learn a lot by sharing them.
Many things that you may not have noticed in a simple Code Review could be uncovered in a peer Code Review. You will see your mistakes and learn from them!
Hi Rafael,
I saw your discussion of DateUtils to share code that is duplicated.
Having a class like DateUtils full of static methods is not nearly as helpful as having not-static methods.
The invokations of static methods cannot easily be refactored to use an object.
Kind Regards,
Johnny
Hello John,
Yeah John, thanks for your feedback, I really appreciate this! I see your point. We get lots of benefits from keeping the state from an object. We must think carefully before deciding what we are going to implement.
Best regards,
Rafael Del Nero
Hello Rafael,
Thanks alot and keep going champ.
Can’t wait to see your next post.
Appreciated.
Hi azdian! I appreciate your comment! Stay tuned for more! Keep the code on!
Hi Azdian, thanks for your comment! Stay tuned for more!
Hello Azdian, thank you for your words 🙂
Late response, but better now than never 🙂
Encapsulate ifs inside their methods, not outside them:
The methods are differentiate with value either is 500 or more than
You have specified with if else block just use the methods
How in case how do we know which to be called here.
Hello Raghava, this was an idea that, in some cases, you could apply it, but in others, not. It wasn’t even an essential code technique. I changed this technique for writing low-coupled and highly cohesive code instead. By following those principles, developers’ code gets much better.
Thank you for this .. to be honest I feel so worried because I be like must I know everything in Java .. what are the important keys I should know in Java .. but trust me I know a lot but sometimes I feel worried because I think I still have to know more ..
Hello Ibrahim, it would be best if you were constantly learning, but at the same time, there is no need to know 100% of all technologies. Understanding 20% of the most essential concepts of technologies you use will enable you to solve 80% of the problems.