Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-12-04
6
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

688 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