Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cleaner code

 
craig long
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic