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 Java Objects (Retired) Harnessing the Power of Objects Helper Methods and Conditionals

Bad Code "Challenge" Suggestion in Java Track

This is a suggestion for Craig the instructor or Treehouse staff. Craig starts talking about not having redundant code in Helper Methods & Conditionals. I would love to see an example of really badly written code, why it is really badly written, and possible ways to trouble-shoot the bad code. I feel like that would be an interesting addition that would further my learning.

1 Answer

One of the examples of "bad" code are mentioned in the video is repeating code. So instead of checking for equality using " == 0 " everywhere where you need it, he wrote a method called "isEmpty()" once and uses it anytime he needs to check. He also makes the method short and concise by just

// Example of good short and concise code.
public boolean isEmpty(){
  return (mPezCount == 0);
}

instead of

// Example of bad code because it is verbose and sort of unreadable.
public boolean isEmpty(){
  boolean isActuallyEmpty = (mPezCount == 0);
  return isActuallyEmpty;  
}

This is just a simple example to get you thinking this way. There are better/more examples where he "refactors/rewrites" bad code into good code in later viedos.