thoughtbot/guides

Ruby can look like natural English

toby-1-kenobi opened this issue · 1 comments

One of the neatest features of Ruby is that it so often comes out looking like natural English. But I think that following this Ruby style guide would inhibit this feature. I think that if the code reads like natural English and that English phrase or sentence actually describes what the code is doing, then this should take precedence over preferring one method over another or avoiding conditional modifiers.

for example this code reads naturally but wouldn't so much if it didn't use the conditional modifier:
redirect_to root_path unless current_user.can_create_report?

While it's good that code can be easily understood this way, I think it's important to remain congnizant that Ruby is not English, and shouldn't be written in a way that twists good programming practice just so we have a nice sounding sentence. If it happens, and it aids understandability, then all the better, but we shouldn't set out with "make it sound like English" as a goal, IMHO.

Your example is innocuous on its own, and even preferable if you're scripting something out, but the "surprise if" has a tendency to get overlooked, and using an unless is often less clear than using an if not. Personally, I would not prefer this in a larger system.