if statement - Better way to program in matrix style selections in plsql? -
by matrix style, mean having n variables, each number of inputs, , having handle possible values. simplest case of multiple boolean values , having handle every combination of true/false. easy if returned values follow patterns, otherwise seems quite difficult.
(if there better name 'matrix style', please comment , tell me can update title.)
the ugly way handle if else chain.
if self.a = 'n' if self.b = 'n' ... else ... end if; else if self.b = 'n' ... else ... end if; end if;
good luck keeping track of mess, more 4 variables.
a more readable way of doing checks together.
if self.a = 'n' , ... self.y = 'n' , self.z = 'n' returnvalue := 'bob'; end if; if self.a = 'n' , ... self.y = 'n' , self.z = 'y' returnvalue := 'birthday party'; end if; ... if self.a = 'y' , ... self.y = 'n' , self.z = 'n' returnvalue := 'what did ever deserve this?'; end if; ... if self.a = 'y' , ... self.y = 'y' , self.z = 'y' returnvalue := 'thank god done!'; end if;
you can make little better if case statement instead of bunch of if/elses, still hard maintain. imagine accidentally putting y instead of n place , having go find it. considering chance errors grows exponentially each new variable added (as @ least double amount of code need write), there chance errors in significant sized problem this.
you can potentially interesting text replacement try reduce errors. did 5 variables. started with...
nnnnn nnnny ... yyyyn yyyyy
then ran find , replace on them using notepad++ try reduce chance of mistyping n or y. end product still looks nasty maintain. i'm wondering if there better ways handle (mostly in terms of maintainability, though efficiency boost without loosing maintainability welcome suggestions). while i'm looking pl/sql solutions, solutions in other languages still welcome because might able translated pl/sql.
edit: in case trying solve problem , wants use current solution, here find , replace.
find: ([y,n])
repeated many times have variables. replace: \t\t\twhen self.valuename = '\1' then\r\n\t\t\t\treturnvalue := ''
self.valuename = '\1'
repeated once each variable have, \1 incremented each time. you'll need set correct number of \t's matches indented should be. works in notepad++, regex mode.
why have problem? assume variable of type consisting out of variables a-z. how populate in first place? can't simplify right there?
but if there no alternative can first check if there 'y' , 'n' in single fields , convert 1 , 0 , make numbers out of , check against numbers. e.g. nnnny becomes 1 , nnnyn becomes 2 etc. if r=1 .. elsif r=2 ..
a better alternative generate code. can form string has "create or replace functionx ..." , execute immediate on it.
Comments
Post a Comment