[okfn-dev] coding style

William Waites ww at styx.org
Wed Jan 26 17:30:41 UTC 2011


Shall put words to this effect on the coding style wiki page,
but wanted to bring it up on the dev lists first (sorry for 
duplicates, crossposting).

Some things that I've noticed in the various codebases that
strike me as very ugly:

Things like this:

    def _start_call_timing(self):
        c.time_call_started = self._get_now_time()

    def _get_now_time(self):
        import datetime
        return datetime.datetime.now()

there is no need whatsoever for these methods or ones like
this. That's two function calls and five lines of code for
what could easily be written as:

    c.time_call_started = datetime.now()

Also, do not import packages, especially standard ones in
the body of a function / method unless there's a good reason
(like the feature is optional and might throw an import error)

Next, there are three problems with the following method:

     @classmethod
     def _uri(self, uri):
         if isinstance(uri, basestring):
             return URIRef(uri)
         else:
             return uri

first, there is a bug, URIRef inherits from unicode and from
basestring so the isinstance check fails -- but this is just
a bug, no big deal.

second, why is this a method and not a utility function? it
has nothing at all to do with the self argument. Functions 
and methods should use all of their arguments otherwise they
belong elsewhere.

third, when using @classmethod, the first argument should be
cls and not self. self is for instances.

That's all for now...

-w
-- 
William Waites                <mailto:ww at styx.org>
http://eris.okfn.org/ww/         <sip:ww at styx.org>
F4B3 39BF E775 CF42 0BAB  3DF0 BE40 A6DF B06F FD45




More information about the okfn-labs mailing list