Skip to content

Instantly share code, notes, and snippets.

@razorcd
Last active June 12, 2016 23:15
Show Gist options
  • Save razorcd/ccaf14eb319be6ba1e80b7605683b6e7 to your computer and use it in GitHub Desktop.
Save razorcd/ccaf14eb319be6ba1e80b7605683b6e7 to your computer and use it in GitHub Desktop.
SOLID Class Principles
- Single Responsability - classes and methods should handle/do only one thing. Methods max 5 lines long, classes max 100 lines long.
- Open Closed - classes should be open for extension and closed for change
- Liskov Substitution - Having a main class and a subclass. Anywhere we use the main we should get same result by using the subclass.
- Interface Segragation - no client should be forced to depend on methods it does not use. Maybe class does not do only one thing? Split the class in more classes (and maybe add a facade).
- Dependency Inversion - Dont tight couple the classes by creating one instance inside another class. Inject them as dependencies when calling the method outside the class.
Use `A.new(b: B.new(5))` instead of `A.new(5)` where `class A; def initialize(i); @b= B.new(i); end`
- Law of Demeter - don't chain methods that could be changed in future. (except when methods returns self)
- Don't repeat yourself - reuse code
- public - what?
- private - how?
- protected -
- abstract class - class that should not be instanciated. Mostly used as parent class or for static methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment