4 Replies - 4691 Views - Last Post: 27 October 2011 - 12:40 PM

#1 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Helper classes

Posted 27 October 2011 - 11:14 AM

Hi everybody,
Let's say you have a class, and a group of helper classes that are only used with that particular class. How do you all normally organize them? What I usually do is to put these helper classes as inner classes if they're small, and put them as top-level classes in the same file as the major class if they're relatively big. I'd be interested to know what everyone else does -- there doesn't seem to be a Java language restriction to either approach.
Zel2008

Is This A Good Question/Topic? 0
  • +

Replies To: Helper classes

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Helper classes

Posted 27 October 2011 - 12:01 PM

View PostZel2008, on 27 October 2011 - 02:14 PM, said:

a group of helper classes that are only used with that particular class.


First, "Helper class" has a particular meaning, which I'm not sure you intended. More here. If that's the idea, then it sounds like those methods should simply belong to the other class?

If you're just making classes that are only used by a given class, that's fine and not particularly evil. In this case, I'd use the same rules as for variable scope; the narrower the better. You always want elements of the program exposed to as few other elements of a program as you can get away with. So, yes, I'd keep them in the only class that would be using them.

If, for any reason, you need to pass complex data out that is contained in such a class, make an interface and pass that.

This post has been edited by baavgai: 27 October 2011 - 12:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: Helper classes

Posted 27 October 2011 - 12:09 PM

Hmm, I've never heard of a helper class being defined as all static methods. I always learned that a helper class was a class that only was used with one other class to aid in its functionality. Am I wordsmithing and splitting hairs, or is there actually another name for what I'm thinking of?

Having methods that belong to a particular class is evil? I'm not sure I quite understand what you mean in your second paragraph. Did you mean that having multiple classes in the same file isn't evil, as long as the helper classes are only used in the major class, or that having lots of nested classes isn't evil?

I can imagine one of two possible layouts (in a single file) here, either:
public class A {}
class B {}
class C {}
more classes...



or:
public class A {
    class B {}
    class C {}
    more classes...
}


This post has been edited by Zel2008: 27 October 2011 - 12:09 PM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Helper classes

Posted 27 October 2011 - 12:31 PM

Prefer this one:
public class A {
    private class B {}
    private class C {}
    more classes...
}



However, if you're doing something like:
public class A {
   private B b ;
   public B getB();
}



Or
public class A {
   private B b ;
   public int getFoo() { return b.getFoo(); }
   public int getBar() { return b.getBar(); }
}



Then you might have issues.

Basically, are you using a class to describe an object that has a particular function, or are you just using it as a namespace?
Was This Post Helpful? 0
  • +
  • -

#5 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: Helper classes

Posted 27 October 2011 - 12:40 PM

That's a good question. I'm not actually building a class like this, just trying to analyze the different ways this can be done.

I've seen things like your example before, and yes, that is very messy if you're using it as an object. That's exactly why I tend to shy away from nested classes in that instance, and put multiple classes in one file.

Rather than doing this:
public class A {
   private B ex;
   public int getFoo() { return b.getFoo(); }

   class B {
       public int getFoo() { return 1; }
   }
}



I'd be much more inclined to do this:
public class A {
   public int getB() { return new B(); /* and then call getFoo() on this later */ }
}

class B {
    public int getFoo() { return 1; }
}



I think lots of nested classes would work if you were using them as a namespace, but it seems like having separate classes in the same file is the best idea when talking about objects.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1