Re: Do I need to refactor this code?
- Posted by DerekParnell (admin) Jul 02, 2013
- 1466 views
TheresNoTime said...
I have an auto-generated flat source code, thousands of monotone lines, many of which have more than a dozen comparisons. Comparisons are often repeated. Sometimes, all except one:
if (a = 0) and (b = 1) and (c = 2) and (d = 3) and (e = 4) and (f = 5) and (g = 6) and (h = 7) and (i = 8) and (x = 9) return 1 end if if (a = 0) and (b = 1) and (c = 2) and (d = 3) and (e = 4) and (f = 5) and (g = 6) and (h = 7) and (i = 8) and (y = 10) return 2 end if if (a = 0) and (b = 1) and (c = 2) and (d = 3) and (e = 4) and (f = 5) and (g = 6) and (h = 7) and (i = 8) and (z = 11) return 3 end if
Obviously, this code can be optimized:
if (a = 0) and (b = 1) and (c = 2) and (d = 3) and (e = 4) and (f = 5) and (g = 6) and (h = 7) and (i = 8) then if (x = 9) return 1 end if if (y = 10) return 2 end if if (z = 11) return 3 end if end if
Does it make practical sense? I know that C++ compilers (at least some of them) are able to do this automatically. What about Euphoria? I use eushroud, if it is important.
Generated Euphoria code is not this sophisticated yet.
You might want to try this approach, depending on how you automate the code generation ...
sequence temp temp = {a,b,c,d,e,f,g,h,i,x} switch temp do case {0,1,2,3,4,5,6,7,8,9} then return 1 end case case {0,1,2,3,4,5,6,7,8,10} then return 2 end case case {0,1,2,3,4,5,6,7,8,11} then return 3 end case end switch