7 ms·
I 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 "The
by moonchrome 4y ago
I 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.
- nonrandomstring 4y agoIn 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.
- ilyt 4y agoif !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 !
- otherflavors 4y agothere is no else unless (or even elsunless, since this isn't javascript) in Perl, might have been unless (predicate) { do_this; } else { do_the_impossible unless impossible_thing; }
- sshine 4y agoTo be fair, Ruby inherited "unless" from Perl. And Perl literally has the opposite design principle, TIMTOWTDI or "there is more than one way to do it". I'm not arguing here -- I prefer the "one obvious way" principle. But you can design a beautiful programming language without regard for the long-term learnings of software engineering. I always liked Perl's "unless", and I always made sure to not abuse it with double negatives or other contorted conditions that are, in themselves, reasonable. I also promised myself I wouldn't write very big programs in Perl.
- aspyct 4y agoRuby is basically Perl++
- lloeki 4y ago> Everyone has an individual background. Someone may come from Python, someone else may come from Perl, and they may be surprised by different aspects of the language. Then they come up to me and say, 'I was surprised by this feature of the language, so Ruby violates the principle of least surprise.' Wait. Wait. The principle of least surprise is not for you only. The principle of least surprise means principle of least my surprise. And it means the principle of least surprise after you learn Ruby very well. For example, I was a C++ programmer before I started designing Ruby. I programmed in C++ exclusively for two or three years. And after two years of C++ programming, it still surprises me. - Matz, c.a 2003
- TurboHaskal 4y agoI came to Ruby from Perl, and I love `unless`. One of the first things I do in any Lisp is build my own unless macro unless the dialect already has it :)
- nolls 4y ago
- gpvos 4y agoI only know `unless` from Perl, and it can make your code clearer if used sparingly. For example it allows you to express preconditions positively: die "You may only use port numbers 1024 and higher" unless $port >= 1024;
- soheil 4y agoIt's not pointless, it makes code read better. It's another option to have. It's not meant for every use case. It indicates you don't understand Ruby if you find yourself using an "unless" in a complex boolean operation. It's meant for simple cases like an inline return statement return unless User.exists(id=100)
- moonchrome 4y agoGod that's such a landmine when reading code. Seeing a return without an explicit change of scope... Why ? To save one line ? Yep that's why I hate ruby - worked on one mature codebase for a year and after seeing various such gems used across the project - from >10 devs - I'm confident I will never touch the language again.
- soheil 4y agoIf 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
- moonchrome 4y agoI 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 - plenty of alternatives these days.
- codesnik 4y agoIt doesn't create no scope. Even for variables.
- multiplegeorges 4y ago
- peoplefromibiza 4y ago> It's the exact opposite of pythons "There should be one– and preferably only one –obvious way to do it" unless python.major_version > 1 other zen of python's rules that did not live up the expectations - beautiful is better than ugly => tell that to numpy or pandas - readability counts => (¬з¬) - if the implementation is hard to explain, it's a bad idea => then the World is full of very bad ideas
- vidarh 4y agoAnd I love it for exactly this kind of design. It's what makes well-written Ruby read well. And while it's one more way of making badly-written Ruby read badly, in my opinion at least with Ruby you have the option of writing code that reads well. I've yet to see Python code that looks readable to me.
- drstewart 4y agoWow, I didn't realize Python did this. So, for example, does it enforce functions with single returns or ones with multiple return sites? Also, does it prefer shallowly nested functions with guard clauses or highly nested if / else clauses? Really curious to see the one and only one obvious way these are handled.
- aqme28 4y agoI love Ruby for this kind of stuff. You don't have to have arguments about which syntax you prefer, you can just use the one you prefer and let your coworkers use theirs. However, a lot of developers love very strict style guidelines. I've never understood why, but it's a discussion I get into very very often.
- soiler 4y agoHm, I have the exact opposite interpretation. This is exactly the kind of feature that leads to arguments. If `unless` didn't exist, what argument could people be having about using `if !`? In a shared codebase, I think there should be as few (equally effective) ways to express an idea as possible. (Obviously there can be more- and less-efficient ways to write a function; I'm referring only to style.) The more arbitrary choices people have, the more time is wasted on choosing one. If you're coding alone, that's fine - you just pick what you like, and spend as long as you like making that decision. But in a shared codebase, developers writing in different styles is a net negative on quality & readability. Spending time debating which style to prefer is a worthwhile, but unnecessary time sink. Automatically enforcing a pre-defined style is efficient and effective - that's why Prettier exists, is widely used, and has few configuration options.
- aqme28 4y ago> In a shared codebase, I think there should be as few (equally effective) ways to express an idea as possible...The more arbitrary choices people have, the more time is wasted on choosing one. I highly disagree! In my experience, forcing arbitrary style leads to so much more time wasted as people debate exactly which styles to enforce. > But in a shared codebase, developers writing in different styles is a net negative on quality & readability. Spending time debating which style to prefer is a worthwhile, but unnecessary time sink I disagree with this too. I have never found this type of style to be something that actually impacts readability and quality. If you using `if !` and I using `unless` is the problem with the codebase, that's not so bad. I have to say, this is why I'm a huge fan of Python's black. It lets me write however I want, while you get to read my code in a consistent style.
- Draiken 4y agoPeople can prefer the "one obvious way" and you can use a language that has that as its motto. Ruby was built with "developer happiness" as an objective and I don't see it changing any time soon. I'll never understand these sorts of articles because they criticize the subject for not being what they explicitly aren't. "I hate spoons because they can't cut like knives"
- fl_ciq 4y agoPython is a communist apartment block, brutal in design and conformity and not very efficient.
- perlgeek 4y agoY'know, I've written a lot of python these past years, and python is just full of aliases and different ways to do things. Just an example: "if x is not None:" is syntactic sugar for "if not x is None:". String formatting: there are f-strings, % replacement, .format, probably more methods. And that's fine, languages evolve over time, add better ways to do things, and don't deprecate the old way. It's the hypocrisy of flaunting that motto while the language is so full of the exact opposite that always annoys me.
- chillfox 4y agoThe problem is that what is obvious depends upon your way of thinking. To me very little in Python makes sense, but I understand that it does to someone and that’s cool. There’s no reason to hate on a language just because it doesn’t click with your way of thinking.