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.
  • 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
    • 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
      Mircea Neacsu
      wrote on last edited by
      #21

      How about:

      #define DOSOMETHING_IF(A) if((A)) DoSomething()

      - No extra calls if condition is false. - No repeated if statements visble

      Mircea

      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

        W Offline
        W Offline
        Wizard of Sleeves
        wrote on last edited by
        #22

        There are only three acceptable reasons for creating a function: 1. If there is more than one occurrence of the segment of code. 2. It makes reading the code more logical.

        if(itShouldBeDone(foo)) {
        doSomething()
        }

        3. to confuse the next poor soul who gets to work on your code.

        function itShouldBeDone(foo){
        return decideWhatToDo(foo);
        }

        function decideWhatToDo(foo){
        let decision= false;
        if(typeof foo == "boolean") {
        switch(foo){
        case true:
        decision= foo;
        break;
        case false:
        decision= !foo;
        break;
        default:
        decision= true;
        }
        }
        return false;
        }

        Nothing succeeds like a budgie without teeth.

        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
          KateAshman
          wrote on last edited by
          #23

          Definitely the first one. Low level functions should only do things, preferably in a class that can get mocked during testing. High level functions should decide which things are worth doing, and are prime targets to test.

          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
            CPallini
            wrote on last edited by
            #24

            The first one.

            "In testa che avete, Signor di Ceprano?" -- Rigoletto

            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
              rob tillaart
              wrote on last edited by
              #25

              I prefer the first as the DoSomething() function is explicit what it does. just like the second it encapsulates all actions to be done, that is what functions are made for. The second MaybeDoSomething(bool foo) function is less explicit. Is the "maybe" related to the bool parameter? Suppose so, but that is an assumption, or? And will it "doSomething" if the parameter is false or when the bool is true? For me the first construct does not have these question and is therefor more clear.

              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
                den2k88
                wrote on last edited by
                #26

                It depends. If The Something is part of the business logic I would encapsulate it, if it is simple flow control I wouldn't. Encapsulating in a different subroutine offers better options for bugfixing, bug reporting, logging, maintenance, refacotring, rewriting, whatever.

                GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                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
                  Private Dobbs
                  wrote on last edited by
                  #27

                  Option 1 unless there is a need for re-use. Going back to my days in machine code when every clock cycle counts then the overhead of the call to a method would leave me no choice but to use option 1.

                  1 Reply Last reply
                  0
                  • D David ONeil

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

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

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

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

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