Click here to Skip to main content

Welcome to the Lounge

   

For lazing about and discussing anything in a software developer's life that takes your fancy except programming questions.

Technical discussions are encouraged, but click here to ask your programming question.

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..... PinprofessionalRob Philpott19-May-14 0:02 
GeneralRe: Argggh! Noooooooo..... PinmemberNeverJustHere19-May-14 0:50 
GeneralRe: Argggh! Noooooooo..... PinmemberRoger Wright19-May-14 9:58 
GeneralSounds about right to me... PinprofessionalV.18-May-14 20:41 
JokeI'm hoping this isn't a (recent) repost PinprofessionalSander Rossel18-May-14 12:00 
GeneralRe: I'm hoping this isn't a (recent) repost PinprofessionalMT_18-May-14 22:03 
JokeRe: I'm hoping this isn't a (recent) repost PinmemberVivic18-May-14 23:14 
GeneralBest Practice Question - How do you prefer to pass a bunch of options to a function? PinprotectorMarc 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? PinprotectorPete O'Hanlon18-May-14 11:59 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? PinprotectorMarc Clifton18-May-14 12:08 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? PinprofessionalEddy Vluggen18-May-14 23:32 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? PinmemberDaniel R. Przybylski19-May-14 7:52 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? PinmemberEd K19-May-14 15:51 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? PinprofessionalSander Rossel18-May-14 12:26 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? PinprotectorPete O'Hanlon18-May-14 12:38 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? PinprofessionalSander Rossel18-May-14 12:48 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? PinmemberMember 798912218-May-14 23:03 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? PinprofessionalSander Rossel19-May-14 10:58 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? PinsitebuilderAndy Brummer18-May-14 12:44 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? PinprofessionalSoMad18-May-14 13:32 
AnswerRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? PinprofessionalKornfeld Eliyahu Peter18-May-14 17:57 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? PinmemberMember 798912218-May-14 23:10 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? PinmemberDan Neely19-May-14 3:35 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? Pinprofessionalgggustafson19-May-14 5:09 
GeneralRe: Best Practice Question - How do you prefer to pass a bunch of options to a function? PinmemberColborne_Greg18-May-14 17:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.8.150327.1 | Last Updated 28 Mar 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid