Question

By matrix style, I mean having n variables, each with some number of inputs, and having to handle all possible values. The simplest case of this is multiple boolean values and having to handle every combination of true/false. This is easy if the returned values follow certain patterns, but otherwise it seems quite difficult.

(If there is a better name than 'matrix style', please comment and tell me so I can update the title.)

The ugly way to handle this is an if else chain.

IF self.A = 'N' THEN
    IF self.B = 'N' THEN
        ...
    ELSE
        ...
    END IF;
ELSE
    IF self.B = 'N' THEN
        ...
    ELSE
        ...
    END IF;
END IF;

Good luck keeping track of that mess, especially with more than 4 variables.

A slightly more readable way of doing this is to do all the checks together.

IF self.A = 'N' AND ... self.Y = 'N' AND self.Z = 'N' THEN
    returnValue := 'Bob';
END IF;
If self.A = 'N' AND ... self.Y = 'N' AND self.Z = 'Y' THEN
    returnValue := 'Birthday Party';
END IF;
...
If self.A = 'Y' AND ... self.Y = 'N' AND self.Z = 'N' THEN
    returnValue := 'What did I ever do to deserve this?';
END IF;
...
If self.A = 'Y' AND ... self.Y = 'Y' AND self.Z = 'Y' THEN
    returnValue := 'Thank God I am done!';
END IF;

You can make that a little better if you do a CASE statement instead of a bunch of if/elses, but that is still very hard to maintain. Imagine accidentally putting a Y instead of an N some place and having to go find it. Considering the chance for errors grows exponentially with each new variable added (as you at least double the amount of code you need to write), there is a good chance for errors in any significant sized problem like this.

You can potentially do some interesting text replacement to try to reduce errors. I recently did this with 5 variables. I started with...

NNNNN
NNNNY
...
YYYYN
YYYYY

Then I ran some find and replace over them using Notepad++ to try to reduce the chance of mistyping a N or Y. But the end product still looks nasty to maintain. So I'm wondering if there are any better ways to handle this (mostly in terms of maintainability, though any efficiency boost without loosing maintainability are also welcome suggestions). While I'm looking specifically for PL/SQL solutions, any solutions in other languages are still welcome because they might be able to be translated to PL/SQL.

Edit: In case anyone is trying to solve this problem and wants to use my current solution, here is the find and replace.

Find: ([Y,N]) repeated as many times as you have variables. Replace: \t\t\tWHEN self.valueName = '\1' THEN\r\n\t\t\t\treturnValue := '' where the self.valueName = '\1' is repeated once for each variable you have, with the \1 incremented each time. You'll also need to set the correct number of \t's so that it matches however much indented it should be. This works in Notepad++, regex mode.

Was it helpful?

Solution

Why do you have that problem? I assume that this is a variable of a type consisting out of variables from A-Z. So how do you populate this in the first place? Can't you simplify right there?

But if there is no alternative you can first check if there is only 'Y' and 'N' in the single fields and convert to 1 and 0 and make numbers out of it and check against the numbers. E.g. NNNNY becomes one and NNNYN becomes 2 etc. Then it is IF r=1 then .. elsif r=2 ..

A probably even better alternative is to generate the code. You can form a string that has the "create or replace functionX as ..." and do an execute immediate on it.

OTHER TIPS

Try to concatenate to one value (self.A || self.B || .. || self.Z) and then use case on values like 'NNNNN', 'NNNNY', 'NNNYN, etc.

EDIT: I made assumtions that:

  • you have large set of one-char variables that...
  • ...you want to translate to single return value...
  • ...covering all-or-many possibile sets.

What you gain is a view of all combinations, one per line, one key under other, in each single-char column having same self.variable. If number of variables is really big, to avoid options like YNNYYYNYNNYNYYYNNY you can improve readability to put space every 3-rd or 5-th or n-th char:

when 'NNN NNN NNN' then '1st choice'
when 'NNN NNN NNY' then '2nd choice'
when 'NNN NNN NYN' then '3rd choice'
... ... ...

As @hol in answer below suggested generating code is a very good choice in that case.

But you, as developer, should know if m-th choice is YYY YNY YY or YYY NYY YY, no other way to be sure whether you get correct results than to check/test code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top