Remove assignments to Parameters

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(New page: The code assigns to a parameter. int discount (int inputVal, int quantity, int yearToDate) { if (inputVal > 50) inputVal -= 2; ... } Use a temporary variable inst...)
 
m (Reverted edits by Ebybymic (Talk); changed back to last version by Matthew Harward)
 
(2 intermediate revisions by 2 users not shown)
Line 18: Line 18:
 
== Additional Resources ==
 
== Additional Resources ==
  
 
+
[http://sourcemaking.com/refactoring/remove-assignments-to-parameters sourcemaking.com]
[[http://sourcemaking.com/refactoring/remove-assignments-to-parameters | sourcemaking.com]]
+

Latest revision as of 03:22, 25 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