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. C# Optional Parameters?

C# Optional Parameters?

Scheduled Pinned Locked Moved The Lounge
csharpcomtestingtools
27 Posts 13 Posters 20 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.
  • R rastaVnuce

    Electron Shepherd wrote:

    all my code still compiles. And that can be a problem. I have a lot of work to do to identify all the places that I need to pass in the new, fourth, parameter

    Actually, the fact that your code sill compiles is a good thing. Instead of hunting the DisplayName calls thought the code, all you need to do is make sure DisplayName handles salutation == null properly. Which it should, optional parameter or not. Even if you do want to check all the DisplayName calls, you can find all references with just two clicks. Doesn't seem like a lot of work to me.

    We are using Linux daily to UP our productivity - so UP yours!

    E Offline
    E Offline
    Electron Shepherd
    wrote on last edited by
    #10

    rastaVnuce wrote:

    you can find all references with just two clicks.

    I'm guessing you don't share code between different projects, then?

    Server and Network Monitoring

    P R 2 Replies Last reply
    0
    • R RCoate

      Electron Shepherd wrote:

      How do you guarantee that you have passed in the fourth parameter everywhere you should have?

      Um, by knowing your code base, checking and testing? Same as when you refactor your code and make alterations to what an overloaded method does. I realise that they are not ideal for all situations and that some people have coding styles that don't gel with the idea.

      E Offline
      E Offline
      Electron Shepherd
      wrote on last edited by
      #11

      On large systems, one person won't know the whole code base. The function may be used by others, in ways that you don't know (and therefore won't test).

      Server and Network Monitoring

      R 1 Reply Last reply
      0
      • E Electron Shepherd

        Named parameters have the same problem. How do you guarantee that you have passed in the fourth parameter everywhere you should have?

        Server and Network Monitoring

        C Offline
        C Offline
        Covean
        wrote on last edited by
        #12

        If the fourth parameter has this relevance why do you set some default value? Wouldn't it be better to set it as 3rd parameter with no default value and let to compiler spit out like hell? By the way you would run in the same problem if there where no default value: Your example above without default values:

        void func(string szParam1, string szParam2) { ... }
        void func(string szParam1, string szParam2, string szParam3) { ... }

        Now if you add some parameter with a default value it should look like this:

        void func(string szParam1, string szParam2) { ... }
        void func(string szParam1, string szParam2, string szParam3) { ... }
        void func(string szParam1, string szParam2, string szParam3, string szParam4) { ... }

        This is exactly the same problem. If you solve your problem by adding an important parameter with default value and asking yourself if you got all places where you need this parameter, then you have no problem with your source code. The problem lies in your application design. I really like it to get default values back. :-D And I never ran in the evil-default-value-problem in my whole lifetime!

        Greetings Covean

        1 Reply Last reply
        0
        • E Electron Shepherd

          rastaVnuce wrote:

          you can find all references with just two clicks.

          I'm guessing you don't share code between different projects, then?

          Server and Network Monitoring

          P Offline
          P Offline
          peterchen
          wrote on last edited by
          #13

          That's a point, but still... You can force the compiler find the location by just making them non-default. (Yo do have a batch build over all projects involved, don't you?)

          Agh! Reality! My Archnemesis![^]
          | FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server.

          1 Reply Last reply
          0
          • E Electron Shepherd

            On large systems, one person won't know the whole code base. The function may be used by others, in ways that you don't know (and therefore won't test).

            Server and Network Monitoring

            R Offline
            R Offline
            RCoate
            wrote on last edited by
            #14

            Electron Shepherd wrote:

            The function may be used by others, in ways that you don't know (and therefore won't test).

            That argument doesn't make any sense to me. I don't see much difference between adding an optional parameter and providing a new overloaded method. The information that the new functionality is available still needs to be shared and then used appropriately. It seems to be mostly a matter of style and preference. Still, my main point in brining this topic up was that I was surprised that optional and named parameters were introduced in C# 4.0. It is something I did not expect to see.

            1 Reply Last reply
            0
            • E Electron Shepherd

              rastaVnuce wrote:

              you can find all references with just two clicks.

              I'm guessing you don't share code between different projects, then?

              Server and Network Monitoring

              R Offline
              R Offline
              rastaVnuce
              wrote on last edited by
              #15

              That's really not the point. If the parameter is critical you don't set a default value. If it's not, it means that it makes difference for a finite number of cases which you're aware about. So, you set a default value which would be properly handled for the rest of the cases. I really don't see a place for confusion. Over a decade of experience in working with a ton of different languages and technologies, I've never had a single issue with the default parameter. On the contrary, I've missed it a lot where not available.

              We are using Linux daily to UP our productivity - so UP yours!

              1 Reply Last reply
              0
              • R RCoate

                I just read this[^] and thought "what on earth is going on here"? When I moved from VB.Net to C#.net, one of the big evangelistic arguments was around optional parameters as opposed to overloaded methods. I always liked optional parameters, but was prepared to give them up if the general feeling was that they where evil. Seems they aren't evil any more. I think Microsoft is just messing with my head and they will be removed in 5.0. I do like the named parameters though. I have been wanting those ever since I did some Office Automation stuff.

                R Offline
                R Offline
                realJSOP
                wrote on last edited by
                #16

                Two words - side effects... What I've noticed is that only ex-VB programmers seem to be excited about optional and named parameters.

                .45 ACP - because shooting twice is just silly
                -----
                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                R S 2 Replies Last reply
                0
                • R realJSOP

                  Two words - side effects... What I've noticed is that only ex-VB programmers seem to be excited about optional and named parameters.

                  .45 ACP - because shooting twice is just silly
                  -----
                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                  R Offline
                  R Offline
                  RCoate
                  wrote on last edited by
                  #17

                  John Simmons / outlaw programmer wrote:

                  only ex-VB programmers seem to be excited about optional and named parameters

                  Not really excited, more surprised. But, yes guilty as charged. ;)

                  W 1 Reply Last reply
                  0
                  • R RCoate

                    John Simmons / outlaw programmer wrote:

                    only ex-VB programmers seem to be excited about optional and named parameters

                    Not really excited, more surprised. But, yes guilty as charged. ;)

                    W Offline
                    W Offline
                    WiGgLr
                    wrote on last edited by
                    #18

                    I'm not convinced about optional parameters, but I can see a potential use for named parameters. That said, it's just more code to write, so I doubt I'd use them :)

                    1 Reply Last reply
                    0
                    • P peterchen

                      about 20 overloads for MessageBox.Show[^] The cases where it subtly fails are much less than where it helps. A less subtle bug would be changing public static void DisplayName (string firstName, string lastName = null) to public static void DisplayName (string firstName, string middleName = null, string lastName = null) ouch! But hey, we can make it, we can break it.

                      Agh! Reality! My Archnemesis![^]
                      | FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server.

                      M Offline
                      M Offline
                      megaadam
                      wrote on last edited by
                      #19

                      peterchen wrote:

                      public static void DisplayName (string firstName, string lastName = null) ==> public static void DisplayName (string firstName, string middleName = null, string lastName = null)

                      That does not compile, due to ambiguity: DisplayName( "Peter", "Not Chen" ); // Cannot be resolved to one of the two overloads

                      ..................... Life is too shor

                      P 1 Reply Last reply
                      0
                      • M megaadam

                        peterchen wrote:

                        public static void DisplayName (string firstName, string lastName = null) ==> public static void DisplayName (string firstName, string middleName = null, string lastName = null)

                        That does not compile, due to ambiguity: DisplayName( "Peter", "Not Chen" ); // Cannot be resolved to one of the two overloads

                        ..................... Life is too shor

                        P Offline
                        P Offline
                        peterchen
                        wrote on last edited by
                        #20

                        I meant changing the prototype from one to another, the second argument suddenly becoming the middle name

                        megaadam wrote:

                        DisplayName( "Peter", "Not Chen" );

                        Who's that?

                        Agh! Reality! My Archnemesis![^]
                        | FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server.

                        1 Reply Last reply
                        0
                        • R RCoate

                          I just read this[^] and thought "what on earth is going on here"? When I moved from VB.Net to C#.net, one of the big evangelistic arguments was around optional parameters as opposed to overloaded methods. I always liked optional parameters, but was prepared to give them up if the general feeling was that they where evil. Seems they aren't evil any more. I think Microsoft is just messing with my head and they will be removed in 5.0. I do like the named parameters though. I have been wanting those ever since I did some Office Automation stuff.

                          S Offline
                          S Offline
                          Stuart Dootson
                          wrote on last edited by
                          #21

                          RCoate wrote:

                          I do like the named parameters though. I have been wanting those ever since I did some Office Automation stuff.

                          I've loved named parameters since I first did some Ada programming in the mid 1990s...thy'r the one thing that makes optional parameters non-evil, IMO...

                          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

                          1 Reply Last reply
                          0
                          • E Electron Shepherd

                            Named parameters have the same problem. How do you guarantee that you have passed in the fourth parameter everywhere you should have?

                            Server and Network Monitoring

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

                            Electron Shepherd wrote:

                            How do you guarantee that you have passed in the fourth parameter everywhere you should have?

                            How do you know you called the 4 parameter overload everywhere you should have instead of the three parameter overload?

                            3x12=36 2x12=24 1x12=12 0x12=18

                            E 1 Reply Last reply
                            0
                            • D Dan Neely

                              Electron Shepherd wrote:

                              How do you guarantee that you have passed in the fourth parameter everywhere you should have?

                              How do you know you called the 4 parameter overload everywhere you should have instead of the three parameter overload?

                              3x12=36 2x12=24 1x12=12 0x12=18

                              E Offline
                              E Offline
                              Electron Shepherd
                              wrote on last edited by
                              #23

                              Becuase my point was to make the parameters non-optional. Then the compiler spots all those cases for you.

                              Server and Network Monitoring

                              1 Reply Last reply
                              0
                              • E Electron Shepherd

                                The trouble with optional parameters comes when you extend them

                                public static void DisplayName (string lastName, string firstName,
                                string middleName = null)

                                If I now add a salutation:

                                public static void DisplayName (string lastName, string firstName,
                                string middleName = null, string salutation = null)

                                all my code still compiles. And that can be a problem. I have a lot of work to do to identify all the places that I need to pass in the new, fourth, parameter (I must need it in at least one place, or else why change the function). Without optional parameters, the compiler does my impact analysis for me. In this case, the worst that happens is that the salutation is missed off a displayed name, but in some cases, you can introduce some subtle bugs.

                                Server and Network Monitoring

                                P Offline
                                P Offline
                                PIEBALDconsult
                                wrote on last edited by
                                #24

                                Electron Shepherd wrote:

                                I must need it in at least one place

                                Not if it's framework code that you may not be using at all. I write a lot of methods I never use.

                                1 Reply Last reply
                                0
                                • E Electron Shepherd

                                  The trouble with optional parameters comes when you extend them

                                  public static void DisplayName (string lastName, string firstName,
                                  string middleName = null)

                                  If I now add a salutation:

                                  public static void DisplayName (string lastName, string firstName,
                                  string middleName = null, string salutation = null)

                                  all my code still compiles. And that can be a problem. I have a lot of work to do to identify all the places that I need to pass in the new, fourth, parameter (I must need it in at least one place, or else why change the function). Without optional parameters, the compiler does my impact analysis for me. In this case, the worst that happens is that the salutation is missed off a displayed name, but in some cases, you can introduce some subtle bugs.

                                  Server and Network Monitoring

                                  S Offline
                                  S Offline
                                  S Senthil Kumar
                                  wrote on last edited by
                                  #25

                                  You would have run into the same problem if you'd used method overloading instead of optional parameters; you'd still have to manually identify the places where the extra parameter should be used. Anyway, you could just comment out the = null and recompile again :)

                                  Regards Senthil _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                                  1 Reply Last reply
                                  0
                                  • R RCoate

                                    I just read this[^] and thought "what on earth is going on here"? When I moved from VB.Net to C#.net, one of the big evangelistic arguments was around optional parameters as opposed to overloaded methods. I always liked optional parameters, but was prepared to give them up if the general feeling was that they where evil. Seems they aren't evil any more. I think Microsoft is just messing with my head and they will be removed in 5.0. I do like the named parameters though. I have been wanting those ever since I did some Office Automation stuff.

                                    P Offline
                                    P Offline
                                    PIEBALDconsult
                                    wrote on last edited by
                                    #26

                                    I finally tried one the other day, then refactored the need away. It's a good tool to have in the toolbox, but I don't expect to use it all that much.

                                    1 Reply Last reply
                                    0
                                    • R realJSOP

                                      Two words - side effects... What I've noticed is that only ex-VB programmers seem to be excited about optional and named parameters.

                                      .45 ACP - because shooting twice is just silly
                                      -----
                                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                      -----
                                      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                                      S Offline
                                      S Offline
                                      S Senthil Kumar
                                      wrote on last edited by
                                      #27

                                      John Simmons / outlaw programmer wrote:

                                      side effects

                                      Did you mean dependencies between parameters?

                                      void Method(int x, int y) {}

                                      int z = 2;
                                      Method(y : z, x : ++z)

                                      Regards Senthil _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                                      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