Best Practices - Programming->Classes/Functions
If we are not using a language, which doesnít support anything but objects what should be our choice in building frequently used functionality. What will be the design approach, class based or function based? Big questions, isnít it? Letís find out if we can put some light on it.
First letís find out what is a class that best fits in lazy programmerís dictionary:
As they all say it should know how to act and what to act upon. In formal terms a set of functionality that has itís own set of data.
Now letís revise what a function is:
A function is a rule or correspondence which associates to each number x in a set A a unique number f(x) in a set B (courtesy: http://archives.math.utk.edu/) (I donít know mathematics, hence Ö).
In designing classes we should always remember the rule that a class has itís own set of data. If we can isolate a class as a closed system in itís own, probably the problem fits in a class model. If we are using a rule to evaluate a value or of specific functionality, which acts upon an external data and derives a resultant, probably it fits in a function model. Letís figure it out in a real life situation.
A problem set forth, in this context, is to find out domain name from an email id called email@example.com :-). Where should it be placed, in a class or a function?
Letís roll the ball with a class. The class should contain, at least
1. A private member to hold the address
2. A public method to set the address
3. A public method to return the domain
In this (poorly designed) class, I have intentionally left out methods, which will validate the id.
Using the functionality requires a few steps more:
1. Create an object of the class
2. Set the ID
3. Get the domain name
These are an approximate estimation of the number of steps involved in building and using a class.
Getting into the function way, how to do it?
To carve the functionality:
1. Create a function, which will return domain from an id, supplied as a parameter
To use it:
1. Call the function, supplying the same with an email id
Which one would you prefer is your choice, but if asked to me, I would suggest latter. I feel itís a lot more easy and flexible way of doing the job.
Sure we should, but before doing, we must ask ourselves twice, are we modeling an object really? If yes, do it. If no, never go for it.
When we have really found an object, simple. Letís work out an example to get ourselves comfortable with. Letís take a game by which we play car racing. In this big system of ours we have a small object called car (Small? I donít know really, I donít have one).
The car class has a set of data, which are:
(and a few hundred thousand more if we really require)
and it has got certain functionality, like;
1. Start the engine
4. Change gear
5. Put lights on
(and a few million more)
Letís ask ourselves the few questions:
1. Does the car has itís own set of data?
2. Is the functionality dependant on the internal data?
3. Is it an object which has itís own attributes and functionality?
The brainstorming points the model to be an object, hence we should go for a class to design it.
After we design a class, it should always be revised for get set methods. If we are using too many of them the class should either be broken down into smaller components or it should better fit in a function model.
To wrap up, if we find something, which is more of a methodology than an object, it should be built as a function. If itís an object with different kinds of functionality having itís own attributes, it should be an object.
<< Best Practices - Programming | Best Practices - Programming->Building Prototypes >>
Re: Best Practices - Programming->Classes/Functions
|All times are GMT +5.5. The time now is 03:25.|