riegercodes
riegercodes

100 Days of Code - Day 18 Object Oriented Programming

Rick Rieger's photo
Rick Rieger
·Mar 10, 2022·

4 min read

100 Days of Code - Day 18 Object Oriented Programming

Hello! My name is Rick and I am in search for gig as a developer :)

This past December I finished up a year long study with a full-stack school in NYC called [Codeimmersives] (codeimmersives.com). We explored HTML, CSS, JavaScript, and how to build applications using the MERN stack. I learned about class-based components, passing props around (prop-drilling) and utilizing the component lifecycle(componentWillMount, componentDidMount, etc). After learning class based components, we moved on to functional based components using hooks, custom hooks, and different ways to manage state, such as the useContext and Redux APIs. (This is just a brief overview of the technologies I studied over the past year). Currently I am enrolled at devCodeCamp learning Python and solving problems. I am documenting my journey by sharing these blog posts. Please help me out by dropping a comment below on what you think!

PermalinkSOLID

SOLID is an acronym for the first five object-oriented design (OOD) principles by Robert C. Martin (also known as Uncle Bob).

These principles can apply to various programming languages and were established as a guide for programmers to follow in order to avoid code smells, refactoring code, and supports Agile and Adaptive software development.

SOLID stands for:

  • S - Single-responsiblity Principle
  • O - Open-closed Principle
  • L - Liskov Substitution Principle
  • I - Interface Segregation Principle
  • D - Dependency Inversion Principle

PermalinkSingle Responsibility Principle

A class, function, etc should only have ONE responsibility. It is important to follow this principle because quite often as an application grows, the code will need to be updated or changed. Following this principle also makes the code easier to read by someone else.

Below is an example of bad practice. Notice the function has multiple responsibilities.

Screen Shot 2022-03-08 at 9.55.25 PM.jpg

Instead, each function has ONE responsibility

Screen Shot 2022-03-08 at 9.56.58 PM.jpg

Screen Shot 2022-03-08 at 9.57.14 PM.jpg

Screen Shot 2022-03-08 at 9.59.26 PM.jpg

Screen Shot 2022-03-08 at 9.58.57 PM.jpg

PermalinkOpen-closed Principle

Screen Shot 2022-03-09 at 8.53.54 AM.jpg

PermalinkWhy is open-close important?

  • When you modify a class or method, there may be unexpected outcomes and bugs.
  • If you work for a larger company, they may have features set in place that have been throughly tested.
  • When you add an extension there is confidence that the existing code will be safe!

PermalinkLiskov Substitution Principle

  • Ability to replace any instance of a parent class with an instance of one of its child classes without negative side effects. If the child class does not have the same variables and methods as the parent then the principal is not adhered to and the code will have bugs. However, you can make a new class that is a child and add functionality (modify)

Screen Shot 2022-03-09 at 8.28.32 PM.jpg

PermalinkInterface Segregation Principle

  • Interfaces should be granularly split and be as small as possible.
  • Code should not depend on methods it doesn't use.

Check out the code below. We have a parent Vehicle class and because a motorcycle class can not use auto drive, it breaks the principal. Methods can not be unused that are inherited from parent classes.

class Vehicle():
  def accelerate(self):
    raise NotImplementedError

  def brake(self):
    raise NotImplementedError

  def impliment_auto_drive(self):
    raise NotImplementedError


class TeslaSelfDriving(Vehicle):
  def accelerate(self):
    print('implimentation to accelerate') 

  def brake(self):
    print('implimentation to brake')

  def impliment_auto_drive(self):
    print('implimentation to auto drive')


class MotorCycle(self):
  def accelerate(self):
    print('implimentation to accelerate') 

  def brake(self):
    print('implimentation to brake')

  def impliment_auto_drive(self):
    raise NotImplementedError

solution below where interfaces are split

from abc import ABC, abstractclassmethod

class Accelerate(ABC):
  @abstractclassmethod
  def accelerate(self):
    pass
class Brake(ABC):
  @abstractclassmethod
  def brake(self):
    pass
class AutoDrive(ABC):
  @abstractclassmethod
  def impliment_auto_drive(self):
    pass


class TeslaSelfDriving(Accelerate, Brake, AutoDrive):
  def accelerate(self):
    print('implimentation to accelerate') 

  def brake(self):
    print('implimentation to brake')

  def impliment_auto_drive(self):
    print('implimentation to auto drive')


class MotorCycle(Accelerate, Brake):
  def accelerate(self):
    print('implimentation to accelerate') 

  def brake(self):
    print('implimentation to brake')

PermalinkDependency Inversion Principle

  • Entities must depend on abstractions, not on concretions. It states that the high-level module must not depend on the low-level module(implementations), but they should depend on abstractions.

Example:

You are a CEO of Coka-Cola with many responsibilities

You wouldn't:

drive a truck

do tax stuff

package bottles

etc..

You would only manage

More to follow on Dependency Inversion Principle with examples.

PythonSOLID principles
 
Share this

Article Series

100 days of code

1

100 Days of Coding Day 1

Hello, my name is Rick and I am in search for gig as a developer :) This past December, I finished u…

100 Days of Coding Day 1
2

100 Days of Coding Day 2

Hello, my name is Rick and I am in search for gig as a developer :) If you are curios as to what led…

100 Days of Coding Day 2
4

100 Days of Coding Day 4

Hello, my name is Rick and I am in search for gig as a developer :). I am familiar with the MERN sta…

100 Days of Coding Day 4
5

100 Days of Coding Day 5 - DS&A with Colt Steele

My resources for this blog post is Colt Steel's course on Udemy, which can be found here. I'm essen…

100 Days of Coding Day 5 -  DS&A with Colt Steele
6

100 Days of Coding Day 6 - Ticket Tracker

I had a terrible time sleeping last night. I was on the couch until 530am, on and off the computer…

100 Days of Coding Day 6 - Ticket Tracker
7

100 Days of Code day 7

I decided to start my day at the computer tackling these error messages I'm receiving from React. …

100 Days of Code day 7
8

100 Days of code Day 8

Codewars 1st challenge Exclamation marks series #1: Remove an exclamation mark from the end of strin…

100 Days of code Day 8
9

100 Days of Code - Day 9 Classes in Java Script

Hello, if you are catching this for the first time, my name is Rick and I'm writing every day for 10…

10

100 Days of Code day 10 Linked List Data Structure

Hello, my name is Rick and I am in search for gig as a developer :) This past December, I finished u…

100 Days of Code day 10 Linked List Data Structure
11

Python is cool.

Actually, I am a 'noobish' kind of guy and just wanted to have a catchy title :) I started learning…

Python is cool.
12

100 Days of Code day 12 - more Python

My name is Rick and I am in search for gig as a developer :) This past December I finished up a year…

13

100 Days of Code Day 13

Hello! My name is Rick and I am in search for gig as a developer :) This past December I finished up…

14

100 Days of Code Day 14 - MORE PYTHON! :)

Hello! My name is Rick and I am in search for gig as a developer :) This past December I finished up…

15

100 Days of Code Day 15 - Python

Hello! My name is Rick and I am in search for gig as a developer :) This past December I finished up…

16

100 Days of Code Day 16 - Problem Solving

Hello! My name is Rick and I am in search for gig as a developer :) This past December I finished up…

100 Days of Code Day 16 - Problem Solving
17

100 Days of Code Day - 17 More Algo's please

Hello! My name is Rick and I am in search for gig as a developer :) This past December I finished up…

100 Days of Code Day - 17 More Algo's please
18

100 Days of Code - Day 18 Object Oriented Programming

Hello! My name is Rick and I am in search for gig as a developer :) This past December I finished up…

100 Days of Code - Day 18 Object Oriented Programming
19

100 Days of Code - Day 19 - First Backend API project using Django

(set up on mac) Like always with any new project create a new repository in GitHub Choose the option…

20

100 Days of Code - Day 20 - Higher-Order Functions in Java Script

Hello! My name is Rick and I am currently enrolled in a coding bootcamp. This past December I finish…

21

100 Days of Code - Day 21 - twoSum LeetCode

Hello! It's been a while since I've posted any articles/blogs, since I've been busy finishing up a …

100 Days of Code - Day 21 - twoSum LeetCode

©2025 riegercodes

Archive·Privacy policy·Terms

Powered by Hashnode - Build your developer hub.

Start your blogCreate docs

Screen Shot 2022-03-08 at 9.56.58 PM.jpg
Screen Shot 2022-03-09 at 8.53.54 AM.jpg
Screen Shot 2022-03-08 at 9.57.14 PM.jpg
Screen Shot 2022-03-08 at 9.58.57 PM.jpg
Screen Shot 2022-03-08 at 9.59.26 PM.jpg
Screen Shot 2022-03-08 at 9.55.25 PM.jpg
Screen Shot 2022-03-09 at 8.28.32 PM.jpg

Follow riegercodes' journey

By following, you'll have instant access to our new posts in your feed.

Continue with GoogleMore options