Replace nested conditional statements with if / else in this block

What would this block look like using if / else statements instead of ternary?

return value == null? name.local? attrNullNS: attrNull: typeof value === "function"? name.local? attrFunctionNS: attrFunction: name.local? attrConstantNS: attrConstant;

(I want to make sure I'm doing this right before tackling 15 more similar blocks ... Ideally I would like to replace all such blocks with a regex, but there seems to be no method? Replace conditional statement with if / else automatically ? )

+3


source to share


3 answers


Good question.

First of all, I agree that the developer who left you with this code must be LARTed.

However, you can figure it out (without formatting your code like Eclipse JSDT ) if you think the syntax is for the Conditional statement LogicalORExpression ? AssignmentExpression : AssignmentExpression

or… : AssignmentExpressionNoIn

and that the largest match wins .

Adjacent expressions referring to the same conditional operation cannot be split either with ?

or :

on both sides, because the grammar does not allow it. So just put yourself in the position of an LL (n) parser that works according to ECMAScript grammar ;-) Repeatedly ask yourself the question: "Could this code be generated to produce this target character for this?"; if the answer is no, reverting to a shorter match until it appears, or failing with a syntax error if production isn't running.

  • return

    (

    value == null ? name.local ? attrNullNS : attrNull : typeof value === "function" ? name.local ? attrFunctionNS : attrFunction : name.local ? attrConstantNS : attrConstant

    )

    ;

  • return (value == null ?

    (

    name.local ? attrNullNS : attrNull

    )

    :

    (

    typeof value === "function" ? name.local ? attrFunctionNS : attrFunction : name.local ? attrConstantNS : attrConstant

    )

    );

  • return (

    (

    value == null

    )

    ? (name.local ? attrNullNS : attrNull) : (

    (

    typeof value === "function"

    )

    ?

    (

    name.local ? attrFunctionNS : attrFunction

    )

    :

    (

    name.local ? attrConstantNS : attrConstant

    )

    ));



So:

if (value == null)
{
  if (name.local)
  {
    return attrNullNS;
  }
  else
  {
    return attrNull;
  }
}
else
{
  if (typeof value === "function")
  {
    if (name.local)
    {
      return attrFunctionNS;
    }
    else
    {
      return attrFunction;
    }
  }
  else
  {
    if (name.local)
    {
      return attrConstantNS;
    }
    else
    {
      return attrConstant;
    }
  }
}

      

(CMIIW.) This can be further reduced to

if (value == null)
{
  if (name.local)
  {
    return attrNullNS;
  }

  return attrNull;
}

if (typeof value === "function")
{
  if (name.local)
  {
    return attrFunctionNS;
  }

  return attrFunction;
}

if (name.local)
{
  return attrConstantNS;
}

return attrConstant;

      

+2


source


It's really fast, just replacing the ternary operators, but I guess it would look something like this:



if (value == null) {
    if (name.local) {
        return attrNullNS;
    }else{
        return attrNull;
    }
}else if (typeof value === "function") {
    if (name.local) {
        return attrFunctionNS;
    }else{
        return attrFunction;
    }
}else{
    if (name.local) {
        return attrConstantNS;
    }else{
        return attrConstant;
    }
}

      

0


source


The syntax ("grouping") of ternary operators and unspecified if-else statements are quite similar, so you can start by replacing each … ?

with if (…)

and each :

with else

. Then wrap a statement return

around each one and use auto indentation. You might even be able to accomplish some of these tasks (step by step) with automatic replacement. As a result, you will receive

if (value == null)
    if (name.local) 
        return attrNullNS;
    else
        return attrNull;
else if (typeof value === "function")
    if (name.local)
        return attrFunctionNS;
    else
        return attrFunction;
else if (name.local)
    return attrConstantNS;
else
    return attrConstant;

      

0


source







All Articles