4 ms·
But on what object? The fact that calling .MoveTo(10, 30) would return the same object it has been called on, and not, for example, a handle to created movemen
by golergka 10y ago
But on what object?
The fact that calling .MoveTo(10, 30) would return the same object it has been called on, and not, for example, a handle to created movement animation, is not exactly obvious.
Modern pattern of methods which just return the objects that they've been called on, by default seems to be very trendy, but I fail to see how is it helpful.
- nostrademons 10y agoSame way that you use any other API: by reading the docs. This is at least a very common pattern; it's highly likely that whoever maintains this code will have seen it before. And if you're using an IDE, the IDE will even tell you this as you hit '.' BTW, it doesn't have to be the same object: if memory is abundant and CPU is not, it can often make sense to create a fresh object with each call. Doing this lets you share partially-constructed objects without worrying that mutation will result in surprising effects: let box = NewCanvas() .DrawLine(50, 0) .DrawLine(50, 50) .DrawLine(0, 50) .DrawLine(0, 0) let scene = [ box.Fill(Color.RED).TranslateTo(200,300), box.Fill(Color.BLUE).TranslateTo(100, 300), box.Fill(Color.GREEN).TranslateTo(400, 500) ] scene.forEach(box => box.Draw())
- golergka 10y ago> Same way that you use any other API: by reading the docs. Best APIs are written in such an obvious way that you don't need to read the docs to read the code and understand what's happening.
- nostrademons 10y agoThat doesn't apply to any of the code examples posted so far in this thread: they would all require documentation about side-effects, shared state, immediate vs. delayed effects, etc.
- julian_t 10y agoEr... on the window, as the indentation and API tells you. It may be "not exactly obvious" to you, but having been doing GUI programming for a long time, this makes complete sense to me. It is also, of course, a matter of personal style and preference. (Aside: some friends tell me, a vi user, that the way Emacs works is obvious. I disagree, but then, I don't use it). And as for "modern"... I've been using this since C++ gained references.
- golergka 10y agoAre you sure? Because to me it looks that first call should return a canvas, and all consecutive calls operate on it.
- Stratoscope 10y agoThe indentation misled you here. The first method call returns a child canvas object that the other methods act on. See my comment nearby in this thread for an alternative way to indent that avoids this confusion.
- golergka 10y agoI think it just proves my point. Do we really want indentation to be that critical to making code readable?