2

I am using quite a few ternary operators instead of select case statements.

These work fine to my best knowledge but I get now a lot inspection warnings from WebStorm telling me that comma expressions could be overly clever and may lead to subtle bugs. There are also warnings about the value assigned never being used.

Is this the correct use of a ternary operator or should I use something different to be on the safe side?

u_avg < siteColorArray[0] ?
  (fillColor = '#FF6600', strokeColor = '#FF8C00') :
  u_avg < siteColorArray[1] ?
    (fillColor = '#FF8C00', strokeColor = '#FFB300') :
      u_avg < siteColorArray[2] ?
        (fillColor = '#FFB300', strokeColor = '#FFD900') :
        u_avg < siteColorArray[3] ?
          (fillColor = '#FFD900', strokeColor = '#FFFF00') :
          u_avg < siteColorArray[4] ?
            (fillColor = '#FFFF00', strokeColor = '#CDE30F') :
            u_avg < siteColorArray[5] ?
              (fillColor = '#CDE30F', strokeColor = '#9CC71E') :
              u_avg < siteColorArray[6] ?
                (fillColor = '#9CC71E', strokeColor = '#6AAA2D') :
                u_avg < siteColorArray[7] ?
                  (fillColor = '#6AAA2D', strokeColor = '#388E3C') :
                  u_avg >= siteColorArray[7] ?
                    (fillColor = '#388E3C', strokeColor = '#2E7D32') :
                    null;

WebStorm doesn't like it a lot...

enter image description here

Edit: Just to clear things up. This works well on Chrome 54+ but I am concerned whether other and older platforms may not like this.

Dennis Bauszus
  • 1,624
  • 2
  • 19
  • 44

1 Answers1

0

You need set some variable to the result:

var test = u_avg < ...
K Scandrett
  • 16,390
  • 4
  • 40
  • 65
  • But the variable test would never be used. Wouldn't that just add an unnecessary variable allocation? – Dennis Bauszus Nov 03 '16 at 12:32
  • Just reviewing what you did in more detail. It's not common to set variables inside the ternary, normally it returns a single value to `fillColor`, and then you'd have another separate statement for `strokeColor`. Does it actually work they way you have it? – K Scandrett Nov 03 '16 at 12:36
  • Yes. it works fine in Chrome on my dev machine. I declare the two colours prior to the ternary chain and then assign these colours to an object after the ternary. I am just concerned about the inspection warning. – Dennis Bauszus Nov 03 '16 at 12:40
  • Just looked up https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator and your example looks fine (learn something new!). But it should still return a value to a variable. – K Scandrett Nov 03 '16 at 12:40
  • And whether there may be browser which do not like this. – Dennis Bauszus Nov 03 '16 at 12:40
  • According to the link I posted it is supported in that format on all major browsers – K Scandrett Nov 03 '16 at 12:41
  • Provided of course that you're still assigning to a variable – K Scandrett Nov 03 '16 at 12:42
  • I actually learned about comma expression in a ternary operator from that article which is why my construction looks exactly like it. Does basic support mean that this should work on earlier versions of all platforms? – Dennis Bauszus Nov 03 '16 at 12:45