curl-library
Re: "The Most Dangerous Code in the World"
Date: Mon, 29 Oct 2012 10:12:04 +0400
On 10/28/2012 10:04 PM, Alan Wolfe wrote:
> Would it be possible to have the function take an enum instead of an int
> on top of whatever other solution you guys use?
>
> Its really telling watching this conversation how useful it would be.
>
> You guys are talking about 0, 1 and 2 which is completely meaningless
> without reading documentation or source code and in fact it would be
> super easy for people to even start confusing the meaning of the values
> in this conversation.
>
> If instead the parameter were something like: kDontCheck,
> kCheckNameOnly, kCheckNameAndHost - but named more in the libcurl
> style - then in code as well as in conversation it would be a lot more
> difficult to do or say the wrong thing.
>
> Imagine if fseek told you to use 0,1 and 2 instead of SEEK_SET,
> SEEK_CUR, SEEK_END. There would be a lot more file i/o bugs out there,
> and you'd constantly have to dig up documentation to remember which
> value did which behavior (does 0 seek to the end? or is that 2?)
>
> The upside: making the enum values be equal to the correct integers
> means old code still works with 0,1 and 2, but the documentation can be
> updated to say to use these enum values.
>
> The downside: It would be nicer if users were FORCED to use the enum
> (compiler error), or strongly suggested (compiler warning). But
> unfortunately I know of no way in C or C++ to cause an error when an int
> is used instead of an enum (if anyone else does it would be rad to see
> it!), and while i think that it makes a compile warning when you use an
> int instead of an enum (i forget for sure), going through the generic
> type of function call that this setting is set through makes it
> impossible to set the parameter expected as an enum so that's out too
> unfortunately.
>
> To really see what i mean, check out these two peices of text from this
> conversation, the first as is, with magic numbers, the second with
> symbolic constants.
>
IMHO, using enum values make APIs much cleaner, avoiding any possible
confusion in using integer values like 0,1,2, etc. I already do this in
my code for all CURL options. But having these enums provided by CURL
itself, makes any future changes in values, as in this case, transparent
to user code and APIs more intuitive to users, especially new users.
-lijo
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2012-10-29