Skip to content

arasateser/Coding-Principles

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Software Design Principles in C#

###Overview This repository explains key software design principles with real-life examples in C#.


SOLID Principles

Single Responsibility Principle (SRP)
A class should have only one reason to change. Example: A paymentProcessor class should only process payments, while a separate transactionLogger class handles just the transaction logs.

Open/Closed Principle (OCP)
Classes should be open for extension but closed for modification. Example: Use interfaces or abstract classes to allow new functionality without modifying existing code.

Liskov Substitution Principle (LSP)
Subtypes should be replaceable for their base types without breaking code. Example: A TroyPayment and CreditCardPayment should work interchangeably when using a PaymentMethod base class.

Interface Segregation Principle (ISP)
Clients should not be forced to depend on interfaces they do not use.
Example: Instead of one ITrancaction interface with payment(), refund() split it into smaller interfaces like IPaymentMethod, IIRefundable.

Dependency Inversion Principle (DIP)
High-level modules should not depend on low-level modules. Both should depend on abstractions. Example: Use dependency injection instead of creating instances inside a class.


DRY (Don't Repeat Yourself)

Avoid duplicating code by using functions, classes, or modules. Example: Instead of duplicating validation logic, create a reusable CalculatePrice(int days, deciaml dailtyRate) method.


KISS (Keep It Simple, Stupid)

Write code that is simple, clear, and easy to understand.
Example: Prefer Dictionary<string, decimal> over switch cases.


YAGNI (You Ain’t Gonna Need It)

Don't add features until you actually need them. Example: Don't add values to the function unlesss you have a use for them in it.


Law of Demeter (LoD)

"Don't talk to strangers" – A class should only interact with its direct dependencies. Example: A Cashier should call Barista.MakeCoffee(), not Barista.CoffeeMachine.BrewCoffee().


Composition Over Inheritance

Favor using object composition instead of deep inheritance trees. Example: A Car has an Engine object instead of extending a Vehicle class.

About

Overview This repository explains key software design principles with real-life examples in C#

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages