tags:

views:

111

answers:

2

In my design I am using objects that evaluate a data record. The constructor is called with the data record and type of evaluation as parameters and then the constructor calls all of the object's code necessary to evaluate the record. This includes using the type of evaluation to find additional parameter-like data in a text file.

There are in the neighborhood of 250 unique evaluation types that use the same or similar code and unique parameters coming from the text file.

Some of these evaluations use different code so I benefit a lot from this model because I can use inheritance and polymorphism.

Once the object is created there isn't any need to execute additional code on the object (at least for now) and it is used more like a struct; its kept on a list and 3 properties are used later.

I think this design is the easiest to understand, code, and read.

A logical alternative I guess would be using functions that return score structs, but you can't inherit from methods so it would make it kind of sloppy imo.

I am using vb.net and these classes will be used in an asp.net web app as well as in a distributed app.

thanks for your input

+2  A: 

In general, putting code that does anything significant in the constructor a not such a good idea, because you'll eventually get hamstrung on the rigid constructor execution order when you subclass.

Constructors are best used for getting your object to a consistent state. "Real" work is best handled in instance methods. With the work implemented as a method, you gain:

  • separation of what you want to evaluate from when you want to evaluate it.
  • polymorphism (if using virtual methods)
  • the option to split up the work into logical pieces, implementing each piece as a concrete template method. These template methods can be overridden in subclasses, which provides for "do it mostly like my superclass, but do this bit differently".

In short, I'd use methods to implement the main computation. If you're concerned that an object will be created without it's evaluation method being called, you can use a factory to create the objects, which calls the evaluate method after construction. You get the safety of constructors, with the execution order flexibility of methods.

mdma
Yeah what I am actually doing is pretty similar to the template method you are talking about I think, most of the actual evaluation is inside methods. Thanks
Curtix
+2  A: 

Executing code in a constructor is OK; but having only properties with no methods might be a violation of the tell don't ask principle: perhaps instead those properties should be private, and the code which uses ("asks") those properties should become methods of the class (which you can invoke or "tell").

ChrisW
i think it is sad that there is a gross misspelling in the TITLE(!!!) of the article you're linking to.
Jason
@Jason - Do you know any good/better article exlaining what "tell don't ask" means?
ChrisW
didn't say anything about the quality of the content. just, cmon, at least spell your title correctly!
Jason