Gotta admit, that does indeed look a hundred times more readable (assuming you're familiar with regular expressions).
I would just like to chime in on this line of code in the final example:
const domain = hostnameAvailable ? window.location.hostname : false;
I think it would be better if the fallback value was null instead of false. It doesn't change anything about the program, but semantically, null makes more sense because the variable domain expects a string value. If we were to stick with using false, then I don't think it makes sense that a domain name is a Boolean value. I think it would make more sense to say that it is null, or rather nonexistent.
But then again, I might just be nitpicking here. Nonetheless, it still is a hundred times more readable than the initial code.
I like your argument and I agree with you. Thank you for the feed back btw!
No problem, man! Great article. It made me think of how I could refactor my code in a similar way.
I’m glad to hear! I will add more to it tomorrow as I wasn’t able to list all my tricks. But this should be a good start. Thank you!
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.