Suppose I have some code looking like this :
void doTasksAndStuff()
{
// Do stuff
...
// Do task
...
// Do stuff
...
// Do same task
...
// Do stuff
...
}
Since the task is done twice, I suppose it is better to factor the corresponding code :
void doTask()
{
// Do task
...
}
void doTasksAndStuff()
{
// Do stuff
...
doTask();
// Do stuff
...
doTask();
// Do stuff
...
}
Now, what if I wrote it like this :
void doTasksAndStuff()
{
auto doTask = []()
{
// Do task
...
};
// Do stuff
...
doTask();
// Do stuff
...
doTask();
// Do stuff
...
}
Since doTask() is only called in the function doTasksAndStuff(), it seems to me to be a clean way of factoring the code without polluting any namespace, but is that the only difference between those two ways of factoring?
In particular, what about performance? What about inlining?