Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java: Cohesion


Each function should do only one thing. This should be done so that the conceptual unit can be understood easily. Sometimes it will be convenient to do two things in a function because you're working on the same data.

Try to resist this temptation.
Make two functions if you reasonably can.

For example, if you write a function to read a file of numbers into a vector (eg, readVector(v)), that's all the function should do.

 

If you know that you also need the sum of the numbers, Here are three ways to solve the problem of reading a vector and summing the elements.

  • Criminally bad. As elements as read it, it's probably most efficient to add them to a global variable. The call would then look like
    // CRIMINALLY BAD. Not obvious that sum is in global variable. readVector(v); > Not only does this function do more than one thing, but the name and call give no clue that it is doing more. The hidden reference to a global variable also increases the coupling (connections between parts of a program), which is bad.
  • Bad. To avoid global variables, which are almost always bad, return the sum as the value of the function.
    // BAD.  Function does two things, and one is unobvious.
    x = readVector(v);
    This is bad because it isn't obvious from that name that a value is returned or what that value might be. It does reduce the coupling and at least it is obvious in this use that it does return a (unknown) value.
  • Poor. A better alternative is
    // POOR.  Understandable, but not very reusable.
    x = readVectorAndComputeSum(v);  
    This clarifies the processing, but doesn't produce simple, reusable building blocks.
  • Good. A better way to do it is two write two functions.
    // GOOD.  The two actions are separated and given good names.
    readVector(v);
    x = sumVector(v);
    This is better because each function does one clear thing (ie, is cohesive). These function building blocks are also much more likely to be reusable. But what about the efficiency of this solution. Isn't the extra function call less efficient? Yes, but the amount is so small that it isn't worth more than a few nanoseconds. If this was in an extremely high performance loop (eg, rendering real-time graphics), then it might be worth thinking about optimizing. What you should optimize for is the readability of programs -- programmer (ie, your) time is worth much more than a few machine cycles. And the extra time you take to type in an extra function is easily repaid in having a simpler program to work on.
Ask programming questions?

 

 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]