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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. The Lounge
  3. Programming peeve of the Day

Programming peeve of the Day

Scheduled Pinned Locked Moved The Lounge
53 Posts 28 Posters 5 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.
  • N Nelek

    If the return is within an "check initialized stuff" at the beginning... nothing. If there are 3 or more returns... it might get so confusing / convoluted as having only one at the end.

    M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

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

    Sure, but that's just convoluted code. Prolonging the confusion by also having to reach the end of the function is only going to make things worse.

    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
    • R Rick York

      If you have allocated anything that is not an automatic object that can be risky. I have dealt with a few customers who had explicit code-style prohibitions against multiple return statements. Given the rest of the nonsense we had to deal with from them that was pretty much ignored. I hated those SFBs so much I refuse to buy any of their products ever again. To give a clue, they used to be referred to as a purveyor of expensive ink.

      "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

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

      Good point about automatic objects. Fortunately, C++ now has unique_ptr, but some languages might not have an equivalent.

      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
      • R Rick York

        I take a similar approach but I don't limit myself to just one return there. I let each sanity check have its own return statement because I find easier to deal with when debugging. Following the input sanitization I try to not have any returns unless a value is returned and then only at the bottom.

        "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

        C Offline
        C Offline
        Chris Maunder
        wrote on last edited by
        #33

        Rick York wrote:

        I let each sanity check have its own return statement

        Full disclosure: I do the same if it makes it cumbersome / ugly otherwise. But one test/return section at the top.

        cheers Chris Maunder

        1 Reply Last reply
        0
        • N NotTodayYo

          Chris Maunder wrote:

          Your rewrite is how it should be done.

          Why? It's not as clear as to what the code will do. Plus, returning from inside an if is bad form.

          L Offline
          L Offline
          Lorenzo Bertolino
          wrote on last edited by
          #34

          Sure, in a function that is 1000 lines long, it isn't clear. In one that fits on the screen is quite obvious. But I'd argue that a function that long isn't clear either way. :-D

          1 Reply Last reply
          0
          • C Chris Maunder

            if (condition)
            {
            return;
            }
            else
            {
            // Do something else
            }

            cheers Chris Maunder

            R Offline
            R Offline
            rob tillaart
            wrote on last edited by
            #35

            It all depends on which measuring stick is used... - # exits in a function - # lines of code - # lines of comments - # paths in the code - readability, - maintainability, modifiability - only use positive condition tests (! using ! and certainly ! !!) And of course if you (think you) are paid by lines of code produced there are many other "solutions"

            1 Reply Last reply
            0
            • C Chris Maunder

              if (condition)
              {
              return;
              }
              else
              {
              // Do something else
              }

              cheers Chris Maunder

              U Offline
              U Offline
              User 14060113
              wrote on last edited by
              #36

              Visual Studio would show you that the else branch in unnecessary.

              1 Reply Last reply
              0
              • C Chris Maunder

                if (condition)
                {
                return;
                }
                else
                {
                // Do something else
                }

                cheers Chris Maunder

                F Offline
                F Offline
                Fueled By Decaff
                wrote on last edited by
                #37

                Hmm, to trying to please people who want a single exit point and clear intent when processing is completed:

                if (condition)
                {
                goto returnStatement;
                }
                // Do something else

                :returnStatement

                return;

                X| :laugh: X|

                C 1 Reply Last reply
                0
                • C Chris Maunder

                  if (condition)
                  {
                  return;
                  }
                  else
                  {
                  // Do something else
                  }

                  cheers Chris Maunder

                  W Offline
                  W Offline
                  Wizard of Sleeves
                  wrote on last edited by
                  #38

                  My career best:

                  function doNothing(something) {
                  return something;
                  }

                  Nothing succeeds like a budgie without teeth.

                  1 Reply Last reply
                  0
                  • C Chris Maunder

                    if (condition)
                    {
                    return;
                    }
                    else
                    {
                    // Do something else
                    }

                    cheers Chris Maunder

                    J Offline
                    J Offline
                    JohaViss61
                    wrote on last edited by
                    #39

                    Just to annoy everyone :laugh:

                    switch (condition)
                    {
                    case true: return;

                    default: // do something else
                    }

                    1 Reply Last reply
                    0
                    • Mike HankeyM Mike Hankey

                      I believe to be totally proper it should be;

                      if ( !condition )
                      {
                      // Do something else
                      return;
                      }

                      return ;

                      The less you need, the more you have. JaxCoder.com

                      B Offline
                      B Offline
                      BryanFazekas
                      wrote on last edited by
                      #40

                      What is the value of the redundant return statement inside the braces? One of my first jobs was processing in a 16K space, and the data was 12K per record. We coded very concisely as we had no room for extraneous code. Today's compilers are far more efficient, but making the processing straightforward and eliminating redundant code is still a good idea.

                      Mike HankeyM 1 Reply Last reply
                      0
                      • B BryanFazekas

                        What is the value of the redundant return statement inside the braces? One of my first jobs was processing in a 16K space, and the data was 12K per record. We coded very concisely as we had no room for extraneous code. Today's compilers are far more efficient, but making the processing straightforward and eliminating redundant code is still a good idea.

                        Mike HankeyM Offline
                        Mike HankeyM Offline
                        Mike Hankey
                        wrote on last edited by
                        #41

                        I'm retired but I work a lot with embedded systems that have small memory spaces so every byte counts. The redundant return statements where a joke...to see how screwed up we could make it!

                        The less you need, the more you have. JaxCoder.com

                        B 1 Reply Last reply
                        0
                        • Mike HankeyM Mike Hankey

                          I'm retired but I work a lot with embedded systems that have small memory spaces so every byte counts. The redundant return statements where a joke...to see how screwed up we could make it!

                          The less you need, the more you have. JaxCoder.com

                          B Offline
                          B Offline
                          BryanFazekas
                          wrote on last edited by
                          #42

                          Mike, you're not even close to how screwed up someone could make this! :laugh: I worked with a guy who was intent on cover EVERY possibility. His program executed 700 lines of code when tabbing between fields. If anyone actually clicked something, it go ugly ....

                          Mike HankeyM 1 Reply Last reply
                          0
                          • B BryanFazekas

                            Mike, you're not even close to how screwed up someone could make this! :laugh: I worked with a guy who was intent on cover EVERY possibility. His program executed 700 lines of code when tabbing between fields. If anyone actually clicked something, it go ugly ....

                            Mike HankeyM Offline
                            Mike HankeyM Offline
                            Mike Hankey
                            wrote on last edited by
                            #43

                            I've no doubt that it could be more screwed up and I've seen it when I was getting payed to program. I wonder how some people get into the field and even more how they stay.

                            The less you need, the more you have. JaxCoder.com

                            B 1 Reply Last reply
                            0
                            • C Chris Maunder

                              if (condition)
                              {
                              return;
                              }
                              else
                              {
                              // Do something else
                              }

                              cheers Chris Maunder

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

                              if(the_code_below_can_not_be_executed) return;
                              // now do what must be done

                              1 Reply Last reply
                              0
                              • Mike HankeyM Mike Hankey

                                I've no doubt that it could be more screwed up and I've seen it when I was getting payed to program. I wonder how some people get into the field and even more how they stay.

                                The less you need, the more you have. JaxCoder.com

                                B Offline
                                B Offline
                                BryanFazekas
                                wrote on last edited by
                                #45

                                Yeah, the lack of ability in some cases is amazing, and not in a good way. My boss tells a story of a former employee who made cut-n-paste an art form. Modern art form. This person appears to have never written a line of code -- everything was copied from other programs and web sites, and this person could not understand why the program would not work. In another situation I taught a COBOL programmer with 5 years experience how to program. I am not a COBOL programmer and have never compiled a single line. This is not picking on COBOL -- this guy did not understand program flow. OTOH, he was fantastic at phone support, which is where he should have been.

                                N 1 Reply Last reply
                                0
                                • F Fueled By Decaff

                                  Hmm, to trying to please people who want a single exit point and clear intent when processing is completed:

                                  if (condition)
                                  {
                                  goto returnStatement;
                                  }
                                  // Do something else

                                  :returnStatement

                                  return;

                                  X| :laugh: X|

                                  C Offline
                                  C Offline
                                  Chris Maunder
                                  wrote on last edited by
                                  #46

                                  That's so, so wrong on so many levels...

                                  cheers Chris Maunder

                                  1 Reply Last reply
                                  0
                                  • C Chris Maunder

                                    if (condition)
                                    {
                                    return;
                                    }
                                    else
                                    {
                                    // Do something else
                                    }

                                    cheers Chris Maunder

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

                                    Looks like a copy/paste error where the code wasn't cleaned up after the paste.

                                    1 Reply Last reply
                                    0
                                    • C Chris Maunder

                                      if (condition)
                                      {
                                      return;
                                      }
                                      else
                                      {
                                      // Do something else
                                      }

                                      cheers Chris Maunder

                                      A Offline
                                      A Offline
                                      agolddog
                                      wrote on last edited by
                                      #48

                                      Of course! It should be:

                                      if (!condition) {
                                      //Do something else
                                      }

                                      1 Reply Last reply
                                      0
                                      • N NotTodayYo

                                        Chris Maunder wrote:

                                        Your rewrite is how it should be done.

                                        Why? It's not as clear as to what the code will do. Plus, returning from inside an if is bad form.

                                        M Offline
                                        M Offline
                                        Mark Starr
                                        wrote on last edited by
                                        #49

                                        Agreed! That’s what GOTO is for. :laugh:

                                        Time is the differentiation of eternity devised by man to measure the passage of human events. - Manly P. Hall Mark Just another cog in the wheel

                                        1 Reply Last reply
                                        0
                                        • C Chris Maunder

                                          if (condition)
                                          {
                                          return;
                                          }
                                          else
                                          {
                                          // Do something else
                                          }

                                          cheers Chris Maunder

                                          J Offline
                                          J Offline
                                          jmaida
                                          wrote on last edited by
                                          #50

                                          // need to do something if conditions are right otherwise don't if( condition ) { do something } return

                                          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