Python Forum
Whats the right way to refactor this Big if/elif/elif ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Whats the right way to refactor this Big if/elif/elif ?
#1
This code is ugly and not pythonic. I am new to python not new to programming. Instead of guessing on one of many possible refectorings, could you tell me the pythonic way to do this?

As you can see a bunch of variables (forward, left, right, etc) are set up with a floating point number. I want to store in a variable "closest_dir" a string name for the variable which contained the smallest value.

forward = calc_range(msg.ranges, 359, 0, 15)
right = calc_range(msg.ranges, 270, 271, 15)
left = calc_range(msg.ranges, 90, 91, 15)
back = calc_range(msg.ranges, 180, 181, 15)
narrow_l1 = sum(msg.ranges[83:87])/5
narrow_l2 = sum(msg.ranges[88:92])/5
narrow_l3 = sum(msg.ranges[93:97])/5
narrow_r1 = sum(msg.ranges[273:277])/5
narrow_r2 = sum(msg.ranges[268:272])/5
narrow_r3 = sum(msg.ranges[263:267])/5
closest_dist = min(narrow_l1, narrow_l2, narrow_l3, narrow_r1, narrow_r2, narrow_r3,
                    forward, left, right, back)
if (closest_dist == forward):
    closest_dir = "forward"
elif (closest_dist == left):
    closest_dir = "left"
elif (closest_dist == right):
    closest_dir = "right"
elif (closest_dist == back):
    closest_dir = "back"
elif (closest_dist == narrow_l1):
    closest_dir = "narrow_l1"
elif (closest_dist == narrow_l2):
    closest_dir = "narrow_l2"
elif (closest_dist == narrow_l3):
    closest_dir = "narrow_l3"
elif (closest_dist == narrow_r1):
    closest_dir = "narrow_r1"
elif (closest_dist == narrow_r2):
    closest_dir = "narrow_r2"
elif (closest_dist == narrow_r3):
    closest_dir = "narrow_r3"
else:
    closest_dir = "bug"
Reply
#2
The first thing I think of is to make a list like so:

dists = [(forward, 'forward'), (left, 'left'), ..., (narrow_r3, 'narrow_r3')]
dists.sort()
closest_dir = dists[0][1]
But then there is the question of building the list. Could you convert your calculations into one function with parameters?

directions = (('left', [359, 0, 15]), ('right', [270, 271, 15], ...)
distances = [(dist_calc(*parameters), direction) for direction, parameters in direction]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with if vs elif print_hello_world 5 1,094 Jan-07-2021, 08:50 AM
Last Post: print_hello_world
  Whats wrong with the elif? inunanimous93 3 609 Nov-30-2020, 03:58 AM
Last Post: deanhystad
  If, elif, else doesn't work well Vidar567 5 1,026 Nov-21-2020, 06:25 PM
Last Post: DPaul
  cant get my elif function to direct me to where i want fvfre 1 399 Oct-25-2020, 05:17 PM
Last Post: deanhystad
  Problem with my If, Elif, and Else code. ParkerS999 3 936 Sep-03-2020, 01:45 AM
Last Post: bowlofred
  If elif else statement not functioning SamDiggityDog 4 950 Jun-03-2020, 12:27 AM
Last Post: SamDiggityDog
  Whats Wrong!? rjay81 3 762 May-13-2020, 08:02 PM
Last Post: rjay81
  Can u see this code and tell whats wrong with it? AhmadKamal 14 2,000 Apr-29-2020, 11:09 AM
Last Post: jefsummers
  python ast if-elif confusion holyghost 3 1,092 Apr-19-2020, 12:36 PM
Last Post: pyzyx3qwerty
  Proper use of if..elif..else statement nick1941 2 983 Mar-06-2020, 11:22 PM
Last Post: nick1941

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020