• Naomi (unregistered)

    This is a pretty typical use case for std::FootGunFactory.

  • 516052 (unregistered)

    Ah, the good old 100 case switch. Remember kids, if the list of conditions alone can't fit on your screen you should refactor.

  • (nodebb)

    Helicopter parents are a bad influence on their children.

  • (nodebb) in reply to 516052

    In this case, even 3 cases would be 3 too many.

  • John R. Strohm (unregistered) in reply to 516052

    There are exceptions to the guideline on a switch(). The most common one is a complex state machine.

    I think my personal record is somewhere around 38 states, in an automated test that parsed a COMPLICATED data file to set up a very complex device. (I don't feel comfortable saying any more than that about it.) There were a lot of things that had to be checked, the sequencing had to be correct, and there were several possible correct ways things could be sequenced.

  • J. (unregistered)

    You would expect that at some point they stop and ask themselves "There has to be an easier way...". Or maybe not. Do the little change and don't touch anything else as I bet there are no automated tests either.

  • my name is missing (unregistered)

    The worst C++ I ever saw in a commercial application (did a short contract there) had very few classes, but each class was subclassed up to 9 levels deep, which each level either adding dozens of new methods/variables and overriding a large number of superclass items. Basically reading the code was painful as you never had any easy way to know what the code you were looking at actually did without lots of tracing up the class chain.

  • (nodebb)

    I have been programming in C++ on and off since the early 1990's. My first thought on seeing a template without a parameter was, "Will that even compile?" Apparently it can, but it really shouldn't. Of course, when I define a C++ template, (in the .h header file by the way) I make sure that the template parameter MUST appear in the parameters of the function somewhere. Otherwise what is the point, other than unnecessarily using compiler resources? My favourite example of a reasonable function template:

    template <class T>
    void swap(T &value1, T &value2)
        T temp = value1;
        value1 = value2;
        value2 = temp;

    What we saw from the article, definitely not reasonable. So many opportunities for compiler optimization thrown away.

  • Foo AKA Fooo (unregistered) in reply to Nutster

    It's not actually a template without a parameter (which AFAIK it really not possible). It's a specialization. The syntax is a bit confusing at first, since specializations can be templates again, e.g. you could do:

    template <typename T1, typename T2> struct foo {};
    template <typename T1> struct foo <T1, int> {};  // partial specialization for the case where the 2nd parameter is int
    template <> struct foo <int, int> {};  // full specialization

    A full specialization needs the "template <>" part to distinguish it from a non-template, but it's actually still a specialization of the original template with (here) 2 parameters.

    This means, they got the syntax right, but not much else.

  • ooOOooGa (unregistered)

    one of those small companies that lives in the shadow of a university. It was founded by graduates of that university, mostly recruited from that university, and the CEO was a fixture at alumni events.

    That is the source of the problem right there. The CEO is trying to keep costs down by hiring recent college graduates who couldn't get jobs somewhere else.

    But I'm sure we all recognized that immediately.

  • (nodebb)

    Points for default: assert(false); I guess?

    This looks a lot like someone started out in C, then got all hot and bothered about switching to OOP but lost interest after the first chapter of "Teach Yourself C++".

  • Randal L. Schwartz (google)

    I once saw a presentation by a rather famous language creator that used the "switch based on type" method in a common base class, and "test if the object can do something and if so call it otherwise do nothing" despite having a common base class for a backstop do-nothing method. In fact he suggested that since the latter happened so often, there should probably be a built-in in the language for it. I haven't checked the snake language recently to see if that ever made it in. It did thoroughly convince me that if the designer of the language was that far off, err, "base" in a 60-minute presentation, that I would have nothing else to do with that language. And haven't. Oh, and I pointed out a design flaw in one of the methods, which he confirmed.

  • Gummy Gus (unregistered) in reply to Naomi

    I briefly worked for a medium-sized company who had a flagship app that should have been doable in maybe 30,000 lines of code, but actually was about 1.5 million lines. And those were super-convoluted templates and called through interface to a fare-thee-well lines. As an example, the app would generate "syntax error in config file" error messages, with an 18-level stack traceback. A useless traceback, as it stopped where the code had done a call through an anonymous interface. We spent three days trying to find the location of the error and eventually just gave up. Even tracing from the error message text proved fruitless. And, oh, the compiler would routinely crash as it was an eight year old compiler with no easy upgrade path. I was solo glad to get canned from that place!!

  • Meh (unregistered) in reply to Nutster

    std::get() would be a function template where it makes sense you have to specify a template argument explicitly. std::make_shared() and std::make_unique() are more examples.

  • David Mårtensson (unregistered) in reply to John R. Strohm

    A lexer parser is another area where quite large switch statements are a valid option, the same with an interpreter, at least as long as you are not planing to dynamically add commands ;)

  • 516052 (unregistered)

    It's just a more poetic way of saying that if a switch has grown so large you are having trouble keeping track of how it works and what its supposed to do odds are it's too large.

  • Pietro (unregistered)

    The variety of footguns that C++ allows you to create is simply stunning. I do not think that there will ever be another language capable of this.

  • I dunno LOL ¯\(°_o)/¯ (unregistered)

    "An isosceles triangle"? Well it wasn't a right triangle, so maybe it was actually a wrong triangle.

  • 516052 (unregistered) in reply to Pietro

    Guns don't shoot feet. Idiots do.

  • jay (unregistered)

    " which means the parent has to know each of its children" This is problematic both in software development and among some of my friends.

Leave a comment on “Switching Your Template”

Log In or post as a guest

Replying to comment #:

« Return to Article