Lambda the Ultimate

inactiveTopic With
started 6/12/2003; 2:22:16 PM - last post 6/15/2003; 5:15:17 PM
Dan Shappir - With  blueArrow
6/12/2003; 2:22:16 PM (reads: 1574, responses: 14)
With
I introduced "With" to VBA because Excel macros were full of cases where you did ten things to the same cell, range, chart, etc. For example if you have a macro that creates a chart, you need to set about 23 options on the chart before it looks the way you like it. In the user interface, you select the chart and do 23 things. This maps nicely to opening a with block and doing 23 things, so the macro recorder could generate with blocks and thus create vastly cleaner code. (Try it... the macro recorder generates With blocks all the time).

A discussion of the With statement in the context of the various PLs that implement it, and those that do not. Includes comments from Joel Spolsky himself, who apparently introduced this facility to VBA.

Posted to critiques by Dan Shappir on 6/12/03; 2:23:24 PM

Adam Vandenberg - Re: With  blueArrow
6/12/2003; 2:41:22 PM (reads: 1008, responses: 1)
Link to the specific thread: http://discuss.fogcreek.com/joelonsoftware/default.asp?cmd=show&ixPost=50125

Ehud Lamm - Re: With  blueArrow
6/12/2003; 11:36:33 PM (reads: 976, responses: 0)
Ada renames which provides a more general facility for renaming declartions can be used to achieve similar goals.

procedure x13 is

type t1 is record x:integer; end record; type t2 is record x:t1; end record; type t3 is record x:t2; end record; v:t3; x:integer renames v.x.x.x; begin v.x.x.x:=5; x:=10; end;

Dominic Fox - Re: With  blueArrow
6/13/2003; 1:08:40 AM (reads: 930, responses: 1)

Compare

document.all.someElement.style.color = 'red'; (JavaScript)
and
ActiveWorkbook.Sheets("Report").Cells(2, 4).NumberFormat = "@" ' Excel VBA
with
Document applyStyleAttribute: red toStyle: color ofElement: someElement
and
ActiveWorkbook applyNumberFormat: "@" toCell: 2@4 inSheet: "report"
(Smalltalk, kinda).

The sort-of-Smalltalk code adheres to the "Law of Demeter": instead of drilling down through a series of collections via the dot syntax, a top-level method provides access to the functionality of objects further along the chain with actually exposing the objects themselves. Smalltalk's syntax makes this kind of coding look cleaner than the VBA/Javascript approach.

I use With a lot in VB coding, because VB makes it easy to string chains of methods together via the dot syntax and painful to do so repeatedly. A lot of the libraries (Excel VBA being a case in point) are designed with the assumption that this is the way you will access objects at a low level of granularity (inner containers of inner containers, or subdivisions of subdivisions), given an object representing an initial context. But the problem With addresses - the verbosity and (potential, depending on how smart your compiler is) inefficiency of repeatedly passing requests along a chain of methods - is a side-effect of this approach, which Demeter-ish Smalltalk syntax effectively removes.

andrew cooke - Re: With  blueArrow
6/13/2003; 5:48:33 AM (reads: 929, responses: 0)
fwiw, a "standard" java hack is to return "this" and chain methods. although i suspect this doesn't work with bean conventions (where setters should probably be void).
class Foo {
  Foo setColour(Colour c) {c_ = c; return this;}
  Foo setSize(int i) {i_ = i; return this;}
  Foo setBar(Bar b) {b_ = b; return this;}
}

myFoo.setColour(red) .setSize(20) .setBar(baz);

Isaac Gouy - Re: With  blueArrow
6/13/2003; 7:00:59 AM (reads: 875, responses: 0)
"standard" java hack is to return "this" and chain methods
That's the default behaviour for Smalltalk methods, when there isn't an explicit return value.

colour: aColour
   colour := aColour

size: aNumber size := aNumber

myFoo colour: #red; size: 20.

Chris Rathman - Re: With  blueArrow
6/13/2003; 8:52:19 AM (reads: 843, responses: 2)
windows.get(TOP).controls[12].canvas.moveto(10,20);
Isn't such chaining an indication that the objects are somehow tightly coupled. Isn't there some law or principle of encapsulation that's being broken? I see code like this in VB quite a bit, but I can't help but wonder whether the object system is really just a glorified multidimensional dictionary (as opposed to array).

Dan Shappir - Re: With  blueArrow
6/13/2003; 12:17:48 PM (reads: 799, responses: 1)
Isn't such chaining an indication that the objects are somehow tightly coupled. Isn't there some law or principle of encapsulation that's being broken?

Such chaining is very common in object hierarchies such as a browser (DHTML) or XML DOM. In this context the objects are tightly coupled as their position in the hierarchy is usually a part of their definition. Also, such objects are unlikely to appear outside that context.

I do find, however, that explicitly specifying such long chains is an indication to the brittleness of the code accessing the DOM. I prefer to navigate to the target object and operate there.

Ehud Lamm - Re: With  blueArrow
6/13/2003; 12:22:02 PM (reads: 824, responses: 1)
Reminds me of a discussion I had with of my students this semester. We discussed the fact that many product APIs (think about Microsoft) are presented as class libraries. I argued that it is far from certain that this is the best approach, and that the rationale behind these designs is the fact that dominant languages are object based (think Javascript, VBA). Obviously, there's a vicious circle: less dominant languages must accomodate these APIs, or else they have no chance of gaining market share.

Oh, by the way, we got to discussing this issue while talking about MS Scripting Host

Dan Shappir - Re: With  blueArrow
6/13/2003; 12:35:09 PM (reads: 851, responses: 0)
Maybe this is the reason so many developers appear to be having some sort of cognitive dissonance with SAX. It breaks the usual DOM mold (as well as being "in reverse").

The issue you mention is certainly valid to my mind, especially in the context of .NET

Ehud Lamm - Re: With  blueArrow
6/13/2003; 12:36:55 PM (reads: 832, responses: 0)
Such chaining is very common in object hierarchies such as a browser (DHTML) or XML DOM.

Sure, but still...

To an SQL guru everything looks like a database table, to to OOP programmers everything looks like class hierarchy. But that's just our paradigms talking ("if all you got is a hammer...")

Of course, if we are programming in an OOP language it is natural (and logical) to design APIs that work this way. What worries me is that this lead to lock-in and people tend to forget that other designs are also possible.

Isaac Gouy - Re: With  blueArrow
6/13/2003; 1:36:29 PM (reads: 799, responses: 0)
this leads to lock-in

This seems to have been one of the things that bothered Richard Gabriel: Objects Have Failed: Discursive Notes

9. People enthused by objects hogged the road, would not get out of the way, would not allow alternatives to be explored—not through malice but through exuberance—and now resources that could be used to move ahead are drying up. But sometimes this exuberance was out-and-out lying to push others out of the way.

Dominic Fox - Re: With  blueArrow
6/14/2003; 12:40:30 PM (reads: 751, responses: 0)

Dan Shappir wrote:

Isn't such chaining an indication that the objects are somehow tightly coupled. Isn't there some law or principle of encapsulation that's being broken?

The Law of Demeter (don't call methods on objects returned by methods on other objects, or "only talk to your immediate friends") may be; but it's open to question whether that really pertains to encapsulation per se: each object in the chain is exposed via the public interface of another object, after all. The point of the Law of Demeter is to reduce coupling, and to avoid situations where a change in one class's interface has to be propagated not only to that class's clients but to its clients' clients, and so on.

Ehud Lamm wrote:

To an SQL guru everything looks like a database table, to to OOP programmers everything looks like class hierarchy. But that's just our paradigms talking ("if all you got is a hammer...")

I did some work this week building some convenience classes for XML-based REST applications - classes for use by an ASP author to retrieve an XML document sent via HTTP POST, for instance. What motivated me to do this, instead of just using the MS-supplied SOAP Toolkit and binding web service invocations straight into methods on COM objects, was the realisation that SOAP used for RMI is a way of making everything look like a method invocation. The disguise isn't altogether convincing. For one thing, the object created to service the method call is discarded as soon as the method returns, so it isn't even the same as accessing a remote component via DCOM. For another thing, the mappings between parameter types and XSD schema types are automatic only for a range of simple types, after which you have to write your own custom handlers anyway. It struck me that what I wanted from web services was resources that could be accessed by any client that could send and receive XML to and from a URL, not a verbose and crippled version of DCOM/CORBA. The MS SOAP toolkit actually makes creating and accessing web services tolerably straightforward, but it does also tend to lock you into to an object/component way of looking at the domain rather than (for instance) a resource/state transfer approach.

Frank Atanassow - Re: With  blueArrow
6/15/2003; 9:25:42 AM (reads: 687, responses: 0)
Richard Gabriel: Objects Have Failed: Discursive Notes

This is pretty interesting. Might be worth a front page topic thread.

Isaac Gouy - Re: With  blueArrow
6/15/2003; 5:15:17 PM (reads: 685, responses: 0)
Might be worth a front page topic thread.
Something for an editor to consider.

introduced "With" to VBA
From Modula2 to Oberon, this kind of WITH statement was removed!
(Although one suspects that was partly so the keyword could be given a different purpose.)

Looking back over the development of that Pascal, Modula, Oberon2 family of languages, it's striking how much experimentation has been aimed at removing features from the language. (The FOR loop was removed in Oberon, and snuck-back 3 years later into Oberon2.)