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. Programming peeve of the Day

Programming peeve of the Day

Scheduled Pinned Locked Moved The Lounge
53 Posts 28 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.
  • C Chris Maunder

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

    cheers Chris Maunder

    S Offline
    S Offline
    Slacker007
    wrote on last edited by
    #4

    if (condition)
    {
    return;
    }

    // Do something else

    what about this annoys you?

    C F 2 Replies Last reply
    0
    • C Chris Maunder

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

      cheers Chris Maunder

      F Offline
      F Offline
      Forogar
      wrote on last edited by
      #5

      Obviously it should have been:

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

      :cool:

      - I would love to change the world, but they won’t give me the source code.

      L Mike HankeyM 2 Replies Last reply
      0
      • S Slacker007

        if (condition)
        {
        return;
        }

        // Do something else

        what about this annoys you?

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

        Your rewrite is how it should be done. Now get off my lawn.

        cheers Chris Maunder

        P N 2 Replies Last reply
        0
        • S Slacker007

          if (condition)
          {
          return;
          }

          // Do something else

          what about this annoys you?

          F Offline
          F Offline
          Forogar
          wrote on last edited by
          #7

          ...erm, your code is not the same logic. You have inverted it.

          - I would love to change the world, but they won’t give me the source code.

          1 Reply Last reply
          0
          • C Chris Maunder

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

            cheers Chris Maunder

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

            I would never do it the same, but I can understand it as a hint and that is ok. Of course

            if (condition) return;
            // Do otherwhise

            would be more appropriate On the other hand: Is it really worth to discuss such little things?

            W C 2 Replies Last reply
            0
            • F Forogar

              Obviously it should have been:

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

              :cool:

              - I would love to change the world, but they won’t give me the source code.

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

              Whehe, that's evil :D

              Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

              1 Reply Last reply
              0
              • L Lost User

                I would never do it the same, but I can understand it as a hint and that is ok. Of course

                if (condition) return;
                // Do otherwhise

                would be more appropriate On the other hand: Is it really worth to discuss such little things?

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

                Member 15092515 wrote:

                On the other hand: Is it really worth to discuss such little things?

                You're absolutely correct. Let's discuss this, instead: if (condition) { return; } else { // Do something else }

                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

                L 1 Reply Last reply
                0
                • W W Balboos GHB

                  Member 15092515 wrote:

                  On the other hand: Is it really worth to discuss such little things?

                  You're absolutely correct. Let's discuss this, instead: if (condition) { return; } else { // Do something else }

                  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

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

                  Ah ok I see you have a problem with 'if ... else' ;P [Edit] Is see in your profile "In real life, a research chemist." Yep, better stop programming. This because programming follow strict rules while chemistry has only exceptions ;P

                  1 Reply Last reply
                  0
                  • C Chris Maunder

                    Your rewrite is how it should be done. Now get off my lawn.

                    cheers Chris Maunder

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

                    Is snot.

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

                    return ;

                    Mike HankeyM 1 Reply Last reply
                    0
                    • C Chris Maunder

                      Your rewrite is how it should be done. Now get off my lawn.

                      cheers Chris Maunder

                      N Offline
                      N Offline
                      NotTodayYo
                      wrote on last edited by
                      #13

                      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.

                      Greg UtasG L M W 4 Replies Last reply
                      0
                      • F Forogar

                        Obviously it should have been:

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

                        :cool:

                        - I would love to change the world, but they won’t give me the source code.

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

                        Got it before I did! :)

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

                        1 Reply Last reply
                        0
                        • C Chris Maunder

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

                          cheers Chris Maunder

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

                          when you refactor some code and are too afraid to remove else clauses.

                          I'd rather be phishing!

                          1 Reply Last reply
                          0
                          • C Chris Maunder

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

                            cheers Chris Maunder

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

                            I like single exit points... That code would drive me nuts too...

                            ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                            -----
                            You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                            -----
                            When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                            C 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.

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

                              What's wrong with returning from inside an if? The rule about only returning at the end of a function leads to convoluted code where a flag is repeatedly used to bypass stuff just to reach the end of the function. Pure dross.

                              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>

                              N N R 3 Replies Last reply
                              0
                              • C Chris Maunder

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

                                cheers Chris Maunder

                                Sander RosselS Offline
                                Sander RosselS Offline
                                Sander Rossel
                                wrote on last edited by
                                #18

                                No religion in the lounge! ;p

                                Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

                                L 1 Reply Last reply
                                0
                                • P PIEBALDconsult

                                  Is snot.

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

                                  return ;

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

                                  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

                                  P B 2 Replies Last reply
                                  0
                                  • Sander RosselS Sander Rossel

                                    No religion in the lounge! ;p

                                    Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

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

                                    hehe :thumbsup:

                                    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

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

                                      If I had a lawn... :mad:

                                      Mike HankeyM 1 Reply Last reply
                                      0
                                      • Greg UtasG Greg Utas

                                        What's wrong with returning from inside an if? The rule about only returning at the end of a function leads to convoluted code where a flag is repeatedly used to bypass stuff just to reach the end of the function. Pure dross.

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

                                        N Offline
                                        N Offline
                                        NotTodayYo
                                        wrote on last edited by
                                        #22

                                        Greg Utas wrote:

                                        The rule about only returning at the end of a function leads to convoluted code

                                        As opposed to it not being clear when and where something can be returned.

                                        Greg UtasG 1 Reply Last reply
                                        0
                                        • P PIEBALDconsult

                                          If I had a lawn... :mad:

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

                                          We have a saying here in the south; Hold my beer[^].

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

                                          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