Replace Temp with Query

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
 
m (Reverted edits by Ebybymic (Talk); changed back to last version by Aidan Bebbington)
 
(2 intermediate revisions by 2 users not shown)
Line 2: Line 2:
  
  
double basePrice = _quantity * _itemPrice;
+
double basePrice = _quantity * _itemPrice;
if (basePrice > 1000)
+
if (basePrice > 1000)
    return basePrice * 0.95;
+
    return basePrice * 0.95;
else
+
else
    return basePrice * 0.98;
+
    return basePrice * 0.98;
  
 
Change to
 
Change to
  
if (basePrice( ) > 1000)
+
if (basePrice( ) > 1000)
    return basePrice() * 0.95;
+
    return basePrice() * 0.95;
else
+
else
    return basePrice( ) * 0.98;
+
    return basePrice( ) * 0.98;
...
+
...
double basePrice( ) {
+
double basePrice( ) {
    return _quantity * _itemPrice;
+
    return _quantity * _itemPrice;
}
+
}

Latest revision as of 03:08, 25 November 2010

Replace a temp variable with a method call. This makes the variable accessible to other parts of the program. It also allows you to write shorter methods as temp values are only locally available to use you must write all the code related to the temp variable within its scope. I have on many times required a variable outside of its scope and made it global- just straight bad and lazy. -Paul Williams


double basePrice = _quantity * _itemPrice;
if (basePrice > 1000)
    return basePrice * 0.95;
else
    return basePrice * 0.98;

Change to

if (basePrice( ) > 1000)
    return basePrice() * 0.95;
else
    return basePrice( ) * 0.98;
...
double basePrice( ) {
    return _quantity * _itemPrice;
}
Personal tools