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. The Lounge
  3. Which do you prefer? A programming question!

Which do you prefer? A programming question!

Scheduled Pinned Locked Moved The Lounge
questioncom
49 Posts 41 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.
  • M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #1

    if (foo)
    {
    DoSomething();
    }

    or:

    MaybeDoSomething(foo);
    ...
    MaybeDoSomething(bool foo)
    {
    if (foo)
    {
    // do the something.
    }
    }

    eh?

    Latest Articles:
    Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

    P R S M D 35 Replies Last reply
    0
    • M Marc Clifton

      if (foo)
      {
      DoSomething();
      }

      or:

      MaybeDoSomething(foo);
      ...
      MaybeDoSomething(bool foo)
      {
      if (foo)
      {
      // do the something.
      }
      }

      eh?

      Latest Articles:
      Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

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

      Depends on context. Can you not avoid the if entirely? :-D Is that called in a tight loop? As presented, I'd stick with the first example.

      1 Reply Last reply
      0
      • M Marc Clifton

        if (foo)
        {
        DoSomething();
        }

        or:

        MaybeDoSomething(foo);
        ...
        MaybeDoSomething(bool foo)
        {
        if (foo)
        {
        // do the something.
        }
        }

        eh?

        Latest Articles:
        Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

        R Offline
        R Offline
        raddevus
        wrote on last edited by
        #3

        Well, maybe this is a trick question, but ... I read and understood the first one immediately. About as quickly as I read it. The second one...I'm still not exactly sure about it. I guess it's all a state of mind. If you like objective, try #1. If you like subjective, try #2. :rolleyes:

        1 Reply Last reply
        0
        • M Marc Clifton

          if (foo)
          {
          DoSomething();
          }

          or:

          MaybeDoSomething(foo);
          ...
          MaybeDoSomething(bool foo)
          {
          if (foo)
          {
          // do the something.
          }
          }

          eh?

          Latest Articles:
          Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

          S Offline
          S Offline
          Slacker007
          wrote on last edited by
          #4

          second one. reduces the amount of potential if statements to just one. first one is easier to understand and read, I admit.

          1 Reply Last reply
          0
          • M Marc Clifton

            if (foo)
            {
            DoSomething();
            }

            or:

            MaybeDoSomething(foo);
            ...
            MaybeDoSomething(bool foo)
            {
            if (foo)
            {
            // do the something.
            }
            }

            eh?

            Latest Articles:
            Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

            M Offline
            M Offline
            MarkTJohnson
            wrote on last edited by
            #5

            The first

            I’ve given up trying to be calm. However, I am open to feeling slightly less agitated.

            1 Reply Last reply
            0
            • M Marc Clifton

              if (foo)
              {
              DoSomething();
              }

              or:

              MaybeDoSomething(foo);
              ...
              MaybeDoSomething(bool foo)
              {
              if (foo)
              {
              // do the something.
              }
              }

              eh?

              Latest Articles:
              Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

              D Offline
              D Offline
              Daniel Pfeffer
              wrote on last edited by
              #6

              I would normally use (1), but if there were many such usages, I might consider (2). The advantage of (2) is that if it turns out that the conditional must be modified, it only need be done in one place:

              Marc Clifton wrote:

              Copy Code

              MaybeDoSomething(foo);
              ...
              void MaybeDoSomething(bool foo)
              {
              if (foo)
              {
              // do the something, and do something else.
              }
              }

              Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

              1 Reply Last reply
              0
              • M Marc Clifton

                if (foo)
                {
                DoSomething();
                }

                or:

                MaybeDoSomething(foo);
                ...
                MaybeDoSomething(bool foo)
                {
                if (foo)
                {
                // do the something.
                }
                }

                eh?

                Latest Articles:
                Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                D Offline
                D Offline
                David ONeil
                wrote on last edited by
                #7

                foo ? DoSomething() : ; :-D Simpler is better!

                The Science of King David's Court | Object Oriented Programming with C++

                D 1 Reply Last reply
                0
                • M Marc Clifton

                  if (foo)
                  {
                  DoSomething();
                  }

                  or:

                  MaybeDoSomething(foo);
                  ...
                  MaybeDoSomething(bool foo)
                  {
                  if (foo)
                  {
                  // do the something.
                  }
                  }

                  eh?

                  Latest Articles:
                  Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

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

                  Not a fan of the second form. If the condition was something else, sure. But otherwise I say: the shorter a boolean lives, the better. Ideally they don't even become "reified": just an ephemeral condition that is used immediately when created, never stored in a variable. Boolean variables are a plague.

                  1 Reply Last reply
                  0
                  • M Marc Clifton

                    if (foo)
                    {
                    DoSomething();
                    }

                    or:

                    MaybeDoSomething(foo);
                    ...
                    MaybeDoSomething(bool foo)
                    {
                    if (foo)
                    {
                    // do the something.
                    }
                    }

                    eh?

                    Latest Articles:
                    Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                    M Offline
                    M Offline
                    Mike Hankey
                    wrote on last edited by
                    #9

                    I prefer the former, why make the call if there's a problem?

                    The less you need, the more you have. Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load? JaxCoder.com

                    M 1 Reply Last reply
                    0
                    • M Marc Clifton

                      if (foo)
                      {
                      DoSomething();
                      }

                      or:

                      MaybeDoSomething(foo);
                      ...
                      MaybeDoSomething(bool foo)
                      {
                      if (foo)
                      {
                      // do the something.
                      }
                      }

                      eh?

                      Latest Articles:
                      Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #10

                      I'm with all the others: first version is what I'd use. It's easier to read, more efficient, and potentially means you don't have to carry the "decision variable(s)" through to the called method.

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      T 1 Reply Last reply
                      0
                      • M Mike Hankey

                        I prefer the former, why make the call if there's a problem?

                        The less you need, the more you have. Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load? JaxCoder.com

                        M Offline
                        M Offline
                        Marc Clifton
                        wrote on last edited by
                        #11

                        What if (no pun intended), the "if" actually required more complex logic, including perhaps some nested stuff, like:

                        if (foo != null)
                        {
                        var data = GetSomeData(foo.SomeValue);

                        if (data has some specific value/s)
                        {
                        DoSomething();
                        }
                        }

                        From an aesthetic point of view, I dislike putting all that into the main method, hence why I've got a couple "Maybe..." methods because the above scenario matches in pseudo-code what I'm actually having to deal with.

                        Latest Articles:
                        Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                        M B A R 4 Replies Last reply
                        0
                        • M Marc Clifton

                          What if (no pun intended), the "if" actually required more complex logic, including perhaps some nested stuff, like:

                          if (foo != null)
                          {
                          var data = GetSomeData(foo.SomeValue);

                          if (data has some specific value/s)
                          {
                          DoSomething();
                          }
                          }

                          From an aesthetic point of view, I dislike putting all that into the main method, hence why I've got a couple "Maybe..." methods because the above scenario matches in pseudo-code what I'm actually having to deal with.

                          Latest Articles:
                          Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                          M Offline
                          M Offline
                          Mike Hankey
                          wrote on last edited by
                          #12

                          True there are always exceptions but for the simple example you gave I still prefer the former for the less complicated logic.

                          The less you need, the more you have. Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load? JaxCoder.com

                          1 Reply Last reply
                          0
                          • M Marc Clifton

                            if (foo)
                            {
                            DoSomething();
                            }

                            or:

                            MaybeDoSomething(foo);
                            ...
                            MaybeDoSomething(bool foo)
                            {
                            if (foo)
                            {
                            // do the something.
                            }
                            }

                            eh?

                            Latest Articles:
                            Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                            R Offline
                            R Offline
                            rnbergren
                            wrote on last edited by
                            #13

                            the first. Almost without exception always. Even if the logic starts to get complex. It will still be understandable to read. PAinful potentially. but the second is harder to read and the more complex it gets the better in comparison to the first, but the first will always be more readable. IMO. ymmv. and all that quid pro quo that goes with all that.

                            To err is human to really elephant it up you need a computer

                            1 Reply Last reply
                            0
                            • M Marc Clifton

                              if (foo)
                              {
                              DoSomething();
                              }

                              or:

                              MaybeDoSomething(foo);
                              ...
                              MaybeDoSomething(bool foo)
                              {
                              if (foo)
                              {
                              // do the something.
                              }
                              }

                              eh?

                              Latest Articles:
                              Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

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

                              I Don't Believe In If Anymore - YouTube[^] :-\

                              1 Reply Last reply
                              0
                              • M Marc Clifton

                                if (foo)
                                {
                                DoSomething();
                                }

                                or:

                                MaybeDoSomething(foo);
                                ...
                                MaybeDoSomething(bool foo)
                                {
                                if (foo)
                                {
                                // do the something.
                                }
                                }

                                eh?

                                Latest Articles:
                                Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                                K Offline
                                K Offline
                                Kschuler
                                wrote on last edited by
                                #15

                                Both have a usage. If it's code that is going to be reused somewhere else, version 2. If it's lots of crazy complicated code, version 2. If it's pretty simple and not reused, version 1.

                                P M 2 Replies Last reply
                                0
                                • M Marc Clifton

                                  if (foo)
                                  {
                                  DoSomething();
                                  }

                                  or:

                                  MaybeDoSomething(foo);
                                  ...
                                  MaybeDoSomething(bool foo)
                                  {
                                  if (foo)
                                  {
                                  // do the something.
                                  }
                                  }

                                  eh?

                                  Latest Articles:
                                  Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                                  J Offline
                                  J Offline
                                  Jon McKee
                                  wrote on last edited by
                                  #16

                                  To me, it mainly depends on whether foo is incidental to (option #1), sufficient for (option #2), or necessary for (option #3) the execution of DoSomething(). Option #3 would be refactoring DoSomething() to include foo so not always an option. So if foo gatekeeps the execution of DoSomething() in this one spot but not others then it's incidental. If foo gatekeeps DoSomething() in a lot of spots but not all then it's sufficient. If foo always gatekeeps DoSomething() then it's necessary.

                                  1 Reply Last reply
                                  0
                                  • K Kschuler

                                    Both have a usage. If it's code that is going to be reused somewhere else, version 2. If it's lots of crazy complicated code, version 2. If it's pretty simple and not reused, version 1.

                                    P Offline
                                    P Offline
                                    peterkmx
                                    wrote on last edited by
                                    #17

                                    Good points ...

                                    1 Reply Last reply
                                    0
                                    • M Marc Clifton

                                      if (foo)
                                      {
                                      DoSomething();
                                      }

                                      or:

                                      MaybeDoSomething(foo);
                                      ...
                                      MaybeDoSomething(bool foo)
                                      {
                                      if (foo)
                                      {
                                      // do the something.
                                      }
                                      }

                                      eh?

                                      Latest Articles:
                                      Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                                      C Offline
                                      C Offline
                                      CodeWomble
                                      wrote on last edited by
                                      #18

                                      I much prefer the first method. Having said that I have been trying to dis the second method and the only things I can say is it looks ugly and is doing more than one thing (both testing if something can be done and doing it.) It has the advantage that the test is explicit that it should be done. And yes, I have well over-thought this... :confused: -showing my working out- Renaming the foo variable to suit what it is being used for:

                                      if (canDoSomething)
                                      {
                                      DoSomething();
                                      }

                                      or:

                                      DoSomethingIfPossible(canDoSomething);
                                      ...
                                      DoSomethingIfPossible(bool canDoSomething)
                                      {
                                      if (canDoSomething)
                                      {
                                      // do the something.
                                      }
                                      }

                                      Or, if renaming the variables is not viable you could try these three convoluted options:

                                      if (CanDoSomething(foo))
                                      {
                                      DoSomething();
                                      }

                                      bool CanDoSomething(bool canDoSomething)
                                      {
                                      return canDoSomething;
                                      }

                                      or:

                                      DoSomethingIfPossible(CanDoSomething(foo));
                                      ...
                                      DoSomethingIfPossible(bool canDoSomething)
                                      {
                                      if (canDoSomething)
                                      {
                                      // do the something.
                                      }
                                      }

                                      bool CanDoSomething(bool canDoSomething)
                                      {
                                      return canDoSomething;
                                      }

                                      or:

                                      DoSomethingIfPossible(foo);
                                      ...
                                      DoSomethingIfPossible(bool foo)
                                      {
                                      if (CanDoSomething(foo))
                                      {
                                      // do the something.
                                      }
                                      }

                                      bool CanDoSomething(bool canDoSomething)
                                      {
                                      return canDoSomething;
                                      }

                                      M 1 Reply Last reply
                                      0
                                      • K Kschuler

                                        Both have a usage. If it's code that is going to be reused somewhere else, version 2. If it's lots of crazy complicated code, version 2. If it's pretty simple and not reused, version 1.

                                        M Offline
                                        M Offline
                                        Marc Clifton
                                        wrote on last edited by
                                        #19

                                        Agreed! :)

                                        Latest Articles:
                                        Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                                        1 Reply Last reply
                                        0
                                        • C CodeWomble

                                          I much prefer the first method. Having said that I have been trying to dis the second method and the only things I can say is it looks ugly and is doing more than one thing (both testing if something can be done and doing it.) It has the advantage that the test is explicit that it should be done. And yes, I have well over-thought this... :confused: -showing my working out- Renaming the foo variable to suit what it is being used for:

                                          if (canDoSomething)
                                          {
                                          DoSomething();
                                          }

                                          or:

                                          DoSomethingIfPossible(canDoSomething);
                                          ...
                                          DoSomethingIfPossible(bool canDoSomething)
                                          {
                                          if (canDoSomething)
                                          {
                                          // do the something.
                                          }
                                          }

                                          Or, if renaming the variables is not viable you could try these three convoluted options:

                                          if (CanDoSomething(foo))
                                          {
                                          DoSomething();
                                          }

                                          bool CanDoSomething(bool canDoSomething)
                                          {
                                          return canDoSomething;
                                          }

                                          or:

                                          DoSomethingIfPossible(CanDoSomething(foo));
                                          ...
                                          DoSomethingIfPossible(bool canDoSomething)
                                          {
                                          if (canDoSomething)
                                          {
                                          // do the something.
                                          }
                                          }

                                          bool CanDoSomething(bool canDoSomething)
                                          {
                                          return canDoSomething;
                                          }

                                          or:

                                          DoSomethingIfPossible(foo);
                                          ...
                                          DoSomethingIfPossible(bool foo)
                                          {
                                          if (CanDoSomething(foo))
                                          {
                                          // do the something.
                                          }
                                          }

                                          bool CanDoSomething(bool canDoSomething)
                                          {
                                          return canDoSomething;
                                          }

                                          M Offline
                                          M Offline
                                          Marc Clifton
                                          wrote on last edited by
                                          #20

                                          I like the "IfPossible" version as well. I'll have to remember that.

                                          Latest Articles:
                                          Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions

                                          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