Split Temporary Variable

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
m (Reverted edits by Ebybymic (Talk); changed back to last version by Matthew Harward)
 
(3 intermediate revisions by 2 users not shown)
Line 11: Line 11:
 
     foo() {<br>
 
     foo() {<br>
 
         int perimeter = 2 * (height + width);<br>
 
         int perimeter = 2 * (height + width);<br>
         System.out.print(perimeter );<br>
+
         System.out.print(perimeter);<br>
         area = height * width;<br>
+
         int area = height * width;<br>
 
         System.out.print(area);<br>
 
         System.out.print(area);<br>
 
     }
 
     }

Latest revision as of 03:11, 25 November 2010

This is when you have create a temporary variable that you use all the way through a method.

   foo() {
int temp = 2 * (height + width);
System.out.print(temp);
temp = height * width;
System.out.print(temp);
}


The variable has two different meanings at various stages of the method. Give each variable a descriptive name making it easier to understand the code.

   foo() {
int perimeter = 2 * (height + width);
System.out.print(perimeter);
int area = height * width;
System.out.print(area);
}