• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to improve code quality of a class which has everything inside 1 big method.  RSS feed

 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a code java class which has been implemented using wrong coding conventions. The The Class with main program has everything inside 1 big method. The main method creates object of the class and calls this big method. I want to break this method into small methods and may be move the method into another class.  Is there a design pattern which I can try to follow for this work?

thanks
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no "design pattern" per se but you can get some ideas from this page: https://coderanch.com/wiki/660020/Main-Pain

What will probably help you is a better understanding of object-oriented programming principles and how Refactoring is done and what are the Object-Oriented Design Principles that motivate various refactoring techniques.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Books like Clean Code by (??) Steve McConnell will have guidance for refactoring that sort of code.

But first, please confirm you are not breaching the bounds of
If it ain't broke, don't fix it.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clean Code was written by Robert "Uncle Bob" Martin.

Steve McConnell wrote Code Complete

I have both. Both are great books although I reach for Uncle Bob advice more often than I do for Steve's.

Steve McConnell's article on Cargo Cult Software Engineering is a must-read for software developers and engineers. I believe that article made the term "cargo cult" a regular part of our jargon.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:... confirm you are not breaching the bounds of
If it ain't broke, don't fix it.

There are times when you must seriously consider this if you want to stay within the bounds of pragmatism. However, when it becomes a hindrance to maintenance and enhancement efforts, it takes very little for me to deem a piece of code "seriously broken" because we can't understand it enough to be able to work with it with some degree of confidence that we won't screw anything up in the process.

I forgot to mention another couple of excellent books that may come in handy for OP:

Working Effectively with Legacy Code, aka the WELC book, by Michael Feathers

Refactoring to Patterns by Joshua Kerievsky

Again, both excellent books that I often use as references in my day job and when I'm just messing around.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure its all in the books, but  to throw in my 2 cents:

Your Java IDE should have plenty of refactoring tools within it.  Use those tools rather than try to do things manually.
In Eclipse one of my standard operations is select code, right click, refactor --> extract method.

The other important thing to do here is: Unit Test
If you can, write the tests BEFORE you refactor.  And make sure they stay green at the end.
Given your description of the current status of the code, it might be easier to pull out and unit test each method separately as you go.

And if you are just refactoring - stick to just that. Refactoring should never change the functionality of code, just organize it better.
Don't try to fix things at the same time you're refactoring. 
Make it two seperate commits into source control
- one to refactor the code
- another to fix the bug

 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all.I think the way to refactor is to create sub methods and separate classes, do refactoring changes then test whether code is working again refactor more changes and again test and so on.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds about right. The devil is in the details though. Follow the thread where Mike Laww is trying to get out of the same situation in a Blackjack game he wrote. I have a feeling we're going to be doing some of that kind of refactoring here soon.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!