Hey there FreeCodeCamp
I’ve been working through the new curriculum for just over 6 weeks now and have so far completed Responsive Web Development and JS Data Structures and Algorithms certifications.
Now that I’ve got a pretty decent amount of code that I’ve built up through these projects, I’ve started thinking about what it would take to ‘go pro’
What are some commonly accepted standards for ‘professional quality code’?
I tend to write pseudocode comments as I work to help me structure my thoughts and as a result, often end up with some pretty lengthy code.
Example: my solution for the Roman Numeral Converter algorithm:
function convertToRoman(num) {
/*
Converts int to roman numerals.
** Only able to handle ints 1 - 399,999 **
*/
// handle errors
if (num >= 400000 || num < 1) {
console.error("convertToRoman is only able to handle integers between 1 and 400000, you passed" + str(num));
return false;
} else if ( num % 1 !== 0) {
console.error("convertToRoman is only able to handle whole numbers, you passed" + str(num));
return false;
}
let numeralsKey = [
["I", "V"], // Ones place: 1:"I", 5:"V"
["X", "L"], // Tens place 10:"X", 50:"L"
["C", "D"], // Hundreds place: 100 : "C", 500 : "D"
["M", "V\u0305"], // Thousands place: 1000: "M", 5000: "V\u0305" == V̅
["X\u0305", "L\u0305"], // Ten Thousands place: 10000: "X\u0305", 50000: "L\u0305"
["C\u0305"] // Hundred Thousands place: 100000: "C\u0305"
];
// convert number to an array of single digits, representing each "place" value
let places = num.toString().split('').reverse(); // array of single digits, reversed so that ones place comes first rather than last
// for each place value, convert to correct roman numerals
places.forEach( function (item, index) {
let x = Number(item);
let placeNumerals = '';
if (x == 0) {
// 0 = nothing; empty string
places[index] = placeNumerals + '';
} else if (x > 0 && x <=3) {
// 1, 2, 3 = "1 value" for the given place, repeated up to 3 times
for ( let j = 0; j < x; j++) {
placeNumerals = placeNumerals + numeralsKey[index][0];
}
places[index] = placeNumerals;
} else if (x == 4) {
// 4: "1 value" then "5 value" for given place. Ex: 4 == "IV"
places[index] = placeNumerals + numeralsKey[index][0] + numeralsKey[index][1];
} else if (x == 5) {
// 5: "5 value" for given place.
places[index] = placeNumerals + numeralsKey[index][1];
} else if (x > 5 && x <= 8) {
// 6, 7, 8: "5 value" for the given place, followed by "1 value" repeated up to 3 times.
placeNumerals = placeNumerals + numeralsKey[index][1];
let remainder = x % 5; // how many "1 value" chars are needed?
for ( let j = 0; j < remainder; j++) {
placeNumerals = placeNumerals + numeralsKey[index][0];
}
places[index] = placeNumerals;
} else if (x == 9) {
// 9: "1 value" for the givem place followed by "1 value" for the NEXT place up
places[index] = placeNumerals + numeralsKey[index][0] + numeralsKey[index + 1][0];
}
});
// now that all the places have been converted to roman numeral equivalents, revert the order back to original order
places.reverse();
// combine placeNumerals for each place into one big string and return
return places.join("");
}
Now as you can see - this thing is a whopper. 70 + lines of code (with lots of whitespace + comments thrown in) .
I’ve seen other solutions on the web that are barely 70 characters:
function romanize(num) {
var lookup = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1},
roman = '',
i;
for ( i in lookup ) {
while ( num >= lookup[i] ) {
roman += i;
num -= lookup[i];
}
}
return roman;
}
(Ok maybe not 70 chars, but you know what I mean).
Comparing the two, I can tell right off the bat that the “shorter” solution is:
- Obviously, shorter
- Simpler, in some ways - based on many repetitions of a simple loop, subtracting from a value and adding to a string, vs mine which decomposes the string, creates and concatenates arrays, etc.
- More efficient? (not sure)
- Maybe fancier?
My question is - if my solution looked more like the other one, would that be better? Would either solution be preferable in a professional development context ?
Bubbling up to first principles - beyond things like:
- Code has to do what it’s supposed to do reliably, and be reasonably resilient
- DRY - do not repeat yourself
- Functional programming - creating functions that explicitly state external dependencies, and don’t mutate anything else in the program
What makes good, professional code?