
Writing clean, testable, high quality code in Python - m3mb3r
https://www.ibm.com/developerworks/aix/library/au-cleancode/
======
barnaby
Nice, this goes into some ground not covered in other python testing
discussions. Great code examples. :-) These are the kinds of articles I come
to hacker news to find.

~~~
phugoid
If you have other (better) people poking around your code and giving you a
hard time, you'll probably keep improving your strategy. But if you're working
alone like I am, it's very important to land on articles like this.

~~~
barnaby
Very true, I've been in the working alone situation, and I think it led to
some bad habits (then again, I've been in big corporations which led to even
worse habits, so... yeah).

------
grovulent
This is a really helpful article for me. I'm a programming newb and have been
banging out ad hoc code for the last six months. Boy do I have some mega-
functions. I've known enough to know that at some point this practice has to
change, but I've been a bit stuck as to how to start making improvements. So
I'm looking forward to working through this example.

~~~
Jach
<http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-4.html> :)

~~~
grovulent
Thanks! Do you think it's really a good idea to be looking at LISP based
stuff? I've barely begun to wrap my head around python.

~~~
Jach
Every programmer should learn a little Lisp! Even if you just get through the
first two chapters I think it would be helpful, and many of the ideas carry
over effortlessly to Python. (Higher order functions, closures, functions
returning functions, etc.) I spend most of my free time coding in Python but
learning Scheme was great for me, and the styles go really well with event-
driven designs you have to implement a lot in JavaScript/ActionScript.

There's also a pretty good series of recorded lectures Berkeley gives in
conjunction with the book over at [http://academicearth.org/courses/the-
structure-and-interpret...](http://academicearth.org/courses/the-structure-
and-interpretation-of-computer-programs) if you have lots of free time.

------
hessenwolf
I would like to be able to share this, amiably, with a few of my colleagues
and bosses who are not programmers, as we recently had to judge a $half-
million piece of software with 2 600 line cashflow functions and I was the
only one complaining.

But, yeah, look, the article is 'fine', but not much more, and very
unfortunately.

Does anybody know of a similar article re modularisation with a more
transparent example, and a more detailed transition between hypothesis and
conclusion? He just seems to say 'less complexity', ???, 'profit'.

~~~
stiff
There is this "Working Effectively With Legacy Code" book by Michael Feathers
that talks primarly about various ways of transforming very messy code to make
it testable.

------
deepu_256
Write tests, run code-coverage and code-smells. While good advice, that's just
saying what to do. Not how to do. You can as well remove the word Python from
the title and the advice holds true in general.

What i would like to see :- what in python we can use that will help us write
quality, concise code. Things like using list comprehensions, using magic
methods, higher order functions(decorators...) and may be something else that
i didn't knew.

~~~
hessenwolf
Do you really think that list comprehensions and magic methods make code more
readable, or did I read that the wrong way around?

~~~
deepu_256
They make code concise.List Comprehensions, magic methods - Yes . implementing
__len__ on your class and using len() everywhere(with list, string and your
new class) is better than coding a method called get_count() , total_count(),
etc etc.

They help make code more declarative rather than filling it with
implementation logic. Anyone can just read the code and understand it instead
of getting bogged down with the implementation. Especially list
comprehensions. [len(i) for i in k] is much better than reading a for loop
implementation looping through k and append len of each object in the loop to
a result array

~~~
hessenwolf
Is that last list comprehension really much better though, keeping in mind
that most of us code in several languages, and might be predominantly context
experts rather than coders at the same time?

x = [] for i in k: x.extend(len(i))

is really, really easy for me to understand instantly, if I have just been
writing an actuarial model using R, c++, c#, vba, sql, html, css and python
for the day, which I have.

Shortness is not always conciseness, methinks.

------
qjz
I use a similar commenting style, but in addition to the description,
arguments and returns, I also list globals used (if any) and side effects
(even if none). I find it very helpful to show if a function depends on or
alters state.

------
bedroomfireflys
IBM's just trying to get brownie-points with web developers because they know
that they won't be around much longer.

~~~
barnaby
Au contraire IBM get it, they got on board with big industry trends a long
time ago, and keep surviving (not thriving, mind you, but keeping up).

~~~
borism
Not thriving? They're amazingly profitable as far as I know.

