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

  • Home
  • SEARCH
  • 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 59793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:03:39+00:00 2026-05-10T18:03:39+00:00

I recently stumbled across this entry in the google testing blog about guidelines for

  • 0

I recently stumbled across this entry in the google testing blog about guidelines for writing more testable code. I was in agreement with the author until this point:

Favor polymorphism over conditionals: If you see a switch statement you should think polymorphisms. If you see the same if condition repeated in many places in your class you should again think polymorphism. Polymorphism will break your complex class into several smaller simpler classes which clearly define which pieces of the code are related and execute together. This helps testing since simpler/smaller class is easier to test.

I simply cannot wrap my head around that. I can understand using polymorphism instead of RTTI (or DIY-RTTI, as the case may be), but that seems like such a broad statement that I can’t imagine it actually being used effectively in production code. It seems to me, rather, that it would be easier to add additional test cases for methods which have switch statements, rather than breaking down the code into dozens of separate classes.

Also, I was under the impression that polymorphism can lead to all sorts of other subtle bugs and design issues, so I’m curious to know if the tradeoff here would be worth it. Can someone explain to me exactly what is meant by this testing guideline?

  • 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-10T18:03:40+00:00Added an answer on May 10, 2026 at 6:03 pm

    Actually this makes testing and code easier to write.

    If you have one switch statement based on an internal field you probably have the same switch in multiple places doing slightly different things. This causes problems when you add a new case as you have to update all the switch statements (if you can find them).

    By using polymorphism you can use virtual functions to get the same functionality and because a new case is a new class you don’t have to search your code for things that need to be checked it is all isolated for each class.

    class Animal {     public:        Noise warningNoise();        Noise pleasureNoise();     private:        AnimalType type; };  Noise Animal::warningNoise() {     switch(type)     {         case Cat: return Hiss;         case Dog: return Bark;     } } Noise Animal::pleasureNoise() {     switch(type)     {         case Cat: return Purr;         case Dog: return Bark;     } } 

    In this simple case every new animal causes requires both switch statements to be updated.
    You forget one? What is the default? BANG!!

    Using polymorphism

    class Animal {     public:        virtual Noise warningNoise() = 0;        virtual Noise pleasureNoise() = 0; };  class Cat: public Animal {    // Compiler forces you to define both method.    // Otherwise you can't have a Cat object     // All code local to the cat belongs to the cat.  }; 

    By using polymorphism you can test the Animal class.
    Then test each of the derived classes separately.

    Also this allows you to ship the Animal class (Closed for alteration) as part of you binary library. But people can still add new Animals (Open for extension) by deriving new classes derived from the Animal header. If all this functionality had been captured inside the Animal class then all animals need to be defined before shipping (Closed/Closed).

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

Sidebar

Ask A Question

Stats

  • Questions 97k
  • Answers 97k
  • 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 You need to set the InitialValue property on your RequiredFieldValidator… May 11, 2026 at 7:26 pm
  • Editorial Team
    Editorial Team added an answer I think you need to detect the rotation and resize… May 11, 2026 at 7:26 pm
  • Editorial Team
    Editorial Team added an answer No, the browser's security model should prevent you from tampering… May 11, 2026 at 7:26 pm

Related Questions

This is starting to vex me. I recently decided to clear out my FTP,
i recently stumbled upon a seemingly weird behavior that Google completely failed to explain.
I'm looking for the perfect Linux C++ debugger. I don't expect success, but the
I'm currently working on a Perl web app LAMP style and recently stumbled upon

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.