Refactor Eliminate With

last modified: January 28, 2009

You may find that some other recordset or object is referenced more often, or that there are just too few references to the "With" object in its block to justify the "With ... End With" block.

Great for eliminating the FeatureEnvy smell that "With" can paper over.

Languages:

Related Refactorings:


In VbClassic: from...

With rsObject
   .Open ...
   If .RecordCount = 0 Then
      .AddNew
      !PK = value
   End If
   !Attr = value
End With

to...

rsObject.Open ...
If rsObject.RecordCount = 0 Then
   rsObject.AddNew
   rsObject!PK = value
End If
rsObject!Attr = value

PascalLanguage: from...

with emp do
begin
   name       := "Joe";
   status     := "X";
   with bday do
   begin
      month :=    6;
      day   :=   27;
      year  := 1958;
   end
end

to...

emp.name       := "Joe";
emp.status     := "X";
emp.bday.month :=    6;
emp.bday.day   :=   27;
emp.bday.year  := 1958;

However

With rsObject
   .Open ...
   If .RecordCount = 0 Then
      .AddNew
      !PK = value
   End If
   !Attr = value
End With

is immaculately clear, and prevents a lot of needless typing. This particular example is one in which I would use a WITH construct if my language provided it. Absolutely.


ContributorsList: JeffGrigg, (name of Pascal example author missing)


[CategoryRefactoring/RefactoringLanguage]


Loading...