Code Gardening is the practice of making small improvements as you see them while you are working, even if they are not directly connected to the feature or defect you’re currently addressing.
Tidying things up as you go is never wrong - just ask any gardener or that friend of yours with the immaculate house.
Tidy esthetic code is provably easier to read, easier to understand and easier to reason about.
Most importantly, because code can be beautiful … and it should be.
Make small changes with little to no risk as you see them. Your automated test suite is your safety net and best friend because you shouldn’t check things in without testing them!
Don’t commingle these changes with others - always use a separate commit. This is a safety measure, making it easier to reverse the changes if there is a problem.
Tip: Use Language Features
The first tip for code gardening is to know - and use - modern language features.
I’ve heard the objection that “some developers may not understand the new features”. While this is true, to a limited extent, it is also true that most developers will be moving forward with the new features. This means that any third-party libraries, samples, blog posts and answers you find online are likely to use those features.
One of the best ways to learn new language features is to slowly introduce them into an existing codebase, getting a feel for how they work at the same time as you improve things.