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. Multiple returns from methods or clean code flow

Multiple returns from methods or clean code flow

Scheduled Pinned Locked Moved The Lounge
questiondiscussioncryptographycollaborationhelp
85 Posts 40 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.
  • D Delphi 7 Solutions

    I think that the Return statement should be dropped in any language, for example look at Delphi (object pascal) they did not have a return statement until recently it seems. A function had to fill up a variable called Result, and because there was no return statement developers where forced to maintain a clean flow. This is how it should be everywhere IMHO

    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #58

    Automatically vetoing that idea, because that's what VB6 did! :laugh: If you're desperate, VB.NET still allows it: To return a value using Exit Function or End Function | How to: Return a Value from a Procedure (Visual Basic) | Microsoft Docs[^]


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      You are welcome to your opinion*, but I like the way it's consistent: the indentation is the whole of the relevant block of code:

      if (a)
      b;

      if (a)
      {
      b;
      c;
      }

      Instead of the inconsistent Allman:

      if (a)
      b;

      if (a)
      {
      b;
      c;
      }

      * As long as your opinion doesn't include using 1TB, of course.

      Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

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

      I have been brainwashed to NEVER do:

      if (a)
      b;

      I think the preference is a matter of taste. I don't find either alternative more "consistent". "Consistent" depends on how you define that word (and the words "relevant code"). When skimming over large blocks of code the braces are - for my eyes - easier to find Allman style. And the b; is very slightly easier on my eye in your Allman example. My guess is that we prefer what we were exposed to when we started walking... coding...

      "If we don't change direction, we'll end up where we're going"

      A 1 Reply Last reply
      0
      • F Forogar

        I have just had a heated argument cage fight lively discussion with some of my team members about ReSharper's suggestion of refactoring code to replace nested ifs with a series of multiple early return statements. This caused horribly messy code that ReSharper actually described in it's help as "more readable"! Using a version of their example code (which is a lot simpler than the actual code in question):

        void SomeFunction(SomeThing s)
        {
        if (s != null)
        {
        if (s.Thing != null)
        {
        // Do Some Process with s.Thing
        .
        .
        .
        }
        }
        }

        becomes:

        void SomeFunction(SomeThing s)
        {
        if (s == null) return
        if (s.Thing == null) return;
        // Do Some Process with s.Thing
        .
        .
        .
        }

        This makes a complete hash of the natural flow of the code and introduces an execution statement (return) on the same line as the "if" which is bad coding practice, and then does it again, and then drops though to do some actual processing. If I was to refactor the code it would do this:

        void SomeFunction(SomeThing s)
        {
        if (s != null && s.Thing != null)
        {
        // Do Some Process with s.Thing
        .
        .
        .
        }
        }

        or possibly:

        void SomeFunction(SomeThing s)
        {
        if (s?.Thing != null)
        {
        // Do Some Process with s.Thing
        .
        .
        .
        }
        }

        Which is a lot cleaner! ...and this isn't even considering a method that returns a value of some sort. Opinions?

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

        P Offline
        P Offline
        Peter R Fletcher
        wrote on last edited by
        #60

        I use 'early' returns only to handle errors (typically input or 'no data' errors that don't merit throwing an exception) where the function is returning some sort of null value or an error code.

        1 Reply Last reply
        0
        • K kalberts

          The first person to use the term "clean flow" has the definition right to the term :-) My experience is that prohibiting return (and continue and break) easily ruins that "clean flow". The clean flow should be for the normal, standard, expected behaviour. If something unexpected occurs, or some special case appears, you should handle it (as far as required) and get out of there! You shouldn't clutter up the rest of the function code with error flags and statuses pertaining to a special condition that was handled much higher up, serving only to skip the rest of the function code. That messes up the rest of the function code. "Then you should raise an exception", some says. That's just another way of returning prematurely; it does not ensure a single point of exit. Besides, the situation is not necessarity exceptional, it may be simply detecting that there is nothing more to be done. Program flow by exceptions is certainly no clean code flow.

          D Offline
          D Offline
          Delphi 7 Solutions
          wrote on last edited by
          #61

          Not sure what you mean I never had to uses error flags and statuses for anything like this. In my experience prohibiting return (and continue and break) never ruined that "clean flow" but always improved it. And without using stuff like ugly flags and statuses. In our coding guidelines the return is prohibited, and since this was introduced we saw the time needed for maintenance and bugfixing dropped noticable. And yes, exceptions are not to be used for program flow, that is very true.

          1 Reply Last reply
          0
          • F Forogar

            I have just had a heated argument cage fight lively discussion with some of my team members about ReSharper's suggestion of refactoring code to replace nested ifs with a series of multiple early return statements. This caused horribly messy code that ReSharper actually described in it's help as "more readable"! Using a version of their example code (which is a lot simpler than the actual code in question):

            void SomeFunction(SomeThing s)
            {
            if (s != null)
            {
            if (s.Thing != null)
            {
            // Do Some Process with s.Thing
            .
            .
            .
            }
            }
            }

            becomes:

            void SomeFunction(SomeThing s)
            {
            if (s == null) return
            if (s.Thing == null) return;
            // Do Some Process with s.Thing
            .
            .
            .
            }

            This makes a complete hash of the natural flow of the code and introduces an execution statement (return) on the same line as the "if" which is bad coding practice, and then does it again, and then drops though to do some actual processing. If I was to refactor the code it would do this:

            void SomeFunction(SomeThing s)
            {
            if (s != null && s.Thing != null)
            {
            // Do Some Process with s.Thing
            .
            .
            .
            }
            }

            or possibly:

            void SomeFunction(SomeThing s)
            {
            if (s?.Thing != null)
            {
            // Do Some Process with s.Thing
            .
            .
            .
            }
            }

            Which is a lot cleaner! ...and this isn't even considering a method that returns a value of some sort. Opinions?

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

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

            When multiple returns reduce the nesting inside a module they make a lot of sense (as well as making the code more readable in my opinion). This is similar to using the && and || conditionals that only evaluation clauses as needed.

            1 Reply Last reply
            0
            • M megaadam

              I have been brainwashed to NEVER do:

              if (a)
              b;

              I think the preference is a matter of taste. I don't find either alternative more "consistent". "Consistent" depends on how you define that word (and the words "relevant code"). When skimming over large blocks of code the braces are - for my eyes - easier to find Allman style. And the b; is very slightly easier on my eye in your Allman example. My guess is that we prefer what we were exposed to when we started walking... coding...

              "If we don't change direction, we'll end up where we're going"

              A Offline
              A Offline
              Andre Pereira
              wrote on last edited by
              #63

              Quote:

              I have been brainwashed to NEVER do: if (a) b;

              Good for you, you are ready to avoid a billion dollar bug because "I don't need braces or consistent rules".

              1 Reply Last reply
              0
              • L Lost User

                sort of depends on what I call the "story" if the story says "if X is null or is [example] an empty list then stop processing" = in my mind early return. if the story says "do thus-ad-that to the contents of X" - to save on errors naturally check X for null/empty makes sense - then of course it's if (X != null) ...

                Forogar wrote:

                ntroduces an execution statement (return) on the same line as the "if" which is bad coding practice

                ?????????????? bad coding practice ????????????????? huh? Nothing is wrong, nor bad, nor unsafe with that code, it is perfectly good code. you confuse practice with style: style is subjective, "bad practice" increases the chance for error or failure. personally I dislike code that runs too many pages, so I often put short single statements and opening braces on the same line as the if (), while () etc. (Coming from K&R style C background.) and I like .... AND SO, before you say it, I'll say it for you: --- you think my style is ugly. Well guess what: ---- I think your style is ugly. AND WHO CARES!! 1. it's the code that matters, NOT THE STYLE, 2. different style IS NOT BAD PRACTICE weather you like it or not. (please don't bring up "readability" bullshit, I find more compact more readable, please don't bring up "industry standard" - my K&R background, and I'm not the only one doing it that way.) 3. you can have the editor re-format [to your preferred] style on one keystroke, so even more WHO CARES. nuff said.

                Message Signature (Click to edit ->)

                A Offline
                A Offline
                Andre Pereira
                wrote on last edited by
                #64

                If you have dealt with Unix fanboys, you'll now doubt have encountered the stupid "which hacky character shall we use for white space: spaces or tabs?". The correct answer is "whatever the team is using, I format it to that before committing". As for consistency, I only really am a stickler with fucking braces. I've seen too many recurrent bugs because a programmer is cute and makes a braceless if. Then the next one comes in, adds one more call and doesn't realise the braces are gone. Ooops, billion dollar bug introduced. Nasty and no need.

                L 1 Reply Last reply
                0
                • F Forogar

                  I have just had a heated argument cage fight lively discussion with some of my team members about ReSharper's suggestion of refactoring code to replace nested ifs with a series of multiple early return statements. This caused horribly messy code that ReSharper actually described in it's help as "more readable"! Using a version of their example code (which is a lot simpler than the actual code in question):

                  void SomeFunction(SomeThing s)
                  {
                  if (s != null)
                  {
                  if (s.Thing != null)
                  {
                  // Do Some Process with s.Thing
                  .
                  .
                  .
                  }
                  }
                  }

                  becomes:

                  void SomeFunction(SomeThing s)
                  {
                  if (s == null) return
                  if (s.Thing == null) return;
                  // Do Some Process with s.Thing
                  .
                  .
                  .
                  }

                  This makes a complete hash of the natural flow of the code and introduces an execution statement (return) on the same line as the "if" which is bad coding practice, and then does it again, and then drops though to do some actual processing. If I was to refactor the code it would do this:

                  void SomeFunction(SomeThing s)
                  {
                  if (s != null && s.Thing != null)
                  {
                  // Do Some Process with s.Thing
                  .
                  .
                  .
                  }
                  }

                  or possibly:

                  void SomeFunction(SomeThing s)
                  {
                  if (s?.Thing != null)
                  {
                  // Do Some Process with s.Thing
                  .
                  .
                  .
                  }
                  }

                  Which is a lot cleaner! ...and this isn't even considering a method that returns a value of some sort. Opinions?

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

                  S Offline
                  S Offline
                  SpiritualMadMan
                  wrote on last edited by
                  #65

                  For me, as a novice, my first concern is can I read and understand the code after a good nights rest. After I get it running, then look for ways to make it better. Of course VS2017 does a lot for me. I use inline returns all the time for avoiding null errors when getting data from a DataGridView. But, for compound conditions it depends on what I am doing and whether they can be easily combined. As for the final version with the "?" for me that obfuscates what is happening. But, then I am a novice. In this case I would probably settle on the next to last version.

                  1 Reply Last reply
                  0
                  • F Forogar

                    I have just had a heated argument cage fight lively discussion with some of my team members about ReSharper's suggestion of refactoring code to replace nested ifs with a series of multiple early return statements. This caused horribly messy code that ReSharper actually described in it's help as "more readable"! Using a version of their example code (which is a lot simpler than the actual code in question):

                    void SomeFunction(SomeThing s)
                    {
                    if (s != null)
                    {
                    if (s.Thing != null)
                    {
                    // Do Some Process with s.Thing
                    .
                    .
                    .
                    }
                    }
                    }

                    becomes:

                    void SomeFunction(SomeThing s)
                    {
                    if (s == null) return
                    if (s.Thing == null) return;
                    // Do Some Process with s.Thing
                    .
                    .
                    .
                    }

                    This makes a complete hash of the natural flow of the code and introduces an execution statement (return) on the same line as the "if" which is bad coding practice, and then does it again, and then drops though to do some actual processing. If I was to refactor the code it would do this:

                    void SomeFunction(SomeThing s)
                    {
                    if (s != null && s.Thing != null)
                    {
                    // Do Some Process with s.Thing
                    .
                    .
                    .
                    }
                    }

                    or possibly:

                    void SomeFunction(SomeThing s)
                    {
                    if (s?.Thing != null)
                    {
                    // Do Some Process with s.Thing
                    .
                    .
                    .
                    }
                    }

                    Which is a lot cleaner! ...and this isn't even considering a method that returns a value of some sort. Opinions?

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

                    M Offline
                    M Offline
                    Martin ISDN
                    wrote on last edited by
                    #66

                    i would go ether way for what produces smaller and simpler code. since this is a trivial example it is difficult to speak in general. in this case i wouldn't go for No.1 it's against my taste, but No.2, No.3 and No.4 are OK. No.2 and No.3 seem to have a different philosophy but they do exactly the same thing, imminent return if s == null. i think No.2 is the way assembly coder would see the situation and No.1 would be how a Pascal programmer sees it. structured style programmers would go for a one exit routine, but sometimes that makes code especially complicated.

                    1 Reply Last reply
                    0
                    • C CodeWraith

                      Urban Cricket wrote:

                      John Carmack uses early returns.

                      Looking at my bookshelf: The Graphics Programming Black Book, by Michael Abrash and with a foreword by John Carmack. Fearsomely thick tome, full of how to write fast graphics code. That kind of processor cycle counting on a 386 or 486 is outdated, while the algorithmic optimizations remain as current as ever, especially if you are able to delegate them to the graphics processor. Early returns as a way to waste no processor cycle too much in a function is only rarely important anymore.

                      I have lived with several Zen masters - all of them were cats. His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.

                      S Offline
                      S Offline
                      sasadler
                      wrote on last edited by
                      #67

                      Unless you're an embedded developer trying to squeak out the quickest code on an underpowered processor. I was always getting projects with weenie processors and grandiose firmware requirements. It seems that the hardware engineer had a rather limited grasp on firmware development and how much space/speed would be required for the project. They eventually figured out that I should be involved in the processor and memory requirements selection.

                      C 1 Reply Last reply
                      0
                      • F Forogar

                        I have just had a heated argument cage fight lively discussion with some of my team members about ReSharper's suggestion of refactoring code to replace nested ifs with a series of multiple early return statements. This caused horribly messy code that ReSharper actually described in it's help as "more readable"! Using a version of their example code (which is a lot simpler than the actual code in question):

                        void SomeFunction(SomeThing s)
                        {
                        if (s != null)
                        {
                        if (s.Thing != null)
                        {
                        // Do Some Process with s.Thing
                        .
                        .
                        .
                        }
                        }
                        }

                        becomes:

                        void SomeFunction(SomeThing s)
                        {
                        if (s == null) return
                        if (s.Thing == null) return;
                        // Do Some Process with s.Thing
                        .
                        .
                        .
                        }

                        This makes a complete hash of the natural flow of the code and introduces an execution statement (return) on the same line as the "if" which is bad coding practice, and then does it again, and then drops though to do some actual processing. If I was to refactor the code it would do this:

                        void SomeFunction(SomeThing s)
                        {
                        if (s != null && s.Thing != null)
                        {
                        // Do Some Process with s.Thing
                        .
                        .
                        .
                        }
                        }

                        or possibly:

                        void SomeFunction(SomeThing s)
                        {
                        if (s?.Thing != null)
                        {
                        // Do Some Process with s.Thing
                        .
                        .
                        .
                        }
                        }

                        Which is a lot cleaner! ...and this isn't even considering a method that returns a value of some sort. Opinions?

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

                        T Offline
                        T Offline
                        TylerMc007
                        wrote on last edited by
                        #68

                        I would argue there is no “one way”. When it makes sense use an early return, then do so. The problem I so often see is both methods are used poorly. Programmers use embedded return statements when they should be throwing and catching errors, or they will have one method which has five different returns (bad). Or they use 5 tier “if” statements which is usually an indication that they may need to refactor code, maybe use function overloading or polymorphic behavior. In my opinion, programming is as much art as it is science, knowing when to apply the rules and when not to.

                        1 Reply Last reply
                        0
                        • S sasadler

                          Unless you're an embedded developer trying to squeak out the quickest code on an underpowered processor. I was always getting projects with weenie processors and grandiose firmware requirements. It seems that the hardware engineer had a rather limited grasp on firmware development and how much space/speed would be required for the project. They eventually figured out that I should be involved in the processor and memory requirements selection.

                          C Offline
                          C Offline
                          CodeWraith
                          wrote on last edited by
                          #69

                          I sure know that. My first computer had a hex keyboard and actually was very much like today's microcontrollers. And I still have it and use it, just to stay sharp. Edit: Picture[^]

                          I have lived with several Zen masters - all of them were cats. His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.

                          S 1 Reply Last reply
                          0
                          • C CodeWraith

                            Mark_Wallace wrote:

                            Save your processor a billionth of a second of effort

                            Depending on the processor and the stack protocol you use to pass parameters and return values, you may find that you gain little to nothing. Been there recently when I had to modify the 'traditional' call protocol of my old computer. Now it uses a second stack to pass parameters and return values, instead of inlining the parameters with the code for the call. Yuck, was way to close to self modifying code!. And I extended the address of the subroutine to 24 bits, so that I now can do bank switching in the calling protocol and call code anywhere in a 16 Mb memory space without the processor noticing anything. Not bad for a 40 year old 8 bit computer. If only there was a convenient way to access data anywhere in that memory space as well.

                            I have lived with several Zen masters - all of them were cats. His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.

                            M Offline
                            M Offline
                            Mark_Wallace
                            wrote on last edited by
                            #70

                            :thumbsup: Sounds like my kind of toy!

                            I wanna be a eunuchs developer! Pass me a bread knife!

                            1 Reply Last reply
                            0
                            • F Forogar

                              I have just had a heated argument cage fight lively discussion with some of my team members about ReSharper's suggestion of refactoring code to replace nested ifs with a series of multiple early return statements. This caused horribly messy code that ReSharper actually described in it's help as "more readable"! Using a version of their example code (which is a lot simpler than the actual code in question):

                              void SomeFunction(SomeThing s)
                              {
                              if (s != null)
                              {
                              if (s.Thing != null)
                              {
                              // Do Some Process with s.Thing
                              .
                              .
                              .
                              }
                              }
                              }

                              becomes:

                              void SomeFunction(SomeThing s)
                              {
                              if (s == null) return
                              if (s.Thing == null) return;
                              // Do Some Process with s.Thing
                              .
                              .
                              .
                              }

                              This makes a complete hash of the natural flow of the code and introduces an execution statement (return) on the same line as the "if" which is bad coding practice, and then does it again, and then drops though to do some actual processing. If I was to refactor the code it would do this:

                              void SomeFunction(SomeThing s)
                              {
                              if (s != null && s.Thing != null)
                              {
                              // Do Some Process with s.Thing
                              .
                              .
                              .
                              }
                              }

                              or possibly:

                              void SomeFunction(SomeThing s)
                              {
                              if (s?.Thing != null)
                              {
                              // Do Some Process with s.Thing
                              .
                              .
                              .
                              }
                              }

                              Which is a lot cleaner! ...and this isn't even considering a method that returns a value of some sort. Opinions?

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

                              M Offline
                              M Offline
                              MSBassSinger
                              wrote on last edited by
                              #71

                              One way into a method, one way out (not counting ref and out parameters, which are intuitively obvious). After 40+ years in software development, my tolerance for lazy programmers that just want to hack up some code and get the minimum done with the least time - is very low. Some software shops are starting to use an approach I have used for years when I hired developers. Instead of hiring 10 "full stack" developers with an eye to cheap labor, H1-B and other inexperienced programmers cranking out poor code that just barely works for today, they take a different road. For those 10 positions, they hire 6 experienced software engineers that understand how to code for value engineering, supportability, likely future needs, performance, readability, and meaningful comments. A 7th junior developer is hired who has an attitude and willingness to learn from the senior developers - a person whose character is teachable. No H1-B's. The end result is better code, faster development cycles, fewer bugs, and lower life cycle cost. Experienced software engineers understand how incredibly sophomoric it is to have multiple returns. Now get off my lawn! :)

                              B 1 Reply Last reply
                              0
                              • C CodeWraith

                                I sure know that. My first computer had a hex keyboard and actually was very much like today's microcontrollers. And I still have it and use it, just to stay sharp. Edit: Picture[^]

                                I have lived with several Zen masters - all of them were cats. His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.

                                S Offline
                                S Offline
                                sasadler
                                wrote on last edited by
                                #72

                                I kind of wish I'd kept my first computer. It was a z80 S100 bus system where I wire wrapped all the boards except for the display card (16 lines by 64 character with a rf modulator to display on a TV). I had a 2kb monitor program (Zapple) that gave you the standard peek/poke memory, dump memory, input/output to I/O, run from an address and set break points. I use to knows most of the z80 instruction set by the numbers. Good times.

                                C 1 Reply Last reply
                                0
                                • F Forogar

                                  I have just had a heated argument cage fight lively discussion with some of my team members about ReSharper's suggestion of refactoring code to replace nested ifs with a series of multiple early return statements. This caused horribly messy code that ReSharper actually described in it's help as "more readable"! Using a version of their example code (which is a lot simpler than the actual code in question):

                                  void SomeFunction(SomeThing s)
                                  {
                                  if (s != null)
                                  {
                                  if (s.Thing != null)
                                  {
                                  // Do Some Process with s.Thing
                                  .
                                  .
                                  .
                                  }
                                  }
                                  }

                                  becomes:

                                  void SomeFunction(SomeThing s)
                                  {
                                  if (s == null) return
                                  if (s.Thing == null) return;
                                  // Do Some Process with s.Thing
                                  .
                                  .
                                  .
                                  }

                                  This makes a complete hash of the natural flow of the code and introduces an execution statement (return) on the same line as the "if" which is bad coding practice, and then does it again, and then drops though to do some actual processing. If I was to refactor the code it would do this:

                                  void SomeFunction(SomeThing s)
                                  {
                                  if (s != null && s.Thing != null)
                                  {
                                  // Do Some Process with s.Thing
                                  .
                                  .
                                  .
                                  }
                                  }

                                  or possibly:

                                  void SomeFunction(SomeThing s)
                                  {
                                  if (s?.Thing != null)
                                  {
                                  // Do Some Process with s.Thing
                                  .
                                  .
                                  .
                                  }
                                  }

                                  Which is a lot cleaner! ...and this isn't even considering a method that returns a value of some sort. Opinions?

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

                                  S Offline
                                  S Offline
                                  SeattleC
                                  wrote on last edited by
                                  #73

                                  I prefer early return for domain errors like passing nullptr into a function that wants to operate on the object. I prefer multiple returns over nested indents because after two or three levels of indentation, I can't remember what conditions held in the block I'm in. I have the painful experience of using a language (Pascal) that only permitted a function to return at the bottom, and I almost always needed an auxiliary variable to record exit from nested loops. You can't get rid of implicit control transfers in languages that have break in switch statements, continue and break in loops, and an explicit return statement. Better to learn to live with it.

                                  1 Reply Last reply
                                  0
                                  • S sasadler

                                    I kind of wish I'd kept my first computer. It was a z80 S100 bus system where I wire wrapped all the boards except for the display card (16 lines by 64 character with a rf modulator to display on a TV). I had a 2kb monitor program (Zapple) that gave you the standard peek/poke memory, dump memory, input/output to I/O, run from an address and set break points. I use to knows most of the z80 instruction set by the numbers. Good times.

                                    C Offline
                                    C Offline
                                    CodeWraith
                                    wrote on last edited by
                                    #74

                                    Great! The good old times never ended for me, partially because I would see it as a great loss if that old computer ever died, partially because such old tech is the is the only option to tinker around with as a hobby without getting poor. I'm certain you know what kind of equipment and knowledge is needed when you want to play around with the most modern stuff. It's more interesting to overcome the limitations of the old stuff anyway. Modern chipsets do that for you, so where is the point in puzzling them together as intended? Why don't you grab a Z80 on ebay and build a new old computer to bring back the old times?

                                    I have lived with several Zen masters - all of them were cats. His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.

                                    S 1 Reply Last reply
                                    0
                                    • F Forogar

                                      I have just had a heated argument cage fight lively discussion with some of my team members about ReSharper's suggestion of refactoring code to replace nested ifs with a series of multiple early return statements. This caused horribly messy code that ReSharper actually described in it's help as "more readable"! Using a version of their example code (which is a lot simpler than the actual code in question):

                                      void SomeFunction(SomeThing s)
                                      {
                                      if (s != null)
                                      {
                                      if (s.Thing != null)
                                      {
                                      // Do Some Process with s.Thing
                                      .
                                      .
                                      .
                                      }
                                      }
                                      }

                                      becomes:

                                      void SomeFunction(SomeThing s)
                                      {
                                      if (s == null) return
                                      if (s.Thing == null) return;
                                      // Do Some Process with s.Thing
                                      .
                                      .
                                      .
                                      }

                                      This makes a complete hash of the natural flow of the code and introduces an execution statement (return) on the same line as the "if" which is bad coding practice, and then does it again, and then drops though to do some actual processing. If I was to refactor the code it would do this:

                                      void SomeFunction(SomeThing s)
                                      {
                                      if (s != null && s.Thing != null)
                                      {
                                      // Do Some Process with s.Thing
                                      .
                                      .
                                      .
                                      }
                                      }

                                      or possibly:

                                      void SomeFunction(SomeThing s)
                                      {
                                      if (s?.Thing != null)
                                      {
                                      // Do Some Process with s.Thing
                                      .
                                      .
                                      .
                                      }
                                      }

                                      Which is a lot cleaner! ...and this isn't even considering a method that returns a value of some sort. Opinions?

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

                                      F Offline
                                      F Offline
                                      frazGJF
                                      wrote on last edited by
                                      #75

                                      What about 1 return and no braces

                                      void SomeFunction(SomeThing s)
                                      {
                                      if (s == null || s.Thing == null) return;
                                      //... more code
                                      }

                                      Just put all your conditions into that 'if' clause. Too many conditions? Use a method to test the conditions and call that method from the 'if' clause Regards GregJF

                                      1 Reply Last reply
                                      0
                                      • F Forogar

                                        I have just had a heated argument cage fight lively discussion with some of my team members about ReSharper's suggestion of refactoring code to replace nested ifs with a series of multiple early return statements. This caused horribly messy code that ReSharper actually described in it's help as "more readable"! Using a version of their example code (which is a lot simpler than the actual code in question):

                                        void SomeFunction(SomeThing s)
                                        {
                                        if (s != null)
                                        {
                                        if (s.Thing != null)
                                        {
                                        // Do Some Process with s.Thing
                                        .
                                        .
                                        .
                                        }
                                        }
                                        }

                                        becomes:

                                        void SomeFunction(SomeThing s)
                                        {
                                        if (s == null) return
                                        if (s.Thing == null) return;
                                        // Do Some Process with s.Thing
                                        .
                                        .
                                        .
                                        }

                                        This makes a complete hash of the natural flow of the code and introduces an execution statement (return) on the same line as the "if" which is bad coding practice, and then does it again, and then drops though to do some actual processing. If I was to refactor the code it would do this:

                                        void SomeFunction(SomeThing s)
                                        {
                                        if (s != null && s.Thing != null)
                                        {
                                        // Do Some Process with s.Thing
                                        .
                                        .
                                        .
                                        }
                                        }

                                        or possibly:

                                        void SomeFunction(SomeThing s)
                                        {
                                        if (s?.Thing != null)
                                        {
                                        // Do Some Process with s.Thing
                                        .
                                        .
                                        .
                                        }
                                        }

                                        Which is a lot cleaner! ...and this isn't even considering a method that returns a value of some sort. Opinions?

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

                                        B Offline
                                        B Offline
                                        BillWoodruff
                                        wrote on last edited by
                                        #76

                                        Fascinating discussion ! While recognizing the special constraints of device driver, and down-to-the-hardware specific, programming, in a high-level language, like C#, with a powerful optimizing compiler, I favor multiple returns, and "defensive" error checking (throw immediately) wherever possible, asap. For me, "deeply nested" is for the (recursive) birds.

                                        «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

                                        1 Reply Last reply
                                        0
                                        • A Andre Pereira

                                          If you have dealt with Unix fanboys, you'll now doubt have encountered the stupid "which hacky character shall we use for white space: spaces or tabs?". The correct answer is "whatever the team is using, I format it to that before committing". As for consistency, I only really am a stickler with fucking braces. I've seen too many recurrent bugs because a programmer is cute and makes a braceless if. Then the next one comes in, adds one more call and doesn't realise the braces are gone. Ooops, billion dollar bug introduced. Nasty and no need.

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

                                          Oh I hear you on the *nix, but as often it's the text editor that mixes spaces with tabs; i.e. 1. inserts a tab ... but then some editors change it to spaces, some don't... 2. <8 spaces>, inserts spaces ... but then some editors change that to a tab, some don't and for more fun if it's an existing source file, some editors change only the edited lines to/from tabs and leave the untouched lines as they were, and some don't. Arrggh (doing some C programming on Linux, pulling down code snippets, getting lots of both tab and space.) So anyway don't blame the *nix fanbois, it's the [history of their] tools. Worked at one company, the system admin decided the standard tab size would be 3 spaces and forced everybody to set the vi editor to honor this - All fine, except when they got source from outside the company set with the standard tab size of 8. Talk about moaning on and on, and when I suggested they really should use the standard size that everyone else on the planet used, "oh no, we won't change, it's everybody else that should change." ... idiots. as to curly braces (and statements on same line as the if ()), as mentioned I'll normally only not use for very simple single statements, i.e. mostly return, break, continue, or perhaps a single method/function call on it's own. i.e. "if (x == 0) return;" it's pretty clear if you want to add a statement before the return that the curlies aren't there. anything longer (even if it's only a single function/method call but with many or long arguments) just like you I'll multi-line and wrap it in curlies for safety.) as said it's my style, not going to claim it's better or worse than another's style.

                                          Message Signature (Click to edit ->)

                                          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