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. Other Discussions
  3. The Weird and The Wonderful
  4. Which code you suggest?

Which code you suggest?

Scheduled Pinned Locked Moved The Weird and The Wonderful
comquestion
103 Posts 44 Posters 4 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.
  • Z ZurdoDev

    No. I would exit the loop.

    There are only 10 types of people in the world, those who understand binary and those who don't.

    R Offline
    R Offline
    Ravi Bhavnani
    wrote on last edited by
    #80

    And that's what example #1 does, not #2. /ravi

    My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

    Z 1 Reply Last reply
    0
    • R Ravi Bhavnani

      And that's what example #1 does, not #2. /ravi

      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

      Z Offline
      Z Offline
      ZurdoDev
      wrote on last edited by
      #81

      Yes, but it returns out of the function, which is the actual debate. So, example 2, even though it has several issues, it does not exit the function, which is what I support. However, what I would do is set the variable as in example #2 and then exit the loop.

      There are only 10 types of people in the world, those who understand binary and those who don't.

      R 1 Reply Last reply
      0
      • R Rajesh Anuhya

        Code1:

           Boolean DoSomething(string\[\] values)
            {
                foreach (string s in values)
                    if (s == "ABC")
                        return true;
                return false;
            }
        

        Code2:

        Boolean DoSomething(string[] values)
        {
        bool retValue = false;
        foreach (string s in values)
        if (s == "ABC")
        retValue=true;
        return retValue;
        }

        in the above 2 codes which code you will suggest and why? waiting for your valuable comments. Thanks --RA

        my Tip/Tricks[^] |Contact me[^]

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

        Neither; I prefer not to end a foreach when a for will work just as well (if not better).

        bool result = false ;

        for ( int i = 0 ; !result && i < values.Length ; i++ )
        {
        result = values [ i ] == "ABC" ;
        }

        return ( result ) ;

        This could also lead to a discussion of ifless programming. :-D

        1 Reply Last reply
        0
        • Z ZurdoDev

          Yes, but it returns out of the function, which is the actual debate. So, example 2, even though it has several issues, it does not exit the function, which is what I support. However, what I would do is set the variable as in example #2 and then exit the loop.

          There are only 10 types of people in the world, those who understand binary and those who don't.

          R Offline
          R Offline
          Ravi Bhavnani
          wrote on last edited by
          #83

          ryanb31 wrote:

          However, what I would do is set the variable as in example #2 and then exit the loop.

          Ah, OK then. :) /ravi

          My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

          1 Reply Last reply
          0
          • Z ZurdoDev

            But you're missing the fact that you can exit a loop when you find what you need. You don't have to continue processing.

            There are only 10 types of people in the world, those who understand binary and those who don't.

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

            Just because you can doesn't mean you should.

            Z 1 Reply Last reply
            0
            • P PIEBALDconsult

              Just because you can doesn't mean you should.

              Z Offline
              Z Offline
              ZurdoDev
              wrote on last edited by
              #85

              Vague but true.

              There are only 10 types of people in the world, those who understand binary and those who don't.

              1 Reply Last reply
              0
              • S Stefan_Lang

                ... until you introduce code that needs clean-up at one point or another. Many of the functions I look at every day are a decade old or more, and consist of several hundred lines of codes with half a dozen levels of nesting or more. Every single one of them allocates stuff, or does something else requiring cleanup. More often than not, this happens before something else happens that necessitates a premature return. Some of the really old functions use goto exit; to immediately jump to the cleanup code. I use a flag. Sure, not everyone works on such a codebase. But the truth is, the majority of programmers doesn't work on brand-new projects either. 80% work on internal programs designed to improve certain processes inside of a single company. Lots of code, and sometimes with a lifetime higher than that of some of their current programmers. In that context, IME, premature returns are almost always a bad idea.

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

                Stefan_Lang wrote:

                premature returns are almost always a bad idea

                :thumbsup: Definitely a code smell.

                1 Reply Last reply
                0
                • R Rajesh Anuhya

                  Code1:

                     Boolean DoSomething(string\[\] values)
                      {
                          foreach (string s in values)
                              if (s == "ABC")
                                  return true;
                          return false;
                      }
                  

                  Code2:

                  Boolean DoSomething(string[] values)
                  {
                  bool retValue = false;
                  foreach (string s in values)
                  if (s == "ABC")
                  retValue=true;
                  return retValue;
                  }

                  in the above 2 codes which code you will suggest and why? waiting for your valuable comments. Thanks --RA

                  my Tip/Tricks[^] |Contact me[^]

                  D Offline
                  D Offline
                  DarkChuky CR
                  wrote on last edited by
                  #87

                  I guess, I would try something like:

                  Boolean DoSomething(string[] values)
                  {

                          bool retValue = false;
                          if (values != null)            
                          for(int i = 0; i < values.Count(); i++)
                          {
                              if (values\[i\] == "ABC")
                              {
                                  retValue = true;
                                  i = values.Count();
                              }
                          }
                          return retValue;
                      }
                  

                  I know the example is a simple loop, but thinking in maintenance and performance this will: - Avoid the internal context and memory usage of a FOREACH (FOR is recommended when u do only 1 single access to the object[i]) - Use of a state variable for a return is recommended rater that having a lot of returns. (readability?) - The Return in the for or foreach cause a BREAK, if I'm not wrong that was expensive in the past, not sure with modern languages.

                  J 1 Reply Last reply
                  0
                  • R Rajesh Anuhya

                    Code1:

                       Boolean DoSomething(string\[\] values)
                        {
                            foreach (string s in values)
                                if (s == "ABC")
                                    return true;
                            return false;
                        }
                    

                    Code2:

                    Boolean DoSomething(string[] values)
                    {
                    bool retValue = false;
                    foreach (string s in values)
                    if (s == "ABC")
                    retValue=true;
                    return retValue;
                    }

                    in the above 2 codes which code you will suggest and why? waiting for your valuable comments. Thanks --RA

                    my Tip/Tricks[^] |Contact me[^]

                    T Offline
                    T Offline
                    TheGreatAndPowerfulOz
                    wrote on last edited by
                    #88

                    the first because it will terminate the loop on the first found string, and is thus faster. The second loops the whole array regardless.

                    If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams
                    You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering.-Wernher von Braun
                    Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

                    1 Reply Last reply
                    0
                    • J jibalt

                      I refuse to hire anyone who subscribes to that inane single exit nonsense. And that code is awful for other reasons too. The correct code is

                      return values.Any(s => s == "ABC")

                      unless it can be proved to be a performance bottleneck.

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

                      That may be OK for C#; but how does it translate to other languages?

                      J 1 Reply Last reply
                      0
                      • R Rajesh Anuhya

                        Code1:

                           Boolean DoSomething(string\[\] values)
                            {
                                foreach (string s in values)
                                    if (s == "ABC")
                                        return true;
                                return false;
                            }
                        

                        Code2:

                        Boolean DoSomething(string[] values)
                        {
                        bool retValue = false;
                        foreach (string s in values)
                        if (s == "ABC")
                        retValue=true;
                        return retValue;
                        }

                        in the above 2 codes which code you will suggest and why? waiting for your valuable comments. Thanks --RA

                        my Tip/Tricks[^] |Contact me[^]

                        J Offline
                        J Offline
                        johannesnestler
                        wrote on last edited by
                        #90

                        I know you were asking for another thing - but the code and alternative you presented are not equal - so someone has to choose Option 1, because this code is the correct one (exit Loop on found string) - so you second DoSomething should do break the loop after a value was found (you will have to add the brackets you left away ;P ) Btw. are you new on "the Internet"? :laugh: It feels about the billionth discussion about early exit vs. single return point - AFAIK this will forever be a matter of style and choise...

                        1 Reply Last reply
                        0
                        • D DarkChuky CR

                          I guess, I would try something like:

                          Boolean DoSomething(string[] values)
                          {

                                  bool retValue = false;
                                  if (values != null)            
                                  for(int i = 0; i < values.Count(); i++)
                                  {
                                      if (values\[i\] == "ABC")
                                      {
                                          retValue = true;
                                          i = values.Count();
                                      }
                                  }
                                  return retValue;
                              }
                          

                          I know the example is a simple loop, but thinking in maintenance and performance this will: - Avoid the internal context and memory usage of a FOREACH (FOR is recommended when u do only 1 single access to the object[i]) - Use of a state variable for a return is recommended rater that having a lot of returns. (readability?) - The Return in the for or foreach cause a BREAK, if I'm not wrong that was expensive in the past, not sure with modern languages.

                          J Offline
                          J Offline
                          johannesnestler
                          wrote on last edited by
                          #91

                          Hm, interesting, I can agree a Little on your first point - although this is no practical thinking for most .NET developers, because the "perfomance" impact is outweighted by more expressive code - I do express something when I write "foreach" (I express I want to do something FOR EACH element, don't depend on index or IList, just IEnumerable, I won't need an index, I don't need a break condition, ...). So the additional 2 context variables used internally by the foreach loop are fast earned back... Point 2 will never be decided on "the Internet" - recommended? - not by me - it depends to much on overall style of the code (a lot of coders first evaluate all arguments and do early returns) and personal choice - and if you do "Microoptimazations" in .NET like you/I did in old c++ days, you should do early returns - so, readability (for you) or performance? Point 3, though irrelevant for this discussion because this code should break on any found case (op alternatives are not equivalent), but I'm wondering why you think setting the variable to the break condition and evaluate it again is faster than a break statement? If this is any faster (I will test) this is intersting to know - it seems you are the master of "high perfomance super optimized .NET code" - but I'm not shure if .NET is the right realm for code which needs this kind of optimizations - shouldn't we do such perf. critical things in native code? Kind regards Johannes

                          D 1 Reply Last reply
                          0
                          • J johannesnestler

                            Hm, interesting, I can agree a Little on your first point - although this is no practical thinking for most .NET developers, because the "perfomance" impact is outweighted by more expressive code - I do express something when I write "foreach" (I express I want to do something FOR EACH element, don't depend on index or IList, just IEnumerable, I won't need an index, I don't need a break condition, ...). So the additional 2 context variables used internally by the foreach loop are fast earned back... Point 2 will never be decided on "the Internet" - recommended? - not by me - it depends to much on overall style of the code (a lot of coders first evaluate all arguments and do early returns) and personal choice - and if you do "Microoptimazations" in .NET like you/I did in old c++ days, you should do early returns - so, readability (for you) or performance? Point 3, though irrelevant for this discussion because this code should break on any found case (op alternatives are not equivalent), but I'm wondering why you think setting the variable to the break condition and evaluate it again is faster than a break statement? If this is any faster (I will test) this is intersting to know - it seems you are the master of "high perfomance super optimized .NET code" - but I'm not shure if .NET is the right realm for code which needs this kind of optimizations - shouldn't we do such perf. critical things in native code? Kind regards Johannes

                            D Offline
                            D Offline
                            DarkChuky CR
                            wrote on last edited by
                            #92

                            Good reply!, I already don't remember if the Break is expensive, hasn't google.... but I remember it was like using the old GOTO, developer just avoids it when possible. Yes, if we just think in the original code example, there is like no discussion, the code is so simple that it will return with the first find, so no real issues. But you have talk about something that I has been seen in the "modern" developers and languages (not sure if can apply to the original topic):

                            "it seems you are the master of ""high perfomance super optimized .NET code"" - but I'm not shure if .NET is the right realm for code which needs this kind of optimizations - shouldn't we do such perf. critical things in native code?"

                            With modern PCs, the increase of memory and processors speeds (also number of CPUS) developers are forgetting about performance, speed and memory issues, its like they don't remember that the memory is not infinite, and that with the speed of data increase the application can go from 1 minute process to 1 week in a couple of months... I'm no a Performance maniac, but I worked like 4 year in Windows Mobile (C#) and I learned how important it is, and in fact has been a good ADD ON in today projects, I can "easy-sly" understand where our current project will fall and in fact they has fail (I was totally ignored by no mobile developers, then they didn't toke my recommendation, 2 months later the application was failed with Out of memory, with Time out, bandwidth, channel amount of data, with tooo long process).. today my coworkers are taking care of it, but they learned in the wrong way, when it failed in Production. I can say I'm a little worried about modern programing, because our Junior are not facing those Memory and Performance issues, even my Cell Phone have more CPUs and Memory than my 4 years old PC... then neither current mobile developers are facing performance issues until its too late and they will be the Senniors of tomorrow

                            J 1 Reply Last reply
                            0
                            • OriginalGriffO OriginalGriff

                              What if you have two nested loops? It's a lot harder to exit them both...

                              The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                              K Offline
                              K Offline
                              Klaus Werner Konrad
                              wrote on last edited by
                              #93

                              goto

                              1 Reply Last reply
                              0
                              • D DarkChuky CR

                                Good reply!, I already don't remember if the Break is expensive, hasn't google.... but I remember it was like using the old GOTO, developer just avoids it when possible. Yes, if we just think in the original code example, there is like no discussion, the code is so simple that it will return with the first find, so no real issues. But you have talk about something that I has been seen in the "modern" developers and languages (not sure if can apply to the original topic):

                                "it seems you are the master of ""high perfomance super optimized .NET code"" - but I'm not shure if .NET is the right realm for code which needs this kind of optimizations - shouldn't we do such perf. critical things in native code?"

                                With modern PCs, the increase of memory and processors speeds (also number of CPUS) developers are forgetting about performance, speed and memory issues, its like they don't remember that the memory is not infinite, and that with the speed of data increase the application can go from 1 minute process to 1 week in a couple of months... I'm no a Performance maniac, but I worked like 4 year in Windows Mobile (C#) and I learned how important it is, and in fact has been a good ADD ON in today projects, I can "easy-sly" understand where our current project will fall and in fact they has fail (I was totally ignored by no mobile developers, then they didn't toke my recommendation, 2 months later the application was failed with Out of memory, with Time out, bandwidth, channel amount of data, with tooo long process).. today my coworkers are taking care of it, but they learned in the wrong way, when it failed in Production. I can say I'm a little worried about modern programing, because our Junior are not facing those Memory and Performance issues, even my Cell Phone have more CPUs and Memory than my 4 years old PC... then neither current mobile developers are facing performance issues until its too late and they will be the Senniors of tomorrow

                                J Offline
                                J Offline
                                johannesnestler
                                wrote on last edited by
                                #94

                                Very good points. I feel we may have quite similar experiences. I also worked for the last 7 years on Windows CE and Embedded with .NET CF and native code. On Win CE 5 (which was a big step already) we had the 32 MB memory limit per process - How funny, that TODAY one of these old CE 5 devices "showed up" on my office-desk with a "pretty" .net machine-visualization on it - customer (programmer) is telling me that he ran into problems... Guess what? -> Out of Memory! So I totally agree with your points about "Juniors" - And I think we as programmers, will face the same story again, after someone comes up with the idea of the next IThing and want's to play tetris on it. When I was a C++ programmer and realized that I didn't do so much optimizing and inlinig and what not any more, I thought: "Finally we arrived where Memory and CPU perf. is "enough" for my use-cases". But when I startet my first embedded device project a few years later, it was the old story again... So I never mean't that Performance/Memory consumption doesn't matter, and in reality I think about it on every line of code I write (a habit hard to discard if you started programming in a time where it was "expensive"). - Like you said: a good Add On even in todays projects. But I think that today the "code" has to do so much more than just "crunch the numbers". We write code in patterns, abstractions, with test support, support for different plattforms or frameworks, for services, devices, serialization scenarios, for easy maintenance, for whatever is needed on the project at hand, and only sometimes there are performance or memory constraints now (when I started this was ALWAYS one of the most important things - "unreadable guru code for safing 2 bytes? - no problem"). It may be a project where RAD and easy "junior-flipping" is wanted - so go for .NET, easy constructs, no optimazitions where deep knowledge is needed (a case where perf. of foreach vs. for should not matter). But another time you will have todo something very "fast" or "cheap on memory", then comes the time to optimize algorithms and code for specific tasks. But if I'm on a CE device, found a perfomance/memory bottleneck, I would hardly think about optimizing the IL code - I'd go for a native solution (maybe C++) and use Interop to call it (if marshalling perfomance impact is not a problem). Because from my experience the "biggest" gain on using native code, is to circumvent the GC... Of course using unsafe code can be an alternative, and optimizing perf/memory is the onl

                                1 Reply Last reply
                                0
                                • R Rajesh Anuhya

                                  Code1:

                                     Boolean DoSomething(string\[\] values)
                                      {
                                          foreach (string s in values)
                                              if (s == "ABC")
                                                  return true;
                                          return false;
                                      }
                                  

                                  Code2:

                                  Boolean DoSomething(string[] values)
                                  {
                                  bool retValue = false;
                                  foreach (string s in values)
                                  if (s == "ABC")
                                  retValue=true;
                                  return retValue;
                                  }

                                  in the above 2 codes which code you will suggest and why? waiting for your valuable comments. Thanks --RA

                                  my Tip/Tricks[^] |Contact me[^]

                                  V Offline
                                  V Offline
                                  Vasudevan Deepak Kumar
                                  wrote on last edited by
                                  #95

                                  How about with little refinements?

                                  bool DoSomething(string[] values)
                                  {
                                  bool blnReturnValue = false;

                                              foreach (string s in values)
                                              {
                                                   blnReturnValue = s.Equals("ABC");
                                                   if (blnReturnValue)
                                                      break;
                                              }         
                                              
                                             return (blnReturnValue);
                                         }
                                  

                                  Vasudevan Deepak Kumar Personal Homepage BRAINWAVE/1.0 Status-Code: 404 Status-Text: The requested brain could not be found. It may have been deleted or never installed.
                                  --Brisingr Aerowing

                                  1 Reply Last reply
                                  0
                                  • M MarkTJohnson

                                    Oh, you must be lots of fun to work with. The "correct code"? The "correct code" is something that is easy to maintain and produces the desired result. In addition your comment does not address the original question. Which is better, multiple returns or a single return? As a maintenance programmer, I fully support a single return statement. Multiple returns are lazy and prone to creating code that doesn't get tested before getting sent to QA. Granted the example was trivial but the underlying question was not.

                                    J Offline
                                    J Offline
                                    jibalt
                                    wrote on last edited by
                                    #96

                                    I'm a pleasure to work with because I know my craft and I write readable, maintainable code, not the kind of garbage that was posted here ... a result of a) harebrained adherence to a pointless single-return dogma and b) not being familiar the available tools, such as LINQ, and not being familiar with the state of the art, which now includes the sort of functional programming that LINQ represents. Functional programming is the ultimate in "single return", because there are no return statements, there are just functions that yield expressions. But if you're writing crufty imperative code, single return is bad because a) several empirical studies show that it has high cognitive load, making it difficult to interpret and subsequently more likely have bugs in the original and more likely to be broken when changed, and b) it results in the Arrow Anti-Pattern (http://www.codinghorror.com/blog/2006/01/flattening-arrow-code.html). Multiple returns are lazy and prone to creating code that doesn't get tested before getting sent to QA. Oh, you must be a joy to work with as you dishonestly defend positions by making up BS arguments. Laziness and failing to unit test has nothing whatsoever to do with single vs. multiple returns (but it does have a lot to do with people who don't read the literature and so know nothing about the technology of software construction). Multiple returns in guard clauses as mentioned at codinghorror are not "lazy", they are orderly and logical. It's not laziness that leads good programmers to avoid unnecessary structure and condition variables, it's competence.

                                    M 1 Reply Last reply
                                    0
                                    • P PIEBALDconsult

                                      That may be OK for C#; but how does it translate to other languages?

                                      J Offline
                                      J Offline
                                      jibalt
                                      wrote on last edited by
                                      #97

                                      Modern languages these days have similar functional facilities ... this includes C++11, and even Java finally in Java 8. Others are Ruby, Python (although limited to one line), Scala, Haskell, Clojure of course ...

                                      1 Reply Last reply
                                      0
                                      • I irneb

                                        Obviously you missed a break in your 2nd method (or I hope so). If you do this, it's actually a hybrid between the 2 methods. A more "strictly" no-goto (even hidden as a break / premature return) would be as per MarkTJohnson's code samples - i.e. add a check in the loop's conditions to see if the state variable has changed. As for those advocating premature returns, but (in the same breath) dissing goto: Such a return is very related to a goto, as is a break inside a loop. All 3 result in much the same assembly code, the only difference is return/break is safer to use than goto - since goto has more freedom to screw-up. It would be similar in stating that you should always use typed pointers, very true - but it's not as if an untyped pointer is actually "that" different, just possible to do more damage. Not to say that goto's should be used, just to point out that you're walking a fine line between a good idea and contradicting yourself. Also I'll give you the benefit of the doubt and not try to encourage to use the Contains (or other similar stuff) instead of the entire function, as some have already pointed out. With the thinking that you used this as a sample to make a point, not a sample to take literally. That said, I tend to find making the 2nd method work properly and efficiently becomes more code. And with more complex functions the extra code becomes exponentially more. Although I don't have trouble reading the principle itself (either one, as long as formatted so the state-set or returns are clearly highlighted, e.g. a blank line after each), I tend to try and write less code (if possible) - so I'd probably go with method 1 if no other reasons are apparent. This particular thing (i.e. premature return vs state variable) I see as subjective in the same sense that some would like / understand recursive more than iterative loops (or visa versa). If all else is the same - i.e. no need for other stuff to happen too just before the return. Where it does become a pain, is if your function is later extended. You might later add a further portion which should run even if the value is already found. Perhaps some side effect, like counting how many times a positive was returned through the object's life (sorry - stupid example, but it happens).

                                        J Offline
                                        J Offline
                                        jibalt
                                        wrote on last edited by
                                        #98

                                        "As for those advocating premature returns, but (in the same breath) dissing goto: Such a return is very related to a goto, as is a break inside a loop." No it isn't ... goto's are unstructured. This is programming technology and history 101 ... actually .001 ... "All 3 result in much the same assembly code" Irrelevant, and not true, or not predictably true.

                                        1 Reply Last reply
                                        0
                                        • J Jonathan C Dickinson

                                          Second method if security is a concern (methods like that are not vulnerable to timing attacks[^]); first in all other cases (the first method will always execute in N time, the second is O(N)).

                                          He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chinese Proverb] Jonathan C Dickinson (C# Software Engineer)

                                          J Offline
                                          J Offline
                                          jibalt
                                          wrote on last edited by
                                          #99

                                          the first method will always execute in N time, the second is O(N) There is no such thing as "N time". They're both O(N), but the average time for the first one is half that of the second one.

                                          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