Solved

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

Posted on 2003-12-04
6
234 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
where is session ID cache stored 1 47
JAVA part two 5 57
Basic Java Case or If-Else statement... 3 43
nextBoolean(double p) for Random class 3 35
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now