Solved

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

Posted on 2003-12-04
6
233 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Expert Comment

by:vzilka
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you all.

I will look for that book.

Thanks.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
factorial example challenge 10 61
mapBully challenge 6 88
Strange loading of website behaviour 3 23
mockito example issue 8 29
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

772 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

9 Experts available now in Live!

Get 1:1 Help Now