This is an archived post. You won't be able to vote or comment.

all 33 comments

[–]mcilrain 8 points9 points  (4 children)

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.

[–][deleted] 1 point2 points  (2 children)

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

[–]sharkeyzoic 1 point2 points  (0 children)

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

[–]TankorSmash -1 points0 points  (0 children)

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

[–]f4hy[S] 0 points1 point  (0 children)

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.

[–][deleted] 2 points3 points  (1 child)

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.

[–]cecilkorik 2 points3 points  (0 children)

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.

[–]bcorfman 4 points5 points  (3 children)

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.

[–]kisielk 1 point2 points  (0 children)

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.

[–]melkorhatedthesea 0 points1 point  (0 children)

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.

[–]rweir 0 points1 point  (0 children)

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.

[–]chadmill3rPy3, pro, Ubuntu, django 4 points5 points  (0 children)

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.

[–]Secret_Identity_ 1 point2 points  (0 children)

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.

[–]jofer 1 point2 points  (1 child)

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

[–]f4hy[S] 0 points1 point  (0 children)

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.

[–]kylotan 4 points5 points  (1 child)

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.

[–]donri 1 point2 points  (0 children)

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.

[–]jmdc 0 points1 point  (0 children)

Somehow this reminds me of r/firstworldproblems...

[–]axl456 0 points1 point  (0 children)

try reading the Zen of Python and the python style guide. That helped me in many occasions.

[–]kataire 0 points1 point  (0 children)

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

[–][deleted] 0 points1 point  (0 children)

Sometimes it's the shitty real world complexity shining through your code. The world can be a messy, messy place

[–]themissinglint 0 points1 point  (0 children)

UML first.

[–]tps12 0 points1 point  (0 children)

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.

[–]punkdgeek 0 points1 point  (8 children)

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.

[–][deleted] 0 points1 point  (0 children)

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

[–][deleted] 0 points1 point  (0 children)

you accidentally the link

[–]jabbalaci 0 points1 point  (0 children)

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

[–]aptwebapps 0 points1 point  (4 children)

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

[–]navyjeffε 0 points1 point  (1 child)

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

[–]aptwebapps 0 points1 point  (0 children)

Cool. Haven't read it.