mardi 26 mai 2020

Condition statement in C#7

i want to avoid following writing style. pointed out simply out of range should be written once

var a = 100;   // range 1..100_000
var b = 10_000; // range 100..30_000_000

if( a <= 1000)
{
    if( b < a * 100)
    {
       // both out of range
    }
}
else if( b < 100_000)
{
    // both out of range
}

came up with following style, doesn't fit at first glance...

if( a <= 1000 ? b < a * 100 : b < 100_000)
{
    // both out of range
}

switch statement? not good...

var lessEqual1000 = "a <= 1000";
var greater1000 = "a > 1000";
switch( a <= 1000 ? lessEqual1000 : greater1000)
{
case lessEqual1000 when b < a* 100:
case greater1000 when b < 100_000:
    // both out of range
}

what is the best?

Aucun commentaire:

Enregistrer un commentaire