Home > other >  how to reduce tertiary conditionals
how to reduce tertiary conditionals

Time:05-19

So I have this code which works perfectly, it's just that it looks ugly and takes up alot of space. is there a way to reduce the amount of lines this code takes? Code here:

radius:
      Platform.OS === 'android'
        ? showMore === 1
          ? androidRadius * 2
          : showMore === 2
          ? androidRadius * 4
          : showMore === 3
          ? androidRadius * 10
          : showMore === 4
          ? androidRadius * 25
          : showMore === 5
          ? androidRadius * 50
          : showMore === 6
          ? androidRadius * 100
          : androidRadius
        : showMore === 1
        ? iosRadius * 2
        : showMore === 2
        ? iosRadius * 4
        : showMore === 3
        ? iosRadius * 10
        : showMore === 4
        ? iosRadius * 25
        : showMore === 5
        ? iosRadius * 50
        : showMore === 6
        ? iosRadius * 100
        : iosRadius,

CodePudding user response:

It's quite easy to clean this kind of condition using mapping, for instance:

const radiusMap = {1: 2, 2: 4, 3: 10, 4: 25, 5: 50, 6: 100};
const platformRadius = Platform.OS === 'android' ? androidRadius: iosRadius;

// finally,
return { radius: radiusMap[platformRadius] || platformRadius };

This will take the radius from the radius map, also returning a default radius if no radius in the map is found.

CodePudding user response:

I usually use object lookups for these situations. So, for showMore cases, you can do this: -

function getRadius(deviceRadius, showMoreValue){
    const showMoreCases = {
        1: deviceRadius * 2,
        2: deviceRadius * 4,
        3: deviceRadius * 10,
        4: deviceRadius * 25,
        5: deviceRadius * 50,
        6: deviceRadius * 100,
    }

    return showMoreCases[showMoreValue] || deviceRadius;
}

And you can use it in the first condition like this: -

radius: Platform.OS === 'android'
        ? getRadius(androidRadius, showMore) 
        : getRadius(iosRadius, showMore)

CodePudding user response:

This solution is one line of code.

To conserve space, it uses an array rather than key/value look up. That works in this case because the showMore values are sequential. We subtract 2 from this index because we only care about range 2-7. Any index outside the range is assigned the default radius.

([2, 4, 10, 25, 50, 100][showMore - 2] || 1) * (Platform.OS === 'android' ? androidRadius : iosRadius)

Test Snippet

It's important to test because, for example, the most up voted answer here actually doesn't work at all. And the error would have been obvious had the code been tested.

// TEST

let androidRadius = 100,
  iosRadius = 101;

["android", "ios"].forEach(os => {

  let Platform = {
    OS: os
  };

  for (let showMore = -2; showMore < 10; showMore  ) {
    console.log(
      "OS: "   os,
      ", showMore: "   showMore,
      ", radius: "   ([2, 4, 10, 25, 50, 100][showMore - 2] || 1) * (Platform.OS === 'android' ? androidRadius : iosRadius)
      

    );

  }

});

CodePudding user response:

Use a switch statement. Example here.

  • Related