Introduce Explaining Variable

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(New page: If you have a complicated expression put them into temporary variables that explain what they do. This can help make code easier to read and understand. if ( (platform.toUpperCase().inde...)
 
m (code block)
Line 1: Line 1:
 
If you have a complicated expression put them into temporary variables that explain what they do. This can help make code easier to read and understand.
 
If you have a complicated expression put them into temporary variables that explain what they do. This can help make code easier to read and understand.
  
 +
if ( (platform.toUpperCase().indexOf("MAC") > -1) &&
 +
(browser.toUpperCase().indexOf("IE") > -1) &&
 +
wasInitialized() && resize > 0 )
 +
{
 +
  // do something
 +
}
  
if ( (platform.toUpperCase().indexOf("MAC") > -1) &&
+
final boolean isMacOs    = platform.toUpperCase().indexOf("MAC") > -1;<br>
(browser.toUpperCase().indexOf("IE") > -1) &&
+
final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1;<br>
wasInitialized() && resize > 0 )
+
final boolean wasResized  = resize > 0;<br><br>
{
+
if (isMacOs && isIEBrowser && wasInitialized() && wasResized)
 +
{
 
   // do something
 
   // do something
}
+
  }
 
+
final boolean isMacOs    = platform.toUpperCase().indexOf("MAC") > -1;<br>
+
final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1;<br>
+
final boolean wasResized  = resize > 0;<br><br>
+
 
+
if (isMacOs && isIEBrowser && wasInitialized() && wasResized)
+
{
+
// do something
+
}
+

Revision as of 00:22, 6 August 2009

If you have a complicated expression put them into temporary variables that explain what they do. This can help make code easier to read and understand.

if ( (platform.toUpperCase().indexOf("MAC") > -1) &&
(browser.toUpperCase().indexOf("IE") > -1) &&
wasInitialized() && resize > 0 )
{
  // do something
}
final boolean isMacOs     = platform.toUpperCase().indexOf("MAC") > -1;
final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1;
final boolean wasResized = resize > 0;

if (isMacOs && isIEBrowser && wasInitialized() && wasResized) { // do something }
Personal tools