Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)be banned ?
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Sun, 06 Jan 2008 10:26:20 -0600
Skybuck Flying wrote:
No the point is:
If the variable version is banned then you can't make that programming mistake.
The mistake being that of passing a static-array variable to the SizeOf function. There's of course no problem with doing that since the size of the variable really is the size of the type.
If you do try to make that mistake the compiler will give you a compile error.
And then a good programmer like you will automatically know what the problem is, must use SetLength for dynamic arrays.
OK, so when I try to pass the variable to SizeOf, the compiler will complain and I'll know to fix something. The fix will not be to call SetLength, though, since that doesn't tell me anything about the size of the variable.
Instead, I'll need to go look up the declared type of the variable and enter that type as the argument to SizeOf instead of the variable itself. The program compiles and all is well.
But now I decide I should have a dynamic array instead of a static array. So I go change the type declaration of the array type. I remove the array bounds from the declaration, press Ctrl+F9, and everything continues to compile.
Oops. The compiler didn't catch the mistake. The mistake that you were hoping to eliminate by forbidding asking for the size of a variable. Being required to ask for the size of a type doesn't gain you anything in this scenario.
--
Rob
.
- References:
- Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable) be banned ?
- From: Skybuck Flying
- Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable) be banned ?
- From: Rob Kennedy
- Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)be banned ?
- From: Rudy Velthuis
- Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)be banned ?
- From: Skybuck Flying
- Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)be banned ?
- From: Rob Kennedy
- Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)be banned ?
- From: Skybuck Flying
- Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable) be banned ?
- Prev by Date: Re: Skybuck's Ban List for 2008 and beyond etc.
- Next by Date: Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)be banned ?
- Previous by thread: Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)be banned ?
- Next by thread: Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)bebanned ?
- Index(es):
Relevant Pages
|