Introduce Explaining Variable

From CSSEMediaWiki
Revision as of 10:00, 24 November 2010 by Ebybymic (Talk | contribs)
Jump to: navigation, search

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;<br>
final boolean isIEBrowser = browser.toUpperCase().indexOf("IE")  > -1;<br>
final boolean wasResized  = resize > 0;<br><br>
if (isMacOs && isIEBrowser && wasInitialized() && wasResized)
{
  // do something
}
Personal tools