Sometimes clarity of thinking during late night programming is elusive…

1
(footerMessage.Trim().Length == 0 ? string.Empty : footerMessage + "\r\n\r\n")

There are two bloopers in this one line of code:

  • a string with nothing but spaces really is an empty string (for practical reasons).
  • it would be nice to put two line breaks on the empty string as well as the non-empty string.

So file this one under redundant, i.e. A practical dictionary entry for Redundant would say “See Redundant”

Note: if your not familiar with this construct, it is a carry over from the C-language.  It simplifies an if statement, when the goal is to return one of two values to a variable, depending on the result of a boolean test.  These quick tests can be quite elaborate, but are most effective for generating max, min, floor (etc, etc) functions where the evaluation is short.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
/* usual evaluation */
 
int Productivity_Percent = 90;
 
float AnnualPay = 35000;
 
if (Productivity_Percent > 85) Annual_Pay = 50000;
 
/* ... or ... */
 
int Productivity_Percent = 90;
 
float AnnualPay = Productivity_Percent > 85 ? 50000 : 35000;

The immediate eval is not always efficient.  Usually the code must evaluate both possible answers, before evaluating which one to return–which can waste time on unneeded work. And in the case above, the if statement is actually slightly more efficient than the immediate eval.

It can be very useful for an inline lookup when initializing a value, instead of using a case statement.  E.g.

1
2
3
4
public void Initialize (string Method)
{
   int PortValue = (Method == "Full Power", 37, (Method == "Half Power", 45, 7));
}