Do I need to refactor this code?
- Posted by TheresNoTime Jul 02, 2013
- 1564 views
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.