Live data from Hacker News

Ask HN: Do you ever go back and admire a piece of code you wrote?

news.ycombinator.com

91–100 of 267 posts

Re: Ask HN: Do you ever go back and admire a piece of code you wrote?

#91
post #22

Not entirely mine but beautiful nonetheless: @dataclass class Node: id: str children: List[Node] = field(default_factory=list) # beautiful Python: traverse a tree depth-first, pre-order (stack based) def __iter__(self): stack = [self] while stack: node = stack.pop() yield node stack = node.children + stack

You should really use deque for the stack

It's what I did in other parts of the project but I haven't noticed any performance increase. I guess standard lists are already fast enough for implementing stacks.

Re: Ask HN: Do you ever go back and admire a piece of code you wrote?

#93

Earlier quoted context omitted.

Except for the return to happen when that's true, you'd need an await. At least in JS.

Not if it's returned by an async function. In JS, async functions automatically await promises that are returned.

That's not actually right. An async function will always return a promise, so if you return a promise directly it's indeed pretty much the same as if you return a promise that waits on that promise; it's the calling function that actually awaits on the returned promise.

The main difference would be error handling: if you have a catch block that returns a different value, returning the promise directly would actually throw in the calling function, whereas if you "return await", it's going to get caught in your catch block, resolving the promise to your own custom value.

Found a detailed explanation here: https://jakearchibald.com/2017/await-vs-return-vs-return-awa...

Re: Ask HN: Do you ever go back and admire a piece of code you wrote?

#94
post #11

The only thing I have ever gone back and admired is when I wrote the following snippet of code: long time; // no see

I once came across some code, written by a coworker, that consisted of deeply nested blocks. I added a comment that simply contained a URL:

  // http://photos3.meetupstatic.com/photos/event/4/b/c/2/600_436939394.jpeg
I don't know if anyone ever noticed this comment and followed the URL, but I hope they did and were amused. (The code was eventually refactored/rewritten, so the comment no longer exists in the codebase.)

Re: Ask HN: Do you ever go back and admire a piece of code you wrote?

#96

All the time. The old joke is about us looking at code we wrote six months ago, and being horrified at what we see. And I've had more than my share of that. But what's ALSO happened, many times, is I look at old code I wrote... and I'm IMPRESSED. Just blown away by the beautiful elegance and power of the abstractions I came up with, the sheer intelligence of the approach, the insight and lucidity oozing from the code…

Me writing docstrings: God this is so much work for nothing... my code should speak for itself. Me 6 months later: Thank god I wrote docstrings, what is this garbled mess.

100%. It's amazing how many GitHub repos have little to no comments, not even file or function overview comments. The amount of time save by potential contributors reading and having to interpret the code is way longer than it would take for the author to write the comments in the first place.

To each their own, but comments are generally a positive addition to code.

Re: Ask HN: Do you ever go back and admire a piece of code you wrote?

#97
Everyone's talking about code, but I do this with carpentry. Right now, I'm working as a trim carpenter in residential construction, and I pride myself on doing as good a job as I can. As a result, when I come back to a job to install door hardware after everything is painted, I sometimes just wander around the house and admire my work. Especially tricky details that took a bit of thinking to get right, even if no-one else will ever notice them.

Re: Ask HN: Do you ever go back and admire a piece of code you wrote?

#98
I've had the joy(?) of working for a company for close to 10 years, moving on to other work for about 5 years, and then returning again.

During my first tour with the company I built, among other things:

- (on a two-person team) an ASP.NET web app that allows payroll admins to extract year-end data from their AP in order to distribute tax forms (W-2s, 1099s, etc.)

- (different two-person team) a mail-room management back-end that allowed us to print/mail over 1 million forms/year for customers of the above

- (yet another two-person team) A corporate intranet product

Since returning to the company I've been on another small team (3 developers) building an independent payroll platform. As part of that effort, we've had to integrate with all the other things I ever built at the company. It's been extremely valuable for our team that I have intimate knowledge of basically all of our integration points across the company. Much of my original comments and code from ~2007-2015 are still in the source tree. Some of that makes me happy, and some of that makes me cringe :)

Re: Ask HN: Do you ever go back and admire a piece of code you wrote?

#99
Best feeling I ever got was when I was starting out using list comprehensions and ternaries in Python. I sometimes got the (slightly creepy) feeling I wasn't so much writing this thing myself but using a feather duster carefully to clean away the cobwebs sort of like an archaeologist. That feeling of "discovery" is what I use to guide me. If it's not there, I know there's a better implementation. Got me thinking about Socrates and how discovery could really just be recollection. So my take is a little bit opposite what you are calling for. It's not "I wrote that!" with pride but "I was used to bring that about!".

Re: Ask HN: Do you ever go back and admire a piece of code you wrote?

#100
Yes. I wrote some Rust code about 3 years ago. I've set it aside until the IDE situation improves, and now I look at it to remind me that I wrote that (not too shabby) code because I want to pick up Rust again at some point and if I went by the tone of the articles on HN (Rust is hard, you're constantly fighting the borrow checker, etc) it would be too discouraging.
Post reply on HN