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

Add guideline for using safe operator. #912

Merged
merged 7 commits into from
Dec 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,34 @@ foo&. bar
foo&.bar
----

=== Safe navigation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue regarding the section, Source code layout.
Maybe it would be a better fit for Misc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate more regarding this comment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not mistaken, presently this new guideline resides in the "Source code layout" section. This section is mostly all about whitespace, formatting, those things that don't change code semantics.

The new guideline suggests code changes that may, depending on constraints, change its behaviour.

Basing on this observation, I propose moving this new guideline to a different section. I don't have a better section in mind, so I suggested "Misc".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it


Avoid chaining of `&.`. Replace with `.` and an explicit check.
E.g. if users are guaranteed to have an address and addresses are guaranteed to have a zip code:

[source, ruby]
----
# bad
user&.address&.zip

#good
user && user.address.zip
----

If such a change introduces excessive conditional logic, consider other approaches, such as delegation:
[source, ruby]
----
#bad
user && user.address && user.address.zip

#good
class User
def zip
address&.zip
end
end
user&.zip
----
=== Spaces and Braces [[spaces-braces]]

No spaces after `(`, `[` or before `]`, `)`.
Expand Down