Can some1 review my code?

Martin P. Hellwig martin.hellwig at dcuktec.org
Sun Apr 19 07:29:48 EDT 2009


zaheer.agadi at gmail.com wrote:
> hi
> 
> I am from java background, I have written some code that actually
> works :)
> Need to some one to look at it and tell me if there are better ways of
> doing same things
> 
> Will some one help?
> 
> Thanks

My crystal ball is a bit cloudy today so forgive me if my suggestions 
may seem off-topic in your request.

I find that reviewing code that hasn't conform to 'standards' distracts 
me from the original intent. Parsing your code through a lint like 
PyLint will suggest 'standard' way of doing things (aim for 10/10) it 
will make things easier to read.

Also code that is unnecessary bloated (e.g. conditions that are never 
executed) can confuse the issue even more so. A good way would be to 
write unit-test against your code out of the perspective of the intended 
use (so not just to execute stuff because it is there) then run it 
through a coverage program. This will display nicely what code is 
executed and what not. The not part is what you look for in this 
perspective since if all the unit-test have what you need, why should 
the uncalled code be included?

Next stop comments, I am all for comments except if you need them to 
translate what the piece of code is doing there, if you can refactor 
your code so it looks more English and is understandable without 
comments (which should be still in there though) then do so.

For example:
range_8 = range(8)
for number in range_8:
     # Loop through 0 to 7 and print it.
     print(number)

instead of:
r = range(8)
for i in r:
     # print i in range(8) loop
     print(i)

* note this is a bad code example, only to clarify what I mean in general

Then look at your code from a functional perspective, if there are 
multiple functions that act in a similar way can you replace them with a 
single function? Then do so.

Is there functionality that looks so common that you think it should be 
in the standard library? It probably is then, make use of it.

Performance; run your code through a execution analyser like profile, do 
the numbers make sense? If not try to figure out why and replace the 
affected algorithm with one that makes more sense.

Finally memory;  Can you scrap duplicate entries of essential the same 
data, without affecting all the above? Might be a good idea to do so.

When done all this you might feel it is not necessary to review the code 
any more, which is then is a good moment to actually request a review :-)

I'll be happy to have a look at it though you might consider posting it 
here, more chance of useful feedback ;-)

-- 
MPH
http://blog.dcuktec.com



More information about the Python-list mailing list