There are some known rules, which refer to various combinations of IF statements (or if/else or switch/case statements etc). These rules can be used for refactoring.
Example 1: "Nested IFs with single body can be always replaced by one IF with AND conditions."
if (a) {
if (b) {
doSomething();
}
}
can be rewritten as:
if (a && b) {
doSomething();
}
Example 2: "Subsequent IFs with the same body can be always replaced by one IF with OR condition."
int i = 0;
if (a) {i = 1;}
if (b) {i = 1;}
can be rewritten as:
if (a || b) {i = 1;}
I'm sure there are many more similar rules. I would like to know if there is a known theory behind this, which gives generic pattern for creating such rules. I would like to see a list of other known rules. I think boolean algebra does not exacly handle this, because it uses mathematical syntax which does not exactly translate to programming syntax.
There are similar questions, which always ask about one rule or another. This question is completely generic, not about particular rules, but about the theory behind them.
Aucun commentaire:
Enregistrer un commentaire