1

Suppose the following two versions of the same class A:

Version 1

class A
{
   public:
      enum class Retrievable { Integer, String };

      A(): m_integer(123), m_string("string") {}
      ~A() {}

      void* get(const Retrievable r)
      {
         switch (r)
         {
            case Retrievable::Integer:
               return static_cast<void*>(&m_integer);
            case Retrievable::String:
               return static_cast<void*>(&m_string);
            default:
               throw;
         }
      }

   private:
      int m_integer;
      std::string m_string;
};

Version 2

class A
{
   public:
      A(): m_integer(123), m_string("string") {}
      ~A() {}

      int getInteger()        { return m_integer; }
      std::string getString() { return m_string;  }

   private:
      int m_integer;
      std::string m_string;
};

In terms of clarity, efficiency, and extensibility, which of these two paradigms -- a single typeless get() function or separate explicit get() functions -- is better?

5
  • The latter. In the former, you need to specify both the parameter r and the return type which is error-prone and superfluous. It's also not possible to make it virtual. The former is useful when you use the specified type in some way, e.g. for dynamic_cast but in this example you don't.
    – StenSoft
    Apr 11, 2016 at 18:44
  • Code that doesn't compile is faster than code that does. Version 1 doesn't compile, so clearly is better performance. Wait, what? If you want to know if option 1 or option 2 is better, please provide actual working code that describes your options. Because the above does not make any sense -- I'd have to fundamentally rewrite one of your options to make it compile, and who knows if I'd rewrite it the same code the same way. Apr 11, 2016 at 19:16
  • @Yakk Honestly, I haven't gotten to a point in my coding where I can compile a working sample yet. However, I ended up using the second method for other reasons. This was a theoretical exercise I was curious to know the answer to. I'm sure I could get something like this to compile. The question was about the theory behind it, not specifically what I posted (I figured it was at least partly wrong).
    – Frostfyre
    Apr 11, 2016 at 20:08
  • @frost except the parts you gloss over -- getting "something like this to compile" -- have performance impacts. Because the resulting code looks very little like what you posted, in at least 2 or 3 different possible ways. Please ask about code that you have compiled, not code that does not work, when askimg about something like performance Apr 11, 2016 at 20:18
  • @Yakk I've updated the code snippets to something that compiles and works.
    – Frostfyre
    Apr 11, 2016 at 20:42

2 Answers 2

3

Option 1 casts things to void* and does run time dispatching, such is slower and less safe and harder to use.

Barring interacting with a scripting engine, I cannot imagine a reason to use option 1. And even then there are many better ways to handle it. An interviewee who proposed option 1 as even an alternative choice to option 2 would be a strong do not hire, and it would make me reconsider the funnel that got the interviewee this far.

In short, option 2 is better.

1

Version A will not compile anyway, because regardless of C you will try to return type which is not C (pointer where C is string etc.)

Version B is maybe better, but also quit crooked. unless you are building some polymorphic type like JSON node or something, the function should describe behaviour, not return type.

again, unless you are writing some JSON class, I don't see much sense of making a function which describe return type rather than behaviour. a Person doesn't have getHand or getLeg functions, he has functions like wave, walk etc.

performace? I doubt that a getter will screw the performance of your application.

2
  • These were just generic names for the concept. So, for example, getInteger() could have been getCurrentGameLevel() and return m_currentGameLevel.
    – Frostfyre
    Apr 11, 2016 at 18:57
  • so basically the comparison is between get(Option::GameLevel) and getGameLevel? without any question the second one. since it describes behaviour and determined at compile time
    – David Haim
    Apr 11, 2016 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.