Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Best Practice Question - How do you prefer to pass a bunch of options to a function?

Best Practice Question - How do you prefer to pass a bunch of options to a function?

Scheduled Pinned Locked Moved The Lounge
questiondiscussioncsharprubyhtml
39 Posts 21 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Marc Clifton

    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

    S Offline
    S Offline
    SoMad
    wrote on last edited by
    #8

    I also subscribe to the struct approach (of course you need to determine the break even point between how many parameters you have before it is worth wrapping them in a struct), but am going to go a little further. I have projects where the function I wish to call may be several layers deeper or I need to add a new request from a client UI and direct a call to a new function on the server - I know you have all been there too and know what I am talking about. In some of my projects, I have a generic message class that can be XML'ified (, compressed, encrypted) and transmitted between server and client. The class has a member variable, which is a list of name/value pairs and this can be used directly for most parameters. When adding new stuff, only the sender and receiver function need to know about the parameters and all layers in between just work without being updated. For more advanced parameters, such as structs, the class has a string member variable that can be an XML string containing any kind of object that has been serialized to XML. That whole core is kind of old and would probably benefit from being updated to JSON. If extreme performance is an issue, I avoid the whole back and forth string conversion and use a different mechanism, but for normal applications the processing time is plenty fast enough and the savings in maintenance when you need to add a new parameter is well worth it. Soren Madsen

    "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

    1 Reply Last reply
    0
    • M Marc Clifton

      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

      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu Peter
      wrote on last edited by
      #9

      In most cases I consider a large number of parameters (let say over 8) as design flaw, however it is possible that you end with such a list, in which case I would use struct/class...

      I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

      "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

      K 1 Reply Last reply
      0
      • M Marc Clifton

        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

        C Offline
        C Offline
        Colborne_Greg
        wrote on last edited by
        #10

        When Dealing with Microsoft related programming products creating your procedures with the parameters (Sender as object, e as eventArgs) as an extremely recommended practice, and have your parameter class inherit from system.eventArgs. This way your code naturally interacts with windows runtime components.

        P 1 Reply Last reply
        0
        • M Marc Clifton

          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

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #11

          I'm not sure that I've ever needed to do that other than in cases similar to database connection strings -- like a factory of some sort -- and then XML works. :-D @"" Obviously, the problem with "but you have absolutely no clue what these optional parameters are unless you look up the online documentation (if it exists)" is taken to a whole new level. :omg:

          You'll never get very far if all you do is follow instructions.

          1 Reply Last reply
          0
          • M Marc Clifton

            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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #12

            I think I have some internal, arbitrary rule that says; If the parameters are basically unrelated except for being passed to that function, then leave them as individual arguments, well defined by their names. If the parameters are related, then wrap them in some structure or other. In other words, I wouldn't wrap parameters in a class or struct just to make the function signature smaller or more legible, but I would do so if it made sense for the parameters to be combined; The danger of wrapping them is that you replace a long function call with a complex struct creation, to no real advantage - reading a method signature with meaningful argument names is self-documentation; the same method with a single argument called "necessaryInfoToDoTheJob is less descriptive!

            J 1 Reply Last reply
            0
            • C Colborne_Greg

              When Dealing with Microsoft related programming products creating your procedures with the parameters (Sender as object, e as eventArgs) as an extremely recommended practice, and have your parameter class inherit from system.eventArgs. This way your code naturally interacts with windows runtime components.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #13

              Except that you have created event arguments for use with none events. The sender is probably going to be completely useless.

              C 1 Reply Last reply
              0
              • L Lost User

                I think I have some internal, arbitrary rule that says; If the parameters are basically unrelated except for being passed to that function, then leave them as individual arguments, well defined by their names. If the parameters are related, then wrap them in some structure or other. In other words, I wouldn't wrap parameters in a class or struct just to make the function signature smaller or more legible, but I would do so if it made sense for the parameters to be combined; The danger of wrapping them is that you replace a long function call with a complex struct creation, to no real advantage - reading a method signature with meaningful argument names is self-documentation; the same method with a single argument called "necessaryInfoToDoTheJob is less descriptive!

                J Offline
                J Offline
                Jorgen Andersson
                wrote on last edited by
                #14

                :thumbsup:

                Wrong is evil and must be defeated. - Jeff Ello[^]

                1 Reply Last reply
                0
                • M Marc Clifton

                  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

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #15

                  You preferred way is also the Windows API one: on MSDN you may find a neat, short page about the function, the big mess being on the passed struct.

                  Veni, vidi, vici.

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • Sander RosselS Sander Rossel

                    I'm personally not a big fan of it, but in .NET you have optional parameters. They look like the example you showed in Ruby (but I don't know if they're the same thing since I don't know Ruby). For example:

                    	private void SomeMethod(bool useThisOption = true, string optionName = "")
                    	{
                    		// ...
                    	}
                    
                    	private void UseOptionalParams()
                    	{
                    		this.SomeMethod(optionName: "MyOption"); // useThisOption is true by default.
                    	}
                    

                    For more information: Named and Optional Arguments[^]

                    It's an OO world.

                    public class SanderRossel : Lazy<Person>
                    {
                    public void DoWork()
                    {
                    throw new NotSupportedException();
                    }
                    }

                    K Offline
                    K Offline
                    kalberts
                    wrote on last edited by
                    #16

                    I believe that strictly speaking, you do NOT have optional parameter in .NET. You have it in C#. But it is handled solely by the compiler, at compile time. Sort of synthetic sugar. (Or sugar-free, since we are talking about parameters who are not there in the source code.) In the .NET assembly, noone can tell whether the value specified as default in the function declaration was really defaulted or specified explicitly by the caller.

                    Sander RosselS 1 Reply Last reply
                    0
                    • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                      In most cases I consider a large number of parameters (let say over 8) as design flaw, however it is possible that you end with such a list, in which case I would use struct/class...

                      I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                      K Offline
                      K Offline
                      kalberts
                      wrote on last edited by
                      #17

                      In the old days, I was working with a Fortran compilers where we had to push the release due to one (important) customer who had run into a limitation of the previous one: It could take only 99 (ninety-nine) parameters. The new version could handle 128 parameters, which was sufficient for the immediate needs of the customer, but the design allowed expansion to 256 parameters. If there ever was a case for Fortran COMMON blocks, I would call this a candidate. (Don't take me wrong - I am not suggesting using COMMON blocks as good programming practice. Nor is 100+ function parameters.)

                      D G 2 Replies Last reply
                      0
                      • P Pete OHanlon

                        I would have to go with wrapping the parameters in a struct/class. That way, I can put all sorts of validation in there that gives the object some context.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #18

                        Some more advantages; it's more readable when there are lots of arguments, one can easily modify the parameter-set without having to touch each method, and one can even inherit a new set of parameters. EventArgs - wonderfull example.

                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                        1 Reply Last reply
                        0
                        • M Marc Clifton

                          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

                          K Offline
                          K Offline
                          kalberts
                          wrote on last edited by
                          #19

                          We have been using the struct practice for years, and I am personally strongly in favor of it. I would like to add a few comments/modifications, though: First, a set of options is not a function declaration issue, but a system data design issue. Two or more functions referring to, say, page layout properties should not declare "their own" parameter structs (possibly overlooking some essential parameter). The set of options affecting page layout, say, is one well defined set for the entirre application (or even more). Second, you should never slump together completely unrelated options in one struct, even if one function (or even several) inspects them all. One option struct defines page layout parameters, another one typographical characteristics, a third one the current user. So you might end up with "several" (i.e. a few) option parameters, but not a hundred of them. (For all practical purposes, this second point is also a requirement for my first point.) Third, as software develops, new options will be added. For an exported library function, it must be prepared to handle calls from applications both newer (supplying a larger struct with added and unknown fields) and older (supplying structs with missing fields) applications, and be able to handle them both. So the struct must identify the version. So one of the fields in the option struct is "This is format 3". An alternative is "This struct contains 44 valid bytes of parameters. The very best is to include both: This struct contains 44 bytes of parameters of format 3" - then you can add parameters in format 3 as long as the extensions are fully compatible, and bump the format code only when an incompatible extension is introduced. So the caller must fill in two extra fields, but then again, the same declaration can be used twenty years later. (Don't expect anyone below thirty to see the value of that...) Another alternativ is of course the Win32 API way: Start with MyFunction. Then, when one option is added, call it MyFunctionEx. After the second extension, make it MyFunctionEx. After extension eight, it is MyFunctionExExExExExExExEx, and so it continues :)

                          1 Reply Last reply
                          0
                          • M Marc Clifton

                            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

                            B Offline
                            B Offline
                            BobJanova
                            wrote on last edited by
                            #20

                            I think it's important that there's an easy way to find which options are defined. I've had issues with Javascript libraries that take an object as a parameter, and expect you to put the right properties in there (the XMLHttpRequest AJAX handler does this iirc). In a statically typed language like C# or Java (or even C, Delphi, C++ etc), it makes sense to use a struct or class (using those words in their C# meaning) for groups of parameters with values, and a flagwise enum for those without. If you don't have enums then the old school approach of constants set to the appropriate bit values and using | to combine them is fine too. The grouping is important, it shouldn't just be public struct StuffUsedByThisFunction, it should have some semantic meaning even outwith the context of the function call. In dynamic languages the convention seems to be dictionaries with lax validation. I agree that this is often unhelpful, but it's encouraged by the object model in those languages which doesn't really do static declaration of valid members. I've not really worked in those languages enough to have developed a good solution, though.

                            T 1 Reply Last reply
                            0
                            • B BobJanova

                              I think it's important that there's an easy way to find which options are defined. I've had issues with Javascript libraries that take an object as a parameter, and expect you to put the right properties in there (the XMLHttpRequest AJAX handler does this iirc). In a statically typed language like C# or Java (or even C, Delphi, C++ etc), it makes sense to use a struct or class (using those words in their C# meaning) for groups of parameters with values, and a flagwise enum for those without. If you don't have enums then the old school approach of constants set to the appropriate bit values and using | to combine them is fine too. The grouping is important, it shouldn't just be public struct StuffUsedByThisFunction, it should have some semantic meaning even outwith the context of the function call. In dynamic languages the convention seems to be dictionaries with lax validation. I agree that this is often unhelpful, but it's encouraged by the object model in those languages which doesn't really do static declaration of valid members. I've not really worked in those languages enough to have developed a good solution, though.

                              T Offline
                              T Offline
                              Tony Moffatt
                              wrote on last edited by
                              #21

                              Dependancy Injection!!

                              B 1 Reply Last reply
                              0
                              • K kalberts

                                In the old days, I was working with a Fortran compilers where we had to push the release due to one (important) customer who had run into a limitation of the previous one: It could take only 99 (ninety-nine) parameters. The new version could handle 128 parameters, which was sufficient for the immediate needs of the customer, but the design allowed expansion to 256 parameters. If there ever was a case for Fortran COMMON blocks, I would call this a candidate. (Don't take me wrong - I am not suggesting using COMMON blocks as good programming practice. Nor is 100+ function parameters.)

                                D Offline
                                D Offline
                                Dan Neely
                                wrote on last edited by
                                #22

                                :omg: :wtf: Just thinking about that is probably enough that you need some medication. Have a :beer: on me.

                                Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt

                                1 Reply Last reply
                                0
                                • M Marc Clifton

                                  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

                                  K Offline
                                  K Offline
                                  Kirk 10389821
                                  wrote on last edited by
                                  #23

                                  Parameters? Why not just use Global Variables??? Wait for it... ROTFLMAO... I did flag this as a JOKE! That is a HORRIBLE IDEA, for the record... And for the record, we used a structure, and it worked beautifully. The tricky part is when you call a function that needs 1 of those parameters... Do you pass the structure, or struct.val3 ? (we based it on the completeness of the function. If we felt it wasn't changing, then we sent as little data as possible, but more than one field meant the structure).

                                  1 Reply Last reply
                                  0
                                  • K kalberts

                                    In the old days, I was working with a Fortran compilers where we had to push the release due to one (important) customer who had run into a limitation of the previous one: It could take only 99 (ninety-nine) parameters. The new version could handle 128 parameters, which was sufficient for the immediate needs of the customer, but the design allowed expansion to 256 parameters. If there ever was a case for Fortran COMMON blocks, I would call this a candidate. (Don't take me wrong - I am not suggesting using COMMON blocks as good programming practice. Nor is 100+ function parameters.)

                                    G Offline
                                    G Offline
                                    gggustafson
                                    wrote on last edited by
                                    #24

                                    COMMON is certainly good "programming practice" as long as each COMMON is named and contains a single value. Otherwise I submit that COMMON should be found non-complying in coding standards.

                                    Gus Gustafson

                                    1 Reply Last reply
                                    0
                                    • M Marc Clifton

                                      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

                                      P Offline
                                      P Offline
                                      patbob
                                      wrote on last edited by
                                      #25

                                      If the function has "too many" arguments, accept that it's ugly and live with it. Putting them in a struct is just hiding the ugliness and moving the complexity elsewhere, possibly remote from the function call itself. In my mind, it just makes for extra work to understand and maintain. The optional argument example is a perfect time to use an overload. That way you have a place to document the unique behavior of that variation of the function, and have documented for the next developer which arguments need to be specified as a set. The struct solution isn't a bad way to hide the problem, although a polymorphic class might be better depending on the needs.

                                      We can program with only 1's, but if all you've got are zeros, you've got nothing.

                                      1 Reply Last reply
                                      0
                                      • P Pete OHanlon

                                        I would have to go with wrapping the parameters in a struct/class. That way, I can put all sorts of validation in there that gives the object some context.

                                        D Offline
                                        D Offline
                                        Daniel R Przybylski
                                        wrote on last edited by
                                        #26

                                        I like that approach. It comes in handy with REST wrappers. You can create a struct that can validate and concatenate either the parameter list that you GET URL will need or the XML that your POST will need in a ToString() like method.

                                        1 Reply Last reply
                                        0
                                        • T Tony Moffatt

                                          Dependancy Injection!!

                                          B Offline
                                          B Offline
                                          BobJanova
                                          wrote on last edited by
                                          #27

                                          Into a function? I don't even see how that makes sense. Generally though I hate dependency injection. It makes it really really hard to find out what type of object you're actually dealing with when you're looking at the code.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups