Help me pick an API design (OO vs functional)
davea at davea.name
Tue Mar 26 11:26:30 CET 2013
On 03/26/2013 05:06 AM, Michael Herrmann wrote:
> On Tuesday, March 26, 2013 12:11:34 AM UTC+1, Ethan Furman wrote:
>> On 03/25/2013 12:29 PM, Michael Herrmann wrote:
>>> notepad_1 = start("Notepad")
>>> notepad_2 = start("Notepad")
>>> notepad_1.write("Hello World!")
>>> notepad_1.press(CTRL + 'a', CTRL + 'c')
>>> notepad_2.press(CTRL + 'v')
>> This is the way to go. Just move your global functions into the Window object (or whatever you call it), break
>> backwards compatibility (major version number change, perhaps?), and call it good.
>> It makes much more sense to call methods of several different objects (which is explicit -- you always know which object
>> is being used) than having a magic function that changes the object in the background (plus you now have to search
>> backwards for the last magic invocation to know -- and what if a called function changes it?).
> Your points are valid and I cannot really say anything against them. The problem with moving all global functions into the Window object is that this adds a lot of syntactic baggage that isn't needed in 90% of the cases. We really prefer the simplicity of
> write("Hello World!")
> press(CTRL + 's')
> write("test.txt", into="File name")
> press(ALT + F4)
> notepad = start("Notepad")
> notepad.write("Hello World!")
> notepad.press(CTRL + 's')
> notepad.write("test.txt", into="File name")
> notepad.press(ALT + F4).
> Also, there's a problem here: The "Save" dialogue that opens in the above script is technically a different window so in theory you would have to introduce a new object to distinguish between the original window that lets you edit your text document from the "Save" window. This is very tedious and error-prone. You are right, though, that we have to do some logic in the background to remember the last window.
> In light of this, could you live with something along the lines of design #4?
> notepad_1 = start("Notepad")
> notepad_2 = start("Notepad")
> write("Hello World!")
> press(CTRL + 'a', CTRL + 'c')
> press(CTRL + 'v')
Seems to me that the official interface should all be methods. However,
you could have a new object which always represents the "focus" window.
Then the USER could define trivial functions:
Somewhere in this thread you mention that save() creates a new window,
so a method-oriented approach would require that the user get that
window object, and call its methods rather than the original window's.
I say that's a very good thing, since the things you send may very well
have very different meanings to the save dialog than they do in the
Another concern I'd have is what happens if the user changes focus with
his mouse? Does that change the meaning you had for focus() in the
above exchange? Do you want your press() method to apply to a different
window when the user changes to that window?
More information about the Python-list