Live data from Hacker News

Please, kill your darlings

blog.ikura.co

21–30 of 84 posts

Re: Please, kill your darlings

#21

I like that type of code, so yes, maybe it's a darling. It's easy to make it more readable by aligning the code with the dots, so that it becomes a pipeline: @sentence = @sentence.split(' ') .map!{|x| x = x[0..0].upcase I definitely prefer that to half a screen page of crappy imperative code, where people over time will add lots of side effects etc. Besides, the middle part is clearly a strawman because @sentence = @…

`map!`, `<<`, and `0..0` are all unnecessary and distracting here. The availability of `capitalize` aside, `map`, `+`, and `0` would be better choices.

Re: Please, kill your darlings

#24
In this thread are literally 300 comments saying "Huh, I don't even Ruby, and I understood it", thereby (in my opinion) completely proving the point.

It's exactly because people pull this sort of thing "Hey, it was really easy to understand for me, how about you?" that I have seen developers feel compelled to put clever oneliners in codebases. Clever oneliners that later end up causing problems for whatever unluckly newbie has to troubleshoot their edge cases.

Just stop it, and unless you're on a word budget or this drastically improves performance, do something sensible that looks like the pseudocode:

  for word in sentence.split(' '):
      word.capitalize_first_letter
(If you want to be all functional, by all means use a map instead.)

So I don't have to deal with the case where your clever function barfs on (hypothetical example, don't think this happens with the current code) the special case where there is a space between the last word and a period that ends the sentence; and you're trying to capitalize the period. [Edit: As expected, looks like this code fails with even common graphemes. Which is fine, but is an argument towards at least trying to make it more comprehensible.]

Re: Please, kill your darlings

#25
post #14

I'm curious to see how more experienced Rubyists on HN would write this. My stab: @sentence.split(' ').map(&:capitalize).join(' ') More terse and more descriptive (imo)

Different behaviour.

capitalize downcases the rest of the each sub-string, the original code did not.

Re: Please, kill your darlings

#26
post #14

I'm curious to see how more experienced Rubyists on HN would write this. My stab: @sentence.split(' ').map(&:capitalize).join(' ') More terse and more descriptive (imo)

pretty experienced rubyist here, would use your version instead of what's in the blog unless benchmarking indicated a penalty that was going to hurt in some egregious way.

Re: Please, kill your darlings

#27

In this thread are literally 300 comments saying "Huh, I don't even Ruby, and I understood it", thereby (in my opinion) completely proving the point . It's exactly because people pull this sort of thing "Hey, it was really easy to understand for me, how about you?" that I have seen developers feel compelled to put clever oneliners in codebases. Clever oneliners that later end up causing problems for whatever unluckly…

Oooooor, its not a 'clever one-liner'. Its just a piece of code. Which, in its native environment e.g. Ruby is what you're expected to understand to be a journeyman of the trade.

The code is not written for newbies. It never will be. That's why they're 'newbies' and not 'professionals'.

Re: Please, kill your darlings

#28
I'm all for clever code when playing around but there are limits to its usefulness. When its over the top, you're not doing yourself or anyone any favors. If something took you a while to come up with, and on top of that its not commented out, its going to annoy the next person, and maybe even yourself when you get back to it 6 months down the road. Well, either annoy or stroke your ego.. Neither contribute much to the quality of the end product.

Here's a quote from Brian Kernighan: "Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?"

https://en.wikipedia.org/wiki/Brian_Kernighan

I've come to appreciate simplicity especially when spending a lot of time dealing with other people's code. If you spend most of your time in other peoples code, simplicity will save you endless amounts of time.

Re: Please, kill your darlings

#29
Either comment your code or move darlings to a properly named function, which is even better.

@sentence = capitalizeEveryWordInThe(@sentence)

Then you can both unit test (if you're into that kind of thing) and refactor without breaking a lot of things.

Re: Please, kill your darlings

#30
There was an HN discussion quite a while ago on a one-line Game of Life in APL. Much more difficult to understand than using any currently popular language.

Here's the discussion: https://news.ycombinator.com/item?id=1041500

Here's a (different) one-line Life implementation, linked to in that discussion: http://catpad.net/michael/apl/

Post reply on HN