Is this code better written using ternary conditional or switch or if-else? [on hold]
$begingroup$
When I'm checking some conditions to return one of a small handful of values, I often end up nesting several ternary conditional operators (?:
) rather than using a series of if..elseif statements.
As a simple example of this, presume I have:
public enum StringValueType
{
Null,
Empty,
Short,
Long
}
public string Value { get; set; }
I'm trying to return the appropriate StringValueType
value in a property:
public StringValueType ValueType =>
Value == null ? StringValueType.Null
: Value == string.Empty ? StringValueType.Empty
: Value.Length < 8 ? StringValueType.Short
: StringValueType.Long;
Is this readable? Are there downsides? I'm not sure I've ever come across this in other people's code before.
For comparison, some alternate ways this might be written:
If-else
public StringValueType ValueType
{
get
{
if (Value == null)
return StringValueType.Null;
else if (Value == string.Empty)
return StringValueType.Empty;
else if (Value.Length < 8)
return StringValueType.Short;
else
return StringValueType.Long;
}
}
Switch+Conditional
public StringValueType ValueType
{
get
{
switch (MatchedDefinitionName)
{
case null:
return StringValueType.Null;
case "":
return StringValueType.Empty;
default:
return Value.Length < 8 ? StringValueType.Short : StringValueType.Long;
}
}
}
c#
New contributor
$endgroup$
put on hold as off-topic by Jamal♦ 2 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
$begingroup$
When I'm checking some conditions to return one of a small handful of values, I often end up nesting several ternary conditional operators (?:
) rather than using a series of if..elseif statements.
As a simple example of this, presume I have:
public enum StringValueType
{
Null,
Empty,
Short,
Long
}
public string Value { get; set; }
I'm trying to return the appropriate StringValueType
value in a property:
public StringValueType ValueType =>
Value == null ? StringValueType.Null
: Value == string.Empty ? StringValueType.Empty
: Value.Length < 8 ? StringValueType.Short
: StringValueType.Long;
Is this readable? Are there downsides? I'm not sure I've ever come across this in other people's code before.
For comparison, some alternate ways this might be written:
If-else
public StringValueType ValueType
{
get
{
if (Value == null)
return StringValueType.Null;
else if (Value == string.Empty)
return StringValueType.Empty;
else if (Value.Length < 8)
return StringValueType.Short;
else
return StringValueType.Long;
}
}
Switch+Conditional
public StringValueType ValueType
{
get
{
switch (MatchedDefinitionName)
{
case null:
return StringValueType.Null;
case "":
return StringValueType.Empty;
default:
return Value.Length < 8 ? StringValueType.Short : StringValueType.Long;
}
}
}
c#
New contributor
$endgroup$
put on hold as off-topic by Jamal♦ 2 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
$begingroup$
When I'm checking some conditions to return one of a small handful of values, I often end up nesting several ternary conditional operators (?:
) rather than using a series of if..elseif statements.
As a simple example of this, presume I have:
public enum StringValueType
{
Null,
Empty,
Short,
Long
}
public string Value { get; set; }
I'm trying to return the appropriate StringValueType
value in a property:
public StringValueType ValueType =>
Value == null ? StringValueType.Null
: Value == string.Empty ? StringValueType.Empty
: Value.Length < 8 ? StringValueType.Short
: StringValueType.Long;
Is this readable? Are there downsides? I'm not sure I've ever come across this in other people's code before.
For comparison, some alternate ways this might be written:
If-else
public StringValueType ValueType
{
get
{
if (Value == null)
return StringValueType.Null;
else if (Value == string.Empty)
return StringValueType.Empty;
else if (Value.Length < 8)
return StringValueType.Short;
else
return StringValueType.Long;
}
}
Switch+Conditional
public StringValueType ValueType
{
get
{
switch (MatchedDefinitionName)
{
case null:
return StringValueType.Null;
case "":
return StringValueType.Empty;
default:
return Value.Length < 8 ? StringValueType.Short : StringValueType.Long;
}
}
}
c#
New contributor
$endgroup$
When I'm checking some conditions to return one of a small handful of values, I often end up nesting several ternary conditional operators (?:
) rather than using a series of if..elseif statements.
As a simple example of this, presume I have:
public enum StringValueType
{
Null,
Empty,
Short,
Long
}
public string Value { get; set; }
I'm trying to return the appropriate StringValueType
value in a property:
public StringValueType ValueType =>
Value == null ? StringValueType.Null
: Value == string.Empty ? StringValueType.Empty
: Value.Length < 8 ? StringValueType.Short
: StringValueType.Long;
Is this readable? Are there downsides? I'm not sure I've ever come across this in other people's code before.
For comparison, some alternate ways this might be written:
If-else
public StringValueType ValueType
{
get
{
if (Value == null)
return StringValueType.Null;
else if (Value == string.Empty)
return StringValueType.Empty;
else if (Value.Length < 8)
return StringValueType.Short;
else
return StringValueType.Long;
}
}
Switch+Conditional
public StringValueType ValueType
{
get
{
switch (MatchedDefinitionName)
{
case null:
return StringValueType.Null;
case "":
return StringValueType.Empty;
default:
return Value.Length < 8 ? StringValueType.Short : StringValueType.Long;
}
}
}
c#
c#
New contributor
New contributor
New contributor
asked 3 hours ago
gregmacgregmac
1042
1042
New contributor
New contributor
put on hold as off-topic by Jamal♦ 2 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Jamal♦ 2 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
The ternary operator is almost never the readable way to go, which is one of the big reasons you see it so rarely. If you do want to use it, you might want to consider some vertical alignment to make it more legible.
public StringValueType ValueType =>
Value == null ? StringValueType.Null
: Value == string.Empty ? StringValueType.Empty
: Value.Length < 8 ? StringValueType.Short
: StringValueType.Long;
That said, vertical alignment is a bit controversial; I find it immensely helpful, but I know a lot of programmers who hate it. I would explain their reasoning, except that none of them have ever explained a coherent reason to me apart from them not being used to it.
While switch statements are frequently the most legible option, since it can't handle a significant portion of the logic, I would probably lean towards your if-else version.
That said, if there were 10-20 cases, and the switch could handle almost all of them, with just one or two having additional logic, I'd probably go with the switch in those cases.
New contributor
$endgroup$
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
The ternary operator is almost never the readable way to go, which is one of the big reasons you see it so rarely. If you do want to use it, you might want to consider some vertical alignment to make it more legible.
public StringValueType ValueType =>
Value == null ? StringValueType.Null
: Value == string.Empty ? StringValueType.Empty
: Value.Length < 8 ? StringValueType.Short
: StringValueType.Long;
That said, vertical alignment is a bit controversial; I find it immensely helpful, but I know a lot of programmers who hate it. I would explain their reasoning, except that none of them have ever explained a coherent reason to me apart from them not being used to it.
While switch statements are frequently the most legible option, since it can't handle a significant portion of the logic, I would probably lean towards your if-else version.
That said, if there were 10-20 cases, and the switch could handle almost all of them, with just one or two having additional logic, I'd probably go with the switch in those cases.
New contributor
$endgroup$
add a comment |
$begingroup$
The ternary operator is almost never the readable way to go, which is one of the big reasons you see it so rarely. If you do want to use it, you might want to consider some vertical alignment to make it more legible.
public StringValueType ValueType =>
Value == null ? StringValueType.Null
: Value == string.Empty ? StringValueType.Empty
: Value.Length < 8 ? StringValueType.Short
: StringValueType.Long;
That said, vertical alignment is a bit controversial; I find it immensely helpful, but I know a lot of programmers who hate it. I would explain their reasoning, except that none of them have ever explained a coherent reason to me apart from them not being used to it.
While switch statements are frequently the most legible option, since it can't handle a significant portion of the logic, I would probably lean towards your if-else version.
That said, if there were 10-20 cases, and the switch could handle almost all of them, with just one or two having additional logic, I'd probably go with the switch in those cases.
New contributor
$endgroup$
add a comment |
$begingroup$
The ternary operator is almost never the readable way to go, which is one of the big reasons you see it so rarely. If you do want to use it, you might want to consider some vertical alignment to make it more legible.
public StringValueType ValueType =>
Value == null ? StringValueType.Null
: Value == string.Empty ? StringValueType.Empty
: Value.Length < 8 ? StringValueType.Short
: StringValueType.Long;
That said, vertical alignment is a bit controversial; I find it immensely helpful, but I know a lot of programmers who hate it. I would explain their reasoning, except that none of them have ever explained a coherent reason to me apart from them not being used to it.
While switch statements are frequently the most legible option, since it can't handle a significant portion of the logic, I would probably lean towards your if-else version.
That said, if there were 10-20 cases, and the switch could handle almost all of them, with just one or two having additional logic, I'd probably go with the switch in those cases.
New contributor
$endgroup$
The ternary operator is almost never the readable way to go, which is one of the big reasons you see it so rarely. If you do want to use it, you might want to consider some vertical alignment to make it more legible.
public StringValueType ValueType =>
Value == null ? StringValueType.Null
: Value == string.Empty ? StringValueType.Empty
: Value.Length < 8 ? StringValueType.Short
: StringValueType.Long;
That said, vertical alignment is a bit controversial; I find it immensely helpful, but I know a lot of programmers who hate it. I would explain their reasoning, except that none of them have ever explained a coherent reason to me apart from them not being used to it.
While switch statements are frequently the most legible option, since it can't handle a significant portion of the logic, I would probably lean towards your if-else version.
That said, if there were 10-20 cases, and the switch could handle almost all of them, with just one or two having additional logic, I'd probably go with the switch in those cases.
New contributor
New contributor
answered 3 hours ago
Ed GrimmEd Grimm
1013
1013
New contributor
New contributor
add a comment |
add a comment |