Hacker News new | past | comments | ask | show | jobs | submit login
Writing clean, testable, high quality code in Python (ibm.com)
175 points by m3mb3r on Dec 20, 2010 | hide | past | favorite | 21 comments



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.


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.


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).


IBM has many quality articles, like this. Thanks for sharing.


Let's hope that there will be more like this :).


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.



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.


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... if you have lots of free time.


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'.


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.


s/\?\?\?/'Less time fucking around debugging stupid errors'/


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.


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


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


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.


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.


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


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).


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


I completely disagree. IBM really do "get it" - they are a company that quietly makes money as they're embedded into corporates, but they have a very open culture these days




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: