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

Pedantic & controversial: rewrite/remove explicit nils #5857

Conversation

ysbaddaden
Copy link
Contributor

Crystal's language is based on Ruby's language, which itself reflects of the Japanese language. In Japanese the context is often not spoken out because it would be redundant (if not rude) to specify it. It's only ever specified to avoid a misunderstanding —but sometimes left confusing, for example in poetry.

Implicitness is part of Ruby's beauty. You can call something instead of self.something() for example, avoiding unrequited noise. Unless an expression evaluates to something it will evaluate to nil or a nilable. An empty method body? it returns nil. An if expression with no else branch? it evaluates to a nilable. A return expression with no value? it returns nil.

Let's fully embrace implicitness once and for all and clean the compiler/stdlib code base.

Lets `if` and `case` expressions implicitly return `nil`, and let
them implicitly return their value unless they're deeply nested and
explicit returns do improve readability.

Methods always return `nil` when their body is empty, let's leverage
this behavior.

Stops returning a `nil` when the method signature returns `Nil` and
marks methods as return `Nil` instead of returning a `nil`.

Keeps an explicit `nil` after some loops (e.g. `Enumerable#each`)
which are supposed to return `nil` but may have been overwritten.

Rewrites some `foo = bar; if foo` statements as a single expression.
@RX14
Copy link
Contributor

RX14 commented Mar 23, 2018

I'm strongly against this. Looking at the diff, it makes the code more confusing for little benefit. Code isn't poetry, it's meant to display clear and unambiguous meaning. Sometimes (all the time in this case), longer code is faster and easier to read.

Does pointless bikeshedding about style get us closer to 1.0? No, let's drop it.

@srcrip
Copy link

srcrip commented Mar 23, 2018

Sometimes (all the time in this case), longer code is faster and easier to read.

Isn't this a slippery slope? You could easily say the same about implicit return values, which is I think something most people would probably say they love about Ruby...

@ysbaddaden
Copy link
Contributor Author

If we can't agree on a style guide, we'll never reach 1.0.

@straight-shoota
Copy link
Member

straight-shoota commented Mar 24, 2018

Implicitness is great and helps writing code that is simple and pretty. But sometimes it is better to be explicit about some things. For example, return nil is equivalent to return without a value. Yet I'd rather use the former in most cases to clearly signal that this is a case where it explicitly returns nil, and return to exit from a method where return type doesn't matter/exists (this is obviously an opinionated example to illustrate there can be slight differences in semantic meanings).

In my opinion this PR adds improvements in some places but not in all and we need to look at the individual subjects to determine how it should be handled.

This PR shows examples of code that could be improved and it can be used to discuss these examples. But instead of just applying the results in code, it is essential to codify them in a style guide for reference and further applications. That's what we should focus on.

@RX14
Copy link
Contributor

RX14 commented Mar 24, 2018

@ysbaddaden we've managed up to now perfectly fine with a hodgepodge of styles. How is that changing?

@j8r
Copy link
Contributor

j8r commented Mar 24, 2018

When I see an alone return in one line, there is absolutely no ambiguity about that is returns nothing, so a nil – because there is nothing after the return keyword.

For now, do we agree at least to have a formatter rule to transform return nil to return? Or if most disagree, the contrary.

We should a least have an unified style on this.

👍: I'm for return everywhere
👎: I'm for return nil everywhere

@RX14
Copy link
Contributor

RX14 commented Mar 24, 2018

No, you use return nil when you're returning nil, and use return when you're returning void. The formatter cannot perform transformations between those two because it cannot know the return type of the function.

@straight-shoota
Copy link
Member

@j8r debating over return nil vs return is totally out of scope here. I explicitly stated that this is opinionated - as are most of these questions. I considered adding a note about not discussing this but I thought it was clear that this is just an unrelated example.

@j8r
Copy link
Contributor

j8r commented Mar 26, 2018

@straight-shoota this PR is about nils. As is, it will not likely be merged because it bring lots of debatable changes. I try to expose one point, debate and be agree on in order to move forward on the subject at least.

I think ensuring a consistent style backed by the (opinionated of course) formatter is important, either for newcomers or old contributors.

To be back on return vs return nil, for me they are the same and do the same. We must decide between them. Having to know when we should use one or the other depending on "void" concepts like @RX14 said is confusing. So we have randomly return or return nil.

Let's have one way to do the same thing for this once and for all - not that complicated, is it?

@asterite
Copy link
Member

I don't mind return or return nil. Both do the same and we allow both. Otherwise, let's just make return nil a syntax error.

I also don't mind a few explicit nil here and there, as the rule for an implicit else branch is nil isn't that obvious (there's nothing to look at).

So my vote is 👎 for this (it makes things consistent, but not necessarily easier to read or understand).

@ysbaddaden ysbaddaden closed this Apr 28, 2018
@ysbaddaden ysbaddaden deleted the pedantic-drop-explicit-nils-as-noise branch June 27, 2018 08:08
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