Re: "with" Coders are Monsters
- From: "m. Th." <a@xxxxx>
- Date: Fri, 13 Jul 2007 11:09:43 +0300
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.
.
- Follow-Ups:
- Re: "with" Coders are Monsters
- From: Nick Hodges (CodeGear)
- Re: "with" Coders are Monsters
- References:
- "with" Coders are Monsters
- From: Johnnie Norsworthy
- Re: "with" Coders are Monsters
- From: Michael Little
- Re: "with" Coders are Monsters
- From: John Herbster
- Re: "with" Coders are Monsters
- From: Michael Little
- Re: "with" Coders are Monsters
- From: David Erbas-White
- Re: "with" Coders are Monsters
- From: Nick Hodges (CodeGear)
- Re: "with" Coders are Monsters
- From: Michael Little
- Re: "with" Coders are Monsters
- From: Nick Hodges (CodeGear)
- "with" Coders are Monsters
- Prev by Date: Re: Native SQLite support in Delphi?
- Next by Date: Re: Funny story
- Previous by thread: Re: "with" Coders are Monsters
- Next by thread: Re: "with" Coders are Monsters
- Index(es):
Relevant Pages
|