Earlier quoted context omitted.
When unsigned three = 1; is accompanied by unsigned five = 5; unsigned seven = 7; what would be the sane conclusion?
The reasonable conclusion is that you should read the surrounding context and try to understand what these variables are used for. The purpose of code review is to understand the code enough that you can spot real bugs; I try to avoid flagging surface-level stuff unless it's really important. IMO, this is a borderline case due to the very clear comment (which is a bit too far away).
`three = 1` in the Linux sourcecode (2014)
81–90 of 105 posts
Re: `three = 1` in the Linux sourcecode (2014)
#82Earlier quoted context omitted.
I once spent two days debugging confusing behaviour in a serial command interface before discovering the line: #define CR_LF "\n"
man, that hurts. When you found the issue were you happy or were you just depressed? I've been there and sometimes finding the root of an issue is not a eureka moment but more just a long, deflating, sigh.
"Write software like the next person to work on it is a psychopath and they know where you live"
Re: `three = 1` in the Linux sourcecode (2014)
#83Earlier quoted context omitted.
The reasonable conclusion is that you should read the surrounding context and try to understand what these variables are used for. The purpose of code review is to understand the code enough that you can spot real bugs; I try to avoid flagging surface-level stuff unless it's really important. IMO, this is a borderline case due to the very clear comment (which is a bit too far away).
And then, having located that comment, try to rename the variable to something more meaningful. Even gap1, gap2, and gap3 would be more informative.
Re: `three = 1` in the Linux sourcecode (2014)
#84Earlier quoted context omitted.
And then, having located that comment, try to rename the variable to something more meaningful. Even gap1, gap2, and gap3 would be more informative.
We have a difference of opinion here. I lean away from bikeshedding in favor of finding more substantive issues. I'm also an old-timer and I'd call this a 3, on a scale from 1 (easily readable code) to 10 (a suitable IOCCC entry)
But if it were my job to touch these lines of code, I would want to leave them better than I found them.
Re: `three = 1` in the Linux sourcecode (2014)
#85Earlier quoted context omitted.
Or at least require a comment explaining it, before someone needs to dig through the code. Powers of three, five, and seven sounds a little bit like Totvald's very own little FizzBuzz game hidden in ext4's file-system source code.
> Totvald's very own little FizzBuzz game hidden in ext4's file-system source code What's that?
- if the data block id is a multiple of 3, replace its contents with 0x03
- if the data block id is a multiple of 5, replace its contents with 0x05
- if the data block id is a multiple of both 3 and 5, replace its contents with 0x15
Re: `three = 1` in the Linux sourcecode (2014)
#86Earlier quoted context omitted.
'Without digging deeper' isn't really what matters here. A better question would be 'after some very cursory further digging, are we worried?' The first thing I ask myself when I see something that looks as wrong as this does is "Is it possible for this piece of code to be completely broken, and for no one to have noticed yet?" This is really a question about two processes - the one where people look at, read, edit a…
> The first thing I ask myself when I see something that looks as wrong as this does is "Is it possible for this piece of code to be completely broken, and for no one to have noticed yet?" Good reflex. In my first job out of school (big research lab), we would occasionally find odd bugs in the code base (as would be expected...bugs happen). The head of the project would often wonder, “how could the system ever have f…
We were called in to help them because their management (and users) were aware they were underwater with the amount of bug fixes, live production data changes they made to keep things working, and impossibility of adding anything new to the system without tipping something else over.
It seems the attitude that "there's no chance we have any bugs" is pretty prevalent anywhere I've ever been, along with the usual mix of "how did this ever work" which I still always find pretty shocking to run across.
Re: `three = 1` in the Linux sourcecode (2014)
#87Many years ago in an age of klocs and flowcharts, at a large three-letter computer company, there were iron-clad coding rules that must be obeyed, no questions allowed. In general you could see their reasoning, but such bureaucratic reasoning doesn't pay off. One example of this was that all numeric values used in a program must be factored out as symbolic constants. The reason for doing this is obvious, but it faile…
Re: `three = 1` in the Linux sourcecode (2014)
#88Earlier quoted context omitted.
I would do this: #define I 1 #define II 2 #define III 3 ... #define XIII 13 There is no zero in roman numerals, but you can use NULL for extra fun! (yes, I am a bad person)
For extra fun, put it into the code like this, including the `...` instead of the actual values. (Make it compile by commenting that line out.) Then define IV somewhere else but make sure to use III before every use IV so someone unfamiliar with these constants see the “right” definitions first.
iv = secrets.randbits(IV)
plaintext = "Text for encryption"
aes = pyaes.AESModeOfOperationCTR(key, pyaes.Counter(iv))
ciphertext = aes.encrypt(plaintext)
print('Encrypted:', binascii.hexlify(ciphertext))Re: `three = 1` in the Linux sourcecode (2014)
#89Re: `three = 1` in the Linux sourcecode (2014)
#90Earlier quoted context omitted.
It's "obvious" if you take into account the corporate culture I guess. The rule of 'no magic numbers' is generally good, but then one has to remember that some numbers (like 0 and 1) are not magic.
0 and 1 can be magic if the context depends on them being arbitrarily 0 or 1. eg. // this byte is unused and always 0 #define UNUSED_CONSTANT_FIELD_VALUE 0 bytes = {HEADER, UNUSED_CONSTANT_FIELD_VALUE, [...]}
the range of the type is the non-negative integers up to (2^32)-1
semantically, the value cannot be null
when the value is 0, the code treats it as a special case
null is treated as 0