5 Quick Points on Coding Practices
Join the DZone community and get the full member experience.Join For Free
A quick, high-level look at coding practices and coding with performance in mind. It's kept really short so that we don't lose focus on the overall picture. The problem we want to avoid is focusing too much on a particular aspect, while leaving gaping holes in others. So, here is the short list (in no particular order):
Code should be readable to understand what is going on in that method.
- Use nice and meaningful names for classes, methods, variables
- Especially, local variables should have useful names
- Use of popular generic names like i, j, result are welcome even though they are not meaningful
People should be able to guess which class will have “XYZ reusable component”
- Putting everything in single class makes code very accessible - but that’s not the right way. Avoid generic names like CommonUtil, CommonServices.
- Split the code into multiple files... but use appropriate name so that they can be found
Code complexity should match domain complexity. Code-mess is avoidable whereas an appropriate level of complexity for the domain is not avoidable. For example, for complex domains like Banking or CRM, code cannot be all that simple. Trying to oversimplify things and making misguided efforts to "keep things simple" is a big risk... your architecture should be designed for the eventual full-release. Don't plan to scale up architecture in "phase-2".
DRY coding – “Don't Repeat Yourself”
Repeated operations should be in method/utility class
- If you Copy-paste code more than 5 times, move it to its own method/utility class
Prioritize which parts to optimize
- Don’t do this: “I will optimize what I usually optimize regardless of which project”
- Categorize performance issues and optimize for the most critical items first
- Don’t over optimize. Code readability and Thread safety are much more important than performance
It's been a short discussion, and there are only a handful of points to take away. But, if you take away only two things from this they should be:
- Code-mess is avoidable whereas an appropriate level of complexity for the domain is not avoidable.
- Categorise performance issues and optimize for the critical items first
Opinions expressed by DZone contributors are their own.