Im not really convinced of this reuse by using traits , personally i think re-use was one of the things promissed by OO that never delivered ( especially cross team) and i would argue the re-use was higher in procedural programming. However we have gotten larger amount of reuse through standard libraries.

The reasons for this IMHO are

  • more psychological , not invented here syndrome.
  • The code is too broad and does not implement it the way i need it.

While traits are one approach to this the other is just to use small objects with tightly defined functional roles ( ie its more important what they do than what they are) . The data side modelling of objects based on what they are and the use of inheritance IMHO leads to compromise objects trying to server too many masters.

With simple objects I create complex ones according to the following rules

  • Inherit if it is of the same type and you expect significant reuse eg DrawColouredCircle from DrawCircle
  • If you expect singnificant reuse use an interface which adds a single property . Interfaces with more than 1 method/ property lead to messy code. class DrawColouredCircle : IRquiresColourProvider .
  • Create a new object which uses composition for the other objects .
  • Lastly use extention methods.

IMHO trying to get reuse into more complex objects is a waste of time since it will rarely match the way the caller wants it which means he may hack about with it etc this is made worse if you want stateless objects. Writing objects to be reusable is also time consuming. Best to just mark as sealed and create new objects and keep interfaces and especially extention methods down to a minimum it will make your code much easier to read and maintain.
By on 11/29/2009 6:09 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us
Built with WebSharper