Re: "with" Coders are Monsters



Nick Hodges (CodeGear) wrote:
[...]


I'm strongly against the use of with -- I think it is bad code and
creates many potential problems. You maybe don't think that.

I'm cool with our difference of opinion. Code as you see fit.


Yep. But as Product Manager you have to support it. (read: guide your user base to use it 'correctly')

Aside to the proposal from the other eMail, I suggest you another one related with our theme. It's a research (RFC) about variable declarations.

1. The problem:
To improve the quality of code we must have:
- the variables declared near to place of their use and in the desired scope and only in the desired scope (see McConnell - CC2)
- any of the identifiers must be in-place human-readable

2. (My) proposal:
a. Improve the control structures with in-line variables which are valid *only* in the structure's scope.
Eg:
with oCrtObj: TMyVeryLongObjectUnit.TMyVeryLongObjectName do
begin
oCrtObj.Prop1:=Val1;
oCrtObj.Prop2:=Val2;
oCrtObj.Method1;
...
<oCrtObj accessible only here!>
end;

for nCounter: integer = 1 to 10 do
begin
<nCounter accessible only here!>
end;

for oCrtObj in TMyCollection counter i: integer do
begin
<i accessible only here!>
end;

if bStatus = Sin(x)=Pi()/2 then
begin
<bStatus accessible here...>
end
else
begin
<...and bStatus accessible here only!>
end;

<the same goes for while..do>

Of course, if the vars are already declared or wrong typed, a compile time error is thrown.

b. Introduce the near-in-place variable declaration. (Even I have some 'feelings' about this, but let's discuss it...). IMHO, it will grow (sometimes) a lot the code maintainability:

procedure Form1.MyVeryBIGAndComplexMethod(Sender: TObject);
var
<lots of vars here>
begin
...
<lots of code here>
...
var
begin
lTemp: TStringList; //if it's already declared then err.
cMsg: string; //if it's already declared then err.
end;

lTemp:=TStringList.Create;
lTemp.LoadFromFile('c:\bla');
cMsg:='';
for i: integer = 0 to lTemp.Count - 1 do
cMsg:=cMsg+lTemp[i];
ShowMessage(cMsg);

lTemp.Free; //the endvar is only for hidding not for res dealloc.

endvar //the scope of the vars ends here - they become 'hidden'
begin
lTemp: TStringList; //this should be freed here? - no.
cMsg: string; //in fact this is automatically deallocated
end; //endvar

...
<more lots of code here>
...
end;

IMHO, this helps (sometimes a lot!) in debugging/maintaining the code, knowing with much more granularity the variable's scope.

hth & my 2c,

--

m. th.
.



Relevant Pages

  • running a function in a different scope
    ... __call__ method to execute the aforementioned function when the instance ... instead of the scope: ... Now the function "vars" does not bother me that much. ...
    (comp.lang.python)
  • Re: Finding the name of a variable used in the caller of a function from within the function itself.
    ... Erwin Moller wrote: ... A function should be totally stand-alone in scope and whatever it needs from the outside world should be passed into it via the argument list. ... This works for any var in the global scope, so vars inside the function itself, other functions, or classes wouldn't be visible. ... You could add a second argument, which would accept an array of defined vars from another scope ...
    (comp.lang.php)
  • Re: || explanation in ruby... in pseudolanguage
    ... upgrades variable scope if it is used outside of the scope (and within a ... p a # accessing a upgrades its scope. ... But block parameters and explicitly declared block local variables are not ... if all vars in block are local it's tough to get one out! ...
    (comp.lang.ruby)
  • Re: scope issue of variable in iterator
    ... With no variable declarations in Ruby, there's no way to control the scope, so the "first occurrence dictates scope" rule applies to Mohammad's example. ... I think that the pipe operators should be treated analogously to the way that C++ treats declarations that occur within an expression; viz., they are scoped to the end of the block dependent upon the expression. ... int i = 5; ...
    (comp.lang.ruby)
  • Re: beginning with ML
    ... Equality tests can't loop. ... dummy value, set up the new scope and assign new scope to the dummy value ...
    (comp.lang.functional)