Deprecating Code Gracefully

In this post I describe a neat but simple trick that you can use to help inform developers of proposed architectural direction changes in the code they are modifying.

This is especially useful on a large, constantly evolving monolithic code base where refactors may take time and require a concerted effort across the whole team.

An example illustrates the concept quite nicely:

bundle exec rspec spec/features/deprecated_classes_spec.rb

Example failure

This is a great call to action.

It encourages teams to have a conversation about the desired approach, read the docs, and collaborate where required. This may be the time to upgrade this part of the system or it maybe just fine to update the sha and carry on, assuming the relevant parties have agreed so. This change also has the benefit of showing up in a pull request and so any changes may need to be justified.

In summary, it provides an opportunity to constantly improve and share the knowledge. One of our principles at Simply Business is “maintenance is a must” and this certainly embraces that.

In Practice

Feature: Deprecated classes

As a developer I want to raise awareness of deprecated classes So that we don’t build on code we want to refactor when we next modify it And therefore we “do it right”

Scenario: Modifying a class which is marked as deprecated due to the new support framework Given a list of deprecated files | file | sha | | app/support/book.rb | f92f8a | | app/support/page.rb | fbb220 |

And a deprecation message: “”” Refactor with a goal!

This test fails because you have made a change to one or more files which we would like to deprecate in favour of our preferred design approach.

Please consult with the guidelines here or talk to the ‘Acme’ group within the company for guidance. “”” When any of the files is detected as being modified Then this test will fail with a deprecation message`

Step file

step “a list of deprecated files” do | files | @files = files.to_h @files.delete(‘file’) end

step “a deprecation message:” do | msg | @msg = msg end

step “any of the files is detected as being modified” do @expected = @files.keys.each_with_object() do | filename, current_shas | contents = (dir,’..’,’..’,filename))) current_shas[filename] = Digest::SHA1.hexdigest(contents) end end

step “this test will fail with a deprecation message” do expect( @expected ).to match( @files ), @msg end

You can view the full source for the example on the Github repository.

Ready to start your career at Simply Business?

Want to know more about what it’s like to work in tech at Simply Business? Read about our approach to tech, then check out our current vacancies.

Martin Sadler

This block is configured using JavaScript. A preview is not available in the editor.