개발방법론/모델링/Refactoring

Consolidate Duplicate Conditional Fragments <IMG SRC = "http://www.refactoring.com/catalog/updated.gif" border=0>

아름프로 2003. 7. 29. 02:09

Consolidate Duplicate Conditional Fragments



The same fragment of code is in all branches of a conditional expression.

Move it outside of the expression.

                if (isSpecialDeal()) {
                        total = price * 0.95;
                        send();
                }
                else {
                        total = price * 0.98;
                        send();
                }




                if (isSpecialDeal())
                        total = price * 0.95;
                else
                        total = price * 0.98;
                send();

For more information see page 243 of Refactoring

Additional Comments


Using with try/catch blocks

Paul Haahr rightly took me to task for being far too glib when I talked about consolidating with exceptions. You can only pull repeated code into a final block if all non-fatal exceptions are caught. This is because the finally block is executed after any exception, including those that don't have a catch clause. (Of course you may prefer that to happen, but that's not a semantics preserving change.)

Contributors


- Paul Haahr




***** 아름다운프로님에 의해서 게시물 복사 + 카테고리변경되었습니다 (2003-12-18 17:27)