Say I have this function
def function_A():
blah
blah
blah
return A
When is it justified to make that long function_A into smaller pieces, say function_B and function_C and then call function_B and function_C inside the function_A?
Say I have this function
def function_A():
blah
blah
blah
return A
When is it justified to make that long function_A into smaller pieces, say function_B and function_C and then call function_B and function_C inside the function_A?
a possible rule of thumb is:
when you feel the need of placing comments to "structure" the method the parts separated by these comments should go into separate method with names derived from the comments you might want to place.
It is a good habit to keep a single layer of abstraction in your methods. That means that you either call other methods or do calculations, not both.
A function or method is meant to only accomplish ONE purpose. If the functions is called add(x, y) you can't have the function add the numbers and then divide them. These are two separate operations.
Also, to reiterate what was said by Timothy, if you feel the need to comment your function (other than the function header documentation), there's a good chance it is too complicated.
Also, an even more general rule of thumb that can be useful to keep in mind is that your functions probably don't need to be longer than 35 lines.