Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

"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?

share|improve this question

marked as duplicate by gnat, Kilian Foth, MichaelT, Michael Kohne, Yannis Rizos Nov 2 '13 at 23:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
I'm assuming this is the article you've read? –  Yannis Rizos Mar 2 '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 2 '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 –  danRhul Mar 2 '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 2 '12 at 12:10

3 Answers 3

up vote 9 down vote accepted

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.

share|improve this answer
    
looking from my code have i done too much? –  KyelJmD Mar 2 '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 2 '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 2 '12 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. –  Boris Yankov Mar 2 '12 at 12:18

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

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

share|improve this answer

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.

share|improve this answer
    
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 2 '12 at 10:36
    
If your case I would create a getPasswordFor(string role); –  Boris Yankov Mar 2 '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. –  Stu Mar 2 '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 2 '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 2 '12 at 11:12

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