File tree Expand file tree Collapse file tree 1 file changed +7
-5
lines changed Expand file tree Collapse file tree 1 file changed +7
-5
lines changed Original file line number Diff line number Diff line change @@ -325,32 +325,34 @@ foo&.bar
325
325
326
326
=== Safe navigation
327
327
328
- Avoid chaining of `&.`. Replace with `.` and an explicit check.
328
+ Avoid long chains of `&.`. The longer the chain is, the harder it becomes to track what
329
+ on it could be returning a `nil`. Replace with `.` and an explicit check.
329
330
E.g. if users are guaranteed to have an address and addresses are guaranteed to have a zip code:
330
331
331
332
[source,ruby]
332
333
----
333
334
# bad
334
- user&.address&.zip
335
+ user&.address&.zip&.upcase
335
336
336
337
# good
337
- user && user.address.zip
338
+ user && user.address.zip.upcase
338
339
----
339
340
340
341
If such a change introduces excessive conditional logic, consider other approaches, such as delegation:
341
342
[source,ruby]
342
343
----
343
344
# bad
344
- user && user.address && user.address.zip
345
+ user && user.address && user.address.zip && user.address.zip.upcase
345
346
346
347
# good
347
348
class User
348
349
def zip
349
350
address&.zip
350
351
end
351
352
end
352
- user&.zip
353
+ user&.zip&.upcase
353
354
----
355
+
354
356
=== Spaces and Braces [[spaces-braces]]
355
357
356
358
No spaces after `(`, `[` or before `]`, `)`.
You can’t perform that action at this time.
0 commit comments