On Mon, Jan 29, 2024 at 1:38 PM Heikki Linnakangas <hlinn...@iki.fi> wrote: > -0.5 from me, for exactly those reasons Robert said. I wouldn't mind > removing the compiler flag as long as we mostly keep the current style > of declarations at top, with exceptions when it really makes sense. But > in practice it would open the floodgates and make things worse overall.
Yeah, this, for sure. If it were done judiciously I wouldn't care, but in practice it wouldn't be. Different people would do wildly different things, ranging from never putting anything mid-block at all, at one extreme, to rearranging the whole flow of the function to allow for more mid-block declarations, at the other. TBH, I wish we could get more consistent about our coding style overall, and clean up some of our historical baggage. We have such beautiful code in so many places, and such ugly code in others. I still can't get over how ugly xlog.c is in particular. Multiple people have attempted to split that file up, or clean it up in other ways, but it's still a soup of unclear global variables and identifier names pulled out of a hat. And it still baffles me why we allow everyone to pick their own system for capitalizing identifiers out of a hat, without even insisting on consistency from one end of the same identifier to the other. > You can also add curly braces to introduce a block like this: > > do_stuff(); > { > int i = 123; > > do_more_stuff(i); > ... > } > > I know many people dislike that too, though. I think it's usually better > than declaring a variable in the middle of a block, because it also > makes you think how long the variable needs to be in scope. I think this style can be appropriate for assertions or debugging code, where you only need the variable if some compiler symbol is defined, and you isolate it to the same block where it's used. I don't tend to like this style for other cases. It looks like you were too lazy to go back to the top of the function and just add the declaration there. I've also found that when I'm uncomfortable moving the variable to the beginning of the block because it doesn't seem to fit with the other stuff declared there, it's usually a sign that I'm going to end up making the block conditional or turning it into a separate function -- and of course if I do either of those things, then suddenly I have a natural scope for my declarations. -- Robert Haas EDB: http://www.enterprisedb.com