4 ms·
Let's take a library implementing http as an example. To be a good abstraction, it should let you retrieve a page in one line of code: page_string = urlope
by ntoshev 17y ago
Let's take a library implementing http as an example. To be a good abstraction, it should let you retrieve a page in one line of code:
page_string = urlopen('http://google.com')
There are a lot of default choices made for you here: all the headers to send, to follow redirects or not, what to do with cookies returned if there are subsequent request to the same domain, etc. There is also information thrown away, like most of the response headers. You may also want to get access to the response as an I/O stream instead of loading it at once in memory.
A good library will be transparent in the sense that it would let you modify each and every one of these defaults and access the information under the covers if you need to, I see no way around this even as the industry matures, or you will get into the Larry Page's sort of trouble desperately trying to modify your user agent string.
I wonder if there is a generic way to make libraries as transparent as possible in a uniform way, perhaps having language support for "default" parameters and a syntax to alter them. Yes, it goes directly against encapsulation.
- andreyf 17y agoGood example. it should let you retrieve a page in one line of code Agreed, but not the property of abstractions I talk about in this post. A good library/language would let you cast the response of urlopen to whatever it is you need - a String, an IOStream, even a DOM object, maybe even do it automatically using type hinting/inference. Parameters are just an optional dict of data to go with the request, could easily be passed an optional variable.
- ntoshev 17y agoYes, the library designer have to anticipate what you could possibly need to alter and expose it as optional parameter. Just looking at how Python's urllib/urllib2 have evolved with versions shows this is no easy task (otherwise the developers of the standard lib would have get it exactly right the first time). And it gets tougher when you actually want to modify behavior (like handling http redirects). The support for modifying behavior in popular languages is basically events, inheritance / interface implementation, and composing pre-built objects to get the desired behavior. This leaves me wanting to have a better way.