Answer the question
In order to leave comments, you need to log in
How to break a large function into several smaller ones?
I read the article "The Art of Writing Simple and Short Functions" https://habrahabr.ru/post/310590/
This topic is constantly procrastinating, and the same question constantly arises in my head.
Obviously, you need to break complex functions into parts. But I often have the problem that internal functions will almost certainly not be reused anywhere. Quite often, a specific function is obtained with a specific set of parameters and a specific result. And what would you call it then? Use parent function name as prefix? Moreover, if we move the code to a separate function, then the question of checking incoming parameters appears, which was probably performed in the parent function. In the parent function, we knew for sure that the parameters were correct, but here it turns out that we either do not check anything, which is bad for the independence of the newly-made function, or once again check what we have already checked before, which negatively affects performance. I mainly program in php and there I have a real problem with this, since there are no local functions in the language. Their presence could improve the situation due to the fact that internal functions have a local scope, which means they should not have unique understandable names, plus they cannot be called from outside, which means there is no point in checking the parameters several times.
What am I doing wrong? Are there times when it's actually better not to break? Or is the presence of such cases unambiguously indicative of bad architecture?
Answer the question
In order to leave comments, you need to log in
Architecture is not measured by the length of features, but by their purpose. A function that performs exactly one task will usually not be long.
Approach the question from the side of testing. Are you satisfied with testing this function as a single black box? Or does it have specific parts that perform full-fledged subtasks, for which it would be nice to have a separate test in case there are changes in the function? If not, and you just have a uniform sheet, there is no need to break it. If there is, why not?
Well, if you are confused by the accumulation of functions that no one else needs, remember that PHP has OOP and all this can be collected in a class, and functions that are not used anywhere else are made private.
Discover namespace .
Never prefix functions, otherwise it will be like this shit `syn_whitelabel_form_whitelabel_partner_node_form_alter()` (this is real code).
Master OOP eventually. Collect your functions within one functional domain into one class.
If the function is internal, declare it private and call it via `self::`.
Regarding parameter validation - PHP allows you to declare parameter types like array or classes. In many cases, these things are sufficient.
You need to break large functions into small ones when the action is repeated at least once or can be used by another part of the project.
A typical example is validators, they can be used everywhere.
You have the wrong approach. You first make some big function, and then you want to break it. So this big function of yours is complete bullshit. That's the problem. Because you already did it wrong.
You should not have a function that first appears, and then you think that it can be made. It should be the other way around. First, a task should appear that needs to be done, and now a function that performs it should appear under this task. And there can be many such functions, and all of them can be different.
For example, you want to print the string "hello" in C.
#include <stdio.h>
int main(void)
{
printf("hello\n");
puts("hello");
fputs("hello\n", stdout);
fprintf(stdout, "hello\n");
fwrite("hello\n", 1, 6, stdout);
return 0;
}
[[email protected] c]$ .ansi t.c -o t
[[email protected] c]$ ./t
hello
hello
hello
hello
hello
[[email protected] c]$
#include <stdio.h>
void f1(void)
{
putchar('h');
putchar('e');
putchar('l');
putchar('l');
putchar('o');
putchar('\n');
}
void f2(char c)
{
putchar(c);
putchar('e');
putchar('l');
putchar('l');
putchar('o');
putchar('\n');
}
void f3(char c)
{
int i;
putchar('h');
putchar('e');
for (i = 0; i < 2; i++)
putchar(c);
putchar('o');
putchar('\n');
}
int main(void)
{
f1();
f2('h');
f3('l');
return 0;
}
[[email protected] c]$ .ansi t.c -o t
[[email protected] c]$ ./t
hello
hello
hello
[[email protected] c]$
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question