Introduce Explaining Variable

You have a complicated expression.

Put the result of the expression, or parts of the expression, in a temporary variable with a name that explains the purpose.


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

	}

For more information see page 124 of Refactoring

| Refactoring Home | | Alphabetical List |