Actually i am just writing down some points (in my archive) which i have learnt till now to write a clean code in
java (even some points holds for others also) for a reminder purpose so that if i skip any of them in future thier i can note it.simply,their i feel to post these points here so that others can also make use of it if they want.
here it is :-
-----------------------
1. choose names for variable which entails its intention.
exampe -
BAD
int i ;
GOOD
int count ;
2. Don't use "_" and "$" while naming a variable till it is not a constant.
3. use the prounanciable names,Don't use Acronym's. BAD-gdprctc , GOOd-goodpractice.
4. Always prefer noun's for naming class,interface,variable and prefer verb
for naming methods.
5. some type of names are common like get,set etc.don't use this type of naming
because if the same name will be used in diffrent classes that will put a
confusion several times during implementation.instead use them as a prefix with
whatever are you getting or setting in method like "getXyz".where Xyz is whatever
method is returning.
6. always use the words for naming which are straightforward in its meaning.
7. try to choose smaller names.
8. always make your method to perform only one type of task,This is the contract of OOP.
9. Try not to use more than 3 arguments.That will be evil.
10. method should do things as it name suggest and nothing beyond that.
12. Use "Exception" if the error is solely related with your programme and "Error" if it is
related with the platform.like if your jvm is running out of space it is related with
platform.
13. Throwing Exception is always a good practice rather than handling or swallow it in a
unnecessary code.
14. prefer readbility and revealing intention by members rather than using comments.
like suppose if you write your code which is readable and as well as revealing its
intention than you don't have to put comment their.use comments when or if you want to put
some warning for it's use or to reveal some important points etc...
15. dont put too much inheritance.As composition is always better than inheritance.
16. always use override when overiding the interface method or superclass method.
17. dont use deprecated methods.and suppose in future if you found some fault with your method
then don't modify or delete it because several other classes may have used it with their policy.
instead deprecate it with annotation.
18. use annotations for revealing any kind of policy in your class.
19. try to use interface types rather than implementing class.
For ex:- List<E> list = new ArrayList<>();
it would save your lot of effort if you later change the implementation class without breaking
the code.
20. Try to make your code to be less dependant on another code.because too much dependancy it needs
lot of efforts for its maintainance.
-----------------------
These points are actually some "Good practice",which you can utilize during coding in order to write a good code.
And i apologize for if i have mention any unnecessary things here.
Thanks!
kind Regards!
praveen.