{{announcement.body}}
{{announcement.title}}

What Defines 'Simple' Code?

DZone 's Guide to

What Defines 'Simple' Code?

Keep it simple.

· Java Zone ·
Free Resource

Simple code is not complex code. OK, well, what does complex code look like? We don't like or want the complex code because it is:

  • hard to read
  • hard to understand
  • difficult to update and maintain

OK, so simple code, therefore, is:

  • easy to read
  • easy to understand
  • easy to update and maintain

As a result of the above, other benefits become more easily within reach. For example, simple code is easier to unit test.

What can we do to ensure we write simple code? Many of the commonly known (but not commonly applied) industry best practices lead to simpler code. For example:

  • Single Responsibility Principle: Bob Martin defines this as a 'single reason to change.' A class should have a single responsibility — one feature that it is responsible for. If there is more than one reason that would require changes to this class, it has more than one responsibility and, therefore, is doing 'too much'.
  • A class or method should do one thing and do that one thing well — not 10 things, not 5, just one thing. Limiting to just one thing reduces the opportunity for complexity to creep in (this is really the same idea as the Single Responsibility Principle).
  • A single method that is short enough that you can easily read it and grasp the whole intent. Too long is when you have to scroll page after page, and at that point, it's difficult to grasp the entire purpose of the method without scrolling around and re-reading. If it takes you too long to read to the end of a method, and by the time you get there you've already forgotten what it was doing at the start, and the start of the method is already several pages off the top of your screen, your method is too long.
  • Clearly named variables, methods, and class names: a clear name that describes what the variable is for (its purpose, what it represents, etc.), what a method does, what a class does, and what it helps to convey or understand. A method that does something other than that what its name describes is not simple and not easy to understand. We don't like surprises.
  • Clear documentation: in Java, we use JavaDoc. Your JavaDoc should describe what the class does and what each public (at least) method does. It should NOT state the obvious; it shouldn't repeat what is already implied from your clear class and method names. If you're just repeating what the method name says, you're not aiding readability; you're adding more content that I have to read but for no gain. For example, this is not useful documentation, although many developers do this:
/** * This method creates a new account. */public Account createAccount(){ ...}


... I know it's a method because I am a developer, you don't need to tell me that. I know this method creates an account, because the method name says so. This JavaDoc adds no additional value, and if there's no additional detail to be added, it would be best just left out.

If you set out from the start to create simple code, it's more easily achievable than creating something too complex and then trying to simplify. Refactoring is your friend, and you should always invest time to refactor when you've finished your first iteration of getting your code working. However, by aiming to avoid complexity from the start, you can make your job easier in the long run.

Topics:
java ,coding ,simple ,simple code ,methods ,names ,single responsibility principle ,single responsibility principle java ,java doc

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}