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. Cosmetic vs More Efficient

Cosmetic vs More Efficient

Scheduled Pinned Locked Moved The Lounge
visual-studiocomalgorithmsquestion
48 Posts 27 Posters 0 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.
  • W W Balboos GHB

    The difference may be slight but one of the conundrums I find myself in is using a ternary operator to handle a default vs non-default assignment. Simplified:

    function whatEver(inVal=NULL) { // here, NULL is a default value for a function argument

    // This ?
    if(inVal==NULL)
    inVal = internalDefault;

    // or this?
    inVal = (inVal==NULL)?internalDefault:inVal;

    } // function whatEver(inVal=NULL)

    The first should be a touch more efficient as it only does an assignment when necessary, but generally an insignificant difference. So - what would you do, and, do you ever pause and consider it before choosing?

    Ravings en masse^

    "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

    "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

    M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #2

    Make it sexy. Keep code as clean and readable as possible. In the grand scheme of things, the compiler will make it efficient whatever the way you write it.

    CI/CD = Continuous Impediment/Continuous Despair

    M 1 Reply Last reply
    0
    • W W Balboos GHB

      The difference may be slight but one of the conundrums I find myself in is using a ternary operator to handle a default vs non-default assignment. Simplified:

      function whatEver(inVal=NULL) { // here, NULL is a default value for a function argument

      // This ?
      if(inVal==NULL)
      inVal = internalDefault;

      // or this?
      inVal = (inVal==NULL)?internalDefault:inVal;

      } // function whatEver(inVal=NULL)

      The first should be a touch more efficient as it only does an assignment when necessary, but generally an insignificant difference. So - what would you do, and, do you ever pause and consider it before choosing?

      Ravings en masse^

      "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

      "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

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

      if(inVal==NULL)
      {
      inVal = internalDefault;
      }

      Wrong is evil and must be defeated. - Jeff Ello

      R A 2 Replies Last reply
      0
      • W W Balboos GHB

        The difference may be slight but one of the conundrums I find myself in is using a ternary operator to handle a default vs non-default assignment. Simplified:

        function whatEver(inVal=NULL) { // here, NULL is a default value for a function argument

        // This ?
        if(inVal==NULL)
        inVal = internalDefault;

        // or this?
        inVal = (inVal==NULL)?internalDefault:inVal;

        } // function whatEver(inVal=NULL)

        The first should be a touch more efficient as it only does an assignment when necessary, but generally an insignificant difference. So - what would you do, and, do you ever pause and consider it before choosing?

        Ravings en masse^

        "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

        "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

        M Offline
        M Offline
        MarkTJohnson
        wrote on last edited by
        #4

        I like the question colon, you can use it inline like

        printf("You have " + count + " new email" + (count <> 1 ? "s" : "" ));

        I’ve given up trying to be calm. However, I am open to feeling slightly less agitated.

        D 1 Reply Last reply
        0
        • M MarkTJohnson

          I like the question colon, you can use it inline like

          printf("You have " + count + " new email" + (count <> 1 ? "s" : "" ));

          I’ve given up trying to be calm. However, I am open to feeling slightly less agitated.

          D Offline
          D Offline
          Daniel Pfeffer
          wrote on last edited by
          #5

          This is a problem for internationalisation (== i18n). Not all languages behave like English re plurals.

          Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

          M 1 Reply Last reply
          0
          • D Daniel Pfeffer

            This is a problem for internationalisation (== i18n). Not all languages behave like English re plurals.

            Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

            M Offline
            M Offline
            MarkTJohnson
            wrote on last edited by
            #6

            Twas just the easiest example I could think of.

            I’ve given up trying to be calm. However, I am open to feeling slightly less agitated.

            1 Reply Last reply
            0
            • W W Balboos GHB

              The difference may be slight but one of the conundrums I find myself in is using a ternary operator to handle a default vs non-default assignment. Simplified:

              function whatEver(inVal=NULL) { // here, NULL is a default value for a function argument

              // This ?
              if(inVal==NULL)
              inVal = internalDefault;

              // or this?
              inVal = (inVal==NULL)?internalDefault:inVal;

              } // function whatEver(inVal=NULL)

              The first should be a touch more efficient as it only does an assignment when necessary, but generally an insignificant difference. So - what would you do, and, do you ever pause and consider it before choosing?

              Ravings en masse^

              "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

              "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

              M Offline
              M Offline
              Mircea Neacsu
              wrote on last edited by
              #7

              I usually write:

              void whatEver (int inval = internalDefault)

              I want to let users see what the function does in case of a NULL value. Otherwise I would have to document what happens when parameter is NULL. function whatever(inval=NULL) is simply not C/C++.

              Quote:

              do you ever pause and consider it before choosing

              I consider every line in my programs. This would be no exception. EDIT: Note that in C++ default value is written in the header file or wherever the function is declared not in the implementation as your code seems to suggest.

              Mircea

              W K A 3 Replies Last reply
              0
              • W W Balboos GHB

                The difference may be slight but one of the conundrums I find myself in is using a ternary operator to handle a default vs non-default assignment. Simplified:

                function whatEver(inVal=NULL) { // here, NULL is a default value for a function argument

                // This ?
                if(inVal==NULL)
                inVal = internalDefault;

                // or this?
                inVal = (inVal==NULL)?internalDefault:inVal;

                } // function whatEver(inVal=NULL)

                The first should be a touch more efficient as it only does an assignment when necessary, but generally an insignificant difference. So - what would you do, and, do you ever pause and consider it before choosing?

                Ravings en masse^

                "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                Greg UtasG Offline
                Greg UtasG Offline
                Greg Utas
                wrote on last edited by
                #8

                I rarely use the ternary operator unless something happens in both situations. I'd write it the first way, and even on one line unless the assignment seemed a likely spot for a breakpoint.

                Robust Services Core | Software Techniques for Lemmings | Articles
                The fox knows many things, but the hedgehog knows one big thing.

                <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
                <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

                W 1 Reply Last reply
                0
                • W W Balboos GHB

                  The difference may be slight but one of the conundrums I find myself in is using a ternary operator to handle a default vs non-default assignment. Simplified:

                  function whatEver(inVal=NULL) { // here, NULL is a default value for a function argument

                  // This ?
                  if(inVal==NULL)
                  inVal = internalDefault;

                  // or this?
                  inVal = (inVal==NULL)?internalDefault:inVal;

                  } // function whatEver(inVal=NULL)

                  The first should be a touch more efficient as it only does an assignment when necessary, but generally an insignificant difference. So - what would you do, and, do you ever pause and consider it before choosing?

                  Ravings en masse^

                  "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                  "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                  B Offline
                  B Offline
                  BernardIE5317
                  wrote on last edited by
                  #9

                  May I inquire why you would not write "whatEver(inVal=internalDefault)" - Cheerio

                  1 W 2 Replies Last reply
                  0
                  • W W Balboos GHB

                    The difference may be slight but one of the conundrums I find myself in is using a ternary operator to handle a default vs non-default assignment. Simplified:

                    function whatEver(inVal=NULL) { // here, NULL is a default value for a function argument

                    // This ?
                    if(inVal==NULL)
                    inVal = internalDefault;

                    // or this?
                    inVal = (inVal==NULL)?internalDefault:inVal;

                    } // function whatEver(inVal=NULL)

                    The first should be a touch more efficient as it only does an assignment when necessary, but generally an insignificant difference. So - what would you do, and, do you ever pause and consider it before choosing?

                    Ravings en masse^

                    "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                    "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

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

                    Yeah, no, I don't use the ternary operator for that.

                    1 Reply Last reply
                    0
                    • W W Balboos GHB

                      The difference may be slight but one of the conundrums I find myself in is using a ternary operator to handle a default vs non-default assignment. Simplified:

                      function whatEver(inVal=NULL) { // here, NULL is a default value for a function argument

                      // This ?
                      if(inVal==NULL)
                      inVal = internalDefault;

                      // or this?
                      inVal = (inVal==NULL)?internalDefault:inVal;

                      } // function whatEver(inVal=NULL)

                      The first should be a touch more efficient as it only does an assignment when necessary, but generally an insignificant difference. So - what would you do, and, do you ever pause and consider it before choosing?

                      Ravings en masse^

                      "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                      "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                      O Offline
                      O Offline
                      obermd
                      wrote on last edited by
                      #11

                      The first is definitely easier to read.

                      1 Reply Last reply
                      0
                      • B BernardIE5317

                        May I inquire why you would not write "whatEver(inVal=internalDefault)" - Cheerio

                        1 Offline
                        1 Offline
                        11917640 Member
                        wrote on last edited by
                        #12

                        Because of this:

                        whatEver(NULL);
                        
                        B 1 Reply Last reply
                        0
                        • 1 11917640 Member

                          Because of this:

                          whatEver(NULL);
                          
                          B Offline
                          B Offline
                          BernardIE5317
                          wrote on last edited by
                          #13

                          I meant to agree w/ Monsieur Mircea Neacsu - Professional Profile[^] in his post The Lounge[^] I simply do not understand the purpose of passing in a value only to discard and replace it. In hopes of becoming a better programmer or is it "coder" or is it "app author" I now know it is not "progamer" a full and lengthy explanation would be most appreciated. I am afraid I am too dim witted to understand how your brief quote "whatEver(NULL)" is an explanation as to why one would not declare it as Monsier Mircea Neacsu suggested. I await your kind reply. - Cheerio

                          1 1 Reply Last reply
                          0
                          • W W Balboos GHB

                            The difference may be slight but one of the conundrums I find myself in is using a ternary operator to handle a default vs non-default assignment. Simplified:

                            function whatEver(inVal=NULL) { // here, NULL is a default value for a function argument

                            // This ?
                            if(inVal==NULL)
                            inVal = internalDefault;

                            // or this?
                            inVal = (inVal==NULL)?internalDefault:inVal;

                            } // function whatEver(inVal=NULL)

                            The first should be a touch more efficient as it only does an assignment when necessary, but generally an insignificant difference. So - what would you do, and, do you ever pause and consider it before choosing?

                            Ravings en masse^

                            "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                            "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                            P Offline
                            P Offline
                            Peter Adam
                            wrote on last edited by
                            #14

                            I think, because the THEN branch is short, the second is more natural. EDIT: I turns into mess when the same condition evaluated many times in the "sentence". Natural languages and regexp have simple backreference to solve the problem.

                            1 Reply Last reply
                            0
                            • B BernardIE5317

                              I meant to agree w/ Monsieur Mircea Neacsu - Professional Profile[^] in his post The Lounge[^] I simply do not understand the purpose of passing in a value only to discard and replace it. In hopes of becoming a better programmer or is it "coder" or is it "app author" I now know it is not "progamer" a full and lengthy explanation would be most appreciated. I am afraid I am too dim witted to understand how your brief quote "whatEver(NULL)" is an explanation as to why one would not declare it as Monsier Mircea Neacsu suggested. I await your kind reply. - Cheerio

                              1 Offline
                              1 Offline
                              11917640 Member
                              wrote on last edited by
                              #15

                              Function logic is really strange, but in any case, with your suggestion, result of whatEver(NULL) call is: inVal is not replaced by internalDefault and remains null, which will cause exception when this pointer is dereferenced.

                              B 1 Reply Last reply
                              0
                              • 1 11917640 Member

                                Function logic is really strange, but in any case, with your suggestion, result of whatEver(NULL) call is: inVal is not replaced by internalDefault and remains null, which will cause exception when this pointer is dereferenced.

                                B Offline
                                B Offline
                                BernardIE5317
                                wrote on last edited by
                                #16

                                Thank you for your reply but doesn't the original code of Monsieur Mircea Neacsu as shown below merely replace inVal w/ internalDefault when inVal is NULL?

                                function whatEver(inVal=NULL) { // here, NULL is a default value for a function argument

                                // This ?
                                if(inVal==NULL)
                                inVal = internalDefault;

                                // or this?
                                inVal = (inVal==NULL)?internalDefault:inVal;

                                } // function whatEver(inVal=NULL)

                                1 Reply Last reply
                                0
                                • W W Balboos GHB

                                  The difference may be slight but one of the conundrums I find myself in is using a ternary operator to handle a default vs non-default assignment. Simplified:

                                  function whatEver(inVal=NULL) { // here, NULL is a default value for a function argument

                                  // This ?
                                  if(inVal==NULL)
                                  inVal = internalDefault;

                                  // or this?
                                  inVal = (inVal==NULL)?internalDefault:inVal;

                                  } // function whatEver(inVal=NULL)

                                  The first should be a touch more efficient as it only does an assignment when necessary, but generally an insignificant difference. So - what would you do, and, do you ever pause and consider it before choosing?

                                  Ravings en masse^

                                  "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                                  "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                                  B Offline
                                  B Offline
                                  BernardIE5317
                                  wrote on last edited by
                                  #17

                                  I prefer

                                  if(inVal==NULL) inVal = internalDefault;

                                  Why waste a line of screen space. It's easy enough to read. Though I actually prefer the declaration assign the argument w/ the valid default value as in

                                  return_type whatEver(arg_type inVal=internalDefault);

                                  I simply do not understand the logic of assigning a value to an argument only to immediately discard and replace it. I would be happy to learn of same. Incidentally I learned from a previous post or article I don't recall which to write e.g.

                                  void foobar(int arg)
                                  {
                                  if(arg != valid_value) return;
                                  // remainder of function performs logic for valid argument
                                  }

                                  instead of
                                  void foobar(int arg)
                                  {
                                  if(arg == valid_value)
                                  {
                                  // block performs logic for valid argument
                                  }
                                  }

                                  so as to minimize the number of indented blocks even though in previous times I preferred minimizing the number of return statements. I find minimizing the number of indented blocks to be easier to understand. - Cheerios

                                  1 Reply Last reply
                                  0
                                  • W W Balboos GHB

                                    The difference may be slight but one of the conundrums I find myself in is using a ternary operator to handle a default vs non-default assignment. Simplified:

                                    function whatEver(inVal=NULL) { // here, NULL is a default value for a function argument

                                    // This ?
                                    if(inVal==NULL)
                                    inVal = internalDefault;

                                    // or this?
                                    inVal = (inVal==NULL)?internalDefault:inVal;

                                    } // function whatEver(inVal=NULL)

                                    The first should be a touch more efficient as it only does an assignment when necessary, but generally an insignificant difference. So - what would you do, and, do you ever pause and consider it before choosing?

                                    Ravings en masse^

                                    "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                                    "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                                    J Offline
                                    J Offline
                                    jsc42
                                    wrote on last edited by
                                    #18

                                    Comments below assume C#, capabilities / syntax in other languages may vary. My preference is, if internalDefault is not a compile-time constant, to overload the function e.g.

                                    void whatEver() => whatEver(internalDefault);
                                    void whatEver(sometype inVal) { /* ... */ }

                                    or (assuming internalDefault is a compile-time constant)

                                    void whatEver(sometype inVal = internalDefault) { /* ... */ }

                                    Better than null would be to use default(type) e.g.

                                    void whatEver(sometype inVal = default(sometype) { /* ... */ }

                                    But in all of theses you should still check that inVal is not null. Alternatively, you can use the null coalescing operator e.g.

                                    void whatEver(sometype inVal = null) // or void whatEver(sometype inVal = internalDefault)
                                    {
                                    inval = inval ?? internalDefault;
                                    }

                                    One day, perhaps, there will be a null coalescing assignment operator so you can do

                                    inVal ??= internalDefault;

                                    - this is a feature that I have wanted in JavaScript since JS1.1 (c 1998) to change having code like

                                    myvar = myvar || somedefault;

                                    into

                                    myvar ||= somedefault;

                                    1 Reply Last reply
                                    0
                                    • M Mircea Neacsu

                                      I usually write:

                                      void whatEver (int inval = internalDefault)

                                      I want to let users see what the function does in case of a NULL value. Otherwise I would have to document what happens when parameter is NULL. function whatever(inval=NULL) is simply not C/C++.

                                      Quote:

                                      do you ever pause and consider it before choosing

                                      I consider every line in my programs. This would be no exception. EDIT: Note that in C++ default value is written in the header file or wherever the function is declared not in the implementation as your code seems to suggest.

                                      Mircea

                                      W Offline
                                      W Offline
                                      W Balboos GHB
                                      wrote on last edited by
                                      #19

                                      This was not intended to be a C++ only question. The C++ on the top of the block is incidental to my picking a code block (should have used plain code).

                                      Ravings en masse^

                                      "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                                      "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                                      M 1 Reply Last reply
                                      0
                                      • Greg UtasG Greg Utas

                                        I rarely use the ternary operator unless something happens in both situations. I'd write it the first way, and even on one line unless the assignment seemed a likely spot for a breakpoint.

                                        Robust Services Core | Software Techniques for Lemmings | Articles
                                        The fox knows many things, but the hedgehog knows one big thing.

                                        W Offline
                                        W Offline
                                        W Balboos GHB
                                        wrote on last edited by
                                        #20

                                        That's pretty much my attitude. One place that's essentially always the ternary operator (for me) is testing for incoming arguments in a php file

                                        $aVal = isset($_REQUEST['val']) ))?isset($_REQUEST['val']:some_default;

                                        There could be a flock of these, one after the other, to handle potential incoming values so (at least) I don't get undefined errors. Further handling of default may occur later on (like not use the value). Because it's always the way I do the checking for this particular file type it falls back to being easy reading. Within the body it's not, however, the default way of doing it.

                                        Ravings en masse^

                                        "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                                        "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                                        Greg UtasG 1 Reply Last reply
                                        0
                                        • W W Balboos GHB

                                          That's pretty much my attitude. One place that's essentially always the ternary operator (for me) is testing for incoming arguments in a php file

                                          $aVal = isset($_REQUEST['val']) ))?isset($_REQUEST['val']:some_default;

                                          There could be a flock of these, one after the other, to handle potential incoming values so (at least) I don't get undefined errors. Further handling of default may occur later on (like not use the value). Because it's always the way I do the checking for this particular file type it falls back to being easy reading. Within the body it's not, however, the default way of doing it.

                                          Ravings en masse^

                                          "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                                          "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                                          Greg UtasG Offline
                                          Greg UtasG Offline
                                          Greg Utas
                                          wrote on last edited by
                                          #21

                                          I don't know PHP, but that looks like a good usage after squinting at it and reading your explanation.

                                          Robust Services Core | Software Techniques for Lemmings | Articles
                                          The fox knows many things, but the hedgehog knows one big thing.

                                          <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
                                          <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

                                          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