Skip to content

Commit 3f97718

Browse files
committed
Extend safe navigation docs about long &. chains
1 parent e66a93a commit 3f97718

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

README.adoc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,32 +325,33 @@ foo&.bar
325325

326326
=== Safe navigation
327327

328-
Avoid chaining of `&.`. Replace with `.` and an explicit check.
328+
Avoid chains of `&.` longer than two. Replace with `.` and an explicit check.
329329
E.g. if users are guaranteed to have an address and addresses are guaranteed to have a zip code:
330330

331331
[source,ruby]
332332
----
333333
# bad
334-
user&.address&.zip
334+
user&.address&.zip&.upcase
335335
336336
# good
337-
user && user.address.zip
337+
user && user.address.zip.upcase
338338
----
339339

340340
If such a change introduces excessive conditional logic, consider other approaches, such as delegation:
341341
[source,ruby]
342342
----
343343
# bad
344-
user && user.address && user.address.zip
344+
user && user.address && user.address.zip && user.address.zip.upcase
345345
346346
# good
347347
class User
348348
def zip
349349
address&.zip
350350
end
351351
end
352-
user&.zip
352+
user&.zip&.upcase
353353
----
354+
354355
=== Spaces and Braces [[spaces-braces]]
355356

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

0 commit comments

Comments
 (0)