Re:Writing clean, testable, high quality code in Python
by barnaby on Sunday December 19, 2010 @10:36PM
(#2023220)
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.
Re:Writing clean, testable, high quality code in Python
by phugoid on Sunday December 19, 2010 @11:50PM
(#2023357)
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.
Re:Writing clean, testable, high quality code in Python
by barnaby on Monday December 20, 2010 @03:03AM
(#2023713)
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).
Re:Writing clean, testable, high quality code in Python
by grovulent on Monday December 20, 2010 @01:08AM
(#2023515)
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.
Re:Writing clean, testable, high quality code in Python
by Jach on Monday December 20, 2010 @12:06PM
(#2024939)
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.
Re:Writing clean, testable, high quality code in Python
by hessenwolf on Monday December 20, 2010 @04:20AM
(#2023833)
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'.
Re:Writing clean, testable, high quality code in Python
by stiff on Monday December 20, 2010 @04:37AM
(#2023870)
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.
Re:Writing clean, testable, high quality code in Python
by deepu_256 on Monday December 20, 2010 @01:59AM
(#2023614)
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.
Re:Writing clean, testable, high quality code in Python
by deepu_256 on Tuesday December 21, 2010 @12:28PM
(#2028516)
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
Re:Writing clean, testable, high quality code in Python
by hessenwolf on Wednesday December 29, 2010 @05:23AM
(#2048424)
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.
Re:Writing clean, testable, high quality code in Python
by qjz on Monday December 20, 2010 @01:32PM
(#2025242)
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.
Re:Writing clean, testable, high quality code in Python
by inovica on Monday December 20, 2010 @06:25AM
(#2023984)
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
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.
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'.
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 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
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.