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 2 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.
  • S Stefan_Lang

    Renzo Ciafardone wrote:

    unnecessary variable

    YMMV. If the alternative is goto, I choose the variable. If it is 15 layers of nested control statements, I choose the variable. If I know that anyone, including me, may be reading and trying to understand that code next month, I'm using a variable. I'm not saying to always use such a variable - only when it helps keeping the code clean and maintainable. IMO, the variable is sensible and necessary in these cases. (Also, these cases cover pretty much all the code I've worked on over the past 30 years)

    Renzo Ciafardone wrote:

    you are wasting an assignation and then you are adding an extra comparison for each block that fails plus the one that's actually true

    You are underestimating the efficiency of an optimizer: in most cases you won't even notice a difference, as the compiler will optimize away any variable that is only used sporadically. The only exception is if there are multiple checks at the top nesting layer: then you need to add one condition to each top level check after the one that contains the 'abort condition'. The alternative would be just one check and moving the rest of the code down one nesting level. The former may have a minor impact on performance (but see below), the latter will always adversely affect code complexity, and thereby the likelyhood of bugs and the effort of maintenance. Your choice. In the past, I've tended to the latter. But now that I have to deal with that same code, I've decided - for my own benefit - to use the former.

    Renzo Ciafardone wrote:

    you got yourself a waste of time well into the millisecond range if not more

    I very much doubt that. More importantly, even if it were true for one in a thousand or even one in ten (meaningful!) applications, don't design and write code under the assumption of the worst possible effect on performance, write under the assumption that you need to maintain and rewrite code often! If you have an application with an expected lifetime measured in weeks rather than months. If that application is extremely performance-critical. If there is no meaningful numerical processing involved that may cause performance problems. If there is no external database, internet connection, file IO or just any IO involved. If you're using a compiler with a c

    R Offline
    R Offline
    Renzo Ciafardone
    wrote on last edited by
    #72

    :confused: Wait a sec. When did i suggest to use a GOTO? I was saying that a RETURN is always a more readable and efficient alternative to a superfluous flag varible.:confused: I too would use a flag if the alternative were a GOTO, there is no YMMV on this subject. For me GOTO is only acceptable in assembler. On any other language If an entanglement of If-elses requires a GOTO to get out, I would rewrite the damn thing because that shit is akin to blasphemy. :wtf:

    S 1 Reply Last reply
    0
    • R Ravi Bhavnani

      ryanb31 wrote:

      I prefer the second method.

      You can't be serious! /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
      #73

      Quote:

      You can't be serious!

      I do like to joke around a lot, but yes, I can be serious at times.

      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 Renzo Ciafardone

        :confused: Wait a sec. When did i suggest to use a GOTO? I was saying that a RETURN is always a more readable and efficient alternative to a superfluous flag varible.:confused: I too would use a flag if the alternative were a GOTO, there is no YMMV on this subject. For me GOTO is only acceptable in assembler. On any other language If an entanglement of If-elses requires a GOTO to get out, I would rewrite the damn thing because that shit is akin to blasphemy. :wtf:

        S Offline
        S Offline
        Stefan_Lang
        wrote on last edited by
        #74

        I didn't say you did, I just listed a number of possible scenarios, some of them beyond those you were considering. You asserted that introducing a flag is unneccessary, and I disagree: if you need to clean up resources, you can not immediately return! In that case, what will you do: copy/paste the clean-up code at every location in your function where you need a premature return, use goto, or introduce a flag? For me, the answer is the latter.

        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
          Jonathan C Dickinson
          wrote on last edited by
          #75

          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 1 Reply Last reply
          0
          • S svella

            Flags and/or gotos are both reasonable approaches in languages that don't have a try...finally construct and I've written code using both approaches in many different languages. Since the OP was obviously C# I assumed that is what we were talking about, and in C# the try..finally (or the "using" construct, when applicable) is definitely the cleanest approach to making sure your resource cleanup happens, even when you don't think exceptions enter into the picture, though in my experience most cases where resource cleanup happens, exceptions at the .NET framework level are almost always a possibility. And no I wouldn't suggest try..finally for the original post because no resource cleanup is involved.

            S Offline
            S Offline
            Stefan_Lang
            wrote on last edited by
            #76

            Ok, lets forget language (old C) specific concerns: Assuming you have exceptions, yes, I agree that you can reasonably handle many cases of premature returns that way. However, my take on exceptions is that code running as expected shouldn't throw one! Finding an element with specific properties in a container does not warrant throwing an exception, whether or not you need clean-up. Look at the following code, ignoring language specific elements:

            // find first elementwithin tolerance of a given value
            // return element ID
            int find(Container c, double value, double tol)
            {
            int result = INVALID_ID; // some constant that is not a valid element ID
            do_some_intialization();
            for(size_t index = 0; index < c.size(); ++index)
            {
            if (element.distance(value) < tol)
            {
            result = element.ID();
            }
            }
            do_some_clean_up();
            return result;
            }

            There are various solutions to short-cut the loop once it finds a fitting element: 1. use some command that breaks out of the innermost loop (in C/C++ you can use break) 2. attach the check (result==INVALID_ID) to the loop header, so it quits once you assign a valid ID. (not sure how to do that with for_each in C#, but a standard for loop lets you add an arbitrary number of stop conditions easily) 3. Introduce a flag variable that indicates when you're done searching. As it would pretty much just store the current value of (result==INVALID_ID) in this case, you might as well go with solution 2 above 4. throwing an exception, catching it with try/finally to ensure proper cleaning up In this example, my suggestion of introducing a flag variable turns out to be unneccesary, as the result variable itself can be used to pretty much the same effect. In my experience, this is often the case, so the effort to use these kind of checks instead of premature returns or gotos is often rather low. Using an exception would certainly work, but it conflicts with my understanding of what an exception means. Also, if you do catch actual error cases with exceptions within that same code, you need to make sure to not catch the 'good-case-exceptions' as errors! If you have no problem with that, the more power to you. But I prefer not to go that route.

            S 1 Reply Last reply
            0
            • S Stefan_Lang

              Ok, lets forget language (old C) specific concerns: Assuming you have exceptions, yes, I agree that you can reasonably handle many cases of premature returns that way. However, my take on exceptions is that code running as expected shouldn't throw one! Finding an element with specific properties in a container does not warrant throwing an exception, whether or not you need clean-up. Look at the following code, ignoring language specific elements:

              // find first elementwithin tolerance of a given value
              // return element ID
              int find(Container c, double value, double tol)
              {
              int result = INVALID_ID; // some constant that is not a valid element ID
              do_some_intialization();
              for(size_t index = 0; index < c.size(); ++index)
              {
              if (element.distance(value) < tol)
              {
              result = element.ID();
              }
              }
              do_some_clean_up();
              return result;
              }

              There are various solutions to short-cut the loop once it finds a fitting element: 1. use some command that breaks out of the innermost loop (in C/C++ you can use break) 2. attach the check (result==INVALID_ID) to the loop header, so it quits once you assign a valid ID. (not sure how to do that with for_each in C#, but a standard for loop lets you add an arbitrary number of stop conditions easily) 3. Introduce a flag variable that indicates when you're done searching. As it would pretty much just store the current value of (result==INVALID_ID) in this case, you might as well go with solution 2 above 4. throwing an exception, catching it with try/finally to ensure proper cleaning up In this example, my suggestion of introducing a flag variable turns out to be unneccesary, as the result variable itself can be used to pretty much the same effect. In my experience, this is often the case, so the effort to use these kind of checks instead of premature returns or gotos is often rather low. Using an exception would certainly work, but it conflicts with my understanding of what an exception means. Also, if you do catch actual error cases with exceptions within that same code, you need to make sure to not catch the 'good-case-exceptions' as errors! If you have no problem with that, the more power to you. But I prefer not to go that route.

              S Offline
              S Offline
              svella
              wrote on last edited by
              #77

              I think you've completely misunderstood what I said. I am in no way advocating introducing new exceptions as a way of prematurely ending a loop. What I am advocating is using language constructs (try...finally or using(...)) specifically designed help ensure safe cleanup regardless of how you exit the function/procedure. It's simply cleaner and easier to get it right than using other methods, especially when there is the possibility of exceptions, which it turns out is almost always when using resources that need to be explicitly cleaned up in languages that provide those constructs.

              1 Reply Last reply
              0
              • Z ZurdoDev

                Quote:

                You can't be serious!

                I do like to joke around a lot, but yes, I can be serious at times.

                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
                #78

                So if the array contained a million strings, the first of which was "ABC", you'd check every single string even though you already know you have a match?  :confused: /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

                  So if the array contained a million strings, the first of which was "ABC", you'd check every single string even though you already know you have a match?  :confused: /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
                  #79

                  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 1 Reply Last reply
                  0
                  • 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
                                          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