Click here to Skip to main content
15,881,413 members
Articles / Programming Languages / C#

OO and Design Principles to strive for

Rate me:
Please Sign up or sign in to vote.
3.23/5 (7 votes)
12 May 2009CPOL2 min read 21.2K   14   8
Design principles to strive for ....

Introduction

Often, while developing software, we can get carried away and "hack" away until we get it working. However, it's sometimes handy to have a guideline.

So here's my pin up list of design principles to strive toward. These are only guidelines, and should be treated as such. They are not rules to follow...

  • Hide details of the implementation: Code to contracts and not implementations. Contracts are either abstract classes or pure interfaces.
  • Keep coupling to a minimum: A class can depend on native types (basic ints, bools, etc.) but should only depend on a select few peer classes. If the dependencies are high, consider this a smell and refactor to reduce the dependency. This can include creating new specialized types that are composed of the dependencies, but again this must be managed correctly.
  • Maintain maximum cohesion: A class should focus on a single concept.
  • Favour composition over inheritance: There’s a few different ways to get objects to collaborate – inheritance, composition, subscription through event handling.
  • Adhere to the Open closed principle: An object should be open for extension (able to extend its behaviour), but closed for modification (while extending the behaviour, you should not modify its source code)
  • Follow LSP where possible. Liskov Substitution principle: objects that have references to base types should be able to use derived types without knowing the difference. The basic concept behind polymorphism. TIP: design objects around behaviour and not data.
  • Use Inversion of control / Dependency Inversion principle: A formal definition: High level modules should not depend upon low level modules. Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.
  • Employ the Interface Segregation principle: Objects should not be forced to depend on interfaces that they don't use.
  • Consider using the Law of Demeter: Only talk to your immediate friends, so the following would be considered a violation:
    • C#
      var payment= Client.Wallet.GetMoney(10.00); 
    • This will be an interesting one to enforce if you plan on sticking to Domain driven design principles which suggest that you should get to entities through their aggregates.
  • Encapsulate the variations: When encountering variants, encapsulate those bits that vary and make them first class citizens.
  • Consider using Factories for object creation.

The above are principles and guidelines and violating them is okay, but only if you understand why doing so is better than following them.

Hope this helps!

fuzzelogic Solutions.com
Read the blog

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) www.fuzzelogicSolutions.com
United Kingdom United Kingdom
You can read my blog at www.fuzzelogicSolutions.com/wordpress

Comments and Discussions

 
GeneralMy vote of 3 Pin
Wonde Tadesse2-Dec-10 15:22
professionalWonde Tadesse2-Dec-10 15:22 
GeneralMy vote of 1 Pin
Leyu14-May-09 4:13
Leyu14-May-09 4:13 
GeneralA good reminder Pin
Phil Martin13-May-09 3:52
professionalPhil Martin13-May-09 3:52 
GeneralMy vote of 5 Pin
Yuriy Levytskyy12-May-09 23:16
Yuriy Levytskyy12-May-09 23:16 
GeneralMy vote of 1 Pin
r.wcs12-May-09 22:25
r.wcs12-May-09 22:25 
GeneralRe: My vote of 1 Pin
Phil Martin13-May-09 3:49
professionalPhil Martin13-May-09 3:49 
GeneralKeep coupling to a minimum Pin
LotharLanger12-May-09 11:02
LotharLanger12-May-09 11:02 
GeneralRe: Keep coupling to a minimum Pin
Zakir Hoosen12-May-09 22:50
Zakir Hoosen12-May-09 22:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.