14

"Extract Till You Drop" is someting I've read in Uncle Bob's blog, meaning that a method should do one thing alone be good at it.

What is that one thing? When should you stop extracting methods?

Let's say I have a login class with the following methods:

 public string getAssistantsPassword()
 public string getAdministratorsPassword()

These retrieve the respective accounts password in the database. Then:

 public bool isLogInOk()

The method compares the password that has been called or selected, and sees if the password the user provided is in the database.

Is this an example of "Extract Till You Drop"? When will you know when you are doing too much of extracting?

gnat
  • 20,543
  • 29
  • 115
  • 306
KyelJmD
  • 981

3 Answers3

32

You've extracted too much when your code itself is more clear than the method name.

Keep in mind, when not sure, almost any programmer I've seen has been building much larger and monolithic methods than optimal, rather than the other way around.

A method with just few lines of code is quite typical of well designed app/library.

Boris Yankov
  • 3,593
15

To quote Clean Code, on the page 36, it says:

So, another way to know that a function is doing more than "one thing" is if you can extract another function from it with a name that is not merely a restatement of its implementation.

BЈовић
  • 14,049
6

The general idea is that you're done extracting when there is nothing else that you can extract and still get a logical lump of code. If there's part of a function that can be taken out, if it does something that you can describe and give a name to, and if you can imagine that you might need to call that from some other part of your code, then you should extract it.

Even if you never need to call it from another part of your code, the fact that it's a separate function means you can write unit tests for it alone, which improves your bug detection, and means you can profile it independently, which improves your ability to assess for performance losses in your code.

Extract code until there is no function that does more than one thing - if you can pull code out of a function into another function, if the new function performs some task, and if you can describe what that task is, then extract that code. If you're worried about performance, you can always inline it later.

Dan
  • 161