Taro Logo
3

How to find opportunities to refactor and clean up code?

Profile picture
Mid-Level Software Engineer [SW2] at Taro Community2 months ago

I know to refactor my commits only based on previous review comments. I code and test it thoroughly most of the time, but it's hard for me to identify places to refactor. How to get good at refactoring or even identifying places to refactor?

93
3

Discussion

(3 comments)
  • 5
    Profile picture
    Engineer @ Robinhood
    2 months ago

    I follow a rough playbook of:

    • Deleting dead code
    • Breaking up large modules into smaller modules
    • Moving code off of older frameworks onto newer frameworks
    • Splitting up code into small files/components

    Usually I start my search of things to refactor at very large, legacy modules or modules under my ownership. After around a year or two of refactoring code and writing code from scratch for new products, things just clicked for me (likely from having a stronger subconcious understanding of what code did/didn't work and what patterns did I commonly have to end up refactoring).

  • 6
    Profile picture
    Eng @ Taro
    2 months ago

    Be careful that you aren't just looking for opportunities to refactor for the sake of refactoring. There can be so many opportunities to refactor different parts of the code base, but you have to make the calculation of whether it's a worthwhile place to refactor. If you are finding it hard to identify places to refactor, I would hold off until you're more familiar with the codebase.

    A good signal that you should refactor is that you and other engineers have a loss of productivity when building in a certain part of the code. If you get a sense of anxiety when touching a certain part of the code, you can probably add it as a candidate as a place where you can refactor the code. You should be having regular 1:1s with people from your team and people on other teams, and you can gauge where frustration in a codebase lies.

  • 4
    Profile picture
    Mid-Level Software Engineer at Other
    2 months ago

    TLDR Version

    Find some guiding principles and how to balance them.

    Also remember, the more experience you get the more you can think of simplifying the process.

    Slightly longer version

    Something that can help for small wins are:

    1. Learn your style guide
    2. Use linters and other code analyzers, they will catch a lot of issues.

    For more in-depth:

    1. Find ways to reduce code complexity and coupling.
    2. Read some books like pragmatic programmer and see how you can use the tips in your development.