Achieving Reusability

talks

Why Reusability?

When it goes Good

  1. Eases later implementations (hopefully)
  2. Might make calling code much more readable.
  3. Less lines of code overall (hopefully)
  4. Work at a higher level, if you feel like you're writing the same code over and over, STOP! You're wasting your time.

When it goes Bad

  1. It might not end up being that reusable
  2. It might make it less readable
  3. It can be time consuming

The Ugly

  1. It can make code very hard to understand
  2. Makes code very indirect and hard to follow

Word of caution...

duplication is far cheaper than the wrong abstraction

However to that I say

Being afraid of making the wrong abstraction is a poor excuse to never do it.

I think the real problem is:

"Being abstract is something profoundly different from being vague… The purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise."

How to make things reusable?

0. Recognize Duplication

If you see some code that's just feels like you've written it before or you've seen it before, just look for it!

* Have you written your own component for something that looks very similar on another page?

1. Smaller Abstraction

SRP

Think Single Responsibility principle!

Tell-tell signs you're doing it wrong: 1. You add arguments for new use cases (read_message) 2. You have to name it with "and" UserCanDoThisAndThat CreateMessageAndSendEmail

2. Make API's easy to remember and use

The best modules are deep: they have a lot of functionality hidden behind a simple interface. A deep module is a good abstraction because only a small fraction of its internal complexity is visible to its users. A shallow module is one whose interface is relatively complex in comparison to the functionality that it provides.

3. Reusable Naming

4. Good documentation

5. Good code organization

6. Tell people about it!

7. Use the platform

Know the standard lib! Using an esoteric standard lib function is good you might not know it yet, but you can learn. Thousands of libraries? Not so good.

Practical Example:

Links: