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. Code Optimize

Code Optimize

Scheduled Pinned Locked Moved The Weird and The Wonderful
comcode-review
53 Posts 26 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.
  • R Offline
    R Offline
    Rajesh Anuhya
    wrote on last edited by
    #1

    Today I found this code, from DAL class

    public Boolean Execute_NoN_Query(string Sqlstring)
    {
    int ResultFlag = 0;
    ResultFlag = MSSqlHelper.SqlHelper.ExecuteNonQuery(SqlServerConnection.Cn, CommandType.Text, Sqlstring);

              if  (ResultFlag != 0)
                  return true;
              else
                  return false;
      }
    

    My Code is ....

    public Boolean Execute_NoN_Query(string Sqlstring)
    {
    return (0 != MSSqlHelper.SqlHelper.ExecuteNonQuery(SqlServerConnection.Cn, CommandType.Text, Sqlstring));
    }

    my Tip/Tricks[^] | "Rajesh-Puli" now "Rajesh-Anuhya"

    B P D K R 6 Replies Last reply
    0
    • R Rajesh Anuhya

      Today I found this code, from DAL class

      public Boolean Execute_NoN_Query(string Sqlstring)
      {
      int ResultFlag = 0;
      ResultFlag = MSSqlHelper.SqlHelper.ExecuteNonQuery(SqlServerConnection.Cn, CommandType.Text, Sqlstring);

                if  (ResultFlag != 0)
                    return true;
                else
                    return false;
        }
      

      My Code is ....

      public Boolean Execute_NoN_Query(string Sqlstring)
      {
      return (0 != MSSqlHelper.SqlHelper.ExecuteNonQuery(SqlServerConnection.Cn, CommandType.Text, Sqlstring));
      }

      my Tip/Tricks[^] | "Rajesh-Puli" now "Rajesh-Anuhya"

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      That

      if(something)
      return true;
      else return false;

      ... is far too prevalent. Its cousin,

      if(something)
      return a;
      else return b;

      ... is at least understandable as some people have an allergic reaction to even simple ternaries (I have no idea why, they are a perfectly valid part of the language and have been since C). Interesting to see someone else who likes to do

      if(0 != ...)

      ... as well.

      F V R 3 Replies Last reply
      0
      • R Rajesh Anuhya

        Today I found this code, from DAL class

        public Boolean Execute_NoN_Query(string Sqlstring)
        {
        int ResultFlag = 0;
        ResultFlag = MSSqlHelper.SqlHelper.ExecuteNonQuery(SqlServerConnection.Cn, CommandType.Text, Sqlstring);

                  if  (ResultFlag != 0)
                      return true;
                  else
                      return false;
          }
        

        My Code is ....

        public Boolean Execute_NoN_Query(string Sqlstring)
        {
        return (0 != MSSqlHelper.SqlHelper.ExecuteNonQuery(SqlServerConnection.Cn, CommandType.Text, Sqlstring));
        }

        my Tip/Tricks[^] | "Rajesh-Puli" now "Rajesh-Anuhya"

        P Offline
        P Offline
        Peter_in_2780
        wrote on last edited by
        #3

        There is a valid reason for using the ResultFlag form. It's called debugging. How do you find out what went wrong when the ExecuteNonQuery returns nonzero? Don't you think the value returned might give you a clue? Peter

        Software rusts. Simon Stephenson, ca 1994.

        M S 2 Replies Last reply
        0
        • P Peter_in_2780

          There is a valid reason for using the ResultFlag form. It's called debugging. How do you find out what went wrong when the ExecuteNonQuery returns nonzero? Don't you think the value returned might give you a clue? Peter

          Software rusts. Simon Stephenson, ca 1994.

          M Offline
          M Offline
          Mohibur Rashid
          wrote on last edited by
          #4

          Agreed with Peter, Beside this just reduce the code, but I dont see any performance improvement. also in some programming language comparison does not return only true and false but sometime it also return -1(VBA)

          F T C 3 Replies Last reply
          0
          • B BobJanova

            That

            if(something)
            return true;
            else return false;

            ... is far too prevalent. Its cousin,

            if(something)
            return a;
            else return b;

            ... is at least understandable as some people have an allergic reaction to even simple ternaries (I have no idea why, they are a perfectly valid part of the language and have been since C). Interesting to see someone else who likes to do

            if(0 != ...)

            ... as well.

            F Offline
            F Offline
            Florin Jurcovici 0
            wrote on last edited by
            #5

            Ternaries ... you're right, as long as the expressions in the ternaries are simple, they are readable. But they can be simple initially, and become monstrous as the code evolves. Which may be why many people avoid ternaries - the same as with braces around blocks consisting of a single statement. Although I don't buy either (no ternaries and braces around single statements) - you write the code as is fit initially, and reformat/refactor as needed when you change it. There's another horror format: boolean b; ... if (b == true) return x; else return y; A variant is b being a boolean function.

            B 1 Reply Last reply
            0
            • M Mohibur Rashid

              Agreed with Peter, Beside this just reduce the code, but I dont see any performance improvement. also in some programming language comparison does not return only true and false but sometime it also return -1(VBA)

              F Offline
              F Offline
              Florin Jurcovici 0
              wrote on last edited by
              #6

              Nope, not right. Any decent debugger/IDE has a facility for evaluating expressions (IIRC VBA has one too - something called the immediate window, I think). Depending on the language, your compiler might not optimize away the additional variable which you use. Besides, more compact code is always a bonus. And even in VBA, for this particular case, what you care about is != 0, which even VBA evaluates correctly, since -1 and 1 are both true, only 0 being false.

              Y 1 Reply Last reply
              0
              • F Florin Jurcovici 0

                Ternaries ... you're right, as long as the expressions in the ternaries are simple, they are readable. But they can be simple initially, and become monstrous as the code evolves. Which may be why many people avoid ternaries - the same as with braces around blocks consisting of a single statement. Although I don't buy either (no ternaries and braces around single statements) - you write the code as is fit initially, and reformat/refactor as needed when you change it. There's another horror format: boolean b; ... if (b == true) return x; else return y; A variant is b being a boolean function.

                B Offline
                B Offline
                BobJanova
                wrote on last edited by
                #7

                Florin Jurcovici wrote:

                Although I don't buy either (no ternaries and braces around single statements) - you write the code as is fit initially, and reformat/refactor as needed when you change it.

                Yes, exactly. And a simple

                return statement ? a : b

                ... is not too hard to read, for sure. Someone here is really passive-aggressive anti-ternary, judging by the downvote my other post got :~ Heh, that pattern is even worse.

                J C 2 Replies Last reply
                0
                • B BobJanova

                  That

                  if(something)
                  return true;
                  else return false;

                  ... is far too prevalent. Its cousin,

                  if(something)
                  return a;
                  else return b;

                  ... is at least understandable as some people have an allergic reaction to even simple ternaries (I have no idea why, they are a perfectly valid part of the language and have been since C). Interesting to see someone else who likes to do

                  if(0 != ...)

                  ... as well.

                  V Offline
                  V Offline
                  Vladimir Svyatski
                  wrote on last edited by
                  #8

                  BobJanova wrote:

                  Interesting to see someone else who likes to do

                  if(0 != ...)

                  ... as well.

                  This is called Yoda condition.

                  C 1 Reply Last reply
                  0
                  • R Rajesh Anuhya

                    Today I found this code, from DAL class

                    public Boolean Execute_NoN_Query(string Sqlstring)
                    {
                    int ResultFlag = 0;
                    ResultFlag = MSSqlHelper.SqlHelper.ExecuteNonQuery(SqlServerConnection.Cn, CommandType.Text, Sqlstring);

                              if  (ResultFlag != 0)
                                  return true;
                              else
                                  return false;
                      }
                    

                    My Code is ....

                    public Boolean Execute_NoN_Query(string Sqlstring)
                    {
                    return (0 != MSSqlHelper.SqlHelper.ExecuteNonQuery(SqlServerConnection.Cn, CommandType.Text, Sqlstring));
                    }

                    my Tip/Tricks[^] | "Rajesh-Puli" now "Rajesh-Anuhya"

                    D Offline
                    D Offline
                    Donkey Master
                    wrote on last edited by
                    #9

                    :thumbsdown:I disagree with the point the OP is trying to make. The original code is easier to read:confused: and debug :(( . The second code has me counting nested parenthesis, :doh: and commas, it takes longer to read and understand when you're not familiar with the code, and it's harder to spot a factual error. X| I reckon that when you're reading the same code over and over :^) , the shorter route works,:cool: but in a professional environment,:suss: readable code is better than clever code.:thumbsup: Except maybe when performance is critical, :) and your compiler is not awesome, ;P like in some embedded systems, or some video games. :|

                    "Computer Science is no more about computers than astronomy is about telescopes." - Edsger Dijkstra

                    R B 2 Replies Last reply
                    0
                    • M Mohibur Rashid

                      Agreed with Peter, Beside this just reduce the code, but I dont see any performance improvement. also in some programming language comparison does not return only true and false but sometime it also return -1(VBA)

                      T Offline
                      T Offline
                      the Kris
                      wrote on last edited by
                      #10

                      If examining the result while debugging is your main concern, you can still write

                      int ResultFlag = MSSqlHelper.SqlHelper.ExecuteNonQuery(SqlServerConnection.Cn, CommandType.Text, Sqlstring);

                      return ResultFlag != 0;

                      I've seen worse though...

                      const bool valTrue = true;
                      const bool valFalse = false;

                      bool doTheWork()
                      {
                      ...more impressive code...

                      if ( result == valTrue ) return true;
                      else return false;
                      }

                      That coder was preparing himself for the time that valTrue became false or something!?

                      L A 2 Replies Last reply
                      0
                      • D Donkey Master

                        :thumbsdown:I disagree with the point the OP is trying to make. The original code is easier to read:confused: and debug :(( . The second code has me counting nested parenthesis, :doh: and commas, it takes longer to read and understand when you're not familiar with the code, and it's harder to spot a factual error. X| I reckon that when you're reading the same code over and over :^) , the shorter route works,:cool: but in a professional environment,:suss: readable code is better than clever code.:thumbsup: Except maybe when performance is critical, :) and your compiler is not awesome, ;P like in some embedded systems, or some video games. :|

                        "Computer Science is no more about computers than astronomy is about telescopes." - Edsger Dijkstra

                        R Offline
                        R Offline
                        Rajesh Anuhya
                        wrote on last edited by
                        #11

                        Good, every one saying about the readability and Debugging, However this is a Small code, which is called by many classes. my point is why should i declare a extra variable "resultflag", where this method called 5000+ times in every 10 seconds.

                        my Tip/Tricks[^] | "Rajesh-Puli" now "Rajesh-Anuhya"

                        R M J J 4 Replies Last reply
                        0
                        • D Donkey Master

                          :thumbsdown:I disagree with the point the OP is trying to make. The original code is easier to read:confused: and debug :(( . The second code has me counting nested parenthesis, :doh: and commas, it takes longer to read and understand when you're not familiar with the code, and it's harder to spot a factual error. X| I reckon that when you're reading the same code over and over :^) , the shorter route works,:cool: but in a professional environment,:suss: readable code is better than clever code.:thumbsup: Except maybe when performance is critical, :) and your compiler is not awesome, ;P like in some embedded systems, or some video games. :|

                          "Computer Science is no more about computers than astronomy is about telescopes." - Edsger Dijkstra

                          B Offline
                          B Offline
                          BobJanova
                          wrote on last edited by
                          #12

                          One line is more readable than four when three of the four are wasted space. That's because you get three more lines of other code on your screen which help to show the class context (unless the whole class fits on one screen, but that's unusual). There's only one operation in that method which is complex enough to require reading, and putting pointless extra lines makes it less readable. And I really hope you were intentionally making a point about spurious material with all those emoticons and extraneous formatting, because it makes your post much harder to read.

                          R 1 Reply Last reply
                          0
                          • B BobJanova

                            One line is more readable than four when three of the four are wasted space. That's because you get three more lines of other code on your screen which help to show the class context (unless the whole class fits on one screen, but that's unusual). There's only one operation in that method which is complex enough to require reading, and putting pointless extra lines makes it less readable. And I really hope you were intentionally making a point about spurious material with all those emoticons and extraneous formatting, because it makes your post much harder to read.

                            R Offline
                            R Offline
                            Rajesh Anuhya
                            wrote on last edited by
                            #13

                            Quote:

                            And I really hope you were intentionally making a point about spurious material with all those emoticons and extraneous formatting, because it makes your post much harder to read.

                            punch :thumbsup: +5

                            my Tip/Tricks[^] | "Rajesh-Puli" now "Rajesh-Anuhya"

                            1 Reply Last reply
                            0
                            • P Peter_in_2780

                              There is a valid reason for using the ResultFlag form. It's called debugging. How do you find out what went wrong when the ExecuteNonQuery returns nonzero? Don't you think the value returned might give you a clue? Peter

                              Software rusts. Simon Stephenson, ca 1994.

                              S Offline
                              S Offline
                              Snorri Kristjansson
                              wrote on last edited by
                              #14

                              Agree. There is another VERY important reason why you should NOT change working production code unless you really must; Don't change tried and tested code! Ok. this code sample you show us is a bit clumsy but it works, right? So why change it? IMHO a good programmer must learnt NOT to change production code unless it's really really needed.

                              B J 2 Replies Last reply
                              0
                              • S Snorri Kristjansson

                                Agree. There is another VERY important reason why you should NOT change working production code unless you really must; Don't change tried and tested code! Ok. this code sample you show us is a bit clumsy but it works, right? So why change it? IMHO a good programmer must learnt NOT to change production code unless it's really really needed.

                                B Offline
                                B Offline
                                BobJanova
                                wrote on last edited by
                                #15

                                I definitely disagree with you there. Cleaning up code results in a (marginally, for any particular instance, but it builds up) nicer codebase to work in and that results in better productivity for everyone on the team. If it's 'tried and tested' then you can check that the tests still pass and therefore be sure you haven't broken anything with your cleanup.

                                S 1 Reply Last reply
                                0
                                • R Rajesh Anuhya

                                  Good, every one saying about the readability and Debugging, However this is a Small code, which is called by many classes. my point is why should i declare a extra variable "resultflag", where this method called 5000+ times in every 10 seconds.

                                  my Tip/Tricks[^] | "Rajesh-Puli" now "Rajesh-Anuhya"

                                  R Offline
                                  R Offline
                                  Rob Grainger
                                  wrote on last edited by
                                  #16

                                  I shouldn't worry about it, if you're accessing a data layer any saving from avoiding allocating 32-bits on the stack is negligible. Premature optimisation is the root of all evil. The only actual "improvement" you've made is that the code occupies less space on screen. All the "improvements" you claim will be performed by a decent optimising compiler on release code anyway. Design for readability/maintainability, then optimise as you have evidence it is worthwhile.

                                  1 Reply Last reply
                                  0
                                  • B BobJanova

                                    I definitely disagree with you there. Cleaning up code results in a (marginally, for any particular instance, but it builds up) nicer codebase to work in and that results in better productivity for everyone on the team. If it's 'tried and tested' then you can check that the tests still pass and therefore be sure you haven't broken anything with your cleanup.

                                    S Offline
                                    S Offline
                                    Snorri Kristjansson
                                    wrote on last edited by
                                    #17

                                    I agree - Cleaning up the codebase is very tempting, it's much nicer to work on "clean" code. But my point is this: Cleaning up production code just for the sake of making it look "nice" is very dangerous because (depending on code size of course) you WILL create new bugs in doing so.

                                    B H 2 Replies Last reply
                                    0
                                    • S Snorri Kristjansson

                                      I agree - Cleaning up the codebase is very tempting, it's much nicer to work on "clean" code. But my point is this: Cleaning up production code just for the sake of making it look "nice" is very dangerous because (depending on code size of course) you WILL create new bugs in doing so.

                                      B Offline
                                      B Offline
                                      BobJanova
                                      wrote on last edited by
                                      #18

                                      You won't if the code is properly tested, or if you can demonstrate equivalence for all inputs (often not as hard as it sounds).

                                      1 Reply Last reply
                                      0
                                      • S Snorri Kristjansson

                                        I agree - Cleaning up the codebase is very tempting, it's much nicer to work on "clean" code. But my point is this: Cleaning up production code just for the sake of making it look "nice" is very dangerous because (depending on code size of course) you WILL create new bugs in doing so.

                                        H Offline
                                        H Offline
                                        Harley L Pebley
                                        wrote on last edited by
                                        #19

                                        Snorri wrote:

                                        But my point is this: Cleaning up production code just for the sake of making it look "nice" is very dangerous because you SOMETIMES will create new bugs in doing so.

                                        FTFY. :-) And sometimes cleaning up code will fix latent bugs that no one has run into yet, or possibly they've hit them and just haven't reported them, or possibly they've hit them and thought that was normal behavior. Creating new bugs will be mitigated with unit tests; which of course one always has before refactoring.

                                        S 1 Reply Last reply
                                        0
                                        • R Rajesh Anuhya

                                          Good, every one saying about the readability and Debugging, However this is a Small code, which is called by many classes. my point is why should i declare a extra variable "resultflag", where this method called 5000+ times in every 10 seconds.

                                          my Tip/Tricks[^] | "Rajesh-Puli" now "Rajesh-Anuhya"

                                          M Offline
                                          M Offline
                                          mrchief_2000
                                          wrote on last edited by
                                          #20

                                          Declaring an extra variable doesn't affect your performance at all. Compilers can (and will) easily do the code reduction you did (note what you did is not an optimization). On the other, having that extra variable does aid in readability and debugging. The only rewrite I would is (if I absolutely have to change something for no reason other than style):

                                          return ResultFlag != 0;

                                          K 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