Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Getting Started with PHP Unit Testing!
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Refactoring restructures the code, without changing its behavior.
Download the completed sample files
Multiple Iterations of the Same Code
Option 1: List of elseif statements
if (in_array($firstThreeLetters, $this->trigraphs)) {
$newWord = substr($word, 3);
$newWord .= $firstThreeLetters . 'ay';
} elseif (in_array($firstTwoLetters, $this->digraphs)) {
$newWord = substr($word, 2);
$newWord .= $firstTwoLetters . 'ay';
} else {
$newWord = substr($word, 1);
$newWord .= $firstLetter . 'ay';
}
return $newWord;
Option 2: Early Return
if (in_array($firstThreeLetters, $this->trigraphs)) {
return substr($word, 3) . $firstThreeLetters . 'ay';
}
if (in_array($firstTwoLetters, $this->digraphs)) {
return substr($word, 2) . $firstTwoLetters . 'ay';
}
return substr($word, 1) . $firstLetter . 'ay';
Option 3: Switch Statement
switch (true) {
case in_array($firstLetter, $this->vowels):
$newWord = $word . 'ay';
break;
case in_array($firstThreeLetters, $this->trigraphs):
$newWord = substr($word, 3);
$newWord .= $firstThreeLetters . 'ay';
break;
case in_array($firstTwoLetters, $this->digraphs):
$newWord = substr($word, 2);
$newWord .= $firstTwoLetters . 'ay';
break;
default:
$newWord = substr($word, 1);
$newWord .= $firstLetter . 'ay';
}
return $newWord;
Option 3b: Switch Statement Early Return
switch (true) {
case in_array($firstLetter, $this->vowels):
$newWord = $word . 'ay';
return $newWord;
case in_array($firstThreeLetters, $this->trigraphs):
$newWord = substr($word, 3);
$newWord .= $firstThreeLetters . 'ay';
return $newWord;
case in_array($firstTwoLetters, $this->digraphs):
$newWord = substr($word, 2);
$newWord .= $firstTwoLetters . 'ay';
return $newWord;
default:
$newWord = substr($word, 1);
$newWord .= $firstLetter . 'ay';
return $newWord;
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We have a few more features to add
to our pig Latin converter tool.
0:00
But first, I want to do some refactoring.
0:05
Refactoring restructures the code
without changing its behavior.
0:08
I don't like having this long list of else
if statements, so I want to change that.
0:13
I could use a switch statement, but
I like having early returns in my methods.
0:19
As soon as a condition is met,
I want to return the results.
0:24
At the end of our first conditional block,
we can return $newWord.
0:28
Now instead of an else if, I can make this
second conditional a separate conditional.
0:36
And return $newWord.
0:45
Now I can remove this else entirely.
0:49
If I make it past the first two checks,
just perform these actions and
0:58
return the $newWord.
1:02
This should not change
the behavior of our code.
1:04
So our test should still pass,
let's give it a try.
1:07
Sure enough, our test still passed.
1:13
Refactoring is a very handy
way to keep your code updated
1:15
as we learn better ways to do things.
1:19
By having test for functionality,
we can be confident that a refactor of one
1:22
section of code will not break the rest of
the application that's using that code.
1:27
I could refactor this further and change
these three lines into a single line.
1:32
We could return the substring plus
the first three letters, and ay.
1:39
Return the substring,
The first two letters and
1:51
ay, And return substring.
1:59
The first letter and ay.
2:06
And run our test again.
2:14
Great, everything still passes.
2:16
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up