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 148159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:54:31+00:00 2026-05-11T08:54:31+00:00

I have seen it written in multiple threads/comments on stackoverflow that using switch is

  • 0

I have seen it written in multiple threads/comments on stackoverflow that using switch is just bad OOP style. Personally I disagree with this.

There will be many cases where you cannot add code (i.e. methods) to enum classes you want to switch on because you don’t control them, perhaps they are in a 3rd-party jar file. There will be other cases where putting the functionality on the enum itself is a bad idea because it violates some separation-of-concerns considerations, or it is actually a function of something else as well as the enumeration.

Last of all, switches are concise and clear:

boolean investable; switch (customer.getCategory()) {     case SUB_PRIME:     case MID_PRIME:         investible = customer.getSavingsAccount().getBalance() > 1e6; break;     case PRIME:         investible = customer.isCeo(); break; } 

I’m not defending every use of switch and I’m not saying it’s always the way to go. But statements like ‘Switch is a code smell’ are just wrong, in my opinion. Does anyone else agree?

  • 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-11T08:54:32+00:00Added an answer on May 11, 2026 at 8:54 am

    Taking your followup:

    What if this is just the ‘investibility’ logic for a customer wishing for a business loan. Perhaps the innvestibility decision of a customer for another product is really quite different … Also, what if there are new products coming out all the time, each with different investibility decisions and I don’t want to be updating my core Customer class every time this happens?

    and one of your comments:

    I’m not entirely sure about holding logic close to the data on which it operates. The real world doesn’t work like this. When I ask for a loan, the bank decides whether I qualify. They don’t ask me to decide for myself.

    you are right, as far as this goes.

    boolean investable = customer.isInvestable(); 

    is not the optimal solution for the flexibility you’re talking about. However, the original question didn’t mention the existence of a separate Product base class.

    Given the additional information now available, the best solution would appear to be

    boolean investable = product.isInvestable(customer); 

    The investability decision is made (polymorphically!) by the Product, in accordance with your ‘real world’ argument and it also avoids having to make new customer subclasses each time you add a product. The Product can use whatever methods it wants to make that determination based on the Customer’s public interface. I’d still question whether there are appropriate additions which could be made to Customer’s interface to eliminate the need for switch, but it may still be the least of all evils.

    In the particular example provided, though, I’d be tempted to do something like:

    if (customer.getCategory() < PRIME) {     investable = customer.getSavingsAccount().getBalance() > 1e6; } else {     investable = customer.isCeo(); } 

    I find this cleaner and clearer than listing off every possible category in a switch, I suspect it’s more likely to reflect the ‘real world’ thought processes (‘are they below prime?’ vs. ‘are they sub-prime or mid-prime?’), and it avoids having to revisit this code if a SUPER_PRIME designation is added at some point.

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

Sidebar

Ask A Question

Stats

  • Questions 100k
  • Answers 100k
  • 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
  • Editorial Team
    Editorial Team added an answer If I understand you correctly, you want to turn a… May 11, 2026 at 7:54 pm
  • Editorial Team
    Editorial Team added an answer Database access will on the whole be quicker (for many… May 11, 2026 at 7:54 pm
  • Editorial Team
    Editorial Team added an answer You can add what is called "metadata" in ActionScript like… May 11, 2026 at 7:54 pm

Related Questions

I am using VB6 and the Win32 API to write data to a file,
I'm developing a Django app, and I'm trying to use Python's logging module for
We have an application written in C#, using .NET Framework 3.0 or 3.5 or
I want to answer questions about data in Erlang: count things, correlate messages, provide

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.