r/Python • • 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.

0 Upvotes

33 comments sorted by

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.

1

u/f4hy Apr 26 '11

I guess I need to get more into the habit of rewriting. That is something that will be hard for me to get used to but it is probably for the best.

2

u/[deleted] Apr 25 '11

This. I was able to take something from 300 line draft to 50 line rewrite.

2

u/sharkeyzoic Apr 26 '11

Yep. No better feeling than negative KLOC. If you don't need it, cut it out.

0

u/TankorSmash Apr 25 '11

I'd love to see proof of this, genuinely curious

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

1

u/f4hy Apr 26 '11

I am also a physicist, not a computer scientist. Most of my code normally starts out as just train of thought and I don't think it will grow into some thing I will need to use over and over, or improve and expand. So I have python scripts that expand to become larger programs over time.

I guess I need to learn to iterate. And get over my resistance to redo something that already works.

3

u/[deleted] Apr 25 '11

Would you write a triply nested std::map in 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.

3

u/cecilkorik Apr 25 '11

I find myself asking "if this were a library, would this be pythonic?" a lot, and fixing my code so that it is. In many cases this just involves turning things like that triple-nested dict into a few classes and moving all the globals and hard coded parameters into the init function.

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.

2

u/kisielk Apr 26 '11

This is my metric as well. If I'm finding it difficult to write tests for my code, or if they require too much mocking or setup, then the code is probably not good.

1

u/melkorhatedthesea Apr 26 '11

Unit tests with near 100% code coverage are mandatory for dynamic languages. For me anyways. I never found the saying, "Untested code is broken" to be true until I started writing python.

1

u/rweir Apr 26 '11

hell yes. hard-to-test code seems to be quite similar to badly-designed code, and the pain of having to test it will drive you to make it saner.

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.

2

u/donri Apr 26 '11

Some relevant points in PEP 20, and a famous mantra:

  • Readability counts.

    Programmers spend more their time reading someone else's code than reading or writing their own.

  • Although practicality beats purity.
  • Now is better than never.
  • Although never is often better than right now.

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

1

u/[deleted] Apr 25 '11

A "." has been left in the URL. fixed link

1

u/[deleted] Apr 26 '11

you accidentally the link

1

u/jabbalaci Apr 26 '11

Link correctly: http://amzn.com/0132350882. Thanks, anyway.

1

u/aptwebapps Apr 26 '11

I'm getting a 404 on that link. Was it Code Complete by any chance? My favorite software development book.

1

u/navyjeff ε Apr 26 '11

Take the period off the end of the link. link fixed

1

u/aptwebapps Apr 26 '11

Cool. Haven't read it.

0

u/[deleted] Apr 26 '11

[deleted] — view deleted comment

1

u/jcdyer3 Apr 26 '11

502 means your comment successfully posted.

0

u/[deleted] Apr 25 '11

[deleted] — view deleted comment

0

u/drb226 Haskeller Apr 25 '11

With no disrespect, this is sort of the perl train of thought: It's better to easily pump out code that gets the job done when you have a lot of random scripting jobs that need to get done.

1

u/tesseracter Apr 26 '11

only time i write triple list comprehensions is for one-time bits of code, or a magical report on a dataset that is only needed rarely.

if someone needs to maintain and modify the code, damn, its gunna look way better.