Live data from Hacker News

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

news.ycombinator.com

261–267 of 267 posts

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

#261

My number one favorite time is after I sold a company. Two years later, the company contacted me as the old CTO and asked me about some piece of core technology. They asked me to take them through it and show them what was going on since they wanted to migrate it into something else. My first question was, "Haven't the other developers ever touched it." They said no. This core piece of technology scaled to way more u…

Wow. You should be proud indeed. Turned out you maintained enough corner cases right there.

How big was it, and did you write it?

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

#262

When I was ~15 I wrote this: $c=`clear`;for(`tail -c+324 $0|zcat`){if(/^p/){print@l;@l=()}elsif(/^d(.+)/){select$u,$u,$u,$1/10}elsif(/^s(.)/){$s=9;$e=-30;$m=-1;if('o'eq$1){$s=-30;$e=9;$m=1}for($i=$s;$i!=$e;$i+=$m){$j=0;for(@l){print substr($_,$i+$j++>0?($i+$j) 2:0,-1)."\n"}select$u,$u,$u,.1;print$c}}elsif(/^c/){print$c;@l=()}else{push@l,$_}}__END__ Which (with ~1.5KB of binary data appended, total under 2KB) creates…

That's really neat. I'm guessing this is Lemonade? https://github.com/shish/lemonade

(Also, leave a blank line before your code and indent each line of it by two spaces to make it formatted like code.)

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

#263

Earlier quoted context omitted.

I think there are levels: Level 1: Garbage code with no or bad comment. Level 2: Garbage code with good comments. Level 3: Good code with comments Level 4: Code good enough that it doesn't need comments, with rare exceptions. Each level is better than the previous. You can't level up directly from 1 to 4. Still, 4 is the best level. I know it sounds weird if you haven't seen it.

I don't think level 4 exists. There is no code for problems of sufficient complexity that are self-explanatory. There are too many hidden assumptions and foreknowledge and tradeoffs and decisions baked into a block of code that, unless it's trivial, there's no way code itself can reflect it adequately.

It does exist, but only for snippets, sections of code, or at best whole individual functions, especially in languages that are concise and expressive (which are most languages that are not Java ;-). But even really good code still needs comments about how things fit together, what to find where, etc., because those things are generally not expressible in programming languages, although a good module system can help a lot with that.

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

#264

Earlier quoted context omitted.

I think there are levels: Level 1: Garbage code with no or bad comment. Level 2: Garbage code with good comments. Level 3: Good code with comments Level 4: Code good enough that it doesn't need comments, with rare exceptions. Each level is better than the previous. You can't level up directly from 1 to 4. Still, 4 is the best level. I know it sounds weird if you haven't seen it.

I don't think level 4 exists. There is no code for problems of sufficient complexity that are self-explanatory. There are too many hidden assumptions and foreknowledge and tradeoffs and decisions baked into a block of code that, unless it's trivial, there's no way code itself can reflect it adequately.

I have three things to say about that:

1. I did allow for "rare exceptions". Some things do need to be explained outside of the code. Maybe we're not so different after all :)

2. There are a number of techniques to write such code. Before I learned them, I had NO IDEA. One is to take what would have been comments and use them as variable or function names. This includes (and I had real resistance before I accepted this) breaking out a variable of function only in order to give it an informative name. "Level 4" doesn't just happen. You work at it for a long time and sharpen your skills in that area.

3. To me, much of the art of writing software is to find ways to divide a complex problem into simple pieces. If my code is real complex, I look for simpler way to write it. And I look hard.

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

#265

Earlier quoted context omitted.

I have, and I do use it for projects (and plan on using it for more). But I also need C for things that require certain performance I'm unable to get out of Go.

Use Rust, then. Rust is like Go, except its binaries are small and fast (except string formatting, which is very runtime, horribly bloated and merely "fast-ish"). Anything C can do, Rust can do better (the string formatting excepted).

What is it with people insisting on using something other than C? You don't know what I'm working on, yet you're insisting I operate completely differently?

It's so weird to me.

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

#266

When I was ~15 I wrote this: $c=`clear`;for(`tail -c+324 $0|zcat`){if(/^p/){print@l;@l=()}elsif(/^d(.+)/){select$u,$u,$u,$1/10}elsif(/^s(.)/){$s=9;$e=-30;$m=-1;if('o'eq$1){$s=-30;$e=9;$m=1}for($i=$s;$i!=$e;$i+=$m){$j=0;for(@l){print substr($_,$i+$j++>0?($i+$j) 2:0,-1)."\n"}select$u,$u,$u,.1;print$c}}elsif(/^c/){print$c;@l=()}else{push@l,$_}}__END__ Which (with ~1.5KB of binary data appended, total under 2KB) creates…

That's really neat. I'm guessing this is Lemonade? https://github.com/shish/lemonade (Also, leave a blank line before your code and indent each line of it by two spaces to make it formatted like code.)

It is indeed :) Looking at the code now, I see so many opportunities for “improvement” like making it run on OSX as well as Linux; but I think that might spoil the magic :P

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

#267

Earlier quoted context omitted.

Remember timecube guy? Seen on a 16-bit DSP... struct timecube { uint16_t millisecond_of_minute; uint16_t minute_of_month; uint16_t month_and_year; };

That's clever. I might steal that at some point. Or combine into a millisecond_of_month on a 32 bit processor. For people that don't play with bits often: Unsigned 16-bit integers have their max as 65,536. Storing data often wastes most of the bits, since a field that goes from 0-1000 doesn't fit in an 8-bit integer, and C doesn't have native integers between 8 and 16 bits long. (You can mask out relevant fields, but…

It also cooperates with leap seconds, by counting faithfully up to 61,000 on such a minute.

IIRC, month and year was a 4-bit and 12-bit bitfield just as you describe.

Post reply on HN