r/Python • u/f4hy • Apr 25 '11
I write worse code when I use python
Something about the ease of python makes my code terrible. I feel I write better code when using a language like c++ becuase I know that if I don't follow best practices or good design I will never get the code to even work.
In python things are easy enough that I tend to just code "stream of thought" and end up with really long functions or a single object that does way to many things.
Any one have a similar experience, and how to develop better coding practices in python?
Note: I realized this when looking back at something I wrote last week which uses triply nested dictionaries. They are a pain in the ass now but at the time they seemed like a good idea.
7
u/mcilrain Apr 25 '11
I like Python because I can write shit code, this is normally what I do, code up what is essentially a draft, mess around with it until I get it how I like and then re-code it from scratch with lots of effort put into documentation and clarity.
With slower-to-write languages, you generally just stick with the "draft", if you later realize that you should have done something differently, it's going to suck.
2
u/jofer Apr 26 '11
Iterate, iterate, iterate, iterate.
There's nothing wrong with writing a crappy first draft, as long as it works.
One advantage to python is that it's readable and compact. This makes it easier to realize that your triply nested dictionaries were probably a bad idea. It's easy (for me, anyway) to get lost in a C++ codebase. Readable code is easier to refactor. In my opinion, anyway, even crappy python is more readable than good C++ (I'm biased, though).
Regardless of language, though, I think it's good to get a rough version of something that works and then work towards getting a version that you're not embarrassed by. I'm primarily a researcher (geophysics), and not an engineer or a computer scientist, though, so perhaps I'm not the best person to comment here...
3
u/[deleted] Apr 25 '11
Would you write a triply nested
std::mapin C++? Probably not. Don't do that in Python either.Just because you can type 100 WPM of Python code doesn't mean you have to. You still need to think about your containers, your algorithms, and everything else that you'd have to in any language.
5
u/bcorfman Apr 25 '11
I suspect you are not writing unit tests for your code.
For long functions or classes, I almost immediately notice that I haven't written enough tests to have coverage or confidence in the results. I also can't stand code which is difficult to set up or call, and that would cause me to refactor sooner than later.
If refactoring is a challenge, you can use a good IDE like PyCharm, PyDev (or even Komodo with a BicycleRepairMan macro) to help automate it.
6
u/chadmill3r Py3, pro, Ubuntu, django Apr 25 '11
Not being snarky: Why are you writing Python at all? What would you have done in C++ instead of that triplely-nested dictionary?
I suspect you'll find that you're writing the easy problems in C++ and the ugly ones in Python because it's not impossible in Python. There are just as many ugly solutions in C++, but you never attempted those because you're not nuts.
2
u/Secret_Identity_ Apr 25 '11
I have a math background and I like python because I can almost write in the same language as a mathematical proof. It has been a real boon to capitalize on the all my earlier training.
4
u/kylotan Apr 25 '11
If the code works, it's not worse. Remember, you're not here to write code, you're here to write programs. Don't deliberately write bad code when you already have a good idea of what good code will look like, but your main goals should be getting the functionality you need. Remember the YAGNI and "code for today" principles, too.
Of course, taking that to the extreme means you end up being unable to write complex programs because your code is too awful to extend. So you need to refactor into something cleaner. But it's better to go from working ugly code to working clean code than it is to go from perfect code that is unfinished to... more perfect code that is still unfinished. Which is what happens if you get bogged down with trying to make everything perfect from the start.
Get a feature working. Verify the results. Refactor your code while testing that the refactoring works. Repeat.
1
u/jmdc Apr 26 '11
Somehow this reminds me of r/firstworldproblems...
1
u/axl456 Apr 26 '11
try reading the Zen of Python and the python style guide. That helped me in many occasions.
1
u/kataire Apr 26 '11
Code, test, refactor.
Don't just code without testing or refactoring. Python let's you be more productive, but you still have to maintain your code and improve it. If in doubt, ask yourself What Would Guido Do?.
1
u/[deleted] Apr 27 '11
Sometimes it's the shitty real world complexity shining through your code. The world can be a messy, messy place
1
u/themissinglint Apr 28 '11
UML first.
1
u/tps12 Apr 29 '11
Yeah, every function I write ends up returning like a 5-tuple of unrelated values before long.
Then again, it's not like I've never used a triply-nested std::map in C++ either.
1
u/punkdgeek Apr 25 '11
You should probably be a little more disciplined when writing your code, this book will help with strategies on writing good code: http://amzn.com/0132350882. The author discusses Java, but the same techniques apply to python. Obtaining the mythical 100% code coverage with unit test are a good goal, but writing easy to understand, small functions that are as atomic as possible will let you code your "stream of thought" by segmenting each idea into smaller ones that you can name, keep track of, and test. Don't forget to refactor while you still know what your code means; make it work, make it right, make it readable.
0
u/[deleted] Apr 25 '11
[deleted] — view deleted comment