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?

5
  • I'm assuming this is the article you've read?
    – yannis
    Commented Mar 2, 2012 at 10:36
  • I've read several articles with regards on the topic, and the link you gave me is one of them
    – KyelJmD
    Commented Mar 2, 2012 at 10:40
  • 1
    I would change the methods to getPassword(Role role) where the Role is either assistant or administrator and can be extended further without having to replicate code
    – ediblecode
    Commented Mar 2, 2012 at 11:06
  • Extract until you're the only one who can figure out what the code is supposed to do. That equals job security.
    – jfrankcarr
    Commented Mar 2, 2012 at 12:10
  • No, a method should do multiple things and be bad at them :P
    – gardenhead
    Commented Jan 17, 2016 at 4:27

3 Answers 3

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.

4
  • looking from my code have i done too much?
    – KyelJmD
    Commented Mar 2, 2012 at 10:48
  • Nope, you are fine. Take a look at some well designed open source frameworks (frameworks tend to be much better designed than components/libs). For example jQuery's source code, or if you are a C# guy, the MVC framework. Commented Mar 2, 2012 at 11:20
  • links please? I can't seem to find it. I am a C# and java guy, but I prefer java though. is there anything for java?
    – KyelJmD
    Commented Mar 2, 2012 at 11:22
  • aspnet.codeplex.com Under MVC. For Java, just look at the source code of any open source component that you already use. Not all are great, but many are. Commented Mar 2, 2012 at 12:18
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.

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.

7
  • But if you keep of extracting , the length of codes gets longer because of other methods, is still that an issue? or not anymore? how about from my example? is it okay? the getAssistantPassword and getAdministratorsPassword queries different tables does that give a logical lump of code?
    – KyelJmD
    Commented Mar 2, 2012 at 10:36
  • If your case I would create a getPasswordFor(string role); Commented Mar 2, 2012 at 10:39
  • Not really, assuming your methods are well named and all belong to the class that they are in then it's fine. I'd rather have lots of little methods than several huge methods because it makes the code much easier to read - you have to keep less things in your mind when you're reading it. Commented Mar 2, 2012 at 10:40
  • @BorisYankov But I query different tables. using that method it would be able to do 2 two things, identify role, query the table for that role. or that's still fine?
    – KyelJmD
    Commented Mar 2, 2012 at 10:48
  • Having the login information in a single table is usually the best practice, and you having to query separate tables is possibly already a bad sign. If we assume it has been done correctly, it is totally OK and even encouraged to have these in two methods. In fact you can have more methods, one doing the data access, one doing some business logic etc. Commented Mar 2, 2012 at 11:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.