Solved

what is better? one method to return values or many methods

Posted on 2003-12-04
6
235 Views
Last Modified: 2010-03-31
Hi.

I was wondering what is better to use,
i have a big project with lots of methods and values.

My program currently has method for returning each value, for example.

int a,b,c;

public int getA()
public int getB()
public int getC()

and i had an idea to make one method to return the value according to a mark.
for example.

public static final A = 0,B=1,C=2;

and in the program to have one method for all:
public int getValue(int field)
{
    int returnValue = 0;
    switch (field)
   {
     case A: returnValue = a;break;
     case B: returnValue = b;break;
     case C: returnValue = c;break;
   }
return returnValue;
}

same things for objects my class can return when the main class asks for it.

What is better with preformance, and is right to do?
0
Comment
Question by:cicurel
6 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9875069
In the example you've shown, the second method is probably more appropriate.  However, if all the values are the same type (int ?), why not store them in an array.  Then the "field" parameter effectively becomes an index.

It really depends on the specifics of your design.

Performance isn't going to be an issue (with the example you've shown).  Consider the maintainability of your code.  What if extra attributes are added (eg. D, E, F ...).  How will you cope with that?  An array could simply be resized with no other changes to the code (except maybe more constants ;-)).  You don't really want to add another load of accessor methods in that case :-)
0
 

Author Comment

by:cicurel
ID: 9875224
In general i have few classes extends the same base class

each one of them has lots of values to return when some of them shared in the base class.

and a main class that can ask for a value from each one of them, which can be of any type such as boolean, int, double, and objects.

i can make two or three methods for example:

public static int TOTAL_REPORT = 0, MONTH_REPORT = 1, A_VALUE, B_VALUE, C_VALUE;

private Object monthly, total;
private int a;
private double b;
private boolean c;
public object getObject(int field) {
  switch (field) {
     case MONTH_REPORT: return monthly;break;
     case TOTAL_REPORT: return total;break;
  }
}

public double getValue(int field)
{
  switch (field) {
     case A_VALUE: return a;break;
     case B_VALUE: return b;break;
    default return 0;
  }
}

public boolean getBoolean(int field)
{
  switch (field) {
     case C_VALUE: return c;break;
    default return false;
  }
}

my question is not based on if it can be done..
my question is based on the lack of programming experience i have, and if it is a good way to do it? is it "good programming"? is it customary to do it that way or another?
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9875322
This is quite difficult to answer given the information you've provided.

If you have a set of related information (eg. 12 values for monthly figures), then these should probably be returned as a block (eg. public double[] getMonthlyFigures()).  Where a value has a specific meaning, it should have it's own accessor.

The decision about which alternative to use is going to be implementation specific and I can't tell from the attribute names you have provided (a, b, c) whether these have any relationship to each other or not.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 9

Expert Comment

by:vzilka
ID: 9878148
If you are using remote objects (EJBs) it is definitely more useful to group them together and send one object.
Otherwise, although making method calls can be considered a "heavy" operation, you should expose set/get methods for each attribute, and not just expose a bulk of attributes.
0
 
LVL 3

Accepted Solution

by:
monkesdb earned 50 total points
ID: 9878902
in general i will create get and set methods for all my member variables

getObject is a little bit obscure, someone looking at that method would have to go look in a reference to understand how to get what they want from it.

in fact, you can make that a simple rule to follow, "if you need to look it up then you need to refactor" (basically, refactoring is "doing something about bad code")

but it can work both ways. in one case (like getObject) you'll need to split the method up into several methods, each with their own useful name, such as getMonthReport / getTotalReport (this is much easier to use, no need for a reference).
  in other cases you will want to recombine methods into one single method with an extra parameter. one example (this should never happen) would be getJanuaryReport, getFebruaryReport... you would just want getMonthReport(int Month).

If you are interested to learn about when to do what kind of refactor and many more simple yet effective ways to write better code i would recommend a book by Martin Fowler called "Refactoring".

Cheers

Hope That Helps
0
 

Author Comment

by:cicurel
ID: 9879013
Thank you all.

I will look for that book.

Thanks.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tomcat administrtor 12 46
tomcat startup error 5 59
eclipse buid path vs tomcat lib path 10 22
Convert from a json string array to a Java object 3 28
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question