Let's see if I can write something, that others understand with no context.
//For this example let's ignore the nuances of names.
function (nameUserinput) {
if(isValidName(nameUserinput)) {
return processName(nameUserinput))
} else {
return { first: '', middle: '', last: '' }
}
}
function isValidName () {
return (containsAtLeastOneSpace(nameUserinput) &&
containsLessThanThreeSpaces(nameUserinput) &&
hasThreeOrMoreCharacters(nameUserinput) &&
noSpacesOnEnd(nameUserinput))
}
function processName (nameUserinput) {
const nameObj = { middle: '' };
const nameUserinputArray = nameUserinput.split(' ');
if(nameUserinputArray.length = 2) {
[nameObj.first,
nameObj.last] = nameUserinputArray;
} else if(nameUserinputArray.length = 3) {
[nameObj.first,
nameObj.middle,
nameObj.last] = nameUserinputArray;
}
}
nameObj and nameUserinput and nameUserinputArray are all the same data in different forms. I think it's helpful to start them with the same prefixes, but change the suffix with the type to communicate that.You could imagine taking this a step further, with nameStatemachine. You might chart a person's course through life, do they go to law school, medical school, get married etc.
I've been meaning to write a blog post on this. But this does follow my most important words first rule. You'll see a lot of similarities there, just not on this exact topic.