Notes on ‘Refactoring ‘ — 3.2 Replace Magic Number with Symbolic Constant

Before Refactoring


 validate1(){
   if(size < 2){
     addError(...); 
   }
 }

 validate2(){
   if(size < 2){
     addError(...); 
   }
 }


After Refactoring

 static final int MIN_SIZE = 2; 
 validate1(){
   if(size < MIN_SIZE){
     addError(...); 
   }
 }

 validate2(){
   if(size < MIN_SIZE){
     addError(...); 
   }
 }


Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.