tag:blogger.com,1999:blog-3870801931584460413.post5459000595088131639..comments2024-01-03T11:08:42.595-08:00Comments on On Software and Languages: Named function arguments for C++Marek Krjhttp://www.blogger.com/profile/16877868679118775297noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-3870801931584460413.post-13100014456120790382010-12-05T07:53:16.467-08:002010-12-05T07:53:16.467-08:00Hi, very interesting post, greetings from Greece!Hi, very interesting post, greetings from Greece!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3870801931584460413.post-36251154624341185902010-08-30T01:41:11.387-07:002010-08-30T01:41:11.387-07:00@Arun Saha:
I think, my interface design was slop...@Arun Saha:<br /><br />I think, my interface design was sloppy to start with. This whole bool optionXXX thing is crap. As you design carefully, you'll see that an option logically isn't a bool value, but an option value! <br /><br />But I normally don't have time to think (sadly) and than have to improve things afterwards, as I won't go so far as to redesign such an interface. An that's what that trick is basically for, to somehow mitigate the technical debt if you want so.<br /><br />Thanks for the comment(s), it improved the understanding of my own code!Marek Krjhttps://www.blogger.com/profile/16877868679118775297noreply@blogger.comtag:blogger.com,1999:blog-3870801931584460413.post-2107294949670096182010-08-25T20:22:29.925-07:002010-08-25T20:22:29.925-07:00Interesting post and comments.
What I like and us...Interesting post and comments.<br /><br />What I like and use is the one similar to Andrew Fray's suggestion.<br /><br />But, note that it changed the function signature from "bool" to "FormattingFlag". If I can change the signature, then I would go for that. If I can't change the signature of func, then I might do the following (a slight variation from the post):<br /><br /> const bool UseOptionX = true;<br /> const bool DontUseOptionX = !UseOptionX;<br /><br />My choice of placing them would be:<br /><br /> 1. As static const inside class XXX, so that I can call func() as<br /><br /> func( 1, "test", XXX::UseOptionX );<br /><br /> 2. In a place where all callers of XXX::func can see<br /><br /> 3. In my namespace/moduleArun Sahahttps://www.blogger.com/profile/04297883033868492520noreply@blogger.comtag:blogger.com,1999:blog-3870801931584460413.post-53338454669515735072010-08-25T00:20:24.217-07:002010-08-25T00:20:24.217-07:00@knatten:
you've got a point here. It passed b...@knatten:<br />you've got a point here. It passed briefly my mind when writing, but then I discardd it as non-issue. But you're right, if the title of a post is "xxx for C++" then the reader expects rather a comprehensive solution.<br /><br />So it's a question of tradeoff really:<br /><br />1. typedef trick: lightweight, general, but no so type safe<br />2. enums by Andrew Fray (BTW I like it more and more as I think about it) - simple, typesafe, not general<br />3. classes with constructors by Codemonkeyism - heavyweight, typesafe, general, possibly killing performance<br />4. Boost - possibly the full solution, possibly dragging a ton of dependencies in...<br /><br />Now decide!Marek Krjhttps://www.blogger.com/profile/16877868679118775297noreply@blogger.comtag:blogger.com,1999:blog-3870801931584460413.post-24456588794100560042010-08-25T00:05:10.037-07:002010-08-25T00:05:10.037-07:00@secret_town:
yes, it's not a panaceum...
@E...@secret_town: <br />yes, it's not a panaceum...<br /><br />@Emre B: <br />1st thought: cool!!!<br />2nd thought: I realized I did it sometimes before, but it wasn't estetically apealling ;-) to me<br /><br />@Let_Me_Be:<br />If you have had a look at a Boost istallation directory (I've got 1.35.o) - it's huge and completely uncomprehensive. I bet there IS a lot of interdependecies (but I don't know for sure).<br /><br />@Andrew Fray:<br />Ok, tht's the classical option. I'd use it to choose from several possiblities, but not for bool or int parameters.Marek Krjhttps://www.blogger.com/profile/16877868679118775297noreply@blogger.comtag:blogger.com,1999:blog-3870801931584460413.post-42654896080479799922010-08-24T10:57:37.874-07:002010-08-24T10:57:37.874-07:00I've got much-much simple (single header, almo...I've got much-much simple (single header, almost no dependencies)<br />of the same concept here:<br /><br />http://www.boost.org/doc/libs/1_44_0/boost/test/utils/named_params.hppGennadiyhttps://www.blogger.com/profile/15199048235731653801noreply@blogger.comtag:blogger.com,1999:blog-3870801931584460413.post-38731147013342371742010-08-24T03:36:34.202-07:002010-08-24T03:36:34.202-07:00Spot the bug:
void f(bool useX, bool useY);
type...Spot the bug:<br /><br />void f(bool useX, bool useY);<br /><br />typedef bool useX;<br />typedef bool useY;<br /><br />f(useY(true), useX(false));<br /><br />While the readability is increased, it can lead to a false sense of security. Kind of like how wrong comments are worse than no comments.<br /><br />Using proper classes for this would let the C++ type system help you avoid mistakes like this. And if the class is just a POD class with one bool, there shouldn't be any overhead either.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3870801931584460413.post-74041782082665051092010-08-24T01:14:24.219-07:002010-08-24T01:14:24.219-07:00I tend to use the enum approach:
enum FormattingF...I tend to use the enum approach:<br /><br />enum FormattingFlag<br />{<br /> EnableFormatting,<br /> DisableFormatting,<br />};<br /><br />void parse(const char* str, FormattingFlag formatting);<br /><br />\\ ...<br /><br />parse("my string", EnableFormatting);<br /><br />Readable, debuggable, with no ctors or dtors.Andrew Frayhttps://www.blogger.com/profile/14321316578604374276noreply@blogger.comtag:blogger.com,1999:blog-3870801931584460413.post-18982163992809787352010-08-24T00:00:27.296-07:002010-08-24T00:00:27.296-07:00Inter-dependencies in Boost? Yes boost is huge, bu...Inter-dependencies in Boost? Yes boost is huge, but not because its a huge library. Boost is huge, because it is a huge collection of independent libraries. Many of those are even header only (no installation, just copy and include).<br /><br />(although I admit, that I have no idea what exactly does this one specific sub-library require)Unknownhttps://www.blogger.com/profile/14158154976201931656noreply@blogger.comtag:blogger.com,1999:blog-3870801931584460413.post-82962055735843916252010-08-23T17:15:21.233-07:002010-08-23T17:15:21.233-07:00This is possible also:
func(1, "test", /...This is possible also:<br />func(1, "test", /*optionX=*/true);Emre Bnoreply@blogger.comtag:blogger.com,1999:blog-3870801931584460413.post-15367222781950044642010-08-23T16:18:53.400-07:002010-08-23T16:18:53.400-07:00This is nice, but only goes so far. If you had (ch...This is nice, but only goes so far. If you had (choke) 13 string parameters, it would not help at all to make sure you didn't switch a couple. Still, nice tactic for readability.secret_townnoreply@blogger.comtag:blogger.com,1999:blog-3870801931584460413.post-2960795591700968122010-08-19T03:20:51.804-07:002010-08-19T03:20:51.804-07:00@Dean Michael Berris
Hi Dean,
wow, this is a com...@Dean Michael Berris<br /><br />Hi Dean,<br /><br />wow, this is a complete implementation of Python's named arguments! <br /><br />But:<br /><br />since I had some problems with ACE in the past, I took a dislike to big frameworks. And if you'll have a look at a Boost installation: it's huge! And there is so much interdependency! If a little nifty trick can spare me all that dependencies then I'm in for it!<br /><br />Having said that, I'm not against Boost (well, not etirely), but in my current work I have to use another big framework already (Qt) so I don't want to mix it with another big one.<br /><br />On the other side, at least one of Boost libraries advertises oneself saying that it hasn't dependencies on other parts of Boost, so maybe my fear isn't well-founded? <br /><br />But nonetheless, thanks for the link! I've missed (or already forgotten) some Boost libraries as it seems, and I'd definitely use the Parameter library when designing a bigger interface!Marek Krjhttps://www.blogger.com/profile/16877868679118775297noreply@blogger.comtag:blogger.com,1999:blog-3870801931584460413.post-34335651608692697842010-08-17T10:03:16.987-07:002010-08-17T10:03:16.987-07:00You'll love Boost.Parameter when you take a lo...You'll love Boost.Parameter when you take a look there.<br /><br />http://www.boost.org/doc/libs/1_44_0/libs/parameter/doc/html/index.htmlDean Michael Berrishttp://cplusplus-soup.comnoreply@blogger.com