7 ms·
Named parameters in C
- abcd_f 12y agoClever, but seems like a solution in a search of a problem.
- davekeck 12y agoThe problem is that arguments are often ambiguous without the accompanying function declaration. (And the problem's exasperated when there are lots of arguments.) The solution is named arguments, but I'm not convinced the added complexity of this solution is a net improvement.
- JetSpiegel 12y agoIf you are writing in C, cryptic and unnecessary truncation is half the fun!
- clarry 12y agoMaybe there are some situations in which named arguments could be a genuine improvement, but I think in general it just encourages sloppiness and unneeded verbosity. It encourages writing functions that needlessly take way too many parameters (can't you instead think of a way to simplify the problem?). It encourages calling functions without paying attention to all the parameters, which you usually shouldn't ever ignore. Just like you shouldn't arbitrarily ignore return values. If you don't know your function like the back of your hand, you need documentation. Of course, documentation might be sparse in sloppy code that encourages people to ignore the details... C is like that really. You really need to pay attention to detail when you're writing C. And if there's too much to worry about, it means you need to redesign and simplify the APIs and/or data you're dealing with. If I encounter functions with lots of parameters in third party libraries I have to use, I will annotate each argument with a comment (after checking the documentation).
- userbinator 12y agoWhen writing in C, usually you either know the function well enough (including having just written it) to have memorised the arguments, have another window containing the declarations, or else you'll be using an IDE that tells you what the arguments are (along with a lot of other information.)
- nitrogen 12y agoThe most frustrating thing in C or C++ is when a header file is missing all of the argument names, so the declarations are just OVERKILL_TYPEDEF_PTR_T somefunc(TYPE1_T, TYPE2_PTR_T, void *); I always document my functions in the headers, especially for private code only I will use. E.g. /* * Like vfprintf(), but prepends a timestamp and the name of the current thread * or process. The output FILE will be locked using lockf() while the * timestamp, thread name, and message are written. The thread name can be set * using set_threadname(). */ int vfptmf(FILE *out, const char *format, va_list args);
- dllthomas 12y agoI find this problem exacerbated by using a lot of primitive types, and ameliorated (though not eliminated) by wrapping things in context-specific single element structs. Given: int price, quantity; compare place_order(price, quantity); with { price_t px = { price }; quantity_t qx = { quantity }; place_order(qx, px); } More verbose (especially where, like above, the values weren't already wrapped), but catches a significant error. It does not permit reordering arguments like the struct approach does, and doesn't help where you have multiple arguments of the same type that it is unreasonable to distinguish.
- xuhu 12y agoDoesn't work in MSVC though.
- pling 12y agoThat's not a proper C compiler though, as Microsoft like to point out regularly as a lame excuse...
- fdej 12y agoYet enough people think it's a C compiler that if you try to use avant garde C99 features in your C project, someone will invariably complain that MSVC doesn't build it.
- ibisum 12y ago.. thus giving up C and moving on to C# instead. (Students, I'm looking at you..)
- pjmlp 12y agoMSVC is actually called MSVC++. Microsoft is committed to C++, as a means to improve systems programming safety with more modern languages. Besides, the code makes use of GCC extensions.
- yxhuvud 12y agoThe article specifically states that it requires C99, so of course it doesn't.
- eq- 12y agoC99 compiler won't do, it also requires a GCC extension (statement expressions).
- cremno 12y agoYes, designated initializers were added in ISO C99 and since Visual Studio 2013 they are also supported by MSVC. But the code is neither ISO C99 nor C11. It uses a GNU C extension called “statement expressions”, which is not supported by MSVC. Also, its preprocessor has quite a few problems. I think after removing statement exprs and running the code through a different PP, MSVC will compile it. Of course, one could just simply use MinGW or Clang instead.
- nitrogen 12y agoNamed arguments can be done by passing a struct as the only parameter to a function, without the complicated macros. Missing arguments become 0, so you get optional parameters for free. func((struct somestruct){.x = 0, .y = 1});
- dsego 12y agoWhat's the point in that? Then you have to write all your functions to accept structs instead of natural arguments.
- ygra 12y agohttp://c2.com/cgi/wiki?ParameterObject http://c2.com/cgi/wiki?ParameterObject Sometimes it's a good way to go.
- simgidacav 12y ago...agreed, what's the point? Besides, a lot of includes are already a mess of #defines, do we really need another bunch of them?
- eliteraspberrie 12y agoThis (compound literals) also has the advantage of being strictly valid C, since compound statements are a GNU extension.
- eq- 12y agoIt's not (strictly) C, though.
- deleted 12y ago[deleted]
- hyperliner 12y agoI think it is an interesting article as a lesson on how macro expansion works. It is however, a terrible example of how to write good code. Any code that hides the language or manipulates for syntactic sugar only adds complexity, bugs and decreases the maintenability of the code. As someone already said, a solution in search of a problem.