Today I Learned - Rocky Kev

TIL Rule of Three in Refactoring

POSTED ON:

TAGS:

The rule was popularized by Martin Fowler in Refactoring[1] and attributed to Don Roberts.

It states that two instances of similar code do not require refactoring,
But when similar code is used three times, it should be extracted into a new procedure.

Overall, wait until you see the duplication happening a third time before you introduce your abstraction.

With more occurrences, it will be easier to find the commonalities to extract.


Related TILs

Tagged:

TIL Indirection and Swallowing the error

Op shared a tryCatch util that is problematic.

TIL reusing variables

Do not reuse variables. Do not double define variables that represent the same thing.

TIL about semaphores

A semaphore limits access to a resource to some number of consumers. Any new requests for the resource will get stalled until later, eventually to be handled.