DEV Community

Wajahat Karim 🇵🇰
Wajahat Karim 🇵🇰

Posted on • Edited on

How Do You Refactor Any Existing and Very Old Codebase?

So at work, I have been working on codebase which is almost 2 years old. It seems like its written by some students who were just started to learn Android development. Here's some quick facts about codebase:

  • No architecture like MVP, MVVM or anything. Each single Activity class does everything like UI work, data persistence, network calls, deep links integrations, business logic, and so on. This results in classes with 20,000 lines of code and makes code navigation a lot harder.

  • Some Activity classes include code for 3-4 screens. Like layouts have 4-5 sub layouts and those are being shown/hidden according to the state of app. Again, each screens' logic, network calls, persistence etc goes into same activity class.

  • The code is in Java and uses lots of old libraries like Volley etc. I have recently shifted codebase to use AndroidX which itself was a big problem. There's no Material Components or anything close to Material Design. Sometimes it feels like the designer is more familiar with iOS. So design patterns, UI interactions are more like iOS way. So sometimes it becomes hard to create such in Android.

  • The app uses lots of flavors and dimensions to create white labeled app for each client using different server, databases, color schemes, and which features to enable etc.

Now we are scaling to more clients day by day. We want to refactor the codebase to Kotlin and preferably use some architecture atleast a little. We are currently removing network calls from all classes and shifting them to a repository class at start.

How do we proceed with this? We would love to know your feedback.

Top comments (5)

Collapse
 
jmfayard profile image
Jean-Michel 🕵🏻‍♂️ Fayard

Just run action convert Java to kotlin and you are done.

No seriously the book on the subject is

amazon.com/Working-Effectively-Leg...

Collapse
 
wajahatkarim profile image
Wajahat Karim 🇵🇰

Thanks for the book reference. I will look into it.

Collapse
 
narshim profile image
Narshim

Well, no surprise, that's how app has been written 8 or 10 years ago.

I would suggest, firstly, try to adhere any architectural pattern. It could be MVVM or MVP. My recommendation would be MVVM as it is easy to understand and personally I was able to convert a pretty big project from no architecture to MVVM with less pain. You can't complete this in a shot, pick a module and then a package, start from there and eventually it has to be done for the whole project. Sometimes I heard from the experts to adhere multiple architectural patterns which isn't a bad idea either, like a combination of MVVM and MVP.

Parallelly you can start converting classes to kotlin which is not easy again. Pick module or package to start with.

Use dagger or kotlin specific dependency injections like Koin which will eventually save you lot of time and open doors for unit testing.

Happy coding

Collapse
 
benjaminstrmer profile image
Benjamin Stürmer • Edited

There isn't any proper case to refactor it step-by-step. Depending on the time/money pressure it could make sense to start on a complete new green field and build the entire thing from scratch.

If you want to stick with the existing project, you could add UI Automator tests. These are independent on the code base and didn't need any reference to the code. But have in mind, these tests are only happy path testing, but after you've written tests for all requirements and use-cases. You could refactore the app code without fear (at least for one specifig language / layout)

Collapse
 
wajahatkarim profile image
Wajahat Karim 🇵🇰

Thank you for your suggestions. Actually creating proposal is proving to be a lot harder than I had imagined.