Class: RuboCop::Cop::SketchupPerformance::TypeCheck

Inherits:
SketchUp::Cop show all
Includes:
RangeHelp
Defined in:
lib/rubocop/sketchup/cop/performance/type_check.rb

Overview

String comparisons for type checks are very slow.

‘entity.class.name == ’Sketchup::Face’‘ is slow because it performs a string comparison. `is_a?` is much faster because it’s a simple type check.

If you know you need a strict type check, compare the classes directly: ‘entity.class == Sketchup::Face`.

Examples:

Poor performance

entity.class.name == 'Sketchup::Face'

Good performance

entity.class == Sketchup::Face

Good performance and idiomatic Ruby convention

entity.is_a?(Sketchup::Face)

Constant Summary collapse

MSG =
'String comparisons are very slow, prefer `.is_a?` '\
'instead.'

Constants inherited from SketchUp::Cop

SketchUp::Cop::SKETCHUP_DEPARTMENT_SEVERITY

Constants included from SketchUp::Config

SketchUp::Config::DEFAULT_CONFIGURATION

Instance Method Summary collapse

Methods inherited from SketchUp::Cop

inherited, #relevant_file?

Instance Method Details

#on_send(node) ⇒ Object



44
45
46
47
48
# File 'lib/rubocop/sketchup/cop/performance/type_check.rb', line 44

def on_send(node)
  return unless string_class_compare?(node)

  add_offense(node, location: comparison_range(node))
end