2 ms·
This blogpost's analysis is incomplete. It needs to discuss the core issue with WHY C programmers originally avoided multiple return statements. Its pretty simp
by dragontamer 9y ago
This blogpost's analysis is incomplete. It needs to discuss the core issue with WHY C programmers originally avoided multiple return statements. Its pretty simple:
void function(){
lock_mutex();
void* thingy = malloc();
if(...)
{
return; // Bug!! You forgot to unlock the mutex
// Bug!! You forgot to free(thingy);
}
// Imagine 100 lines of code here
free(thingy); // Hard to remember with 100 lines of code in the way
unlock_mutex();
}
When functions grow to hundreds of lines of code, you need to ensure that all cleanup functions are called. Modern programming languages have features to handle this case "automatically" (Python "with", C++ RAII, Java finally). Which is why its fine today.