lundi 8 octobre 2018

How to manage a lot of user options without if/else

I'm working on an old fortran code which performs different types of calculations depending on which configuration the user selects.

Let's say, for example, that the user selects "Car A", with a 2.0 liters Diesel engine and gull-wing doors.

Then, the program proceeds to initialize a series of control codes, such as:

IF (CAR == 'CAR A') THEN
    CONTROL_1 = 0 ! type of engine
    CONTROL_2 = 2 ! type of doors
    ...
    CONTROL_25 = 7 ! ...
ELSE IF (CAR == 'CAR B') THEN
    ...
END IF

Then:

IF (CONTROL_1 == 0) THEN
    CONTROL_12 = 1 ! size of engine of type 1
    ...
END IF 

Depending on the values of CONTROL_(xx), the program executes different sets of calculations. In order to select the appropriate branch, there are countless very long chains of IF/ELSEstatements.

Needless to say, this is a nightmare to debug and to edit.

So, I guess that my question is: Is there a more readable way to manage this situation? For example, initializing a single configuration code at the beginning and using a CASE statement? This would generate a lot of duplicated code though... Is there a "best practice" for this kind of situation?

Thanks a lot!

P.S.: I know that there are quite a few posts about how to avoid multiple if/else statements, but I haven't found anything specific on this problem.

Aucun commentaire:

Enregistrer un commentaire