Replace Assignment with Initialization


Refactoring contributed by Mats Henricson

You have code that first declares a variable and then assigns a value to it

Make it into a direct initialization instead


void foo() {
   int i;
   // ....
   i = 7;
}



void foo() {
   // ...
   int i = 7;
}

Motivation


You often see functions in which the programmer has first declared lots of variables that will be used in that function, and then, further down the function, assigns values to them. This is often an artifact from older programming languages, where you had to declare in the beginning of the function all variables that are used in it. In C++ and Java this is not necessary, and in C++ this old style of programming can give you slower programs, since a declaration of a variable in C++ often means a call to the default constructor. In Java, declarations of variables are cheap, if not even free, but the problem is that you end up with unnecessary lines of code that adds no value to your program - you have two lines of code that can be expressed with one line of code.

Another problem is the risk that at some parts of the program a variable is not explicitly initialized, so that its initial value may not be obvious, or even undefined, as is often the case in C++.

Mechanics


  • Move the declaration of the variable to where that variable is first used,
    just before the assignment, making sure that you haven't reduced the scope
    of the variable so that it isn't in scope at some other place where it
    is used

  • Compile and test  

  • Replace the declaration and assignment with a direct initialization

  • Compile and test  

  • If appropriate, declare the temp as final, and compile and test again.


Example


    Start with this code.

    void foo() {
            int i;
            // ....
            i = 7;
    }

    The first move is to move the declaration of the variable to just before
    where it is assigned to:


    void foo() {
            // ....
            int i;
            i = 7;
    }

    I can now compile and test this. The most common mistake is that you have reduced the scope of the variable so that it no longer is in scope at some other place where it is used. Fortunately C++ and Java compilers will give you and error if this has happened.

    Then it is time to replace the declaration + assignment with the initialization:

    void foo() {
            // ....
            int i = 7;
    }

    If this also compiles and runs as before, then it is the end of the refactoring.

    However if the value of i doesn't change, then it's a good idea to make that explicit by declaring i as final



    void foo() {
            // ....
            final int i = 7;
    }




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

카테고리

분류 전체보기 (539)
이야기방 (19)
토론/정보/사설 (16)
IBM Rational (9)
U-IT (0)
SOA/WS/ebXML (110)
개발방법론/모델링 (122)
J2SE (34)
J2EE (60)
DataBase (39)
Open Projects (30)
BP/표준화 (50)
Apache Projects (15)
Web/보안/OS (22)
Tools (7)
AJAX/WEB2.0 (1)
Linux/Unix (1)
영어 (0)
비공개방 (0)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

달력

«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

글 보관함

Total :
Today : Yesterday :