3 ms·
... or, instead of (ab)using a ternary, the same idea in CoffeeScript: return if not username error: "No Username Given.", field: 'name' else if
by jashkenas 15y ago
... or, instead of (ab)using a ternary, the same idea in CoffeeScript:
return if not username
error: "No Username Given.", field: 'name'
else if not password
error: "No Password Given.", field: 'pass'
else if username.length < 3
error: "Username is less than 3 Characters.", field: 'name'
else if password.length < 4
error: "Password is less than 4 Characters.", field: 'pass'
else if not (/^([a-z0-9-_]+)$/i).test(username)
error: "Username contains invalid characters.", field: 'name'
else
false
Hopefully a bit more readable. Link to see the compiled JS output: http://goo.gl/QhR9c http://goo.gl/QhR9c
- swannodette 15y agoThe whole point of the OP was revealing the structure of the data being matched (like a routing file). This code completely obscures that. And I'm not sure this is more readable as the number of conditions grow.
- jashkenas 15y agoOne programmer's readability is another programmer's "[_ (_ :guard #(< % 3)) _ _]" ;)
- swannodette 15y agoOne programmer's readability is another programmer's o 'PARAM_START ParamList PARAM_END FuncGlyph Block', -> new Code $2, $5, $4 ;)
- mattdeboard 15y agoI agree with swannodette that the CoffeeScript version is not better than the JS implementation but for a different reason. In my experience code brittleness is directly correlated to the number of "else if" conditions in a single block.