My top Clean Code snippets

For quite a while the Clean Code movement has been an area focusing on excellence and best practices in the software industry. I have compiled a list of my top snippet cleanups, there are small so are easily remembered and put into context. They can be done in almost any dynamic language (JS and PHP included).



State comparison
Dont compare text

if (text === ‘removing’) {
   //state currently removing

Its BAD and can change.
Use classes.

if (hasClass(‘removing’)) {
   //state currently removing

GOOD as easy and reliable.



Dont indent due to error flow
Dont create indent for normal flow due to error checking.
BAD

if ($myVar !== 1) {
   //error
} else {
   //normal flow
}

GOOD
Let flow continue in same level.

if ($myVar !== 1) {
   //error
   return;
}
//normal flow



Dont hard-code item count.

$total = 2;

BAD
This could dynamically change or need updating.

$total = count ($myItems);

GOOD



Smaller if blocks
Do your best to loop for patterns in the logic which can be abstracted to improve readability and size of code.
BAD

if (expression) {
   $returned = $this->myFunc($arg, $another, ‘first’);
} else {
   $returned = $this->myFunc($arg, $another, ‘second’);
}
return $returned;

GOOD

if (expression) {
   $lastArg = ‘first’;
} else {
   $lastArg = ‘second’;
}
return $this->myFunc($arg, $another, $lastArg);



Remove multiple !NOT’s

if (!$this || !$that || !$why)

BAD
If all are NOT, can reverse it. Its hard to negate properties so doing it once is easier than doing it 3 times.

if !($this && $that $&& why)

GOOD



jQuery each 1 liner (again)

$(“.my-list li a”).each(function () {
   $(this).attr("tabindex", "1");
});

BAD

$(“.my-list li a”)

is an array.
Can just do:

$(“.my-list li a”).attr("tabindex", "1");

GOOD



jQuery serialize object

&var=one&tea=two;

BAD

myObject = {var: ‘one’, tea: ‘two’ }
& + $.param( myObject );

GOOD
Now suitable for URL (param doesnt do the first &).



jQuery each 1 liner
Starting with

myFunc(item, element) { }

Use:

list.each( function( i, el ) {
   myFunc(i, el) {
});

BAD

list.each(this.myFunc);

GOOD



Chain jquery calls

$('#message').prependTo('.extra-info');
$('#message').show();

BAD:

$('#message').prependTo('.extra-info').show();

GOOD



PHP check far down in object exists in 1 line
Nested IF’s for a single property.

if (isset($json)) {
  if (isset($json->error)) {
    if (isset($json->error->details)) {

BAD

if (isset($json, $json->error, $json->error->details)) {

GOOD



PHP return boolean 1 line

if ($this->isBlue() === 0 && $this->isCircle() ) {
   $bool = true;
} else {
   $bool = false;
}
return $bool;

BAD

return $this->isBlue() === 0 && $this->isCircle()

GOOD



PHP if statement 1 line
IF conditional with 1 line

if ($type === ‘shape’ || $type === ‘shape_large’) {
   $isShape = true;
}

BETTER:

$isShape = ($type === 'shape' || $type === "shape_large");

Could even abstract this into function

isShape()

and return.



PHP arrays 1 line

foreach ($items->getElements() as $elements) {
   $myElements[] = $elements;
}

BAD
Dont always need a NESTED FOREACH

$myElements = array_merge($myElements, $items->getElements());

GOOD



More to come in the future on dynamic languages and clean coding.

2 thoughts on "My top Clean Code snippets"

Elmira

Thinking like that is really imrevssipe

Qid Squid

Great cleanup best practices!

A note about a subtle effect…
PHP if statement 1 line:
This ternary would be more accurate because the original won’t have \$isShape defined:
!(\$type === ‘shape’ || \$type === “shape_large”) ?: \$isShape = true;

For you or others it might be irrelevant or not the point, of course.
Oh – also – the random special character “smart” quotes in the code snippets are pesky.

Leave a Reply