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 one is better [modified]

Which one is better [modified]

Scheduled Pinned Locked Moved The Weird and The Wonderful
11 Posts 7 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.
  • K Khaniya

    //Updated Hello everyone I just want to know cost of Assignment statement Isok = false; and if condition if(Isok == true) Isok=false; Thanks First One

    bool IsOk=false;
    while(i < 1000)
    {
    ………….
    …………//IsOk may change here
    ………….
    if(i< 100)
    {
    IsOk=false;
    }
    i++;
    }

    Second

    bool IsOk=false;
    while(i < 1000)
    {
    ………….
    ………… //IsOk may change here
    ………….
    if(IsOk && i< 100)
    {
    IsOk=false;
    }
    i++;
    }

    Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please

    modified on Monday, September 27, 2010 4:58 AM

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

    What language is that?

    1 Reply Last reply
    0
    • K Khaniya

      //Updated Hello everyone I just want to know cost of Assignment statement Isok = false; and if condition if(Isok == true) Isok=false; Thanks First One

      bool IsOk=false;
      while(i < 1000)
      {
      ………….
      …………//IsOk may change here
      ………….
      if(i< 100)
      {
      IsOk=false;
      }
      i++;
      }

      Second

      bool IsOk=false;
      while(i < 1000)
      {
      ………….
      ………… //IsOk may change here
      ………….
      if(IsOk && i< 100)
      {
      IsOk=false;
      }
      i++;
      }

      Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please

      modified on Monday, September 27, 2010 4:58 AM

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #3

      I don't see why you would want/need to check it is True just to set it to false. So i would go with the first option.

      Dave Find Me On: Web|Facebook|Twitter|LinkedIn CPRepWatcher now available as Packaged Chrome Extension, visit my articles for link.

      P 1 Reply Last reply
      0
      • D DaveAuld

        I don't see why you would want/need to check it is True just to set it to false. So i would go with the first option.

        Dave Find Me On: Web|Facebook|Twitter|LinkedIn CPRepWatcher now available as Packaged Chrome Extension, visit my articles for link.

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

        Maybe it involves a costly network or database transaction?

        K 1 Reply Last reply
        0
        • K Khaniya

          //Updated Hello everyone I just want to know cost of Assignment statement Isok = false; and if condition if(Isok == true) Isok=false; Thanks First One

          bool IsOk=false;
          while(i < 1000)
          {
          ………….
          …………//IsOk may change here
          ………….
          if(i< 100)
          {
          IsOk=false;
          }
          i++;
          }

          Second

          bool IsOk=false;
          while(i < 1000)
          {
          ………….
          ………… //IsOk may change here
          ………….
          if(IsOk && i< 100)
          {
          IsOk=false;
          }
          i++;
          }

          Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please

          modified on Monday, September 27, 2010 4:58 AM

          R Offline
          R Offline
          Richard A Dalton
          wrote on last edited by
          #5

          They both suck. Isok seems to be handling two conditions. The one hidden in the comments, that could change it. And the less than 100. That's confusing. In any case You don't need an if. Isok = I < 100 Would work. Sorry for caps and formatting, I'm on a phone. Rd

          1 Reply Last reply
          0
          • P PIEBALDconsult

            Maybe it involves a costly network or database transaction?

            K Offline
            K Offline
            Khaniya
            wrote on last edited by
            #6

            yes you got it so what will you prefere?

            Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please

            D 1 Reply Last reply
            0
            • K Khaniya

              yes you got it so what will you prefere?

              Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please

              D Offline
              D Offline
              DaveAuld
              wrote on last edited by
              #7

              Even if it does involve an expensive operation, with the code structure we can see, i still cannot see how it makes a difference. If the code before the 'if' checks the isOk to perform the operation then maybe, but if isOK is already true, and you need to set it back to false if i<100, then maybe the whole way the operation is being done need to be reviewed. If your logic requires for the the operation to continue until i > 100, then it doesn't matter what happens with isOK, or if it is true already. just set it back to false and get on with what you need to do. I am maybe not explaining very well what i'm trying to say, but i hope you catch my drift.

              Dave Find Me On: Web|Facebook|Twitter|LinkedIn CPRepWatcher now available as Packaged Chrome Extension, visit my articles for link.

              1 Reply Last reply
              0
              • K Khaniya

                //Updated Hello everyone I just want to know cost of Assignment statement Isok = false; and if condition if(Isok == true) Isok=false; Thanks First One

                bool IsOk=false;
                while(i < 1000)
                {
                ………….
                …………//IsOk may change here
                ………….
                if(i< 100)
                {
                IsOk=false;
                }
                i++;
                }

                Second

                bool IsOk=false;
                while(i < 1000)
                {
                ………….
                ………… //IsOk may change here
                ………….
                if(IsOk && i< 100)
                {
                IsOk=false;
                }
                i++;
                }

                Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please

                modified on Monday, September 27, 2010 4:58 AM

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

                At the moment I'm more concerened with the meaning of while(int i=0). Will it compile? Will it execute the loop?

                E 1 Reply Last reply
                0
                • P PIEBALDconsult

                  At the moment I'm more concerened with the meaning of while(int i=0). Will it compile? Will it execute the loop?

                  E Offline
                  E Offline
                  Electron Shepherd
                  wrote on last edited by
                  #9

                  PIEBALDconsult wrote:

                  Will it compile? Will it execute the loop?

                  Yes. No.

                  Server and Network Monitoring

                  1 Reply Last reply
                  0
                  • K Khaniya

                    //Updated Hello everyone I just want to know cost of Assignment statement Isok = false; and if condition if(Isok == true) Isok=false; Thanks First One

                    bool IsOk=false;
                    while(i < 1000)
                    {
                    ………….
                    …………//IsOk may change here
                    ………….
                    if(i< 100)
                    {
                    IsOk=false;
                    }
                    i++;
                    }

                    Second

                    bool IsOk=false;
                    while(i < 1000)
                    {
                    ………….
                    ………… //IsOk may change here
                    ………….
                    if(IsOk && i< 100)
                    {
                    IsOk=false;
                    }
                    i++;
                    }

                    Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please

                    modified on Monday, September 27, 2010 4:58 AM

                    A Offline
                    A Offline
                    AspDotNetDev
                    wrote on last edited by
                    #10

                    At first, I thought the coding horror was the "while(int i = 0)" (or whatever it was), but I didn't understand why you posted two similar versions. Now that you changed it, I don't understand what the coding horror is at all.

                    [Forum Guidelines]

                    E 1 Reply Last reply
                    0
                    • A AspDotNetDev

                      At first, I thought the coding horror was the "while(int i = 0)" (or whatever it was), but I didn't understand why you posted two similar versions. Now that you changed it, I don't understand what the coding horror is at all.

                      [Forum Guidelines]

                      E Offline
                      E Offline
                      elitig
                      wrote on last edited by
                      #11

                      Maybe the coding horror is premature optimisation? Unless the programmer has determined that this is a bottleneck, the better one is the one that expresses the logic most clearly (the one without the extra test, for my money), not the one with the lowest "cost". And if the programmer has determined that this is a bottleneck, they can use the same tools to find out which is faster.

                      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