Is this code better written using ternary conditional or switch or if-else? [on hold]












0












$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;
}
}
}









share|improve this question







New contributor




gregmac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$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.


















    0












    $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;
    }
    }
    }









    share|improve this question







    New contributor




    gregmac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $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.
















      0












      0








      0





      $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;
      }
      }
      }









      share|improve this question







      New contributor




      gregmac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $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#






      share|improve this question







      New contributor




      gregmac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      gregmac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      gregmac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 hours ago









      gregmacgregmac

      1042




      1042




      New contributor




      gregmac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      gregmac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      gregmac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      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.






















          1 Answer
          1






          active

          oldest

          votes


















          0












          $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.






          share|improve this answer








          New contributor




          Ed Grimm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          $endgroup$




















            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0












            $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.






            share|improve this answer








            New contributor




            Ed Grimm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            $endgroup$


















              0












              $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.






              share|improve this answer








              New contributor




              Ed Grimm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






              $endgroup$
















                0












                0








                0





                $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.






                share|improve this answer








                New contributor




                Ed Grimm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                $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.







                share|improve this answer








                New contributor




                Ed Grimm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer






                New contributor




                Ed Grimm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered 3 hours ago









                Ed GrimmEd Grimm

                1013




                1013




                New contributor




                Ed Grimm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                Ed Grimm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                Ed Grimm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.















                    Popular posts from this blog

                    How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

                    is 'sed' thread safe

                    How to make a Squid Proxy server?