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

Name Things Well

If you name your methods well enough, you may not even have to leave comments. Making your code as human-readable as possible will only lead to better code.

David Bernstein user avatar by
David Bernstein
CORE ·
Oct. 09, 17 · Opinion
Like (6)
Save
Tweet
Share
5.77K Views

Join the DZone community and get the full member experience.

Join For Free

I've said that the five code qualities we've been discussing are quantifiable. That is, we can create more of them if we want. Let's discuss how to do this.

The one code quality I'd like to focus on is cohesion. To me, it makes the most sense and I can easily see when my code is cohesive and when it isn't. The number one way I do this is to ask myself if I can come up with a good name for it. If I can come up with a good name for something then I've scoped it correctly. I've defined its place in the world. I've said what it does so its intention is clear.

To non-software developers, the act of naming something may seem mundane or even trivial, but to professional software developers, we know that the greater part of developing software is all about coming up with really good names. After all, everything gets named in code: data and behavior.

Naming is how we imbue the system with knowledge. It's how we communicate what we're doing in our code to other developers. Names should be active and say plainly what the code does. I like using long names for things so I can clearly articulate what it is. In every IDE I've worked in in the last ten years, you just have to type the name in once and the next time you need to refer to the name it will autocomplete after a few keystrokes. Today, with most compilers, names can be up to 256 characters and I like to use as many as I feel I need to so I can express the intention behind what it represents.

Invariably I find that if I'm having trouble naming something that one or more of the code qualities are missing, especially cohesion, and that tells me to break things down into simpler entities.

I try to include domain terms in the names I use, and I try to exclude software jargon as much as possible. I know it's a lofty goal that I can't fully achieve but I like to keep my code as readable as possible to non-developers.

My wife is not a developer. She's a filmmaker. If she can read through some code I've written and get a general sense of what's going on and what it's all about then I'm pretty satisfied it's clear. But if she's completely confused, I'll take the time to rewrite the code so it's more understandable rather than pepper it with comments.

In fact, every time I see a what comment that describes what the code is doing, I find I can usually replace it with a method call and give that method a meaningful name. This is far more valuable than a comment. It makes the code itself expressive.

I have been chastised before for telling programmers to "write uncommented code." But many lines of comments in code are either a nuisance or are downright misleading, and everyone would be better off if it didn't exist. If you want to write a lot of comments then do it with your check-ins in version control where it could be more useful to say what you did.

One thing I find that's interesting as a design pattern developer is that I often end up with rather generic names for things. For example, the polymorphic method in a strategy pattern might be called process and the class name may represent how the strategy is processed. Since I use design patterns a lot, I often end up with generic method names, but I'm fine with that.

I like my methods to create observable behaviors in the system and I like to name my methods to reveal what observable behavior will happen when the method is invoked.

I also like naming things for how I use them. I find I do this a lot when I write automated tests. Doing test first development, I think about how I'm going to call a behavior before I think about how I'm going to implement that behavior. That's a really important, fundamental approach of good software development because it allows me to define what I want to do before I start focusing on how to do it.

I talked a lot about encapsulation in the past and one of the ways I define encapsulation is by hiding implementation details and only revealing what a service does. This involves more than just defining method parameters and return values. The method's name is also important. Names imply behavior and we want to imply the right behaviors with as few presuppositions on implementation details as possible. In other words, we want to name things for what they do, not how they do it.

code style Software development

Published at DZone with permission of David Bernstein, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Remote Debugging Dangers and Pitfalls
  • Taming Cloud Costs With Infracost
  • Hackerman [Comic]
  • Real-Time Stream Processing With Hazelcast and StreamNative

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: