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. if...AND...else! [modified]

if...AND...else! [modified]

Scheduled Pinned Locked Moved The Weird and The Wonderful
question
14 Posts 12 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.
  • C Offline
    C Offline
    che3358
    wrote on last edited by
    #1

    public bool CheckValue(int first, int second) { if(first > second) return true; else return false; } Folks like this way because it is better than the following way? return first > second;

    modified on Friday, May 30, 2008 1:36 PM

    C J P S O 10 Replies Last reply
    0
    • C che3358

      public bool CheckValue(int first, int second) { if(first > second) return true; else return false; } Folks like this way because it is better than the following way? return first > second;

      modified on Friday, May 30, 2008 1:36 PM

      C Offline
      C Offline
      Chris Meech
      wrote on last edited by
      #2

      CheckValue. :wtf: :wtf: I'd also recommend

      public bool IsFirstGreaterThanSecond(int nFirst, int nSecond)
      {
      ..

      :)

      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] Donate to help Conquer Cancer[^]

      1 Reply Last reply
      0
      • C che3358

        public bool CheckValue(int first, int second) { if(first > second) return true; else return false; } Folks like this way because it is better than the following way? return first > second;

        modified on Friday, May 30, 2008 1:36 PM

        J Offline
        J Offline
        Jeremy Tierman
        wrote on last edited by
        #3

        That is legit. The VB to VB.Net migration tool and the VB.Net to C# translation tool did it, so it must be so. :laugh:

        1 Reply Last reply
        0
        • C che3358

          public bool CheckValue(int first, int second) { if(first > second) return true; else return false; } Folks like this way because it is better than the following way? return first > second;

          modified on Friday, May 30, 2008 1:36 PM

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

          che3358 wrote:

          is better than

          Certainly not better, but even MS presenters will do that because it matches what they're saying*. Unfortunately the newbies in the crowd get the idea that it is the proper way to write it. It's part of why I voted "Microsoft" to this week's poll. * That's an assumption, but I saw it several times during the VS 2008 {launch} event, I cringed every time.

          1 Reply Last reply
          0
          • C che3358

            public bool CheckValue(int first, int second) { if(first > second) return true; else return false; } Folks like this way because it is better than the following way? return first > second;

            modified on Friday, May 30, 2008 1:36 PM

            S Offline
            S Offline
            supercat9
            wrote on last edited by
            #5

            che3358 wrote:

            Folks like this way because it is better than the following way?

            It's a bit easier to set a breakpoint on, is it not? Further, such code may appear in cases where (1) it had at some point been necessary to do more on the 'true' or 'false' case than is now necessary, or (2) it will likely in future be necessary to do more on the 'true' or 'false' cases than is now necessary.

            1 Reply Last reply
            0
            • C che3358

              public bool CheckValue(int first, int second) { if(first > second) return true; else return false; } Folks like this way because it is better than the following way? return first > second;

              modified on Friday, May 30, 2008 1:36 PM

              O Offline
              O Offline
              Oshtri Deka
              wrote on last edited by
              #6

              As someone said before me, it's easier to debug, but I think this is just lack of experience in its best ;). I had my share of awkward code (true horror when compared to this) when I was junior programmer :D.

              1 Reply Last reply
              0
              • C che3358

                public bool CheckValue(int first, int second) { if(first > second) return true; else return false; } Folks like this way because it is better than the following way? return first > second;

                modified on Friday, May 30, 2008 1:36 PM

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

                It may be a bit more verbose, but it's also easily understandable. The generated code may perhaps be less then optimal, but it would be a good candidate for examining the disassemblies to see if any optimisation occurs there. Even without optimisation this should have little impact unless this method is called very often. So for me it comes down to a readability versus efficiency choice and obviously the tastes are quite different there.

                A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                W 1 Reply Last reply
                0
                • C che3358

                  public bool CheckValue(int first, int second) { if(first > second) return true; else return false; } Folks like this way because it is better than the following way? return first > second;

                  modified on Friday, May 30, 2008 1:36 PM

                  R Offline
                  R Offline
                  Rage
                  wrote on last edited by
                  #8

                  This ensures the real type to be returned (bool)

                  if (first > second)
                  return true;
                  else
                  return false;

                  while this returns the result type of >, which may not be bool (in C for instance, there is no such thing as bool, and if you create your own bool type, you cannot force > to return "your" bool, hence a necessary cast to avoid a level 4 warning for casting an int on an unknown type):

                  return first > second;

                  Plus returning the result of an operation is forbidden by some standards in embedded code, for instance (MISRA).

                  ~RaGE();

                  I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
                  Do not feed the troll ! - Common proverb

                  1 Reply Last reply
                  0
                  • C che3358

                    public bool CheckValue(int first, int second) { if(first > second) return true; else return false; } Folks like this way because it is better than the following way? return first > second;

                    modified on Friday, May 30, 2008 1:36 PM

                    K Offline
                    K Offline
                    KarstenK
                    wrote on last edited by
                    #9

                    Writing such a function is non-sense. X| Instead: CheckValue(first, second) write: (first > second)

                    Greetings from Germany

                    1 Reply Last reply
                    0
                    • C che3358

                      public bool CheckValue(int first, int second) { if(first > second) return true; else return false; } Folks like this way because it is better than the following way? return first > second;

                      modified on Friday, May 30, 2008 1:36 PM

                      Y Offline
                      Y Offline
                      Yusuf
                      wrote on last edited by
                      #10

                      che3358 wrote:

                      Folks like this way because it is better

                      why is it better? :|

                      Yusuf

                      1 Reply Last reply
                      0
                      • L Lost User

                        It may be a bit more verbose, but it's also easily understandable. The generated code may perhaps be less then optimal, but it would be a good candidate for examining the disassemblies to see if any optimisation occurs there. Even without optimisation this should have little impact unless this method is called very often. So for me it comes down to a readability versus efficiency choice and obviously the tastes are quite different there.

                        A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                        W Offline
                        W Offline
                        Ware Work
                        wrote on last edited by
                        #11

                        love the sig!

                        WarePhreak Programmers are tools to convert caffiene to code.

                        L 1 Reply Last reply
                        0
                        • W Ware Work

                          love the sig!

                          WarePhreak Programmers are tools to convert caffiene to code.

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

                          Thanks. I just wonder where all this will end. Today there are many more users, but they have even less knowledge than many users in 'the good old days'. But that does not keep them from thinking that everything can be done with the wave of a hand. Perhaps some already think that you need some kind of mystic powers to get those machines going :)

                          A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                          W 1 Reply Last reply
                          0
                          • L Lost User

                            Thanks. I just wonder where all this will end. Today there are many more users, but they have even less knowledge than many users in 'the good old days'. But that does not keep them from thinking that everything can be done with the wave of a hand. Perhaps some already think that you need some kind of mystic powers to get those machines going :)

                            A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                            W Offline
                            W Offline
                            Ware Work
                            wrote on last edited by
                            #13

                            What I find even worse are some of those same users who have some "tool" (i.e. Access) that they believe makes them a programmer. And then you look at what they've done and it is magic it even works.

                            WarePhreak Programmers are tools to convert caffiene to code.

                            1 Reply Last reply
                            0
                            • C che3358

                              public bool CheckValue(int first, int second) { if(first > second) return true; else return false; } Folks like this way because it is better than the following way? return first > second;

                              modified on Friday, May 30, 2008 1:36 PM

                              R Offline
                              R Offline
                              realJSOP
                              wrote on last edited by
                              #14

                              Actually, you should scope it with parens:

                              return (first > second);

                              On the other hand, why write a function that does that when you could eliminate the overhead and stack usage by doing the comparison in the calling function?

                              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                              -----
                              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                              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