WET is better

Code, Refactoring

title: WET is better created_at: 2021-05-18T14:52:16.823Z updated_at: 2023-12-25T14:52:16.823Z tldr: on writting better code is_published: 1 star: false category: Code, Refactoring share_type: add

Write everything twice. Yes, it's better.

Say, there is a requirement to display user image and name. we put the logic in a component and start using it.

There is another requirement to display user image and name in a tooltip. Do we create a new component or modify the existing one? We have one more requirement to show if the user is active, now?

The most typical approach is to modify the existing component and start modifying the logic. We are not repeating ourselves so it's DRY right?

No. we are trying to modify a closed component, which is not a good idea, to begin with more on open-close.

DRY is having the knowledge of what is to be abstracted from re-implementation. Writing logic twice for a different requirement is perfectly ok.

Looking back at the problem, it is perfectly ok to create a different component for each of those use cases. Re-implementing the UI logic is OK. We should be abstracting away the logic to get user details. Testing these components individually and to extend is a lot easier.

Another example I recently encountered. There are three pages and each has a drawer component to be implemented. Almost the same design, and the same logic. Merge everything in one place? Nope, changing the design in one causes the changes on every other page. Side effects are not desirable on UI abstractions. It's better to re-implement the rendering logic to each page and abstract the data logic.

Also, do yourself and your team a huge favor and comment on the reason for abstraction. You won't be able to reason about the same thing exactly twice.

Having thoughts ?

Add yours