• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

What is the best way to start refactoring?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a poorly written function to change the price of a product that takes into account many different conditions. You need to refactor, since the function itself is terribly unreadable. Where to start refactoring or what methods to use to start refactoring?

I tried to find a service for automatic refactoring or code analysis, I did not find anything.
 
Marshal
Posts: 8831
631
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Where to start refactoring or what methods to use to start refactoring?


In unit tests. If you don't have them, this is where you start, by writing them. Otherwise, how you'd know you didn't break anything? By writing unit tests, you also would have a chance to understand what this code is actually doing in the first place.

That's not Java code. Perhaps JavaScript.
 
Sheriff
Posts: 17631
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My go-to methods of refactoring are all about clarity and organization:

1. Rename for clarity of intent
2. Extract to eliminate duplication, and to push down implementation details
3. Compose to achieve a single level of abstraction (SLAP)

These refactorings are fairly simple and safe to do, especially if you have local variables or limited scope variables. If you have an IDE that can do automatic refactoring, all the better, since that's much safer than manual refactoring. If you're not disciplined and methodical, manual refactoring is dangerous and could lead to breaking the current functionality.

It's important to have tests that will tell you if you broke something while you were refactoring. Without that safety net, refactoring is risky at best. Rename and Extract + Compose are the least risky of all refactorings I know and they can help you loosen up tightly coupled code enough to be able to add unit tests for isolated sections of the code.
 
Junilu Lacar
Sheriff
Posts: 17631
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what I mean by using Rename/Extract/Compose:

This only illustrates the idea of extracting a section of code that does one small job so that the big method starts becoming a Composed method. You'd do that multiple times for various sections in the big method until it's only a few lines of code consisting of calls to the extracted methods.

The name strawberry is used on purpose to force you to think of a better name, so you can Refactor-Rename. This is a strategy I learned from Arlo Belshee's Naming is a Process
 
Pole Polenko
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what about the uniqueness of the code? Can I use one code for two different customers?
 
Author
Posts: 14
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the topic of "refactoring" is very interesting, it might be worth looking at the book "Practical Design Patterns for Java Developers" (https://a.co/d/6EPQuiq)
It explores over the chapters through the proposals how to approach challenges.  
If I look at the examples in the thread, the book can give you a guide on how to use the design pattern through refactoring, because IMHO one of the goals of refactoring is maintainability.

those are my 2 Cents
 
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having a fairly thorough set of regression tests is the best way to start refactoring, that way you can quickly tell if you've broken anything.
 
Miro Wengner
Author
Posts: 14
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True, I forgot to mention it implicitly. Refactoring requires creating a sufficient set of tests. Since it is being refactored, IMHO: I would consider that the codebase under consideration may contain a hidden problem that is not consistent with the business scenarios or intended technologies. Therefore I'd select the "lowest" layer from the desired high level abstraction, could be for example client as todays apps are very often emitting data over http layer (TCP/IP). And start producing a sufficient coverage (TDD like approach).
Then changing the code base accordingly by following OOP,APIE=>Design Patterns to produce maintainable, understandable and extendable result.
This may also support also another software development approaches like Domain Driven Design, Even Driven Design or etc.

therefore I've referenced the book above "Practical Design Patterns for Java Developers" (https://a.co/d/6EPQuiq) as there are sections exploring those approaches,
Hope it helps!
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic