DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

What Defines 'Simple' Code?

Keep it simple.

Kevin Hooke user avatar by
Kevin Hooke
·
Feb. 26, 19 · Tutorial
Like (9)
Save
Tweet
Share
11.71K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Published at DZone with permission of Kevin Hooke, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Validate Three Common Document Types in Python
  • Easy Smart Contract Debugging With Truffle’s Console.log
  • Efficiently Computing Permissions at Scale: Our Engineering Approach
  • A Guide To Successful DevOps in Web3

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: