2 ms·
> Spitting discrete parts of functionality into separate functions, even if called once makes the code more readable even if they only get called once. The key
by loup-vaillant 11y ago
> Spitting discrete parts of functionality into separate functions, even if called once makes the code more readable even if they only get called once.
The key point is discrete. If you fail to carve the program at its joints, and no more, you'll only make it worse. I too separate functions even if they're called only once. I just require that the interface of the function be easier to understand than its implementation. That's not always true, especially for short, impure functions.
> You don't clutter the would be calling functions scope with variables only needed within the would be in-lined functions body.
I don't need functions to do that. I'm not writing JavaScript. Here:
{
int foo;
float bar;
baz(foo, bar);
{
int wiz;
float woo;
wak(wiz, woo);
}
klo();
// klonk(wiz); // error: wiz not in scope
}
> When you read the code you the function name and return type can describe what is being done rather than trying to grok the body of the function code itself.
I can still put comments. They notoriously rot, but the same is true of function names. Plus, comments aren't restricted to identifiers, so I can write plain English:
{
// ...
{ // Waks the wiz with the woo
int wiz;
float woo;
wak(wiz, woo);
}
// ...
}