Click here to Skip to main content
13,257,666 members (76,869 online)

Welcome to the Lounge

   

For discussing anything related to a software developer's life. Technical discussions are encouraged, but click here to ask your programming questions.

The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. No flame wars, no abusive conduct, no programming questions and please don't post ads.
 
GeneralRe: Argggh! Noooooooo..... Pin
Rob Philpott19-May-14 0:02
professionalRob Philpott19-May-14 0:02 
GeneralRe: Argggh! Noooooooo..... Pin
NeverJustHere19-May-14 0:50
memberNeverJustHere19-May-14 0:50 
GeneralRe: Argggh! Noooooooo..... Pin
Roger Wright19-May-14 9:58
memberRoger Wright19-May-14 9:58 
GeneralSounds about right to me... Pin
V.18-May-14 20:41
professionalV.18-May-14 20:41 
JokeI'm hoping this isn't a (recent) repost Pin
Sander Rossel18-May-14 12:00
professionalSander Rossel18-May-14 12:00 
GeneralRe: I'm hoping this isn't a (recent) repost Pin
MT_18-May-14 22:03
professionalMT_18-May-14 22:03 
JokeRe: I'm hoping this isn't a (recent) repost Pin
Vivic18-May-14 23:14
memberVivic18-May-14 23:14 
GeneralBest Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Marc Clifton18-May-14 11:33
protectorMarc Clifton18-May-14 11:33 
Key-Value Options:

Let's say you have a function that can take a bunch of options for how to build something, say some auto-generated HTML, when passing them as parameters is just too cumbersome? Personally, I would just put all the options into a struct and pass an instance of the struct into the function. The nice thing about that is, the struct (or class, if you wish) documents all the possible options.

Now, in the land of Ruby, I see everybody everywhere using key-value pairs associated with symbols, like this:

{option1: true, option2: "foobar", option3: 42}

Now of course the symbols usually have some intelligent meaning, but you have absolutely no clue what these optional parameters are unless you look up the online documentation (if it exists) and then can be sprawled across numerous pages on the website.

And worse, in the Ruby code, these options are of course usually tested using the symbol:

if opts[:option1] ... end

such that, if you mistype the "key" (symbol), nothing is going to complain to you, unless the programmer checks the option list for unknown options, which I have NEVER seen done.

Now, there's lots of alternatives in the, say, C# world. Your function can take a variable number of parameters. You can require that the caller provides a callback for resolving options. You can just put everything into the function's parameter list, and so forth. Or, like Rubyists, you can pass in a dictionary or some such thing of options.

Valueless Options (aka flags):

So far, the above discussion deals with options that have associated values. There is also the issue of "valueless" options -- if the "key" is present, then the option is "selected." One of the most common ways of passing in valueless options to a function in most languages is of course with an enum, especially when you can use the "or" operator to combine multiple valueless options. Now, mind you, in Ruby, there is no concept of an enum, which is a serious drawback in my opinion.

But I'm curious what people consider to be their own best practice.

Marc
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Pete O'Hanlon18-May-14 11:59
protectorPete O'Hanlon18-May-14 11:59 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Marc Clifton18-May-14 12:08
protectorMarc Clifton18-May-14 12:08 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Eddy Vluggen18-May-14 23:32
professionalEddy Vluggen18-May-14 23:32 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Daniel R. Przybylski19-May-14 7:52
memberDaniel R. Przybylski19-May-14 7:52 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Ed K19-May-14 15:51
memberEd K19-May-14 15:51 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Sander Rossel18-May-14 12:26
professionalSander Rossel18-May-14 12:26 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Pete O'Hanlon18-May-14 12:38
protectorPete O'Hanlon18-May-14 12:38 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Sander Rossel18-May-14 12:48
professionalSander Rossel18-May-14 12:48 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Member 798912218-May-14 23:03
memberMember 798912218-May-14 23:03 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Sander Rossel19-May-14 10:58
professionalSander Rossel19-May-14 10:58 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Andy Brummer18-May-14 12:44
sitebuilderAndy Brummer18-May-14 12:44 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
SoMad18-May-14 13:32
professionalSoMad18-May-14 13:32 
AnswerRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Kornfeld Eliyahu Peter18-May-14 17:57
professionalKornfeld Eliyahu Peter18-May-14 17:57 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Member 798912218-May-14 23:10
memberMember 798912218-May-14 23:10 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Dan Neely19-May-14 3:35
memberDan Neely19-May-14 3:35 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
gggustafson19-May-14 5:09
professionalgggustafson19-May-14 5:09 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Colborne_Greg18-May-14 17:59
memberColborne_Greg18-May-14 17:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy |
Web04 | 2.8.171114.1 | Last Updated 23 Nov 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid