Thinking a code design through
One of the essential things of good design is to think things through. For example, which of these products is well designed?
or
It's clear to see the difference between a good design and a bad one. Or it should be. In my opinion the difference between a good and bad design is that a really good design has been given a lot of thoughts and the designers have tried to reduce the design to the essential parts. E.g. why do I need stickers on my laptop? Or why would I need a touch pad and 10 different other buttons and a scrolling wheel - - when it's possible to do these things with just the touch pad. Good design applies to code as well and it's clear to see when a design is well thought and when a design is crammed together. Let me do an example in code. I am doing a framework where I need request and response objects. I have taken a look at these two libraries:
Looking through the code of these two projects one will see a clear difference between a design that's well thought and one that's crammed together. Let me just paste in code and you can do your own conclusions: WebOB Request classclass Request(object):
## Options:
charset = None
unicode_errors = 'strict'
decode_param_names = False
## The limit after which request bodies should be stored on disk
## if they are read in (under this, and the request body is stored
## in memory):
request_body_tempfile_limit = 10*1024
def __init__(self, environ=None, environ_getter=None, charset=NoDefault,
unicode_errors=NoDefault,
decode_param_names=NoDefault, **kw):
if environ is None and environ_getter is None:
raise TypeError(
"You must provide one of environ or environ_getter")
if environ is not None and environ_getter is not None:
raise TypeError(
"You can only provide one of the environ and environ_getter arguments")
if environ is None:
self._environ_getter = environ_getter
else:
if not isinstance(environ, dict):
raise TypeError(
"Bad type for environ: %s" % type(environ))
self._environ = environ
if charset is not NoDefault:
...
Werkzeug's Request classclass Request(BaseRequest, AcceptMixin, ETagRequestMixin,
UserAgentMixin, AuthorizationMixin):
"""Full featured request object implementing the following mixins:
- `AcceptMixin` for accept header parsing
- `ETagRequestMixin` for etag and cache control handling
- `UserAgentMixin` for user agent introspection
- `AuthorizationMixin` for http auth handling
"""
Lesson to learnDesigning beautiful designs is done by thinking things through.. Personally, I have been bad at cramming things since it's the fastest way to accomplish a task, but keep this in mind when you hack something together:
8. Nov 2008
•
Code
·
Code improvement
·
Code rewrite
·
Python
|
|