1

My background and instincts tell me I should always create a tighter and more explicit interface to a function by requiring parameters like this:

bool someFunc(int param1, int param2, char param3, float param4) {
   ...
}

or requiring an object (struct or class) like:

class someObject {
    ...
    int p1;
    int p2;
    char c1;
    float p4;
}

I have been told by my boss that I should be using something like:

bool someFunc(void *params[], int size) {
   ...
}

because it creates more extensible (you can iterate over parameters this way) and faster code.

I am only interested in improving my abilities, but my instincts go against this method. Is he right?

Web Devie
  • 1,207
  • 1
  • 13
  • 30
Chad Befus
  • 1,918
  • 1
  • 18
  • 18
  • 1
    You can't iterator over `params`. Did you mean to write `bool someFunc(void* params[])` or something along those lines (you'd also need to know the number of arguments)? If so, I'd be rather surprised if it were faster than passing built-in types by value. – Dietmar Kühl Oct 28 '13 at 23:31
  • You'd probably get more attention for this question posting it in http://programmers.stackexchange.com/ :-) – Karl Nicoll Oct 28 '13 at 23:32
  • @DietmarKühl - yes, what I meant, thanks. Updated to reflect. – Chad Befus Oct 28 '13 at 23:39
  • Second on Dirtmar's comments. You can't dereference `void*` without casting it back to a real type. Also, the input argument is a pointer array without length, so you don't know the size of the array either. – tonga Oct 28 '13 at 23:55
  • @tonga - You are both right, it requires the number of arguments as well for his implementation. Updated to reflect. – Chad Befus Oct 29 '13 at 00:16

3 Answers3

5

Horrible idea. I can't list the reasons why it's bad in a single answer, but the main problem is that it just doesn't work. As a simple example, you can't pass 3, and if you pass 0 it becomes a nullptr. More importantly, you have to cast the values back to a given type anyway, so why not specify the type in the signature?

Now there's a real C++ alternative, variadic templates:

template<typename... Arguments>
void Foo(Arguments... parameters);

In this case, the compiler will know all the types in Arguments..., there's no need to cast anything.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • You are correct that I would have to cast the types back, it was recommended that I do this by passing the type of each parameter as another array in the parameters. I have never seen, or worked with variadic templates before but I have learned a lot reading about them (thanks). Unfortunately our compiler does not allow for c++11 features yet. – Chad Befus Oct 29 '13 at 01:22
0

I disagree with the above answer, your instincts are right you should try and create a explicit interface to a function. There are times where you can't have an explicit interface, and then you should consider variadic template arguments, but they're rare. Two Ints a char and float seem like a perfectly reasonable function parameters.

However you're in a very sticky situation, you don't want to go against your boss. I would be skeptical of any programming advice from him, he's either not a very good programmer or worse one of those old school hacky c programmers (see if he uses MACROS everywhere). My advice is to do it his way now, then if you're ever working with that function again later fix it and try and get someone else to review your code.

nykwil
  • 144
  • 4
  • Thanks for the advice, and validation. I am in the very sticky situation you outline and I intend on writing the best code I can, even if those are the grounds that get me fired (I have no trouble finding another job). I just am junior enough to not be sure when I am right or he is -- thus the post. I want to learn the correct answer and I will implement that. – Chad Befus Oct 29 '13 at 01:29
0

Your boss is crazy. Such debauchery had some place in C, and even there you would use varargs, not this crazy construct. If you want to program in a dynamically-typed language, either don't use C++, or use arrays of variant types (say boost::variant or QVariant).

The "extensibility" your boss is looking for and obviously missing is otherwise known as function/method overloading. It has its place as a part of a proper design, not as an edict.

Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • I agree with you on every point, except that where I work we unfortunately cannot afford the expense of using things such as boost or even STL. – Chad Befus Nov 06 '13 at 00:10
  • 1
    @user2705235: So, your boss thinks that your own home-baked solution done by people with much less C++ experience will be better than `boost::variant`? Yeah, good luck with that. Popular libraries such as `boost` have the benefit of many nit-pickers trying to show themselves off by finding problems. Things like `boost::variant` are often optimized to hell and back, multiple times. – Kuba hasn't forgotten Monica Nov 06 '13 at 15:33