Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the syntax of some if blocks to one line #5976

Closed
wants to merge 1 commit into from

Conversation

j8r
Copy link
Contributor

@j8r j8r commented Apr 20, 2018

Change some if blocks to one-liners and other minor syntax improvements.

@j8r j8r force-pushed the if_cleanups branch 2 times, most recently from d38a765 to 010e524 Compare April 20, 2018 23:31
@j8r j8r changed the title Improve the syntax of some if blocks to one line WIP: Improve the syntax of some if blocks to one line Apr 21, 2018
@j8r j8r force-pushed the if_cleanups branch 2 times, most recently from af30edf to 4504b67 Compare April 21, 2018 15:05
Change some if blocks to one-liners and other minor syntax improvements
@straight-shoota
Copy link
Member

If these edits are really an improvement is highly questionable. Some perhaps, others probably not.

Many changes are purely about style and most of the trailing ifs don't improve readability at all. Neither are early returns always useful.
I don't think it makes any sense to merge this until there is a proper style guide.

@j8r j8r changed the title WIP: Improve the syntax of some if blocks to one line Improve the syntax of some if blocks to one line Apr 21, 2018
@j8r
Copy link
Contributor Author

j8r commented Apr 21, 2018

We don't have to keep all this modiifcations, but some obsiously improve the readability, like replacing

if true
  this
else
  that
end

by

return this if true
that

and

if true
  this
end

by

this if true

@straight-shoota
Copy link
Member

No, I don't think these replacements are particularly improving readability. In some places, maybe. But not in general.

@Sija
Copy link
Contributor

Sija commented Apr 21, 2018

Like @straight-shoota said, many even exceed standard line width of 80 characters, which highly reduces code readability.

@j8r
Copy link
Contributor Author

j8r commented Apr 21, 2018

80 characters like PEP8?

@j8r
Copy link
Contributor Author

j8r commented Apr 21, 2018

Can we consider at least to transform little conditions to one-liners? This is of course not possible when we do variable affectations inside the condition. Maybe it is possible to do it with the formatter.

@asterite
Copy link
Member

Sorry, but I'm not sure about this. Depending on the case I prefer and if in one line, the body in another line. In fact many times I find the suffix if harder to read. So this PR hinders readability for me.

Let's not discuss this kind of code style. If it compiles and the formatter accepts it, it's fine.

@j8r
Copy link
Contributor Author

j8r commented Apr 21, 2018

Ok LGTM I will look at the formatter to improve it. By the way I think there should have a rule for this.

@j8r j8r closed this Apr 21, 2018
@asterite
Copy link
Member

Please let's not add a width limit to the formatter. It's not useful to me. Monitors are bigger now.

@Sija
Copy link
Contributor

Sija commented Apr 21, 2018

@asterite It's not just about the monitors, our brains are not rly suited for reading such long lines either (at least mine 😉).

@asterite
Copy link
Member

If it's a long line, put a line break where you think it's convenient. The problem is that a formatter puts the end of line where it can, not where it's nice. I'm against that and I won't change my mind, sorry. gofmt doesn't do it either.

@Sija
Copy link
Contributor

Sija commented Apr 21, 2018

I don't think it's a formatter's job either, yet keeping 80 character line width as reference point is imho a good thing to have.

@RX14
Copy link
Contributor

RX14 commented Apr 22, 2018

@Sija but 80 characters is a guideline. I super often break past 80 characters. We're not going to implement any kind of length metrics in the formatter, that's something for humans to think about.

@j8r
Copy link
Contributor Author

j8r commented Apr 22, 2018

Without having the 80 characters limit globally, I was just underlining the fact that some conditions can be better written.
Long lines are a thing, but long chaining conditions are sometimes quite hard to read, or the contrary lots of little conditions blocks inside each others.
A formatter that can smartly change the condition line may be good. I don't know, maybe splitting to multi-lines according && and || when we have more than 3 of them, or put it in one line when there is a single condition.

@j8r
Copy link
Contributor Author

j8r commented Apr 23, 2018

The only way to know this is to experiment with the formatter to see if the result is nice to be kept or not 😄
I will play with lt when some other work will be finished.

I exagerate a bit, but looking a this make me sad. I can't believe there is nothing to do to beautify this:

if true
 if true
   if true
     puts "hello"
   end
  end
end

@Sija
Copy link
Contributor

Sija commented Apr 23, 2018

@RX14 that's what I meant writing about a reference point, and so I'd leave it as is (a guideline).

@hugoabonizio
Copy link
Contributor

@j8r Instead of a formatter's job, it'd fit better on an Ameba rule 😁

@j8r j8r deleted the if_cleanups branch October 16, 2018 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants