On Wed, Mar 19, 2025 at 02:42:21PM -0500, Corey Minyard wrote: > On Wed, Mar 19, 2025 at 08:21:20PM +0100, Markus Armbruster wrote: > > Corey Minyard <co...@minyard.net> writes: > > > > > Is this official coding style? I'm not a big fan of having return > > > statements in the middle of functions, I generally only put them at > > > the beginning or the end. > > > > There's nothing in docs/devel/style.rst. > > > > I count more than 42,000 return statements with indentation > 4. These > > are either within some block, or incorrectly indented. I'd bet my own > > money that it's the former for pretty much all of them. > > > > I count less than 130 labels right before a return statement at end of a > > function. > > > > Based on that, I'd say return in the middle of function is > > overwhelmingly common in our code. > > > > Ok. It's not a huge deal to me. I think it's more dangerous to > have returns in the middle; they are easy to miss and an "out:" at the > end make it more clear there are returns in the middle. But that's > just my opinion. To make wholesale changes like this I would prefer > it be in the style guide. But, I don't want to start a holy war, > either. Sigh.
In traditional C, I would agree with you that mid-function 'return's are often a bad idea, because they complicate free'ing of memory and tend to actively encourage memory/resource leaks. With our adoption of g_auto/g_autofree, that problem has been eliminated across a decently large subset of code. This swings the balance so that having mid-function 'return's often (but not always) results in shorter & easier to understand code, with few leak possiblities, provided g_auto/autofree is sufficient to deal with all cleanup needs. There will still be cases where it makes more sense to use 'goto' for cleanup, since g_auto/autofree is sufficient in all scenarios. Thus I don't think we should have a rule that strictly dictates either way. Better to leave it upto author's judgement call as to which approach results in clearer code for each particular function. I would still encourage maximising use of 'g_auto/autofree' where practical. With regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|