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

    Y Offline
    Y Offline
    YahiaEQ
    wrote on last edited by
    #29

    Depends on context but esp. in complex scenarios I like the second form because it allows for having clearer understand of the flow while being composable: I could have a bigger methods which calculates certains states and then calls several MayBe-methods... It allows the MayBe-method to be called based on the state which in turn can be easily serialized and deserialized (which is not so easy with an "if"-Statement).

    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
      Delphi 7 Solutions
      wrote on last edited by
      #30

      from a debugging point of view the second is a nightmare, you need to step into the maybe to know if it happend or not. I will take the first code any time

      1 Reply 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

        B Offline
        B Offline
        BryanFazekas
        wrote on last edited by
        #31

        Marc Clifton wrote:

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

        This is a different question. The original question is very simplistic. If we were discussing a real scenario, I'd ask what is the conditional logic, how many times will it be repeated and in what context(s), how large is the function in which it's called, etc. But for this simplistic scenario, I'd use #1 because it is not efficient -- either in coding and probably execution -- to call a function for no useful reason.

        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
          Kirk 10389821
          wrote on last edited by
          #32

          this reminds me of a code review comment I received, for using:

          while (true)
          {
          // About 30 FAILURE comparisons for rating an insurance policy like this;
          failure_code = 123;
          if (failure_condition1) break;
          failure_code = 125;
          if (failure_condition2) break;
          ...

          failure_code = 0; // Success
          break;
          }

          if (failure_code>0)
          process_error(failure_code);
          else
          process_policy;

          To me, the fake structure of the while loop supported the early exit (once we know we failed) The code was so overly complicated (insurance rules), that I could only prove correctness by literally applying the "rules & regs" line by line! For what it's worth, the Business Analyst could understand, and the programmers complained that the while loop was throwing them off. [Comments were added]

          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

            G Offline
            G Offline
            Gary Wheeler
            wrote on last edited by
            #33

            I don't like the idea of having a DoSomething() and a MaybeDoSomething that only adds a simple if {...} around a call. My approach would have been:

            void DoSomething(bool something_allowed)
            {
            if (something_allowed)
            {
            // logic from original DoSomething()
            }
            }

            I've worked with code that was written such that there is only one or two block constructs per method. Naming ends up stupid (OpenFileIfFoundAndNotAlreadyOpen(), anyone?), and it makes it very difficult to follow logic. I would much rather have a method run a little longer yet be able to see the entire algorithm in one place.

            Software Zen: delete this;

            1 Reply 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

              A Offline
              A Offline
              Alister Morton
              wrote on last edited by
              #34

              If the test is complex and gets used more than once, there's an argument for giving the test its own method. That way the test is isolated, and can be changed later, but it's not hidden away in the MaybeDoSomething method. So you might end up with if (TestIfWeNeedToDoSomething( ... )) { DoSomething(); } // lots of code follows. if (TestIfWeNeedToDoSomething( ... )) // again { DoSomething(); } and so on. A little inelegant, though.

              1 Reply Last reply
              0
              • D Daniel Pfeffer

                :rolleyes: This isn't the obfuscated C contest, and there is no prize for using the minimal number of keystrokes.

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

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

                Obfuscated is when you use partial template specialization for the task! :laugh:

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

                H D 2 Replies Last reply
                0
                • D David ONeil

                  Obfuscated is when you use partial template specialization for the task! :laugh:

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

                  H Offline
                  H Offline
                  honey the codewitch
                  wrote on last edited by
                  #36

                  it wasn't me, i swear! *hides*

                  Real programmers use butterflies

                  1 Reply Last reply
                  0
                  • D David ONeil

                    Obfuscated is when you use partial template specialization for the task! :laugh:

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

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

                    :omg: :wtf: X|

                    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
                    • OriginalGriffO OriginalGriff

                      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!

                      T Offline
                      T Offline
                      thewazz
                      wrote on last edited by
                      #38

                      Aka single resp.

                      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
                        Reelix
                        wrote on last edited by
                        #39

                        The first one. Why are you even calling the second one if you're not going to use it?

                        -= Reelix =-

                        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
                          sasadler
                          wrote on last edited by
                          #40

                          First one.

                          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
                            charlieg
                            wrote on last edited by
                            #41

                            Depends on the complexity of the method containing #1. I prefer showing logic as soon as possible *in context*. I'm also a big believer in encapsulation for the soul purpose of containing the application in edible portions.

                            Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759

                            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
                              r_hyde
                              wrote on last edited by
                              #42

                              First option, full stop. -but- Here's a fun little twist, if your language of choice supports this sort of thing (I'll use C# to illustrate):

                              void ExecuteIf(Func predicate, Action thingToExecute)
                              {
                              if (predicate())
                              thingToExecute();
                              }

                              ExecuteIf(() => foo, DoSomething);

                              ...but I'm not convinced that this is any easier to read, it imposes a little extra overhead, and I would still stick with your first option. Just wanted to throw it into the mix :cool:

                              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
                                #43

                                The 2nd option is like eating something you shouldn't.

                                It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                                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
                                  Martin ISDN
                                  wrote on last edited by
                                  #44

                                  if (foo)
                                  {
                                  DoSomething();
                                  }

                                  avoid the function call if not necessary. what you see is what it does.

                                  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

                                    A Offline
                                    A Offline
                                    AnotherKen
                                    wrote on last edited by
                                    #45

                                    The first is more simple and will run faster when compiled. However, the second might be preferable if you plan to run that bit of code a lot of times since then you just re-type one call and it makes the code easier to read.

                                    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
                                      rjmoses
                                      wrote on last edited by
                                      #46

                                      It depends! 1) If the section of code is executed frequently, then #2 adds the cpu overhead of the call/return therefore slowing the program down. 2) If the DoSomething code is relatively simple and is only used in this one location, then it should be coded in place in order to make readability, understandability and comprehension better. 3) If the Dosomething code is frequently used code, then #1 makes it clearer when something is done. The answer to this question is a choice of balancing performance vs readability vs understandability vs comprehension. To me, code should always be written such that I or anyone else can comprehend the intent and methodology of the original programmer in a few seconds.

                                      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

                                        U Offline
                                        U Offline
                                        User 14060113
                                        wrote on last edited by
                                        #47

                                        The answer is a clear "Depends!" ;-)

                                        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

                                          P Offline
                                          P Offline
                                          PSU Steve
                                          wrote on last edited by
                                          #48

                                          First. I prefer only calling logic when the situation warrants. You always call MaybeDoSomething in the second, even if it really does nothing...

                                          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