Past Lesson Note

This note is from 2 months ago. You're viewing content from a previous lesson.

Daily Note for March 26, 2026 Past Lesson

You will learn about object oriented programming

You will have a quiz on this when I get back: 

 

  1. Watch this: https://www.youtube.com/shorts/V49MJEhsz_I
  2. Watch this, and follow along: https://www.youtube.com/watch?v=ZDa-Z5JzLYM&list=PL-osiE80TeTsqhIuOqKhwlXsIBIdSeYtc&index=

The most important in object oriented programming:

The Single Responsibility Principle (SRP) states that every class, module, or function in a program should have one, and only one, reason to change. This principle aims to keep your code modular, cohesive, and easier to maintain by focusing each unit on a single well-defined purpose.

Think of it like building Lego: each brick serves a specific role, and combining them creates complex structures. Similarly, SRP encourages building your program from well-defined, specialized units that can be combined to achieve larger goals.

Benefits of SRP:

  • Increased maintainability: Smaller, focused units are easier to understand, test, and modify.
  • Reduced coupling: Classes with less functionality are less dependent on other parts of the code, making changes less ripple-effecty.
  • Improved reusability: Units with a singular purpose can be reused in different contexts more easily.

 

Examples:

  • Bad SRP: A Student class that handles registration, calculates grades, and sends notifications. This violates SRP because it has multiple reasons to change (e.g., registration process update, grading algorithm change, email format change).
  • Good SRP:
    • StudentRegistration class handles enrollment logic.
    • GradeCalculator class calculates and stores grades.
    • NotificationService sends notifications based on events (e.g., registration, grade updates).

Each unit in the "good SRP" example has a clear responsibility, making it easier to maintain and adapt changes without affecting other parts of the code.

Remember: SRP isn't about minimizing functionalities within a unit. It's about ensuring each unit has a focused purpose, even if it involves several related functionalities.

By adhering to SRP, you can build cleaner, more manageable, and ultimately more sustainable code in your OOP projects.