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

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?
cicurelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimmackCommented:
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
cicurelAuthor Commented:
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
jimmackCommented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

vzilkaCommented:
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
monkesdbCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cicurelAuthor Commented:
Thank you all.

I will look for that book.

Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.