Wednesday, February 22, 2012

Choosing good abstractions

Coupling and cohesion

The efficiency of the programs you design depends on how well you choose the classes they use.

Well-abstracted classes lead to programs that are easy to maintain and extend. However, if you choose bad abstractions, you could end up with an error-prone application that doesn't model the problem domain effectively.
Simplicity is the key to choosing good classes.

The classes you choose should capture only one key abstraction. If a class performs two functions, then you should split it into two separate classes, each with its own function.
There are five key concepts you should consider when choosing classes:
  • coupling
  • cohesion
  • sufficiency
  • completeness
  • primitiveness
These allow you to measure how well-designed classes are.
Coupling refers to how tightly connected classes are. If a class is very dependent on other classes to carry out its functions, then it is strongly coupled.
Strongly coupled classes can lead to problems in program design. Because such classes are interdependent, they are difficult to extend, and bugs in one can affect others.

This interdependency can also make it difficult for other programmers to understand programs that contain such classes.
To perform adequately, classes must be connected to other classes. But you should choose a level of connection that doesn't make one class too dependent on another.

This is particularly important when you design for inheritance.
Though inheritance is a form of coupling, it can make programs easier to design and extend.

So when choosing classes, you need to balance the requirements of inheritance against the need for weak coupling.
Cohesion is a measure of how closely related the elements in a class are. These elements are the states, behaviors, and functionalities of the class.
A class whose members are simply grouped together and have little in common is only coincidentally cohesive. Such a class might have two unlinked functions, or it may group object states with unrelated object behavior.

Because its elements do not cohere to realize a single purpose, such a class is confusing, difficult to use, and often too complex to implement.
Let's say you used a class named BankEmployee to model how bank staff are paid and how they interact with customers.

Since the elements of these separate functionalities are unrelated, this class would be a bad abstraction.
Classes with good cohesion are well defined and contain elements that properly belong together.

Such classes are easy to understand and use because they serve a definite purpose – they have functional cohesion.

Sufficiency, completeness, and primitiveness
Sufficiency, completeness, and primitiveness are related principles.

A good class needs to balance the requirements of each of these principles before it can function well.
  • Sufficiency
  • Completeness
  • Primitiveness
Sufficiency is the characteristic that ensures classes are able to function effectively.

You must create classes that contain all the attributes and operations necessary to perform the tasks you require of them. So if you create a Car class, you must include an accelerate operation – otherwise no car object will be able to change speed.

Sufficiency means that you should include only those attributes and operations that are needed for the system you are modeling.

If you want to create a class that models a typical car, you need to specify things like its speed and model.

But you don't need to specify what type of suspension the car uses – this would complicate the class without adding any important functionality to it.

Whether a class is sufficient or not depends on its purpose and on the level of detail it is intended to capture.

If you need to represent something in great detail, then you might have to include specifications that would not be needed in a more abstract model.
Completeness determines whether the interface of the class captures all the behavior of the class.

The attributes and operations you give to a class must be general enough to allow other classes to communicate with it.
Primitiveness means that you shouldn't make a class unnecessarily complicated. It is a principle that lets you control a large spectrum of behavior with just a few operations.

For any class, you should choose operations and attributes that are very basic.

To create more complex class states, you can simply change the values of the attributes you have chosen.

Or you can combine simple operations to create complex ones.

Let's say that in the Car class, you want to model the way gear changes work.

A vertical rectangle with three compartments depicts the Car class with the class name, Car, in the first compartment. The second compartment contains the attributes which are: engineSize, color, model, maxSpeed, currentSpeed. The third compartment contains the operations which are: accelerate(), brake(), turn(), getSpeed(), shiftUp(), shiftDown(), shiftUpTwoGears().

You will need to create a shiftUp operation, but you won't need a shiftUpTwoGears operation. You can simply use the more primitive shiftUp operation twice.

Primitiveness is very closely related to completeness.

To make a class complete, you need to account for all its behavior. But by defining this with primitive functions, you ensure that you don't have to list every possible permutation of a class's behavior.

The level of primitiveness you choose depends on how you want your program to perform.

If you need a program to run very fast, it may be better to use a complex function once rather than using a primitive one several times.


Classes should capture only one key abstraction. Classes that depend on other classes to function are strongly coupled. You should choose classes with weak coupling, because this makes programs easier to extend, maintain, and debug. Classes should also be functionally cohesive.

To be sufficient, a class must have enough attributes and operations to capture all the relevant states and behavior of its objects. You should make the set of these properties sufficiently complete to allow the purpose of the class to be easily understood, but you should also try to keep the class as simple as possible. You can do this by observing the principle of primitiveness.


Truely a very good article on how to handle the future technology. After reading your post,thanks for taking the time to discuss this, I feel happy about and I love learning more about this topic. keep sharing your information regularly for my future reference. This content creates a new hope and inspiration with in me. Thanks for sharing article like this. The way you have stated everything above is quite awesome. Keep blogging like this. Thanks.
Hadoop training in chennai

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

ccna training in chennai Tnagar

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Java Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai

Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing

dot net training in chennai

php training in chennai

java training in chennai

for preparing bank exam and group exam , we are providing an online test model questions papers

Bank Exam Questions and Answers

Group Exam Questions and Answers

Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

iOS Training in Chennai

Very nice post here and thanks for latest smartphone applications it .I always like and such a super colors of phone for these post.Excellent and very cool idea and great models and different kinds of the more information's.

digital marketing course in chennai

Thanks for this blog. provided great information. All the details are explained clearly with the great explanation. Thanks for this wonderful blog. Step by step processes execution are given clearly.Know the details about different thing.
Web Development Company in India

Thank you for sharing such a nice and interesting blog with us. I have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information. I would like to suggest your blog in my dude circle.
SEO Company in Chennai

Learning new technolgy would help oneself at hard part of their career. And staying updated is the only way to survive in current position. Your content tells the same. Thanks for sharing this information in here. Keep blogging like this. Android Training in Chennai

Finding the time and actual effort to create a superb article like this is great thing. I’ll learn many new stuff right here! Good luck for the next post buddy..
Sharepoint Training in Chennai

These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful and knowledgeable information.
Android App Development Company

great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
web design Company
web development Company
web design Company in chennai
web development Company in chennai
web design Company in India
web development Company in India

Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
Mobile App development Company
Ios App development Company

Abstractions would mean a lot of use to the new era java programmers because they help in simplifying coding. In fact, despite the high skills needed for the applications, their usefulness still stands out in this era. When you ask the java experts, you will be told that the Java is creating an excellent development environment. Hire trained Law School Admission Essay Editors online.

Great article. After reading this article i learnt something new information about hadoop from this article..

big data training in chennai | best hadoop training in chennai

cI read the post and I have really enjoyed your blogs posts.looking for the next post.
Digital Marketing Training In Bangalore.

This is extremely helpful info!! Very good work. Everything is very interesting to learn and easy to understood.
Hadoop Training in Chennai

I appreciate your style of writing because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also informative. I hope to read more and more interesting articles from your blog. All the best.
Psoriasis Treatment

this is very nice blog this studying course information very useful to everyone who have learning this information.this education information is very helpful to start my carrier with technology.

Hadoop Training in Chennai

Just read your website. Good one. I liked it. Keep going. you are a best writer your site is very useful and informative thanks for sharing!

Herbal Shampoo
Dandruff Treatment
Antifungal Cream
Vitiligo Medicines

I must thank you for the efforts you have put in spending this site. I am hoping to out the same high-grade content by you later on as well. In truth, your creative writing abilities has inspired me to get my own, personal blog now..
Java Training in Chennai
Dot Net Training in Chennai
Cloud Computing Training in Chennai
Digital Marketing Training in Chennai
SAS Training in Chennai
SEO Training in Chennai
AWS Training in Chennai
Microsoft Azure Training in Chennai

its really very useful topic. it should be help us more and then thanks to post article like this.
Eczema Treatment
Psoriasis Oil
Hyperpigmentation Treatment

Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
Android Training in Chennai
Ios Training in Chennai

Your blog is very unique and interesting. It makes reader to come back and visit again.

Selenium Training in Chennai

• Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updatingAzure Online Training

This is very nice post thanks for updating this information and Thanks for your sharing with us.
Web design company in chennai

Thank you for sharing such a nice and interesting blog with us.
SAP SD Training in Chennai

I got lot of information from your blog.And thanks for sharing!!!!
SAP scm Training in Chennai

This is really amazing information. I like to read this information. Thanks for sharing this wonderful information.
Web development company in bangalore
Outsource magento ecommerce services india
ECommerce Website developers in bangalore

very informative blog and very useful for readers. making us to learn more from your blog.keep on updating your blog

VMware Training in Chennai

Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. posts. I hope you post again soon.

Selenium Training in Chennai is an online platform that
allows people to find offers and the greatest
deals in and around OMR. It is the directory
of the business in and around omr. It helps
internet users to surf the required deals and

Deals in OMR

It is really very excellent,I find all articles was amazing.Awesome way to get exert tips from everyone,not only i like that post all peoples like that post.Because of all given information was wonderful and it's very helpful for me.
SAP Training in Chennai
SAP ABAP Training in Chennai
SAP FICO Training in Chennai
SAP MM Training in Chennai

Very nice post here thanks for it I always like and search such topics and everything connected to them.Excellent and very cool idea and the subject at the top of magnificence and I am happy to comment on this topic through which we address the idea of positive.


Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

Wellnesscentres in Chennai
Weightloss in Chennai
Weightgain in Chennai

Nice information.. Thanks for sharing. Addmen group is one of the best omr software provide in India and abroad also. If you want any kind of omr software India you just have to visit our website and see the different omr software.
omr software

Post a Comment