Earlier quoted context omitted.
In team environments you typically sacrifice convenience for maintainability and while you may not have a problem with optional parenthesis, it invites ambiguity and a more junior programmer may make a mistake because of it so the idea is to close off that edge case completely and not have to worry about it. This is the same reason a lot of shops ban the terniary operator for anything more than the simplest of variab…
I agree with your general point, but I don't really see how omitting those parentheses invites ambiguity. def reply_to_post text end What could anyone imagine text to be in this context aside from an argument? I get that using parentheses is more familiar coming from languages with C syntax, but I see no other rationale. I've never worked with anyone who stumbled over this. It's always just: 'oh cool, I didn't realiz…
Github Ruby Styleguide
31–40 of 73 posts
Re: Github Ruby Styleguide
#32Re: Github Ruby Styleguide
#33Earlier quoted context omitted.
> first_user = User.find(4) && second_user = User.find(6) It looks like it would evaluate as: first_user = (User.find(4) and second_user = User.find(6)) That is to say, the assignment comes after the boolean operation, which is unexpected.
Well, I expect first_user = User.find(4) && second_user = User.find(6) to be literally equivalent to second_user = User.find(6) first_user = User.find(4) && second_user I honestly can't see why you might want it to mean something else... Edit: I see. if you do something like if (first_user = User.find(4) && second_user = User.find(6)) { .. } it might bite you. You might expect it to be equivalent to first_user = User…
Having an assignment that could be skipped by short-circuiting also seems like bad practice, but I realize it was designed to be a toy example
Re: Github Ruby Styleguide
#34I agree with almost everything except for using named groups over the $1-$9 variables in regular expression matching. This may be the case if there are many capture groups in the expression, but I would argue that /(? regexp)/ =~ string is a fair deal more difficult to read than simply /(regexp)/ =~ string Almost everybody familiar with PCRE will be familiar with the simpler form, and it's usually the case that the s…
The following are equivalent: if /(? regexp)/ =~ string puts meaningful_var end if /(regexp)/ =~ string puts $1 end This may not look like much, but bare in mind you may be using the current selection in several places, at which point you'll probably use the following for readability either way: meaningful_var = $1 In any case I think that sometimes the latter is preferable, that's why I'm not into these kind of blac…
if string =~ /First: (.*?) Last: (.*?)\s/
first_name = $1
last_name = $2
# etc...
end
looks far better to me than if string =~ /First: (?.*?) Last: (?.*?)\s/
# etc...
endRe: Github Ruby Styleguide
#35Re: Github Ruby Styleguide
#36Now-a-days we have computers which can show more than 80 characters at a time. "Keep lines fewer than 80 characters", Is this recommendation still valid?
Now get off my damned lawn.
Re: Github Ruby Styleguide
#37Earlier quoted context omitted.
To anyone who's wondering, it seems like 'and' and 'or' are the same as && and ||, but with lower precedence [0]. Does anyone have a good reason why they both exist? It's a pretty big gotcha. [0] http://stackoverflow.com/questions/1426826/difference-betwee...
It's an imported Perlism, back from the days of the old assert idiom `foo == bar or die`. If `foo == bar` evaluates to true, the `or` is short-circuited, else it calls the built-in `die` function which kills the process.
@current_user.logged_in? or redirect_to login_path
@current_user.can? :do_this or render :status => 403
shirt.blue? and return bar
put_suit_on or put_pants_onRe: Github Ruby Styleguide
#38Now-a-days we have computers which can show more than 80 characters at a time. "Keep lines fewer than 80 characters", Is this recommendation still valid?
There can also be a code win: when forced to deal with short lines, it's often easier to split a long line of code into two easier to understand lines than it is to split on an operator.
Sometimes a split line is a lot worse than a long line, which is why the rule shouldn't be applied religious -- you should only be called on it if you have many lines over the limit.
IMO, 80 is a little bit tight; I prefer 100. But I wouldn't bike shed it if there was a standard in place.
Re: Github Ruby Styleguide
#39I can't stress enough how important it is to _not_ use `and` and `or`. As a former die-hard fan of those operators, it bit me in the ass so hard when those didn't exactly work the way they were supposed to. I've been using && and || now for years and I haven't looked back. /PSA
"1 * 3 + 4 / 20 && foo" is unreadable. No one wants to memorize the order of operations for every language they use and have to figure it out in their head. Make life easier for the people who have to maintain that line of code you just wrote.
Re: Github Ruby Styleguide
#40Now-a-days we have computers which can show more than 80 characters at a time. "Keep lines fewer than 80 characters", Is this recommendation still valid?
Moreover if you need lines longer than 80 chars you're probably doing it the wrong way: long lines are difficult to understand