Read this post ‘unless’ you’re not a Ruby developer
111–120 of 326 posts
Re: Read this post ‘unless’ you’re not a Ruby developer
#112Earlier quoted context omitted.
If you're returning who cares about the scope? Are you saying visually you'd like to have indentation inside the if body? If that's the case there is also nothing stopping you in most languages from doing something as nasty as: a=1;b=2;c=3;d=a+b==c?4:5;return d
I do because return means end of scope. Except in your example it hides the fact that it's creating it's own scope. I'm not arguing other languages can't produce bad code, just that ruby is particularly suited for it especially as number of developers working on code increases. I've seen people propose a linter to enforce consistency - but at that point I might as well chose a language with better design choices - pl…
Re: Read this post ‘unless’ you’re not a Ruby developer
#113and why does Rubocop’s Ruby Style Guide matter?
Re: Read this post ‘unless’ you’re not a Ruby developer
#114Earlier quoted context omitted.
If you're returning who cares about the scope? Are you saying visually you'd like to have indentation inside the if body? If that's the case there is also nothing stopping you in most languages from doing something as nasty as: a=1;b=2;c=3;d=a+b==c?4:5;return d
I do because return means end of scope. Except in your example it hides the fact that it's creating it's own scope. I'm not arguing other languages can't produce bad code, just that ruby is particularly suited for it especially as number of developers working on code increases. I've seen people propose a linter to enforce consistency - but at that point I might as well chose a language with better design choices - pl…
You're seriously proposing that adding a linter has the same organizational costs as changing languages entirely. Meanwhile, back in the real world...
Ruby has major advantages over many other languages and a linter is basic tooling you should have in every development environment.
Re: Read this post ‘unless’ you’re not a Ruby developer
#115In Common Lisp, there is `unless' and `when' but they don't allow `else' branch. (when CONDITION BODY) (unless CONDITION BODY) The value of the statement is BODY or NIL if it wasn't executed. I can't remember a time when it wasn't clear (though you can always build usage that are unclear).
I think they mainly exist because `if' otherwise requires a `progn' if you want more than one expression in the true branch.
(cond ((massivep building)
(print-mass-warning building)
(install-antigrav-module building)))Re: Read this post ‘unless’ you’re not a Ruby developer
#116statement unless condition is a wonderful bit of syntactic sugar. It reads like natural English if the variable or method it is evaluating is named well. Yes, sometimes you have to refactor into a traditional if. Sometimes you make the conditional its own method (which you’d end up doing anyway, assuming it didn’t stop at two or three conditions.) These are light tasks as the project grows. Starting a line with unles…
That's a problem for those who learn it as a second language and have to translate "unless" to "if not" every single time.
Re: Read this post ‘unless’ you’re not a Ruby developer
#117 if cond
a = 1
end
leaks a to outside and a will be nil if condition does not happenRe: Read this post ‘unless’ you’re not a Ruby developer
#118I don't have a problem with double negation but I hate ruby for this kind of design - pointless aliases for everything. It's the exact opposite of pythons "There should be one– and preferably only one –obvious way to do it" - they intentionally create solutions that have zero practical benefit - it's just fuels arguments based on preferences and introduces mental overhead due to inconsistency.
In Perl I once saw someone write; unless predicate do this else unless impossible_thing do the impossible What it did at runtime I have no idea, but it broke my brain for the rest of the week.
if !cond1
do this
elsif !cond2
do that
?it's literally just `!` equivalent. Honestly I haven't seen much code using unless in wild. Especially that it is literally shorter to just !
Re: Read this post ‘unless’ you’re not a Ruby developer
#119 def my_method(required_thing)
raise 'required_thing is required' unless required_thing.present?
...
end
In practice, this hasn't been as issue as a consequence of the tooling and ecosystem around Ruby.Re: Read this post ‘unless’ you’re not a Ruby developer
#120I take issue with the word unless. "Un" means opposite and "Less" means, well less, so unless means more. So `unless` should just be a straight alias for `if`.