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 = @…
Please, kill your darlings
21–30 of 84 posts
Re: Please, kill your darlings
#22Re: Please, kill your darlings
#23Re: Please, kill your darlings
#24It'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
#25I'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)
capitalize downcases the rest of the each sub-string, the original code did not.
Re: Please, kill your darlings
#26I'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)
Re: Please, kill your darlings
#27In 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…
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
#28Here'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@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
#30Here'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/