Code inspection: Convert 'if' statement into 'switch'
This inspection reports series of if
statements and suggests converting them into a single switch
statement or switch
expression.
A single switch
can make your code cleaner and easier to understand compared to multiple if
statements, especially as the number of tested cases increases.
class Parser
{
Marker Parse(string marker)
{
if (marker is "<=HEADER=>")
return Marker.Header;
if (marker is "<=FOOTER=>")
return Marker.Footer;
return Marker.None;
}
}
enum Marker {None, Header, Footer}
class Parser
{
Marker Parse(string marker)
{
return marker switch
{
"<=HEADER=>" => Marker.Header,
"<=FOOTER=>" => Marker.Footer,
_ => Marker.None
};
}
}
enum Marker {None, Header, Footer}
Last modified: 08 May 2024