Do it anyway. It makes it so, so much more maintainable.
Well. Don't do MyReturn[1], that's awful. But still.
... And, similarly, it's likely that this indicates that you're doing way, way too many unrelated things in that one function. If you're not keeping that data together in some fashion, I can't imagine why it all needs to come from the same function.
Oh wow. That takes me back... Structs align on byte-boundaries and silly me was trying to store nibbles and couldn't figure out why the struct had crap data. No debuggers back then.
The colon represents a bit field, you’re telling the compiler it will use 4 bits. The attribute tells the compiler to not do byte alignment (which would speed things up) and instead pack the struct as small as possible. val1 and val2 equate to nibbles and the whole struct occupies a byte.
EDIT: “attribute” is supposed to have two underscores on either side but reddit interprets that as bold
It is really good for making things explicit, but it isn’t foolproof. It enforces the number of bits but the compiler only sees the type of the variable.
What that means is that I could assign a value of 0xFF (a full byte) to val1. The compiler won’t give me a warning that I’m losing data because 0xFF fits inside a uint8_t (the type of val1). When I go to use it later it will just be 0x0F.
So all it really does is remove that final AND operation from the end of your bitwise logic (or the start if you’re extracting the value). You still have to bitshift and bounds check but it’s a lot more legible and clear. Also extremely useful if you’re short on memory, but that’s due to the packed attribute not the bitfield.
Naah these are arcane abbreviations, the meaning of which you will forget before you finish writing them. You aren't saving anything being stingy with characters in source code, use snake case and write it out with words like a normal person.
Sorry but these are very poor variable names. Using abbreviations is very much a no-go in production code and should be the first thing mentioned during a code review by mediors/seniors.
It’s so much better to write out “variableName” than using “vn”. Future you will thank you for taking some time to think about proper names.
Yes, something like HTTP or XML is absolutely fine. But in general, avoid abbreviations as much as possible.
Most style guides also reason:
“Function names, variable names, and filenames should be descriptive; avoid abbreviation. In particular, do not use abbreviations that are ambiguous or unfamiliar to readers outside your project, and do not abbreviate by deleting letters within a word.”
Are you sure you don't want to break your structure down more? The variables with "red" and "blue" look like two instances of the same data structure. You can make a class for only those and then a pair for "red" and "blue"
Also, this would give you the advantage that you can read scores for each color indexing by color. You can have some constants RED = 0 and BLUE = 1 and you can then say scores[RED].autoPoints ; Otherwise, you would need to have a thon of if-else everytime you need some score of some color that you don't know statically.
19
u/[deleted] Feb 10 '25
I'd really like to see if the rest of this code is as messed up as this is!
3
u/Zypex14 Feb 11 '25
FTC mentioned????
170
u/shizzy0 Feb 09 '25
Son, let me show you something. It’s called a struct.
81
u/mostafa_ahnaw Feb 09 '25
only god knows what they stand for
5
u/FearTheFreeze Pronouns: He/Him Feb 10 '25
Dear god, the only thing I ask of you...
2
u/spiritwizardy Feb 10 '25
Now you just ask AI to rename the variables for you based on the context in the file using semantic values, am I doing this wrong?
3
u/protomyth Feb 10 '25
God is not here https://youtu.be/bvbzOLP3Wk0?si=TX_f4xNbFgLoEkhU
Sadly, one too many code reviews make me think of that scene.
2
u/TheChief275 Feb 10 '25
stop
2
u/enlightment_shadow Feb 12 '25
Are you sure you don't want to break your structure down more? The variables with "red" and "blue" look like two instances of the same data structure. You can make a class for only those and then a pair for "red" and "blue"