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

                              R Offline
                              R Offline
                              Rusty Bullet
                              wrote on last edited by
                              #49

                              That is a different situation than posed. The overhead of a function call could be justified in this case.

                              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