Skip to content

Instantly share code, notes, and snippets.

@bolshakov
Last active July 3, 2023 14:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bolshakov/2240761b9280110bb2da to your computer and use it in GitHub Desktop.
Save bolshakov/2240761b9280110bb2da to your computer and use it in GitHub Desktop.
SOLID Code Smells

Single Responsibility

A class should have one, and only one, reason to change.

  • Large Class

    • You can't easily describe what the class does in one sentence.
    • You can't tell what the class does without scrolling.
    • The class needs to change for more than one reason.
    • The class has more than seven methods.
    • The class has a total flog score of 50.
  • Divergent Change

    • You can't easily describe what the class does in one sentence.
    • The class is changed more frequently than other classes in the application.
    • Different changes to the class aren't related to each other
  • Duplicated Code

    • You find yourself copying and pasting code from one place to another.
    • Shotgun surgery occurs when changes to your application require the same small edits in multiple places

Open/closed principle

You should be able to extend a classes behavior, without modifying it.

  • Large Class

    • You can't easily describe what the class does in one sentence.
    • You can't tell what the class does without scrolling.
    • The class needs to change for more than one reason.
    • The class has more than seven methods.
    • The class has a total flog score of 50.
  • Divergent Change

    • You can't easily describe what the class does in one sentence.
    • The class is changed more frequently than other classes in the application.
    • Different changes to the class aren't related to each other

Liskov substitution principle

Derived classes must be substitutable for their base classes.

  • Explicit checking for type
    • is_a?
    • kind_of?
    • case
    • respond_to?

Interface segregation principle

Make fine grained interfaces that are client specific.

  • ActiveRecord methods outside of ActiveRecord models.

Dependency inversion principle

Depend on abstractions (module or duck type), not on concretions (class).

  • depend on things that change less often than you do
  • Explicit checking for type
    • is_a?
    • kind_of?
    • case
    • respond_to?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment