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:
What I could do (and have seen done) is the more compact:
Or going one step further:
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:
I believe that code is logically identical to:
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?
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?