Conditional Elimination: it can't be done. All you can do is to move the conditionals to somewhere else and to make it clear what your code is doing. It's always worth remembering that:
Programs must be written for people to read, and only incidentally for machines to execute.
Which begs the question 'how do you do this?' There are a few possibilities.
- Refactoring - examine your code closely and see whether there are any recurring patterns in the logic, if 'yes' spin them off into subroutines.
- Dispatch table - you may be able to improve readability by using a dispatch table.
- Callbacks - you may be able to improve readability by using callbacks
- Multiple dispatch - if you are using OO, multiple dispatch might improve readability
Of course, each of these depends on what the code actually does, and how it can be simplified. IME, and all things being equal (which they rarely are), I'd probably try dispatch tables first.
I'd just like to reiterate that, in each of the suggestions above, you are not eliminating the conditions, you're just moving them somewhere else in the code.
As an aside, I'd like to throw in that, doing any of this takes time. Making code beautiful can take lots of time and we all have limited resources. So you might want to ask yourself, 'is it worth it?'
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.