Home > other >  How to avoid the IF ELSE statements
How to avoid the IF ELSE statements

Time:01-18

I have the following code, which does not look very nice. What methods could I use to make it look better? Imagine that there are a lot of IF ELSE statements. As an example, I would like to distinguish whether the left is smaller or larger than the right. And more distances are added. (It's about a robot that is supposed to detect obstacles in front of it.)

distance1 = left_LOX.distance();
distance2 = middle_LOX.distance();
distance3 = right_LOX.distance();

if (distance1 < 100 || distance2 < 100 || distance3 < 100)
  distance_case = 1;
else if (distance1 < 300 || distance2 < 300 || distance3 < 300)
  distance_case = 2;
else
  distance_case = 3;

// Abfragen für die Automatik
switch (distance_case)
{
case 1:
  target = 0;
  robot.Stop();
  delay(2000);
  break;
case 2:
  target = 4000;
  robot.Forward();
  break;
case 3:
  target = 6000;
  robot.Forward();
  break;
}

An idea on how to make it better would be nice.

CodePudding user response:

you can use if ... else if ... else block directly if the distance_case does not have long term use. It unnecessarily introduces new variables which has no purpose. Or define an enum if you see, in long term, there will many such cases. Reader can also quickly make sense of the values.

CodePudding user response:

dmin= min(left_LOX.distance(), middle_LOX.distance(), right_LOX.distance());
if (dmin < 100)
{
    target = 0;
    robot.Stop();
    delay(2000);
}
else if (dmin < 300)
{
   target = 4000;
   robot.Forward();
}
else
{
   target = 6000;
   robot.Forward();
}

CodePudding user response:

If you have a lot of distances (hundreds, thousand), it's easier to group all the distances together in a container such as vector first, and then use standard algorithms. E.g. something like this: This is assuming you have the C std library available on the esp32 platform/compiler you are using - it should be possible at least up to C 11 - see e.g. https://medium.com/geekculture/modern-c-with-esp32-dcd3918dd978.

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    // Just an example vector initialisation - replace this with the required x_Lox.distance() calls.
    std::vector<int> distances = {300, 500, 500, 200, 100 };
    const auto min = *std::min_element(distances.begin(), distances.end());
    
    std::cout << "Min distance: " << min;

    return 0;
}

Once you have the minimum distance, you can run through the various cases for handling. This is based on the assumption that you are only interested in the shortest distance to choose your robot's action. If you have more complex logic, the code also gets more complex.

CodePudding user response:

You can add if it looks better for you.

distance_case = (distance1 < 100 || distance2 < 100 || distance3 < 100)   
                (distance1 < 300 || distance2 < 300 || distance3 < 300)   
                (distance1 >= 300 || distance2 >= 300 || distance3 >= 300);
  • Related