(b) in the absence of an explicit type it's unclear whether the control variable should be interpreted as signed or unsigned (etc.).
(b) Unless I'm overlooking something, this can be derived from the initial and final value expressions
How does one prevent (for lack of better / more neutral words) "code dilution". That is, how does one prevent to have to many declarations amidst the code that expresses the algorithm?
Or how do others deal with reading code, that requires continued skipping forward over inlined declarations? And eventually extra scrolling just to see the remainder of the code?
[...] (lets assume, even if not always true, that for understanding "what the code does" the exact type of each variable is not immediately needed / i.e. can be checked at a later/different time)
Declarations are part of the algorithm. It does matter if I declare a variable as byte or qword, in relation to speed, memory usage, and behavoir.
Interesting, that you should bring up range issues in the 2nd part, yet overlooking them in the first.
for n := func_qword_A to func_qword_B - 1 do ....; // or func_cardinal, if your target arch does not support qword
What would the loop var be?
- qword? That overflows to $FFFFFFFFFFFFFFFF if the func_qword_B returned zero. A long running loop. (And cheap work around will fail, -1 is also a computed value and can be much bigger substraction)
- int64? That overflows if the func_qword_* return a bigger value. (and if used in expressions in the loop, the sign may matter a lot)
As for the relation to my question. I get your point, but in my way of working it does not apply.
Looking at the 2 probably most common cases that I may read the code:
- general understanding
- searching a bug (by code review, instead of debugger / though in some cases that may matter for a debugger run too)
In (1) I can simple rely that ordinal types have proper size, and it wont matter. At best it will matter if I see that code intentional over/underflows to wrap around. (e.g. a pointer into a roundbuffer with size 2^8, 2^16, 2^32). But then the algorithm/code will tell me that it does rely on the overflow, and again I know the size must be chosen to match.
EDIT: yes, you could conceive some highly optimized math code that shortcuts stuff by using overflows... For me that makes a "below tiny" fraction of my readings (currently 0 / but of course may happen / see gain/loss calculations)
In (2), I will have to check that size, there you are right I have to travel the extra distance.
But then in (2) that is a once off check. I loose 1 or 2 seconds in this case. Most of the reading I have to do is for (1), so I do save a lot more time in those readings than I loose in the few cases of (2).
So yes, I can and do understand specific cases where (leaving all else aside) it "makes sense". But I can't get the overall math to work. I can't just leave away that there are other tasks too.
But maybe that mix is different for everyone.... I do work on code-bases that
- are worked on by teams
- have considerable age (decades)
So encountering code I either don't know or don't remember is frequent.
Any time saved in "reading for understanding" far outweighs any time used when I need to know the type or declare it.
I guess, if I only did small stuff, that I finish in a month or two, and then never need to change again => then the amount of time for reading would be less.
Actually, that is occasionally the case (and I do have experience with inline declares). When I need a "one off throw away tool/script" I may do that in some script language, and I may do inline vars. But, I only work on that code for usually less than an hour, maybe 2 or 3 in extreme cases. Hardly any reading of code needed.
When I had to recover such code later (really rare), it has bitten me... (When I done bigger stuff in JS, I moved declarations to the top whenever possible)
The most common issues (and that is inline other than "for" vars) is that I know there is a variable "foo", I don't see where it is declared, and I try using it before its declared. And I then spend time on resolving that error.
Again, not wanting to stop anyone, if they feel its better for them (as long as no one expects me to read it). Just broadening my understanding of what/why others do...