3

I am doing a project in Java which has a lot of methods that require multiple return objects. For this I have to keep creating private classes which encapsulate the return objects. The objects make sense as a FontResult in my code will return the font name and the font size for example but constantly creating new objects for each return type I require feels wrong and somehow like I am trying to circumvent how Java should be written. Is this wrong or is it fine to do it like this? Should I be structuring my code in more of a tell-dont-ask approach?

An example is as follows:

String test = "hello";
StringResult result = getInformation(test);
int length = result.length;

private StringResult getInformation(String test) {
  int length = test.length();
  char firstChar = text.charAt(0);
}

private class StringResult {
  int length;
  char firstChar;

  StringResult(int length, char firstChar) {
    this.length = length;
    this.firstChar = firstChar;
  }
} 
kabeersvohra
  • 1,049
  • 1
  • 14
  • 31
  • Is your question about the number of private classes you're defining, or the number of _instances_ of those classes instantiated? – searlea Jul 19 '16 at 13:09
  • I often create private classes which I only instantiate once for returning from a specific method – kabeersvohra Jul 19 '16 at 13:10
  • 1
    This is a very general question. Could you provide a concrete example (code) of what you're wondering about? – Ray Jul 19 '16 at 13:14
  • Splitting the question in two: *is creating lots of classes bad style?* Possibly; it might indicate an overly-complicated approach; it's hard to say without a more concrete example. *should you make nested classes private?* If nothing outside your class needs to know about them, absolutely: minimize visibility. – Andy Turner Jul 19 '16 at 13:14
  • It was supposed to be general, the code I am writing is for a company and I cannot release it unfortunately – kabeersvohra Jul 19 '16 at 13:14
  • Can you do a similar example? Surely you can create something that won't tell us any secrets about your company, right? Talking about a general case is always hard. A certain type of program might make it even necessary to commit a lot of exceptions from your own coding style. That said, remember why you do things in some way. Is it readable and thus maintainable? Is if efficient, in speed and storage? Does it offer the advantages of encapsulation, that is especially to make sure that errors happen locally and can easily be isolated? – Aziuth Jul 19 '16 at 13:24
  • @Aziuth just added an example – kabeersvohra Jul 19 '16 at 13:32

1 Answers1

3

While it is occasionally necessary to have something like "multiple return objects", it is often a sign indicating that you are passing around too much information. Some possible situations:

  1. You pass a lot of data from one object to the other so that the objects are very tightly coupled -> you should probably have just one class.

  2. You are passing around information that nobody uses -> Erase it.

  3. you are passing around information between methods inside a class which really should be a private field of the class.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • With regards to #3, is having global variables within a class advisable? I would have assumed that passing around more data is better than having variables which are passed around. Global variables would heavily reduce the amount of data passed around in my case – kabeersvohra Jul 19 '16 at 13:13
  • A private field that holds information regarding the object is no problem. Remember that you should only create public getters or setters for it if they are really necessary (often they are not). Furthermore, make all fields final that are only set in the constructor to ensure they do not (accidentally) change. – J Fabian Meier Jul 19 '16 at 13:16
  • yes but it is within the class, using variables within the class is what I have been trying to avoid which may be where my issue lies? – kabeersvohra Jul 19 '16 at 13:18
  • I have not been using getters and setters within the private classes, simply been using them to wrap up information into a single object to return from the method – kabeersvohra Jul 19 '16 at 13:20
  • As I said, you can have private fields in a class (and use them in all methods of that class). There is not general problem in that. – J Fabian Meier Jul 19 '16 at 13:20
  • ok cool, thank you – kabeersvohra Jul 19 '16 at 13:21