Skip to content

Latest commit

 

History

History
44 lines (31 loc) · 1.02 KB

Nil-Check.md

File metadata and controls

44 lines (31 loc) · 1.02 KB

Nil Check

Introduction

A Nil Check is a type check. Failures of Nil Check violate the "tell, don't ask" principle. Additionally to that, type checks often mask bigger problems in your source code like not using OOP and / or polymorphism when you should.

The Nil Check code smell is a case of Simulated Polymorphism.

Example

Given

class Klass
  def nil_checker(argument)
    if argument.nil?
      puts "argument is nil!"
    end
  end
end

Reek would emit the following warning:

test.rb -- 1 warning:
  [3]:Klass#nil_checker performs a nil-check. (NilCheck)

Current Support in Reek

Nil Check reports use of

  • .nil? method
  • == and === operators when checking vs. nil
  • case statements that use syntax like when nil
  • use of the safe navigation operator like foo&.bar

Configuration

Nil Check offers the Basic Smell Options.