Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Helper Methods

Is there a simple definition and example for helper methods?

Thanks :)

1 Answer

Hi Michael,

Helper methods are used to quickly ascertain the state of something within the class. Often, they are used to compare to zero, returning a true/false outcome that can help route code execution via an if statement, for example. One of the courses on here uses a good example. A member variable (it is called mBarsCount but it doesn't matter) has a maximum value defined by a constant in the class and the variable can be incremented an decremented as required by the code. Two helper methods exist for this variable, one to compare it to zero and the other to compare it to the defined max value. Both of these methods return a true or false outcome.

So, while it is possible to do either of these comparisons within your code, the use of helpers both removes duplication of code by creating a method for a commonly used requirement, and it makes the code easier to read (if you name the method sensibly!).

I hope that, erm, helps. :+1:

Steve.