Mini essays

How to delete code – the story of interface

How to Delete Code: The Interface Story

How to delete code, safely of course, is usually a major problem. Difficult and You always second guess every decision because You don’t know what You don’t know. It is not just hitting the „delete” button. You need to make a smart decision and second guess your solution. How to remove unnecessary code with a focus ? Refactor the code, use interfaces and then test it twice remembering about user experience.

TL; DR 😉

Define one interface. Create two implementations – legacy, modern and any other. Unify! Use it in already existing services or create such services by refactoring code. Switch between them instantly, use both at the same time and compare results in logs if You have to. Your application code never changes. It will depend on the interface contract, not the specific implementation.

  • Interface: I want to get a drink
  • Implementation: Different glasses that You can use

Why to delete code ?

Codebase can become bloated, unused features, old functionality and/or quick fixes that are obsolete. Building a feature not always implies refctoring and removing the code. We usually are too scared to delete things because uf the unknown consequences. This makes code hard to maintain, slower, difficult to understand. However removing this code improves performance, readability and overall quality.

How do you decide what to delete? Usually IDE will tell us what is unused and is safe to delete based on many linters, wart removers or code analysis so on. Let us talk about the help we coud get from interfaces.

The Role of Interfaces in deleting code

An interface tells us what users care about. This will tell us what should be kept or remove. Interfaces can help you make such decisions.

1. Deprecate code

When a feature is on the way out we should mark it for the transition period before it is time to remove it. If a feature does not impact UI or is very rarely used it has the potential for deletion. In example a button, link or menu had not been used in a long time, it is time to delete it ?

2. Simplify UX

UI should streamline user journey and help spot bloatwar. If an interface is revolving around a few core features any extra code probably could be deleted. Otherwise it would be inside those core functionalities.

3. Refactor -> remove legacy

Time flies and Your application might have many versions of the interface or similar logic in different places ( almost duplicates, but not quite). For example one legacy api, one custom for a particular client,one for mobile and of course the up to date version. Try to unify this, this might be hard due to refactoring needed and a tech debt nature of this.

4. Refactor for better UI

Redesigning an interface usually implies rewriting some code underneath. Follow the „Scout rule” to make it better. Try to scqueeze in some refactor in the main task in Your next sprint.

Principle of least surprise

During code deletion keep in mind the principle of least surprise. Users and developers should not be suprised by any changes:

  • Test: When in doubt make it twice ( thrie ? )
  • Communicate: Tell everyone about it and communicate clearly.
  • Alternatives: If You remove anything You should provide at least a better solution. Doesn`t have to be backward compatible but should it be ? Ask business and interested parties.

Rules for deleting code

  1. Kaizen – small deletions: Remove gradually, even line by line if You have to, test as you go.
  2. Tests: Create automated tests (regression also) to catch any errors that could occur after deletion.
  3. Feedback: Try and gather some feedback, in ex. from testers, business analysts, users.
  4. Documentation: Changelog, adr or similar docs, keep track of what and why was deleted.

Conclusion

Deleting code is a cumbersome task, especially on a big, long lived projects. Establish a clear process on how to conclude such actions. Cleaner codebase means higher quality of performance, maintenance, user experience. Next time You will have to delete code try and think about how an interface might help You.

Możliwość komentowania How to delete code – the story of interface została wyłączona
Piotr Kowalski