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. Classic

Classic

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharp
30 Posts 17 Posters 47 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D dighn

    Personally I don't find that to be horrible. It's completely correct and only slightly more redundant. It really makes no difference in practice. And no I'm not trying to defend my usage of it :) I don't lie :) :)

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

    I agree it's not horrible and that it can add to readability but it is still one comparison too many. It might get more obvious in different situation. Like (some stupid example): bool IsNumberEven(int number) {  return ((number % 2 == 0) ? true : false); } int Main() { int someNumber = 2^100; for (int i = 0; i < 100; i++) {  while (IsNumberEven(someNumber) == true)  {   someNumber /= 2;  } } } I think it is similar as using String object when one should use StringBuilder. It's OK for small number of iterations or single comparison, but can be time-consuming for large number of iterations.

    D O 2 Replies Last reply
    0
    • L Lost User

      I agree it's not horrible and that it can add to readability but it is still one comparison too many. It might get more obvious in different situation. Like (some stupid example): bool IsNumberEven(int number) {  return ((number % 2 == 0) ? true : false); } int Main() { int someNumber = 2^100; for (int i = 0; i < 100; i++) {  while (IsNumberEven(someNumber) == true)  {   someNumber /= 2;  } } } I think it is similar as using String object when one should use StringBuilder. It's OK for small number of iterations or single comparison, but can be time-consuming for large number of iterations.

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

      I've always assumed that if(aBool) is simply a short-hand for if(aBool == true), and they should be completely equivalent to the compiler. If they are not for some reason, it would be a trivially easy optimization for the compiler to make.

      L 2 Replies Last reply
      0
      • D dighn

        I've always assumed that if(aBool) is simply a short-hand for if(aBool == true), and they should be completely equivalent to the compiler. If they are not for some reason, it would be a trivially easy optimization for the compiler to make.

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

        I don't know if that is true. Maybe someone with deeper knowledge than me could answer that. If it is true though, I should 'recompile' my coding practices ;)

        1 Reply Last reply
        0
        • D dighn

          I've always assumed that if(aBool) is simply a short-hand for if(aBool == true), and they should be completely equivalent to the compiler. If they are not for some reason, it would be a trivially easy optimization for the compiler to make.

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

          I ran some tests for speed for both cases and the results show that your assumption is correct. It seems compiler is smarter than I thought ;) So this case is closed and I learned something new to me and that is always a good thing :)

          W 1 Reply Last reply
          0
          • L Lost User

            I'm sure you have all come across something like this. At least I see it way too often. So here it is in C# style: bool someBoolValue; if (someBoolValue == true) { // do something } else { // do something else }

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #10

            I'm going to be pedantic here and state that the do something condition will never be executed in this example. BTW - to answer your question, the compiler recognises that if (a == true) is functionally equivalent to if (a) because it is clever enough to know that all parts of the if condition must be boolean conditions.

            Deja View - the feeling that you've seen this post before.

            1 Reply Last reply
            0
            • L Lost User

              I'm sure you have all come across something like this. At least I see it way too often. So here it is in C# style: bool someBoolValue; if (someBoolValue == true) { // do something } else { // do something else }

              L Offline
              L Offline
              Luis Alonso Ramos
              wrote on last edited by
              #11

              In some languages TRUE is actually an int, mostly 1. But BASIC languages (VB for one), define TRUE as NOT FALSE (where not is the 2's complement), so TRUE is actually -1. If you are interacting with different languages, you have to take this into account -- for example, a Windows API return value in Visual Basic.

              Luis Alonso Ramos Intelectix Chihuahua, Mexico

              My Blog!

              V 1 Reply Last reply
              0
              • D dighn

                Personally I don't find that to be horrible. It's completely correct and only slightly more redundant. It really makes no difference in practice. And no I'm not trying to defend my usage of it :) I don't lie :) :)

                P Offline
                P Offline
                peterchen
                wrote on last edited by
                #12

                It's ok e.g. in C#, but in C/C++ this habit can lead to nasty bugs. If you work with multiple libraries, you probably have to deal with bool, BOOL and VARIANT_BOOL. While bool is fine (unless abused in the assignment), BOOL is usually used as "any nonnegative value is true", and the respective TRUE constant is defined sometimes as 1, sometimes a (!FALSE) which would expand to -1. VARIANT_BOOL explicitely defines -1 as VARIANT_TRUE. I know that some people prefer if (someridiculouslylongexpression==false) over if (!someridiculouslylongexpression) since the not can get lost quickly for the quick reader. However, I discourage this habit, as it may fail with the "fak" boolean types.


                We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                My first real C# project | Linkify!|FoldWithUs! | sighist

                1 Reply Last reply
                0
                • D dighn

                  Personally I don't find that to be horrible. It's completely correct and only slightly more redundant. It really makes no difference in practice. And no I'm not trying to defend my usage of it :) I don't lie :) :)

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

                  dighn wrote:

                  slightly more redundant

                  :confused: It is a way bit explicit right? Do you mean to say it affects any of the coding guidelines? C# actually advocates and prefers explicit typecasting too. And similarly an explicit mention should be a welcome though a bit strange. What do you way? :)

                  Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                  1 Reply Last reply
                  0
                  • L Luis Alonso Ramos

                    In some languages TRUE is actually an int, mostly 1. But BASIC languages (VB for one), define TRUE as NOT FALSE (where not is the 2's complement), so TRUE is actually -1. If you are interacting with different languages, you have to take this into account -- for example, a Windows API return value in Visual Basic.

                    Luis Alonso Ramos Intelectix Chihuahua, Mexico

                    My Blog!

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

                    Not with C# compiler right? The compiler 'barks' and strongly recommends an explicit casting. Isn't it?

                    Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                    L 1 Reply Last reply
                    0
                    • V Vasudevan Deepak Kumar

                      Not with C# compiler right? The compiler 'barks' and strongly recommends an explicit casting. Isn't it?

                      Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                      L Offline
                      L Offline
                      Luis Alonso Ramos
                      wrote on last edited by
                      #15

                      Yep, in C# you can't compare a bool to 1 or an int to true without explicit casting.

                      Luis Alonso Ramos Intelectix Chihuahua, Mexico

                      My Blog!

                      1 Reply Last reply
                      0
                      • L Lost User

                        I ran some tests for speed for both cases and the results show that your assumption is correct. It seems compiler is smarter than I thought ;) So this case is closed and I learned something new to me and that is always a good thing :)

                        W Offline
                        W Offline
                        wk633
                        wrote on last edited by
                        #16

                        You ran a test for speed??? I put it in an app:

                            bool IsNumberEven(int number)
                            {
                                return ((number % 2 == 0) ? true : false);
                            }
                            private void Form1\_Load(object sender, EventArgs e)
                            {
                        
                                if (IsNumberEven(2))
                                {
                                }
                        
                                if (IsNumberEven(2) == true) { }
                            }
                        

                        checked with ildasm, and they both turn into exactly the same IL code.

                        // Code size 34 (0x22)
                        .maxstack 2
                        .locals init ([0] bool CS$4$0000)
                        IL_0000: nop
                        IL_0001: ldarg.0
                        IL_0002: ldc.i4.2
                        IL_0003: call instance bool csharpWindowsApp.Form1::IsNumberEven(int32)
                        IL_0008: ldc.i4.0
                        IL_0009: ceq
                        IL_000b: stloc.0
                        IL_000c: ldloc.0
                        IL_000d: brtrue.s IL_0011
                        IL_000f: nop
                        IL_0010: nop
                        IL_0011: ldarg.0
                        IL_0012: ldc.i4.2
                        IL_0013: call instance bool csharpWindowsApp.Form1::IsNumberEven(int32)
                        IL_0018: ldc.i4.0
                        IL_0019: ceq
                        IL_001b: stloc.0
                        IL_001c: ldloc.0
                        IL_001d: brtrue.s IL_0021
                        IL_001f: nop
                        IL_0020: nop
                        IL_0021: ret

                        1 Reply Last reply
                        0
                        • D dighn

                          Personally I don't find that to be horrible. It's completely correct and only slightly more redundant. It really makes no difference in practice. And no I'm not trying to defend my usage of it :) I don't lie :) :)

                          J Offline
                          J Offline
                          John R Shaw
                          wrote on last edited by
                          #17

                          Actually I do not know why that was even posted, there was absolutely nothing wrong with that code. Now if there was something in the “do something…” sections, that showed a problem – then it might have made since as a problem – otherwise it is great and legitimate code. Tinko101 – can just ignore my comment, because I have only been writing code for about 19 years or so, but I agree with you. ;)

                          INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                          T 1 Reply Last reply
                          0
                          • L Lost User

                            I agree it's not horrible and that it can add to readability but it is still one comparison too many. It might get more obvious in different situation. Like (some stupid example): bool IsNumberEven(int number) {  return ((number % 2 == 0) ? true : false); } int Main() { int someNumber = 2^100; for (int i = 0; i < 100; i++) {  while (IsNumberEven(someNumber) == true)  {   someNumber /= 2;  } } } I think it is similar as using String object when one should use StringBuilder. It's OK for small number of iterations or single comparison, but can be time-consuming for large number of iterations.

                            O Offline
                            O Offline
                            OR0N
                            wrote on last edited by
                            #18

                            Testing if the number is even with modulus is the real horror .. :P

                            L T 2 Replies Last reply
                            0
                            • O OR0N

                              Testing if the number is even with modulus is the real horror .. :P

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

                              Actually the real horror would be: bool IsEven(int someNumber) {  if (someNumber == 2 || someNumber == 4 || someNumber == 6 || // ok, you get the point)  {   return true;  }  else  {   return false;  } } :)

                              O 1 Reply Last reply
                              0
                              • L Lost User

                                Actually the real horror would be: bool IsEven(int someNumber) {  if (someNumber == 2 || someNumber == 4 || someNumber == 6 || // ok, you get the point)  {   return true;  }  else  {   return false;  } } :)

                                O Offline
                                O Offline
                                OR0N
                                wrote on last edited by
                                #20

                                A rumour has it that it always can be worse :)

                                1 Reply Last reply
                                0
                                • J John R Shaw

                                  Actually I do not know why that was even posted, there was absolutely nothing wrong with that code. Now if there was something in the “do something…” sections, that showed a problem – then it might have made since as a problem – otherwise it is great and legitimate code. Tinko101 – can just ignore my comment, because I have only been writing code for about 19 years or so, but I agree with you. ;)

                                  INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                                  T Offline
                                  T Offline
                                  TClarke
                                  wrote on last edited by
                                  #21

                                  The only thing I can see wrong is that the boolean value isn't initialized. Now that is ban practice as in C/C++ values only get a default value in Debug mode. In Release mode it could be anything

                                  T 1 Reply Last reply
                                  0
                                  • T TClarke

                                    The only thing I can see wrong is that the boolean value isn't initialized. Now that is ban practice as in C/C++ values only get a default value in Debug mode. In Release mode it could be anything

                                    T Offline
                                    T Offline
                                    Tom Clement
                                    wrote on last edited by
                                    #22

                                    I assumed the posted code was just missing an elipsis. Presumably there was something to set the Boolean value.

                                    Tom Clement Serena Software, Inc. www.serena.com articles[^]

                                    T 1 Reply Last reply
                                    0
                                    • T Tom Clement

                                      I assumed the posted code was just missing an elipsis. Presumably there was something to set the Boolean value.

                                      Tom Clement Serena Software, Inc. www.serena.com articles[^]

                                      T Offline
                                      T Offline
                                      TClarke
                                      wrote on last edited by
                                      #23

                                      Tell the compiler that ;)

                                      1 Reply Last reply
                                      0
                                      • D dighn

                                        Personally I don't find that to be horrible. It's completely correct and only slightly more redundant. It really makes no difference in practice. And no I'm not trying to defend my usage of it :) I don't lie :) :)

                                        K Offline
                                        K Offline
                                        kenpanda
                                        wrote on last edited by
                                        #24

                                        mmm...not sure !:mad: I already see worse ! You write a first test if (boolValue == true) during debugging, it appaers that you have to change the test if (boolValue != true) after a second debug you wish to change it again if (boolValue != false) As summary, do you think that this code is really readable, even the performance is the same... : **if (boolValue != !false)**:laugh:

                                        1 Reply Last reply
                                        0
                                        • L Lost User

                                          I'm sure you have all come across something like this. At least I see it way too often. So here it is in C# style: bool someBoolValue; if (someBoolValue == true) { // do something } else { // do something else }

                                          D Offline
                                          D Offline
                                          dean wilde
                                          wrote on last edited by
                                          #25

                                          I dont think theres anything wrong with that, the compiler is going to optimize it anyways, the reason I like it is because is simpler to read than the alternative and simpler is always better in code. The only argument you could make that one shouldnt do that is if you mistype and put if(someBoolValue=true) and do an assignment instead of a comparision. The workaround to that is to either put constants on the left hand side if(true==someBoolValue), which you should already be doing so the compiler catches other similar mistakes (like null checks), or set the compiler warning level to 4, which I would also recommend you do to let the compiler detect other bugs.

                                          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