Click here to Skip to main content
15,891,744 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Argggh! Noooooooo..... Pin
Rob Philpott18-May-14 23:02
Rob Philpott18-May-14 23:02 
GeneralRe: Argggh! Noooooooo..... Pin
NeverJustHere18-May-14 23:50
NeverJustHere18-May-14 23:50 
GeneralRe: Argggh! Noooooooo..... Pin
Roger Wright19-May-14 8:58
professionalRoger Wright19-May-14 8:58 
GeneralSounds about right to me... Pin
V.18-May-14 19:41
professionalV.18-May-14 19:41 
JokeI'm hoping this isn't a (recent) repost PinPopular
Sander Rossel18-May-14 11:00
professionalSander Rossel18-May-14 11:00 
GeneralRe: I'm hoping this isn't a (recent) repost Pin
MT_18-May-14 21:03
professionalMT_18-May-14 21:03 
JokeRe: I'm hoping this isn't a (recent) repost Pin
Vivi Chellappa18-May-14 22:14
professionalVivi Chellappa18-May-14 22:14 
GeneralBest Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Marc Clifton18-May-14 10:33
mvaMarc Clifton18-May-14 10: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? PinPopular
Pete O'Hanlon18-May-14 10:59
mvePete O'Hanlon18-May-14 10:59 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Marc Clifton18-May-14 11:08
mvaMarc Clifton18-May-14 11:08 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Eddy Vluggen18-May-14 22:32
professionalEddy Vluggen18-May-14 22:32 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Ed K19-May-14 14:51
Ed K19-May-14 14:51 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Sander Rossel18-May-14 11:26
professionalSander Rossel18-May-14 11:26 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Pete O'Hanlon18-May-14 11:38
mvePete O'Hanlon18-May-14 11:38 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Sander Rossel18-May-14 11:48
professionalSander Rossel18-May-14 11:48 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
kalberts18-May-14 22:03
kalberts18-May-14 22:03 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Sander Rossel19-May-14 9:58
professionalSander Rossel19-May-14 9:58 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Andy Brummer18-May-14 11:44
sitebuilderAndy Brummer18-May-14 11:44 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
SoMad18-May-14 12:32
professionalSoMad18-May-14 12:32 
AnswerRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Kornfeld Eliyahu Peter18-May-14 16:57
professionalKornfeld Eliyahu Peter18-May-14 16:57 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
kalberts18-May-14 22:10
kalberts18-May-14 22:10 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Dan Neely19-May-14 2:35
Dan Neely19-May-14 2:35 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
gggustafson19-May-14 4:09
mvagggustafson19-May-14 4:09 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Colborne_Greg18-May-14 16:59
Colborne_Greg18-May-14 16:59 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pin
Pete O'Hanlon18-May-14 20:20
mvePete O'Hanlon18-May-14 20:20 

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.