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