Best Practices - Programming->Classes/Functions

Discussion in 'Programming' started by rahulnaskar, Nov 1, 2006.

  1. rahulnaskar

    rahulnaskar New Member

    Aug 26, 2004
    Likes Received:
    Trophy Points:

    Classes and 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: (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 :). 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.

    Shouldn’t we use classes then?

    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 should we do it then?

    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:
    1. Colour
    2. Model
    3. Trim
    4. Vin
    (and a few hundred thousand more if we really require)

    and it has got certain functionality, like;
    1. Start the engine
    2. Accelerate
    3. Brake
    4. Change gear
    5. Put lights on
    (and a few million more)

    Should all of it go in different functions?

    Nop, never.

    Why not?

    Let’s ask ourselves the few questions:
    1. Does the car has it’s own set of data?
    Ans: Yes
    2. Is the functionality dependant on the internal data?
    Ans: Yes
    3. Is it an object which has it’s own attributes and functionality?
    Ans: Yes

    The brainstorming points the model to be an object, hence we should go for a class to design it.

    Classes need introspection

    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 >>
    shabbir likes this.
  2. mschouhdry

    mschouhdry New Member

    Jun 19, 2008
    Likes Received:
    Trophy Points:
    nice sharing

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice