Over a million developers have joined DZone.

5 Quick Points on Coding Practices

DZone's Guide to

5 Quick Points on Coding Practices

Free Resource

Sensu is an open source monitoring event pipeline. Try it today.

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 readability

Code should be readable to understand what is going on in that method.

  1. Use nice and meaningful names for classes, methods, variables
  2. Especially, local variables should have useful names
  3. Use of popular generic names like i, j, result are welcome even though they are not meaningful

Code accessibility

People should be able to guess which class will have “XYZ reusable component”

  1. Putting everything in single class makes code very accessible - but that’s not the right way. Avoid generic names like CommonUtil, CommonServices.
  2. Split the code into multiple files... but use appropriate name so that they can be found

Code complexity

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

  1. If you Copy-paste code more than 5 times, move it to its own method/utility class


Prioritize which parts to optimize  

  1. Don’t do this: “I will optimize what I usually optimize regardless of which project”
  2. Categorize performance issues and optimize for the most critical items first
  3. 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

Sensu: workflow automation for monitoring. Learn more—download the whitepaper.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}