Using boolean variable for assignment vs. strictly flow control

Status
Not open for further replies.

mcmnky

Ars Tribunus Militum
2,625
Subscriptor
Is there any consensus on using boolean variables for flow control vs. assignment to a property?

The current situation I have is making text on a form visible to the user if the current record has a certain property. What I like to do is:

Code:
label.Visible = false;
bProperty = hasProperty(recordID);
if (bProperty)
{
	label.Visibile = true;
}


What I could do (and have seen done) is the more compact:

Code:
bProperty = hasProperty(recordID);
label.Visibile = bProperty;

Or going one step further:

Code:
label.Visibile = hasProperty(recordID);

This particular example is a good candidate for the last approach, but in general my concern is sacrificing clarity for brevity. I'm not putting this code onto punch cards and there's no need for extreme optimization to fit the limited memory of a cartridge. While I want efficient code, there's no reason to make code harder to maintain just to be clever.

So even for this simple example I like the first approach to be consistent with more complex cases, such as:

Code:
if (bPerformedRO)	
{
	SampleTests_dg.FindColumn("PERFORMEDBY").ReadOnly = true;
	SampleTests_dg.FindColumn("PERFORMEDON").ReadOnly = true;
	SampleTests_dg.FindColumn("PERFORMEDCOMMENT").ReadOnly = true;
}
else if (!((SampleTests_dg.GetCurrentRowData("APPRSTS") == null) &&
	(SampleTests_dg.GetCurrentRowData("OSTATUS") != 'Cancelled') &&
	(SampleTests_dg.GetCurrentRowData("TS") != 'Cancelled')))
{
	SampleTests_dg.FindColumn("PERFORMEDBY").ReadOnly = true;
	SampleTests_dg.FindColumn("PERFORMEDON").ReadOnly = true;
	SampleTests_dg.FindColumn("PERFORMEDCOMMENT").ReadOnly = true;
}
else 	
{
	SampleTests_dg.FindColumn("PERFORMEDBY").ReadOnly = false;
	SampleTests_dg.FindColumn("PERFORMEDON").ReadOnly = false;
	SampleTests_dg.FindColumn("PERFORMEDCOMMENT").ReadOnly = false;
}

I believe that code is logically identical to:

Code:
if (!((SampleTests_dg.GetCurrentRowData("APPRSTS") == null) &&
	(SampleTests_dg.GetCurrentRowData("OSTATUS") != 'Cancelled') &&
	(SampleTests_dg.GetCurrentRowData("TS") != 'Cancelled')))
{
	SampleTests_dg.FindColumn("PERFORMEDBY").ReadOnly = 
	SampleTests_dg.FindColumn("PERFORMEDON").ReadOnly = 
	SampleTests_dg.FindColumn("PERFORMEDCOMMENT").ReadOnly = true;
}
else 	
{
	SampleTests_dg.FindColumn("PERFORMEDBY").ReadOnly = 
	SampleTests_dg.FindColumn("PERFORMEDON").ReadOnly = 
	SampleTests_dg.FindColumn("PERFORMEDCOMMENT").ReadOnly = bPerformedRO;
}

But I don't see anything gained by the second example here, other than fewer lines of code. If bPerformedRO is true there's no need to check the compound conditional statement. I'm also not a fan of the this = that = true statement. Electrons are not is such short supply that we should avoid this = true; that = true;

That do you folks think?
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
I'd rather use data binding, but assuming my UI framework was stuck in imperative-land:

Code:
class Grid
{
    public void SetPerformanceReadOnly(bool ro)
    {
        this.FindColumn("PERFORMEDBY").ReadOnly         = ro;
        this.FindColumn("PERFORMEDON").ReadOnly         = ro;
        this.FindColumn("PERFORMEDCOMMENT").ReadOnly    = ro;
    }
    ...
}

void YourCallback(Grid SampleTests_dg)
{
    bool performanceIsWritable = 
        SampleTests_dg.GetCurrentRowData("APPRSTS") == null &&
        SampleTests_dg.GetCurrentRowData("OSTATUS") != 'Cancelled' &&
        SampleTests_dg.GetCurrentRowData("TS") != 'Cancelled';
        
    SampleTests_dg.SetPerformReadOnly( !performanceIsWritable );
}
 

Tiibiidii

Smack-Fu Master, in training
85
First of all: Richard Berg's snippet is the way I'd write it as well

[url=http:/civis/viewtopic.php?p=27787769#p27787769:3tmzfyya said:
mcmnky[/url]":3tmzfyya]Is there any consensus on using boolean variables for flow control vs. assignment to a property?
This is a bit of a misnomer/false dichotomy

you're not using flow control XOR property assignment

it's (flow control + property assignment) vs simply property assignment
This particular example is a good candidate for the last approach, but in general my concern is sacrificing clarity for brevity. I'm not putting this code onto punch cards and there's no need for extreme optimization to fit the limited memory of a cartridge. While I want efficient code, there's no reason to make code harder to maintain just to be clever.

That do you folks think?

In both the short and longer examples, the second alternative is definitely simpler and arguably clearer to understand.

In fact, I try to steer clear of imperative flow control as much as possible... if/else inside an expression is ok, but if/else around some statements force you to think about branches, state and makes it harder to test and isolate intermediate results.

Having shorter/more concise high level code is not a matter of efficiency/optimization/memory footprint or the like. It's about removing confusing (and misleading) redundancy, and fitting code in a way that's easier to visually parse and understand at a glance

Concerning the `this = that = true`, I agree that in this case it's not helpful... but it's more due to the fact that `SampleTests_dg.FindColumn("PERFORMEDCOMMENT").ReadOnly` is a mouthful, and forcing to return the line on each variable kinda defeats the point
(on the other hand, multiple assignment is also useful for when you'll have to modify the code... since it'll make it impossible for you to modify 2 of these `true` and forget the other one)
 

wallinbl

Ars Legatus Legionis
14,319
Subscriptor
[url=http:/civis/viewtopic.php?p=27788625#p27788625:18ipi8t0 said:
Richard Berg[/url]":18ipi8t0]I'd rather use data binding
Definitely this. More so if your boolean test has the potential to change while the form is up - let the UI's event system handle that kind of thing for you, toggling the fields off when the underlying values change. Otherwise, you'll have to write code to wire all that up, and that's just wasting time that could be spent on more productive business logic.
 
D

Deleted member 43669

Guest
When I see:

Code:
if(cond) {
  foo = true;
}
else {
  foo = false;
}

and similar, I just want to pluck my eyes out.

Brevity is most of the times equivalent to clarity. Longer code is harder to grok and keep in your head completely.

I'm not talking about turning descriptive variable names into single letters- I'm talking about simplifying control flow, using functions to reduce repetition, using higher-order constructs for conciseness, etc.
 
but in general my concern is sacrificing clarity for brevity
In your first example "label.Visibile = hasProperty(recordID);" is both clearer and shorter. So there's no trade off here.

For the second example, neither one is what I would call readable. Using chained assignment is 'clever' for no particular benefit. The main issue is that the condition you're looking for is not clear and the change does nothing to address that problem.

If I understand the condition correctly, the data should be marked read-only iff any one of a number of things is true. It's easy to say that directly:
Code:
bool readOnly = bPerformedRO ||
                (SampleTests_dg.GetCurrentRowData("APPRSTS") != null) ||
                (SampleTests_dg.GetCurrentRowData("OSTATUS") == 'Cancelled') ||
                (SampleTests_dg.GetCurrentRowData("TS") == 'Cancelled');

SampleTests_dg.FindColumn("PERFORMEDBY").ReadOnly = readOnly;
SampleTests_dg.FindColumn("PERFORMEDON").ReadOnly = readOnly;
SampleTests_dg.FindColumn("PERFORMEDCOMMENT").ReadOnly = readOnly;

There's a pretty good book on the topic: The Art of Readable Code.

Another one is Code Complete, which covers more than just readabilty and is a good handbook for new developers.
 
Status
Not open for further replies.