sa
sa

Reputation:

Need help with ternary operators!

I have this code:

field = string.Format(Str, value1, value2,
        found == true ? fieldName : "", found  == true ? "product" : "");

Is there a way to combine the two found == true ternary operation into a more succinct piece of code?

Upvotes: 0

Views: 625

Answers (9)

Stuart Thompson
Stuart Thompson

Reputation: 1859

That line of code took my manual parser a few moments longer to digest than I would like. I'm all for succinct and elegant coding, but wouldn't you rather come across this in a few months time, albeit over a few more lines?

if (found)
// Use empty strings for fieldName and productName
field = String.Format(Str, value1, value2, fieldName, productName);
else
// Use the fieldName and productName in the string
field = String.Format(Str, value1, value2, String.Empty, String.Empty);

Just my personal taste, but I know which one I'd rather encounter.

Upvotes: 0

Steve Gilham
Steve Gilham

Reputation: 11277

Since String.Format takes a params array, the one major variation not yet suggested would be

field = string.Format(Str, found
                      ? new object[] {value1, value2, fieldName, "product"}
                      : new object[] {value1, value2, string.Empty, string.Empty});

Upvotes: 0

Christian C. Salvadó
Christian C. Salvadó

Reputation: 828170

What about:

field = found ? string.Format(Str, value1, value2, fieldName, "product")
              : string.Format(Str, value1, value2, "", "");

Only one ternary, more readable than the original example, but I would consider a if/else statement for readability...

Upvotes: 2

Larsenal
Larsenal

Reputation: 51196

Long lines with multiple ternary operators can be really illegible. Line breaks help a little with readability.

field = string.Format(Str,
    value1,
    value2,
    found ? fieldName : "",
    found ? "product" : "");

Upvotes: 7

Sam Harwell
Sam Harwell

Reputation: 100059

Stop comparing found to true. You want the condition on whether or not the item was found, not whether or not found is true:

found ? fieldName : string.Empty

Upvotes: 1

Paul Williams
Paul Williams

Reputation: 17040

You can abbreviate it like so:

field = string.Format(Str, value1, value2, found ? fieldName : "", found ? "product" : "");

Upvotes: 1

David
David

Reputation: 73604

This is just an opinion but... YUK! I hate reading code like this.

Ternary operators may make for less code, but readability is suffering here. Don't go for less lines of code if it hurts readability.

Upvotes: 6

erikkallen
erikkallen

Reputation: 34421

Comparing to true is unnecessary:

field = string.Format(Str, value1, value2, found ? fieldName : "", found ? "product" : "");

Upvotes: 0

Dominic Rodger
Dominic Rodger

Reputation: 99841

You don't need the == true bit:

field = string.Format(Str, value1, value2, found ? fieldName : "", found ? "product" : "");

However, in my view the following is easier to read:

if (found) {
    field = string.Format(Str, value1, value2, fieldName, "product");
}
else {
    field = string.Format(Str, value1, value2, "", "");
}

Upvotes: 19

Related Questions