容çè
- Use a guard clause instead of wrapping the code inside a conditional expression.
ãã®ãã
以ä¸ã®ãããªãµã³ãã«ã³ã¼ããããã¨ããã
def foobar(foo = nil) if foo.nil? return 'foo' else return 'bar' end end puts foobar(ARGV[0])
ä¸å¿ã以ä¸ã®ããã«åãã
$ bundle exec ruby test.rb foo $ bundle exec ruby test.rb a bar
Rubocop ã®æã«æãã㨅
$ bundle exec rubocop test.rb Inspecting 1 file C Offenses: test.rb:2:3: C: Use a guard clause instead of wrapping the code inside a conditional expression. if foo.nil? ^^ test.rb:3:5: C: Redundant return detected. return 'foo' ^^^^^^ test.rb:5:5: C: Redundant return detected. return 'bar' ^^^^^^ 1 file inspected, 3 offenses detected
ç¡ææ²å½¹ã
ä¸æ¬
ä½ãåé¡ã
Use a guard clause instead of wrapping the code inside a conditional expression.
æ¡ä»¶åå²ã®ãã¹ããæ·±ããªãã®ã¯ããããããguard clause ãå©ç¨ãã¾ãããã¨ã®ãã¨ãå¾ã¯ return
ãåé·ã¨ã®ãã¨ã
ãªãã¡ã¯ã¿ãªã³ã°
åé ã®ã¦ã³ã³ã¼ãã¯ä»¥ä¸ã®ããã«ãªãã¡ã¯ã¿ãªã³ã°åºæ¥ãã
def foobar(foo = nil) return 'foo' if foo.nil? 'bar' end puts foobar(ARGV[0])
Rubocop ã以ä¸ã®ããã«ç¡äºéæ¾ã
$ bundle exec rubocop test.rb Inspecting 1 file . 1 file inspected, no offenses detected
ãããã¨ããããã¾ããï¼
- https://techracho.bpsinc.jp/hachi8833/2016_10_11/26950
- https://gist.github.com/hiroshiro/95632194ab2b408d34b7
- http://d.hatena.ne.jp/asakichy/20091211/1260492230
æ¡ä»¶åå²ã®ãã¹ãã¯è¦æ³¨æã§ãã