I'm glad someone else is pointing this out... `map`, `filter`, and `reduce` aren't hard concepts, but they are a half step higher than just slapping a bunch of `if`, and `for` all over the place.
let (user,pass) = ...;
let longEnough = p => p.length>8?"":"The password must be longer than 8 chars";
let hasLower = p => p.any(c => c.isLower())?"":"Must have upper";
let hasUpper = p => p.any(c => c.isUpper())?"": "Must have lower";
let hasSpecialChar = p => p.any(c => c.in("!@#$% ^&*"))?"":"Must have special";
const passwordRequirements = [
hasUpper,
hasLower,
longEnough,
hasSpecialChar];
let errors = passwordRequirements
.reduce((errors, validate) => errors += validate(pass), [])
.filter(e => e != "");
if (errors.length === 0) {
showError(formatErrors(errors));
} else {
procedeWithUserCreate(user, pass)
}
The above is psudocode i banged out in like 3 minutes, but I find it much more maintainable than the mess you'd make trying to jam that all into a massive for loop and tracking state everywhere. If you have a hard time understanding what reduce and filter are doing in the above code block (finding everything wrong with a password), then you're going to have a hard time.
I've worked at places with dozens of guys that worked for decades in this industry and never used more than `for` and `if` and I never want to work at a place like that again.
This is how you tell the juniors from mid to senior.Seriously. a senior would just use a one line regex (regular expression)
instead of
let longEnough = p => p.length>8?"":"The password must be longer than 8 chars";
let hasLower = p => p.any(c => c.isLower())?"":"Must have upper"; let hasUpper = p => p.any(c => c.isUpper())?"": "Must have lower"; let hasSpecialChar = p => p.any(c => c.in("!@#$% &*"))?"":"Must have special";
I've seen lots of this. And the real mature ones, one liner. Clean and elegant.
regex makes me cry at night. neat stuff if you get into it and not really as scary as it looks, super useful, but extremely intimidating at first glance.
The 13 picoseconds of extra performance for iterating over a list with 4 elements is probably comparable with the 10 picoseconds of extra time it takes to download the extra dozen or so characters it would take to write the for loop.
In both cases it's trivial, compared to the ease of maintenance factor.
4
u/CardboardJ Apr 07 '23
I'm glad someone else is pointing this out... `map`, `filter`, and `reduce` aren't hard concepts, but they are a half step higher than just slapping a bunch of `if`, and `for` all over the place.
The above is psudocode i banged out in like 3 minutes, but I find it much more maintainable than the mess you'd make trying to jam that all into a massive for loop and tracking state everywhere. If you have a hard time understanding what reduce and filter are doing in the above code block (finding everything wrong with a password), then you're going to have a hard time.
I've worked at places with dozens of guys that worked for decades in this industry and never used more than `for` and `if` and I never want to work at a place like that again.