Home > OS >  Dealing with multiple condition if statement in Dart
Dealing with multiple condition if statement in Dart

Time:07-10

Hi I need help simplifying this if statement with multiple conditions but I'm not sure how to as every attempt I've had cluttered my code even more :

if (isAirplane(element) && isFlying(element) && isRed(element))    return redFlyingAirplane;
if (isAirplane(element) && isFlying(element) && !isRed(element))   return greenFlyingAirplane;
if (isAirplane(element) && !isFlying(element) && isRed(element))   return redGroundedAirplane;
if (isAirplane(element) && !isFlying(element) && !isRed(element))  return greenGroundedAirplane;
if (!isAirplane(element) && isFlying(element) && isRed(element))   return redFlyingHelicopter;
if (!isAirplane(element) && isFlying(element) && !isRed(element))  return greenFlyingHelicopter;
if (!isAirplane(element) && !isFlying(element) && isRed(element))  return redGroundedHelicopter;
if (!isAirplane(element) && !isFlying(element) && !isRed(element)) return greenGroundedHelicopter;

EDIT :

Adding some extra context in which I'm using this :

 late BitmapDescriptor redFlyingAirplane;
 late BitmapDescriptor greenFlyingAirplane;
...

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      _initMarkers();
    });
  }

  void _initMarkers() async {
    redFlyingAirplane = await LoadBitmaps.redFlyingAirplane;
    greenFlyingAirplane = await LoadBitmaps.greenFlyingAirplane;
    ...
  }


 BitmapDescriptor resolveBitmap(Something element) {
if (isAirplane(element) && isFlying(element) && isRed(element))    return redFlyingAirplane;
if (isAirplane(element) && isFlying(element) && !isRed(element))   return greenFlyingAirplane;
...
 }

CodePudding user response:

Nested If Statements may be used in this case. There is no need to use else conditions since you return a value for every case. The rest of the code won't run in any way if the condition is met.

  if (isAirplane(element)) {
    if (isFlying(element)) {
      if (isRed(element)) return redFlyingAirplane;

      return greenFlyingAirplane;
    }
    if (isRed(element)) return redGroundedAirplane;

    return greenGroundedAirplane;
  }

  if (isFlying(element)) {
    if (isRed(element)) return redFlyingHelicopter;

    return 'greenFlyingHelicopter';
  }

  if (isRed(element)) return redGroundedHelicopter;
  
  return 'greenGroundedHelicopter';

CodePudding user response:

I've taken a similar approach as BBK, but extracted variables for the methods that are called > 1 times, not knowing how "costly" those operations are, and added the use of ternary operator to make it more compact:

  var isRedColor = isRed(element);
  var isFlyingNow = isFlying(element);

  if (isAirplane(element)) {
    if (isFlyingNow) {
      return isRedColor ? redFlyingAirplane : greenFlyingAirplane;
    }
    return isRedColor ? redGroundedAirplane : greenGroundedAirplane;
  }
  if (isFlyingNow) {
    return isRedColor ? redFlyingHelicopter : greenFlyingHelicopter;
  }
  return isRedColor ? redGroundedHelicopter : greenGroundedHelicopter;

CodePudding user response:

If your data is regular and homogeneous, as in this case, consider using a lookup table. (I changed the 8 values to an enum, but they could be an icon or whatever.)

print(lookup[isAirplane ? 0 : 1][isFlyingNow ? 0 : 1][isRedColor ? 0 : 1]);

final lookup = [
  [
    [IconState.redFlyingAirplane, IconState.greenFlyingAirplane],
    [IconState.redGroundedAirplane, IconState.greenGroundedAirplane],
  ],
  [
    [IconState.redFlyingHelicopter, IconState.greenFlyingHelicopter],
    [IconState.redGroundedHelicopter, IconState.greenGroundedHelicopter],
  ],
];

You could extend this idea to use nested maps for more complex tables.

  • Related