File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes cleaner code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cleaner code" Watch "cleaner code" New topic

cleaner code

craig long

Joined: Jan 19, 2010
Posts: 7
I recently completed this program assignment for my beginning java class and it works well. I passed the assignment all right but was wondering if there is anything I can do to cleanup the code? Some of it seems wordy, particularly the comparisons for a winner at the bottom lines 146 - 158. thanks

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The first thing I would do to clean this up would be to move the major data structures (e.g., "board") from being local variables to member variables, and then start breaking the one long method up into a number of much smaller, single-purpose routines. As a start, consider that every where you've written a comment like "initialize board", you could instead have a call to a method named "initializeBoard()" which did just that one thing. Sometimes these methods would operate on shared data like "board", while other times they'd communicate with main() using arguments and return values.

Once the code is broken into these small chunks, then you can consider which ones need the most work to make them easy to understand.

[Jess in Action][AskingGoodQuestions]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Clean code is a particularly subjective phrase, however looking at what you have, the first thing I would advise would be to decompose your singel main method into smaller methods. These methods should do exactly one thing, and return a result. If you start down that road, your main method might begin to look something like this:

Once you start decomposing your program into smaller units, you usually end up with methods that are smaller, and easier to understand (and test).

Edit: Ernest beat me to the punch!

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
I agree. Here's the link:
subject: cleaner code
jQuery in Action, 3rd edition