To caps or not to caps that is the question?

Eric Wald eswald at brainshell.org
Tue Dec 18 16:25:12 MST 2007


Steve wrote:
> newval = (myval == someval) ? somefunction : otherfunction;
> Expands out to...
> if(myval == someval)
> {
>    newval = somefunction;
> }else{
>    newval = otherfunction;
> }

No, it expands out to

  if (myval == someval) {
     newval = somefunction;
  } else {
     newval = otherfunction;
  }

> That works great on it's own, but if I'm trying to do it as part of
> another larger statement it makes the whole statement overall less
> clear.

The statement in question, however, is a separate beast.

  letter = (counter == 0) ? toupper(letter) : letter;

is better expressed as

  if (!counter) letter = toupper(letter);

Even the first double-length line is better expressed as a ternary
inside an if, to avoid the same kind of assignment to self, and to avoid
confusing the reader with nested ternary operators:

  if (letter == oldletter) {
    letter = (coin <= 60)? PickChar(vowels): PickChar(consonants);
  }

What I find most odd, though, is that you never pick a new character
first, so this conditional is always true after the first iteration.

- Eric



More information about the PLUG mailing list