There is more than one way to solve a problem.

In fact, there are hundreds.

There are diluted and ill-thought out ways. There are clear and concise ways.

Refactoring code is a process of reviewing your code, taking things out that aren’t necessary and reducing it to its most basic functional form. It aims to take you to clarity.

I have finished my day by working on Pig Latin, a JS challenge from FreeCodeCamp.

The aim is to convert any string to PigLatin which is explained by FreeCodeCamp clearly like so:

Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an “ay”.

If a word begins with a vowel you just add “way” to the end.

This challenge has been a really interesting process that I wanted to share. My code underwent 2 main revisions, and I’m sure it can be done better. (Let me know how!)

The first dealt mechanically with the consonant cluser problem with numerous if functions checking to see how many consonants were in the consonant cluster in the first three letters. My 20 line solution:

function translate(str) {
var vowels = [‘a’,’e’,’i’,’o’,’u’];
var first;
var strArray = str.split(“”);
if (vowels.indexOf(str.charAt(0)) >= 0) {
return str + “way”;
}
else if (vowels.indexOf(str.charAt(0)) < 0 && vowels.indexOf(str.charAt(1)) && vowels.indexOf(str.charAt(2)) < 0) {
first = strArray.splice(0,3);
return strArray.join(“”) + first.join(“”) + “ay”;
}
else if (vowels.indexOf(str.charAt(0)) < 0 && vowels.indexOf(str.charAt(1)) < 0) {
first = strArray.splice(0,2);
return strArray.join(“”) + first.join(“”) + “ay”;
}
else if (vowels.indexOf(str.charAt(0)) < 0) {
first = strArray.splice(0,1);
return strArray.join(“”) + first.join(“”) + “ay”;
}
}
translate(“glove”);

As you can see, there’s a lot of repetition there. So I decided that I would make a for loop that would count the number of consonants first. This was quite tricky as I had to break the loop if it meant a vowel, but I got there in the end. This made the code better in my opinion as it dealt with less of a mechanical human approach and used the tools that JS has. However it was longer with 21 lines!

function translate(str) {
var vowels = [‘a’,’e’,’i’,’o’,’u’];
var three = str.substr(0,3);
var consonantNumber = 0;
var strArray = str.split(“”);
var first;
for (i=0;i<three.length;i++) {
if (vowels.indexOf(three[i]) >= 0) {
break;
}
else {
consonantNumber += 1 ;
}}
if (consonantNumber === 0) {
return str + “way”;
}
else {
first = strArray.splice(0,consonantNumber);
return strArray.join(“”) + first.join(“”) + “ay”;
}
}
translate(“glove”);

I then noticed I had lots of variables that were taking up way too much room. I realised that instead of splitting the string into an array and splicing,  I could use .replace() and substr() to make it simpler, using the consonantNumber variable I had produced in my for loop to declare the length of the substring. This reduced the code to 18 lines:

function translate(str) {
var vowels = [‘a’,’e’,’i’,’o’,’u’];
var consonantNumber = 0;
var three = str.substr(0,3);
for (i=0;i<three.length;i++) {
if (vowels.indexOf(three[i]) >= 0) {
break;
}
else {
consonantNumber += 1 ;
}}
if (consonantNumber === 0) {
return str + “way”;
}
else {
return str.replace(str.substr(0,consonantNumber), “”) + str.substr(0,consonantNumber) + “ay”;
}
}
translate(“glove”);

As you can see, the code only reduced in length slightly with each revision. However, by the last revision, I think it became clearer and much better in terms of using the right tools for the job.

So what did I learn?

  1. There are many ways of solving problems. Not all are created equal.
  2. Refactoring code can mean decreasing size, but that’s not the main reason to do it.
  3. Refactoring code is a process by which you make your code simpler to read, using the most that the language you are using and your knowledge base have to offer.
  4. Refactoring forces you to think from different angles and makes you a better programmer. It makes you learn new methods that will help you with other, potentially more complex, problems in the future.
  5. Refactoring is a lot of fun!

Tomorrow I will continue on with the challenges, today I completed 3. If you have any feedback on my code, please get in touch!