Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.