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

Clean Code

Wouldn't this be much more effective and maintainable code?

public String getColor() { Random rNG = new Random(); return mColors[rNG.nextInt(mColors.length)]; }

1 Answer

all comes down to personal preference (and coding guidelines of the place you work or the team you work with).

If it looks good as a single line, i tend to end up putting it on a single line or my IDE (integrated development environment) does it for me. Most of the time this is just getters and setters for me.

However you may have trouble finding errors in your code if you place larger methods on as few lines as possible. For example if the code you posted wasn't:

public String getColor() { Random rNG = new Random(); return mColors[rNG.nextInt(mColors.length)]; }

but instead

public String getColor() { Random rNG = new Random(); return mColors[rNG.nextInt(mColors.length] }

It might not be as easy to find.

But different strokes for different folks.