Aside from the bug, what makes this bad in your opinion?
Just trying to understand as I could easily see myself writing something like this and having it regarded as "bad code". The lack of commenting about the approach is definitely not great... but I can still make out what it intends to do?
A simple thing, but how about moving all that code in the if block into a separate, well-named function, so that the logic and flow is easier to reason about, and you only have to crowd your mind with implementation details if you need to?
Beyond that, I'll let someone with more knowledge of C tell you. I think I see some magic numbers that could be dealt with.
Most of my life is spent in C and you’re most likely correct on all your points (although it depends on the situation).
I would add that they should be probably be using an enum for that 0x53 in the if condition, the header and bitmapheader char array should be a struct.
Also I don’t particularly like how flSz is being reused.
In comparison to a lot of the C code I’ve seen, this isn’t too bad lol
Hey, I never get to see C code here! I love C. And this is horrible and funny. That said, it does remind me of a hack I once was in a PNG decoder. See PNG chunk headers have a header name that is 4 characters. You could take a pointer to that, cast it to a pointer-to-int, and use int comparison to check what the header name was. Way faster than char comparison and a for loop! In retrospect, probably should've used vector functions instead.
You're assuming the endianness of your system. Most of the C code I've written normally involves writing for embedded platforms so you either do the shifting stuff the op does or htonl stuff other commentators have stated; just in case you have to port to a platform that's big endian. Normally gcc will do the optimization for you if it's relevant.
20
u/[deleted] Sep 15 '21
Aside from the bug, what makes this bad in your opinion?
Just trying to understand as I could easily see myself writing something like this and having it regarded as "bad code". The lack of commenting about the approach is definitely not great... but I can still make out what it intends to do?
Just wrote this for getting some insight :)
29
u/Opposite_Bus_3385 Sep 15 '21 edited Sep 15 '21
flSz[3] = imBytes >> 32;
That doesn't work.
34
u/keddir Sep 15 '21
At first I thought it is some kind of disassembled code
15
u/Matt5sean3 Sep 15 '21
This doesn't seem horrible. I guess there are some things to take issue with, but it seems mostly fine.
htonlinsteadWhat am I missing?
7
u/Espiring Sep 15 '21
The most triggering is the space between * and imageF
5
u/botle Sep 15 '21
It's not too bad. It's just C.
4
u/Igotbored112 Sep 15 '21
Hey, I never get to see C code here! I love C. And this is horrible and funny. That said, it does remind me of a hack I once was in a PNG decoder. See PNG chunk headers have a header name that is 4 characters. You could take a pointer to that, cast it to a pointer-to-int, and use int comparison to check what the header name was. Way faster than char comparison and a for loop! In retrospect, probably should've used vector functions instead.
2
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Sep 15 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
1
u/WhenSharksCollide Sep 15 '21
I'm not sure if the eye hurty colors are intentional but it's not helping the code look any better.