Live data from Hacker News

Throw away your first draft of your code

ntietz.com

151–160 of 230 posts

Re: Throw away your first draft of your code

#151
I have a different approach: I'm ok to throw away significant chunks of code as long as I have important new ideas that allow me to do more with less.

Often the problem with refactors is that don't come with enough of these new ideas , there's no real progress, it's mostly moving things around.

Re: Throw away your first draft of your code

#153
This would sound like questionable advice except that the best code I wrote in college resulted from an accident…

I’d sat down all night and written some C for a graphics class. Everything was great, yay, victory. Proceeded to tar it up per class guidelines and did something like this:

tar cvf assignment.c assignment.h data.txt readme whatever.cuz

And as many of you no doubt recognize, I’d forgotten to supply a destination name before listing the source files.

So the code was gone. My brain was gone, too, so I explained what happened and sent what I had, including a working executable, to the TA. He gave me an extra day to finish.

So the next day I sat down and wrote it all again and as mentioned above, it was some of the best code I’d written. Doing it again from the top saved all sorts of dead ends and mistaken approaches.

Re: Throw away your first draft of your code

#154
The famous comedian John Cleese speaks about this where he loses a script and has to rewrite it from memory before a deadline. Then he finds the old one and finds out his new one is much better despite thinking it was the same. So he started using it as a technique for better script writing. It is referenced here but I can’t find the video https://www.ideaconnection.com/right-brain-workouts/00186-jo...

Re: Throw away your first draft of your code

#155
post #132
post #129

As an ML-focused python dev I have never been able to break the habit of REPL-driven development, but I find it works really well for "building code that works" rather than coming up with a tower of abstractions immediately. A typical python development workflow for me is: * Start with a blank `main` file and proceed linearly down the page, executing as I go. * Gradually pull out visually awkward chunks of code and p…

by REPL here you mean jupyter notebook?

I also primarily write ML-focused Python. For me, having originally learned R and C at the same time, nothing has ever surpassed RStudio as a dev environment. For the past several years my preferred setup has been tmux and Vim with vim-slime in one pane and IPython in the other.

(Personally, and speaking only for myself, I hate Jupyter notebooks with a burning passion. I think they are one of the worst things ever to have happened to software development, and definitely the worst thing ever to have happened to ML/data science.)

Re: Throw away your first draft of your code

#156

This would sound like questionable advice except that the best code I wrote in college resulted from an accident… I’d sat down all night and written some C for a graphics class. Everything was great, yay, victory. Proceeded to tar it up per class guidelines and did something like this: tar cvf assignment.c assignment.h data.txt readme whatever.cuz And as many of you no doubt recognize, I’d forgotten to supply a desti…

This is exactly the type of story I tell when people are anti pair programming. I ask them if they’ve ever lost a days code (usually asking if they ever used source safe) and that when they finally knuckled down to write it again, it took about 20 minutes. As the writing the code wasn’t really the hard part or the bottleneck, solving the problem was the hard part. Hence pair programming having two heads on one problem to solve it better/faster, find the edge cases etc.

Out of curiosity, was it really fast for you to do it in that extra day or did it take all day?

Re: Throw away your first draft of your code

#157

I like to build my first version in one file with as few abstractions as possible. No helper functions, unless absolutely necessary, just straight-forward code. Building good abstractions later on is a lot easier than starting with wrong ones in the first place.

This is an underrated way of doing things that I've come to appreciate over time. Heck, I'll even go live with the all-in-one-file thing until there's a reason to split it up. It's all peanuts anyway compared to bigger decisions like what API, DBMS, DB schema, other deps, libs, etc you use.

Definitely agree, if it works (tests prove it works) it can go live.

Re: Throw away your first draft of your code

#158
post #129

As an ML-focused python dev I have never been able to break the habit of REPL-driven development, but I find it works really well for "building code that works" rather than coming up with a tower of abstractions immediately. A typical python development workflow for me is: * Start with a blank `main` file and proceed linearly down the page, executing as I go. * Gradually pull out visually awkward chunks of code and p…

I too agree with pretty much everything you say. Just want to add that I pretty much solely use ptpython[0]. It can handle line breaks in pasted code, vim (or emacs) bindings and syntax highlighting, and much more.

[0] https://github.com/prompt-toolkit/ptpython

Re: Throw away your first draft of your code

#159
post #108

Earlier quoted context omitted.

I don't really feel that attraction to complete rewriting. I wonder if the people who do are very smart, and thus able to hold more state in their head, so ugly code bothers them more even if it's not actively a problem, because they are able to have background tasks in their mind to worry about it? And at the same time, perhaps their code is less encapsulated, because they didn't optimize for abstraction, they optim…

Smart people can ignore ugly code. It's the people who get easily confused that need to see clean and easy to understand code.

Humans always make mistakes and are tired now and then, and the smartest people can still appreciate not needing to solve puzzles just to understand or debug their own code.

Of course clean and readable means very different things to different people, but I don't thing I ever regretted cleaning up code, while I can think of a lot of instances where not doing it wasted a lot of time.

Re: Throw away your first draft of your code

#160

My boss doesn't allow me.

Only if you told them you were done, when you weren’t (if following an approach described in the article). I suspect the author also meant, it won’t take longer with this approach. Possibly even faster that without it is completely viable.
Post reply on HN