15

How to find opportunities to refactor and clean up code?

Profile picture
Mid-Level Software Engineer [SW2] at Taro Communitya year 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?

1.4K
4

Discussion

(4 comments)
  • 16
    Profile picture
    Engineer @ Robinhood
    a year 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).

  • 13
    Profile picture
    Eng @ Taro
    a year 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.

  • 8
    Profile picture
    Mid-Level Software Engineer at Other
    a year 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.
  • 8
    Profile picture
    Tech Lead @ Robinhood, Meta, Course Hero
    10 months ago

    My advice is to not force it - It's hard enough as is shipping your core roadmap work on time. You will develop the refactoring muscle naturally if you are mindfully doing all of the following:

    1. Ship quality pull requests that get lots of feedback and look for patterns among that feedback
    2. Review a lot of your teammate's code and pay attention to how more senior teammates review the same pull requests you are (again, look for patterns)
    3. Pay attention when something feels off and are unafraid to dig deeper (this often leads to a refactoring opportunity)

    I go into all of these behaviors alongside concrete messy code patterns to refactor in my new course here: [Course] Level Up Your Code Quality As A Software Engineer