Which do you prefer? A programming question!
-
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 Subscriptionsfoo ? DoSomething() : ;
:-D Simpler is better!The Science of King David's Court | Object Oriented Programming with C++
-
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 SubscriptionsNot 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.
-
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 SubscriptionsI 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
-
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 SubscriptionsI'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 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
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 -
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 SubscriptionsTrue 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
-
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 Subscriptionsthe 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
-
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 -
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 -
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 SubscriptionsTo me, it mainly depends on whether
foo
is incidental to (option #1), sufficient for (option #2), or necessary for (option #3) the execution ofDoSomething()
. Option #3 would be refactoringDoSomething()
to includefoo
so not always an option. So iffoo
gatekeeps the execution ofDoSomething()
in this one spot but not others then it's incidental. Iffoo
gatekeepsDoSomething()
in a lot of spots but not all then it's sufficient. Iffoo
always gatekeepsDoSomething()
then it's necessary. -
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.
-
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 SubscriptionsI 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;
} -
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.
Agreed! :)
Latest Articles:
Client-Side Type-Based Publisher/Subscriber, Exploring Synchronous, "Event-ed", and Worker Thread Subscriptions -
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;
}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 -
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 SubscriptionsHow about:
#define DOSOMETHING_IF(A) if((A)) DoSomething()
- No extra calls if condition is false. - No repeated if statements visble
Mircea
-
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 SubscriptionsThere 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.
-
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 SubscriptionsDefinitely 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.
-
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 -
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 SubscriptionsI 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.
-
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 SubscriptionsIt 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