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

If-Then-Else Is a Code Smell

If-then-else is a common pattern, but should it be replaced with something more elegant? What pattern can we follow? Read on to find out more and for an example.

Yegor Bugayenko user avatar by
Yegor Bugayenko
·
Aug. 14, 16 · Opinion
Like (19)
Save
Tweet
Share
27.91K Views

Join the DZone community and get the full member experience.

Join For Free

In most cases (maybe even in all of them), if-then-else can and must be replaced by a decorator or simply another object. I've been planning to write about this for almost a year but only today found a real case in my own code that perfectly illustrates the problem. So it's time to demonstrate it and explain.

Fargo (1996) by Coen Brothers
Fargo (1996) by the Coen Brothers

Take a look at the class DyTalk from yegor256/rultor and its method modify(). In a nutshell, it prevents you from saving any data to DynamoDB if there were no modifications of the XML document. It's a valid case, and it has to be validated, but the way it's implemented is simply wrong. This is how it works (an oversimplified example):

class DyTalk implements Talk {
  void modify(Collection<Directive> dirs) {
    if (!dirs.isEmpty()) {
      // Apply the modification
      // and save the new XML document
      // to the DynamoDB table.
    }
  }
}

What's wrong, you wonder? This if-then-else forking functionality doesn't really belong to this object — that's what's wrong. Modifying the XML document and saving it to the database is its functionality, while not saving anything if the modification instructions set is empty is not (it's very similar to defensive programming). Instead, there should be a decorator, which would look like this:

class QuickTalk implements Talk {
  private final Talk origin;
  void modify(Collection<Directive> dirs) {
    if (!dirs.isEmpty()) {
      this.origin.modify(dirs);
    }
  }
}

Now, if and when we need our talk to be more clever in situations where the list of directives is empty, we decorate it with QuickTalk. The benefits are obvious: the DyTalk class is smaller and therefore more cohesive.

If it's possible to convert if-then-else forking to a decorator, it has to be done. But the question is bigger than just that. Can we make a rule out of it? Can we say that each and every forking is bad and should be moved out of a class? What about forking that happens inside a method and can't be converted to a decorator?

I'm suggesting this simple rule: If it's possible to convert if-then-else forking to a decorator, it has to be done. If it's not done, it's a code smell. Make sense?

Code smell

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Brief Overview of the Spring Cloud Framework
  • Fraud Detection With Apache Kafka, KSQL, and Apache Flink
  • How Observability Is Redefining Developer Roles
  • How To Create and Edit Excel XLSX Documents in Java

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: