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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • What ChatGPT Needs Is Context
  • Security Challenges for Microservice Applications in Multi-Cloud Environments
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices
  • Clear Details on Java Collection ‘Clear()’ API

Trending

  • What ChatGPT Needs Is Context
  • Security Challenges for Microservice Applications in Multi-Cloud Environments
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices
  • Clear Details on Java Collection ‘Clear()’ API

A Compound Name Is a Code Smell

Yegor Bugayenko user avatar by
Yegor Bugayenko
·
Jan. 25, 15 · Interview
Like (1)
Save
Tweet
Share
8.69K Views

Join the DZone community and get the full member experience.

Join For Free

do you name variables like textlength , table_name , or current-user-email ? all three are compound names that consist of more than one word. even though they look more descriptive than name , length , or email , i would strongly recommend avoiding them. i believe a variable name that is more complex than a noun is a code smell. why? because we usually give a variable a compound name when its scope is so big and complex that a simple noun would sound ambiguous. and a big, complex scope is an obvious code smell.

the meaning of life (1983) by terry jones and terry gilliam
the meaning of life (1983) by terry jones and terry gilliam

the scope of a variable is the place where it is visible, like a method, for example. look at this ruby class:

class csv
  def initialize(csvfilename)
    @filename = csvfilename
  end
  def readrecords()
    file.readlines(@filename).map |csvline|
      csvline.split(',')
    end
  end
end

the visible scope of variable csvfilename is method initialize() , which is a constructor of the class csv . why does it need a compound name that consists of three words? isn't it already clear that a single-argument constructor of class csv expects the name of a file with comma-separated values? i would rename it to file .

next, the scope of @filename is the entire csv class. renaming a single variable in the class to just @file won't introduce any confusion. it's still clear what file we're dealing with. the same situation exists with the csvline variable. it is clear that we're dealing with csv lines here. the csv prefix is just a redundancy. here is how i would refactor the class:

class csv
  def initialize(file)
    @file = file
  end
  def records()
    file.readlines(@file).map |line|
      line.split(',')
    end
  end
end

now it looks clear and concise.

if you can't perform such a refactoring, it means your scope is too big and/or too complex. an ideal method should deal with up to five variables, and an ideal class should encapsulate up to five properties.

if we have five variables, can't we find five nouns to name them?

adam and eve didn't have second names. they were unique in eden, as were many other characters in the old testament. second and middle names were invented later in order to resolve ambiguity. to keep your methods and classes clean and solid, and to prevent ambiguity, try to give your variables and methods unique single-word names, just like adam and eve were named by you know who :)

Code smell

Published at DZone with permission of Yegor Bugayenko. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • What ChatGPT Needs Is Context
  • Security Challenges for Microservice Applications in Multi-Cloud Environments
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices
  • Clear Details on Java Collection ‘Clear()’ API

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

Let's be friends: