aspose file tools*
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
Author

cleaner code

craig long
Greenhorn

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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: cleaner code