-1

Is there a good practice to split part of complex method's functionality to several smaller methods only in terms of good code readability (but maybe paying some performance for methods calling)? Or this is some kind of "bad style"?

Shelest
  • 620
  • 7
  • 18
  • This is far too vague, generally you should have small methods but you really should provide an example if you want more usefull feedback. – Ben Robinson May 23 '12 at 13:35
  • http://en.wikipedia.org/wiki/Code_refactoring – Massimiliano Peluso May 23 '12 at 13:36
  • @Cicada, i think that any "one thing" could be separated to several smallest. Btw, what is the place to ask such questions, if it is not? – Shelest May 23 '12 at 14:37
  • @Cicada, I realy does not understand why such child questions as http://stackoverflow.com/questions/7074/what-is-the-difference-between-string-and-string/215422#215422 have so much popularity, and my question which simply needs a lot of good practice and nice programming style to answer - is not properly... I just wanted to hear experienced people, and i think - this is the best place. – Shelest May 24 '12 at 11:08

4 Answers4

4

The very basic rule is that your method should do only one thing. If you detect that is doing many different things, then you have a clear refactoring oportunity.

If you reach the point where your method has a single responsability but the size is too long anyway, try to extract "helper" functionality to separated methods. You might even detect code that can be promoted to separated classes.

TDD development is a great methodology to avoid this kind of issues since it really helps to clearly separate concerns and to avoid a bunch of code on single methods just for the sake of testability. If you don't write concise methods, it becames too hard to test them properly.

Claudio Redi
  • 63,880
  • 13
  • 118
  • 146
  • Okay, I think I heard something like i wanted. Claudio Redi, Massimiliano Peluso, Mohammed ElSayed, Thanks a lot for your's answers =) – Shelest May 23 '12 at 14:19
3

If you think about reusing code (The DRY principle), you should consider refactoring. Split the method content into different small module based on the functionality so that it can be reusable. Ex : If you have a method which saves a customer registration details and create a new order for him. you could probably check that many methods like CheckUserExist, SaveUser and SaveOrder. You should be able to reuse this functionalities in other areas of your code as necessary. Splitting it into modular pieces makes your code more readable too.

Shyju
  • 197,032
  • 96
  • 389
  • 477
3

Unclebob (Robert C. Martin) considers that methods should be no larger than 4-5 lines of code. His motto, concerning this, is "Extract till you drop". Personally, i believe this is a very good practice.

Visual Studio allows you to extract a method by pressing CTRL+R, M.

sebi
  • 1,721
  • 3
  • 25
  • 40
  • Ctrl+R, M - how's about that. That's cooooool. 4-5 lines seems insane to me, but what do I know. – Yatrix May 23 '12 at 13:48
  • I thought the same at first. But I can't stress out how much cleaner my code became after I tried to apply this rule. Of course, you will always have some methods that will be large. The idea is to keep them as small as possible. – sebi May 23 '12 at 13:49
  • Small as possible I agree with 100%. – Yatrix May 23 '12 at 13:51
1

in addition to Claudio's answer, there are some SMELLS that if detected, it means that you have to refactor and split your code.

1- non-DRY code: if you find yourself copying/pasting some lines into more than one place, this is a bad smell that needs to be placed in a center placed, and to be called from as many as it should be.

2- Methods with hundreds of lines: any good method should not exceed 50 lines of code, maybe more or less than this number, but rest assured that this method of 346 lines is not a good thing to do.

3- Too many parameters: a long list of parameters in your methods make readability and code quality worse.

4- code invasion: Methods that are using many blocked from another class, should exist inside this class.

Hope that helps.

Community
  • 1
  • 1
Mohammed Swillam
  • 8,682
  • 3
  • 33
  • 45