Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 116417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:10:29+00:00 2026-05-11T03:10:29+00:00

This question is motivated by something I’ve lately started to see a bit too

  • 0

This question is motivated by something I’ve lately started to see a bit too often, the if..else if..else structure. While it’s simple and has its uses, something about it keeps telling me again and again that it could be substituted with something that’s more fine-grained, elegant and just generally easier to keep up-to-date.

To be as specific as possible, this is what I mean:

if (i == 1) {     doOne(); } else if (i == 2) {     doTwo(); } else if (i == 3) {     doThree(); } else {     doNone(); } 

I can think of two simple ways to rewrite that, either by ternary (which is just another way of writing the same structure):

(i == 1) ? doOne() :  (i == 2) ? doTwo() : (i == 3) ? doThree() : doNone(); 

or using Map (in Java and I think in C# too) or Dictionary or any other K/V structure like this:

public interface IFunctor() {     void call(); }  public class OneFunctor implemets IFunctor() {     void call() {         ref.doOne();     } }  /* etc. */      Map<Integer, IFunctor> methods = new HashMap<Integer, IFunctor>(); methods.put(1, new OneFunctor()); methods.put(2, new TwoFunctor()); methods.put(3, new ThreeFunctor()); /* .. */ (methods.get(i) != null) ? methods.get(i).call() : doNone(); 

In fact the Map method above is what I ended up doing last time but now I can’t stop thinking that there has to be better alternatives in general for this exact issue.

So, which other -and most likely better- ways to replace the if..else if..else are out there and which one is your favorite?

Your thoughts below this line!


Okay, here are your thoughts:

First, most popular answer was switch statement, like so:

switch (i) {     case 1:  doOne(); break;     case 2:  doTwo(); break;     case 3:  doThree(); break;     default: doNone(); break; } 

That only works for values which can be used in switches, which at least in Java is quite a limiting a factor. Acceptable for simple cases though, naturally.

The other and perhaps a bit fancier way you seem to sugges is to do it using polymorphism. The Youtube lecture linked by CMS is an excellent watch, go see it here: ‘The Clean Code Talks — Inheritance, Polymorphism, & Testing’ As far as I understood, this would translate to something like this:

public interface Doer {     void do(); }  public class OneDoer implements Doer {     public void do() {         doOne();     } } /* etc. */  /* some method of dependency injection like Factory: */ public class DoerFactory() {     public static Doer getDoer(int i) {         switch (i) {             case 1: return new OneDoer();             case 2: return new TwoDoer();             case 3: return new ThreeDoer();             default: return new NoneDoer();         }     } }  /* in actual code */  Doer operation = DoerFactory.getDoer(i); operation.do(); 

Two interesting points from the Google talk:

  • Use Null Objects instead of returning nulls (and please throw only Runtime Exceptions)
  • Try to write a small project without if:s.

Also in addition one post worth mentioning in my opinion is CDR who provided his perverse habits with us and while not recommended to use, it’s just very interesting to look at.

Thank you all for the answers (so far), I think I might have learned something today!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:10:29+00:00Added an answer on May 11, 2026 at 3:10 am

    These constructs can often be replaced by polymorphism. This will give you shorter and less brittle code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I do a lot of scraping, using excellent python packages… May 11, 2026 at 3:52 pm
  • added an answer Here's the perl code. Counting words can be somewhat subjective,… May 11, 2026 at 3:52 pm
  • added an answer It's the time (also known as 'round trip time') between… May 11, 2026 at 3:52 pm

Related Questions

This question is motivated by something I've lately started to see a bit too
I've set hibernate.generate_statistics=true and now need to register the mbeans so I can see
As the title suggests, I would like to see if this is something people
I am trying to use Difflib.SequenceMatcher to compute the similarities between two files. These
How do map providers (such as Google or Yahoo! Maps) suggest directions? I mean,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.