|
rjmccall posted:Clearly you're complaining about GNU whitespace conventions. I often do, but this isn't GNU coding style, GNU coding style is worse. This code has the opening brace on the same line for blocks. GNU style prefers code:
|
# ? Apr 27, 2013 11:10 |
|
|
# ? Jun 8, 2024 07:46 |
|
Doctor w-rw-rw- posted:You do realize that "do{ ... } while(false);" is, in other places, a practical and useful construct in C, right? It creates a block around which multiple expressions or statements are still treated as one so as to not disrupt braceless if's. Sure, if you're writing a macro. Not so much anywhere else.
|
# ? Apr 27, 2013 11:13 |
|
Doctor w-rw-rw- posted:You do realize that "do{ ... } while(false);" is, in other places, a practical and useful construct in C, right? It creates a block around which multiple expressions or statements are still treated as one so as to not disrupt braceless if's. The horror was *supposed* to be that this guy was amazingly getting around the "single entry, single exit, no goto" rules by using a do...while (FALSE);, but: rjmccall posted:That is a rather awesome mistake; I'm not sure I've seen it before. It takes talent to soldier on against that kind of misparenthesization. Holy gently caress . I didn't even see that.
|
# ? Apr 27, 2013 13:45 |
|
"Do while false" isn't a coding horror, it's a low-budget and high performance exception handling mechanism used when you find the use of goto a slippery slope. VVV To be clear, I didn't know about it either until I went and looked for more examples. Never expected to see it advocated. Movac fucked around with this message at 14:49 on Apr 27, 2013 |
# ? Apr 27, 2013 14:00 |
|
I didn't even know it was "a thing". To be honest, we all use goto to do fast exit and cleanups.
|
# ? Apr 27, 2013 14:21 |
|
Extracting the block out into a separate function would be preferable, especially if you're unit testing.
|
# ? Apr 27, 2013 14:25 |
|
If you can use scope-based cleanup instead that's better (either RAII, or using directives, or even just try-finally), but if you can't then goto is really the best option. Goto has always been the low-level option you turn to if your language doesn't natively support a particular control-flow pattern you want to implement.
|
# ? Apr 27, 2013 14:26 |
|
baquerd posted:Extracting the block out into a separate function would be preferable, especially if you're unit testing. Show me how you would do that on the code I wrote then. Jabor posted:If you can use scope-based cleanup instead that's better (either RAII, or using directives, or even just try-finally), but if you can't then goto is really the best option. If MSVC (and let's be honest here, SunPro) supported gcc cleanup attributes or an RAII equivalent in C, I'd switch over in heartbeat. It's a nice feature.
|
# ? Apr 27, 2013 14:31 |
|
Suspicious Dish posted:Show me how you would do that on the code I wrote then. C++ code:
|
# ? Apr 27, 2013 15:04 |
|
Suspicious Dish posted:The horror was *supposed* to be that this guy was amazingly getting around the "single entry, single exit, no goto" rules by using a do...while (FALSE);, but: Came to complain about do/while, shocked when goons found out that his code was horribly broken. The coding horror here is you, Suspicious Dish.
|
# ? Apr 27, 2013 16:00 |
|
Suspicious Dish posted:If MSVC (and let's be honest here, SunPro) supported gcc cleanup attributes or an RAII equivalent in C, I'd switch over in heartbeat. It's a nice feature. Yep, hidden behind appropriate macros it makes for nicer code, the systemd guys use it roughly like this code:
|
# ? Apr 27, 2013 16:58 |
|
Edit: Ugh, never mind, the coding horror is me.
floWenoL fucked around with this message at 18:40 on Apr 27, 2013 |
# ? Apr 27, 2013 18:37 |
|
Sedro posted:
Wow, there are some layers, here. At first I thought that if you changed while(false) to while(success) that it would fix the code, but then I realized that it just creates an infinite loop when success becomes TRUE. Then I realized that the programmer intentionally did this to make the code flow in a specific way; he wanted the code chunk in the do/while loop to run exactly once, but he also wanted to be able to break out of it and then run some other cleanup code if anything went wrong. There are better ways to do this, but breaking the do/while loop does give the desired flow. Neat e: Or what everyone above me said, that'll teach me to read further before posting. This is the kind of thing that you do when you want a single point of return and aren't comfortable with passing pointers, I guess. QuarkJets fucked around with this message at 19:32 on Apr 27, 2013 |
# ? Apr 27, 2013 19:28 |
|
The thing also leaves the file open if some errors happen and opens the file in (probably) the wrong mode. The sensible thing is just to use gotos for the control flow instead of making a funnylooking goto out of do...while and break: code:
|
# ? Apr 27, 2013 22:42 |
|
Funny how even after going to the trouble of using the single run loop as a ghetto goto for a cleanup block they still managed to miss some things to clean up. Well, without seeing the implementation of path_join I can't be sure, but it seems likely that path needs to be freed at some point.
|
# ? Apr 28, 2013 02:41 |
|
Suspicious Dish posted:
|
# ? Apr 28, 2013 16:16 |
|
Foxfire_ posted:The sensible thing is just to use gotos for the control flow instead of making a funnylooking goto out of do...while and break: No need for gotos or breaks code:
|
# ? Apr 30, 2013 02:49 |
|
Side effects? Not in my conditionals.
|
# ? Apr 30, 2013 02:51 |
|
My favorite part is the segfault when one of the early conditionals fails. It's the little touches...
|
# ? Apr 30, 2013 03:32 |
|
mjau posted:No need for gotos or breaks For some reason, the thing that bothers me the most here is just the lack of use of De Morgan's Law.
|
# ? Apr 30, 2013 04:28 |
|
Do while false really isn't that horrific a construct as weird C things go. Still grimace every time I see it used but I can't get too worked up about it. That malloc line is a classic C horror though.
|
# ? Apr 30, 2013 04:43 |
|
Suspicious Dish posted:My favorite part is the segfault when one of the early conditionals fails. It's the little touches... It looks technically correct (the best kind of correct) to me. The conditionals are required to short circuit out as soon as one is true, so the later ones never run.
|
# ? Apr 30, 2013 04:46 |
|
Huh, I thought free(NULL); would segfault, but it seems it's entirely safe. I didn't know.
|
# ? Apr 30, 2013 04:48 |
|
Suspicious Dish posted:Huh, I thought free(NULL); would segfault, but it seems it's entirely safe. I didn't know. Why do something like 90% of C and C++ programmers not know how malloc, new, free and delete work?
|
# ? Apr 30, 2013 04:53 |
|
I know how malloc and free work, I just didn't expect free to allow NULL as a special case, since to me it's an invalid input (you can't free a null pointer after all) and the C library typically just says "if you pass in an invalid input, it's your fault". But perhaps the utility of having unguarded frees is so convenient that they put it in there.
|
# ? Apr 30, 2013 05:00 |
|
Suspicious Dish posted:I know how malloc and free work, I just didn't expect free to allow NULL as a special case, since to me it's an invalid input (you can't free a null pointer after all) and the C library typically just says "if you pass in an invalid input, it's your fault". But perhaps the utility of having unguarded frees is so convenient that they put it in there.
|
# ? Apr 30, 2013 05:07 |
|
Suspicious Dish posted:I know how malloc and free work, I just didn't expect free to allow NULL as a special case, since to me it's an invalid input (you can't free a null pointer after all) and the C library typically just says "if you pass in an invalid input, it's your fault". But perhaps the utility of having unguarded frees is so convenient that they put it in there. Yeah. In your defense, I do agree that it's opposite of what you would expect from C (and delete in C++). Undefined behavior for a null pointer would be more in line with the design philosophy.
|
# ? Apr 30, 2013 05:08 |
|
Coding horror: a C++ code base where the author doesn't realize delete, like malloc, works with nulls. He then proceeds to manage all his memory manually, with null checks around the deletes.
|
# ? Apr 30, 2013 07:36 |
|
Scaevolus posted:Coding horror: a C++ code base where the author doesn't realize delete, like malloc, works with nulls. He then proceeds to manage all his memory manually, with null checks around the deletes. Ehhh, not realizing that delete works with nulls isn't a horror. Maybe you could describe some of the manual memory management? Is it particularly horrible or is the null checking the part that you find horrible?
|
# ? Apr 30, 2013 08:59 |
|
Suspicious Dish posted:I know how malloc and free work, I just didn't expect free to allow NULL as a special case, code:
|
# ? Apr 30, 2013 15:53 |
|
QuarkJets posted:Ehhh, not realizing that delete works with nulls isn't a horror. Maybe you could describe some of the manual memory management? Is it particularly horrible or is the null checking the part that you find horrible? code:
|
# ? Apr 30, 2013 16:14 |
|
Scaevolus posted:When this repeats for 20 members: argh, one quick function would have solved all that.
|
# ? Apr 30, 2013 17:05 |
|
Isn't mMembers 1 through 20 is the real horror? Or perhaps codebases that feel the need to delete something that possibly doesn't exist?
|
# ? Apr 30, 2013 17:20 |
|
evensevenone posted:Isn't mMembers 1 through 20 is the real horror? I'd hope that that is for example. (also: use smart pointers)
|
# ? Apr 30, 2013 17:24 |
|
evensevenone posted:Or perhaps codebases that feel the need to delete something that possibly doesn't exist?
|
# ? Apr 30, 2013 18:49 |
|
At the place I used to work, people couldnt be arsed reading the delete documentation so they made a template function to delete pointers code:
code:
|
# ? Apr 30, 2013 21:04 |
|
awesmoe posted:At the place I used to work, people couldnt be arsed reading the delete documentation so they made a template function to delete pointers Three checks if the pointer is null. Gotta really check hard. Just in case. It's the most important part of any program I'll have you know! One time I didn't check if a pointer was null and my computer completely destroyed itself.
|
# ? Apr 30, 2013 21:11 |
|
I simply can't fathom how large C++ applications and 3D games can even function, given how insanely complex C/C++ is, notably with regards to memory allocation. My hat goes off (or condolences?) to those to can make it work. Does anyone actually think C++ is fine and dandy, or would you/they prefer a more modern language with GC, a simple and useful standard library, etc., if they had the choice?
|
# ? Apr 30, 2013 21:27 |
|
Pilsner posted:I simply can't fathom how large C++ applications and 3D games can even function, given how insanely complex C/C++ is, notably with regards to memory allocation. My hat goes off (or condolences?) to those to can make it work. Does anyone actually think C++ is fine and dandy, or would you/they prefer a more modern language with GC, a simple and useful standard library, etc., if they had the choice? GC is frustrating and unpredictable and limited, RAII is much, much simpler than GC, c++ is fine wrt memory management as long as people don't write code like it's 1994. The standard library is incredibly limited but they're working on it.
|
# ? Apr 30, 2013 21:45 |
|
|
# ? Jun 8, 2024 07:46 |
|
Pilsner posted:I simply can't fathom how large C++ applications and 3D games can even function, given how insanely complex C/C++ is, notably with regards to memory allocation. My hat goes off (or condolences?) to those to can make it work. Does anyone actually think C++ is fine and dandy, or would you/they prefer a more modern language with GC, a simple and useful standard library, etc., if they had the choice? Well one of the reasons you would use C++ is because you don't want GC if you're writing stuff that performs well. It isn't that hard to make stuff not leak - if you're calling new or free or malloc this day an edge and aren't working on embedded software, you're doing poo poo wrong. Smart pointers and referenced counted smart pointers can handle everything for you, but the problem is that everyone has their own convention - your company needs to pick a convention for how to deal with memory allocation, have tools that enforce this convention on check in, and you need to run static analysis tools.
|
# ? Apr 30, 2013 21:46 |