CleanCode logo
NAVIGATION: first page in sectionprevious pageup one levelnext pagefinal page in section

Guideline RP5: Condense code for clarity

Take advantage of the constructs of the language within which you are working. The ternary "?" operator--available in C, Java, JavaScript, Perl, and others--is great for avoiding duplicate code. See the example below.

EXAMPLEJava, JavaScript
Instead of:
	if (cond) stuff = "foo" + obj.meth(a)
	else stuff = "bar" + obj.meth(a)
	stuff = (cond ? "foo" : "bar") + obj.meth(a)
Instead of:
	if ($cond) { $stuff = "foo" . $obj->meth($a) }
	else { $stuff = "bar" . $obj->meth($a) }
	$stuff = ($cond ? "foo" : "bar") . $obj->meth($a)
Valid XHTML 1.0!Valid CSS!Get CleanCode at Fast, secure and Free Open Source software downloads
Copyright © 2001-2013 Michael Sorens • Contact usPrivacy Policy
Usage governed by Mozilla Public License 1.1 and CleanCode Courtesy License
CleanCode -- The Website for Clean DesignRevised 2013.06.30