13

"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
  • 21,442
  • 29
  • 112
  • 288
KyelJmD
  • 971
  • 5
  • 10
  • 20
  • I'm assuming [this is the article](http://blog.objectmentor.com/articles/2009/09/11/one-thing-extract-till-you-drop) you've read? – yannis Mar 02 '12 at 10:36
  • I've read several articles with regards on the topic, and the link you gave me is one of them – KyelJmD Mar 02 '12 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 Mar 02 '12 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 Mar 02 '12 at 12:10
  • No, a method should do multiple things and be bad at them :P – gardenhead Jan 17 '16 at 04:27

3 Answers3

31

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,573
  • 1
  • 23
  • 29
  • looking from my code have i done too much? – KyelJmD Mar 02 '12 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. – Boris Yankov Mar 02 '12 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 Mar 02 '12 at 11:22
  • http://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. – Boris Yankov Mar 02 '12 at 12:18
14

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.

Peter Mortensen
  • 1,050
  • 2
  • 12
  • 14
BЈовић
  • 13,981
  • 8
  • 61
  • 81
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
  • 2
  • 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 Mar 02 '12 at 10:36
  • If your case I would create a getPasswordFor(string role); – Boris Yankov Mar 02 '12 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. – Stuart Leyland-Cole Mar 02 '12 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 Mar 02 '12 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. – Boris Yankov Mar 02 '12 at 11:12
  • @Boris Yankov The Reason why I have different table because of Different Account types. Administrators,Physician and assistant – KyelJmD Mar 02 '12 at 11:23
  • Yes, but you usually separate the login information in another table and join by Id with the other tables when needed. – Boris Yankov Mar 02 '12 at 12:19