File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -323,6 +323,34 @@ foo&. bar
323323foo&.bar
324324----
325325
326+ === Safe navigation
327+
328+ Avoid chaining of `&.`. Replace with `.` and an explicit check.
329+ E.g. if users are guaranteed to have an address and addresses are guaranteed to have a zip code:
330+
331+ [source, ruby]
332+ ----
333+ # bad
334+ user&.address&.zip
335+
336+ #good
337+ user && user.address.zip
338+ ----
339+
340+ If such a change introduces excessive conditional logic, consider other approaches, such as delegation:
341+ [source, ruby]
342+ ----
343+ #bad
344+ user && user.address && user.address.zip
345+
346+ #good
347+ class User
348+ def zip
349+ address&.zip
350+ end
351+ end
352+ user&.zip
353+ ----
326354=== Spaces and Braces [[spaces-braces]]
327355
328356No spaces after `(`, `[` or before `]`, `)`.
You can’t perform that action at this time.
0 commit comments