Skip to content

Variables -vs- Classes – OOP design

September 1, 2011

Sometimes, its hard to see the need classes when simple variables will do the trick.

I have had this problem in the past, many times. As I am solving some problem a simple variable had done the trick. The need to defined a Class with its additional *complexity seemed unnecessary.  My initial inclination is to solve the program as quickly as possible, and then more on to a new problem, with an eye on the end result, which is showing my customer what they were asking for.

This is shortsighted.

If the program I am writing is solving some real problem and offers benefit to my customer, it will evolve and morph as it is used.  The morphing process means the program will grow in complexity and get bigger.  New requirements will be asked for.  The simple variables I used in the initial design will grow into more simple variables that relate to the same thing (entity).  Before long I will have a bunch of related variables littered about in a method or likely a bunch of methods.

As the program gets more complex, it becomes harder to reverse engineer and introduce better design.  This is a compelling reason to NOT BE SHORTSIGHTED regarding the INITIAL design of your program.  Plan for your program to morph and for new requirements to be introduced.  Follow an OOD by using classes to represent your domain (problems you are solving). Even if the class initially seems a little sparse.

For example, if you program deals with people, rather than defining simple variables like this:

String firstname;

String lastname;

Define a class for the person, like this:

public class Person{

public String firstname;

public String lastname;

}

If your program deals with widgets, rather than defining  simple variables like this:

int widgetNum;

String widgetName;

Define a class for the widget, let this:

public class Widget{

public int widgetNum;

public String widgetName;

}

The class offers a natural way for you to organize related entity data and operations on that data (using a noun), like person or widget. The class is a wrapper around all things related to that entity.

The class is very SCALABLE, thats a fancy meaning ” it can grow as needed ” or ” it wont break down as you ask more of it “.

As you demand more things from your program, like a persons age, hire date and starting salary, you can simply add the data items (attributes – fields) to your existing class, like this:

public class Person{

public String firstname;

public String lastname;

public int salary;

public int age;

public int hire_date;

}

If you demand more things from your program, like widget price, type and classification, you can simply add the data items (attributes – fields) to your existing class, like this:

public class Widget{

public int widgetNum;

public String widgetName;

public double price;

public String type;

public String classification;

}

Classes will REDUCE COMPLEXITY in your program by ENCAPSULATING, a fancy word for ” putting together things that should be together ” or  ” bundling of data ” and increasing COHESION, a fancy word for  ” how well things relate and word together  “.

The program built with many classes is MUCH more prepared for the inevitable morphing and changing requirements.

 

Advertisements

From → Java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: