News for hackers. Stuff that matters.
Re:Please, kill your darlings by mmastrac on Tuesday March 08, 2016 @11:26AM (#11246223)
I'd suggest an alternate approach: add enough commentary to your one-liners so that a reader can parse that instead of the statement. Bugs can hide in the expanded versions of code just as easily. By offering the reader of the code a clear summary of the next one-liner, they can quickly scan it if they are trying to find a bug nearby and decide if it is potentially the culprit.

On top of that, consider making every bit of clever code a reusable library snippet instead. If it becomes part of the vocabulary of your codebase it'll be easier for others to understand it by looking at call sites.

eg:

  # Capitalize the first letter of every word
  @sentence = @sentence.split(' ').map!{|x| x = x[0..0].upcase << x[1..-1]}.join(' ')
Re:Please, kill your darlings by bradlenox on Tuesday March 08, 2016 @11:27AM (#11246251)
I agree. Someone's "darling" code might be a truly innovative or unique way of solving a problem, and providing enough commentary should help avoid the problems the author warns about.
Re:Please, kill your darlings by mhurron on Tuesday March 08, 2016 @11:32AM (#11246291)
It's amazing, their whole complaint is solved by proper software development practices - Comment your code.
Re:Please, kill your darlings by prodigal_erik on Tuesday March 08, 2016 @12:52PM (#11247007)
Code that's clear enough not to need a comment is better than code that has a comment. Comments can be outdated or mistaken and they consume screen real estate.
Re:Please, kill your darlings by mhurron on Tuesday March 08, 2016 @01:09PM (#11247145)
You still need to document intent. No code is self documenting.
Re:Please, kill your darlings by dllthomas on Wednesday March 09, 2016 @11:32AM (#11253892)
In some cases, naming may be sufficient to communicate intent.
Re:Please, kill your darlings by CaptSpify on Tuesday March 08, 2016 @11:36AM (#11246318)
To be fair: What is proper practice, is far from what is common practice
Re:Please, kill your darlings by TTPrograms on Tuesday March 08, 2016 @11:35AM (#11246310)
Not to mention it may be a performance optimization.
Re:Please, kill your darlings by sgt101 on Tuesday March 08, 2016 @11:55AM (#11246505)
Which is a good reason, if that optimization is needed.
Re:Please, kill your darlings by ludamad on Tuesday March 08, 2016 @12:23PM (#11246739)
I'd wrap it in a function called capitalizeWords and be done for the day. I also had 0 problem understanding the meaning of this line. If your code base is consistently made of small functions that look like this I actually don't see a major readability issue. (The article remains very valid)
Re:Please, kill your darlings by knodi123 on Tuesday March 08, 2016 @12:36PM (#11246856)
Why use "map!"? you're already working on a disposable copy of your data, there's no need to mandate "in place".

Could this be done with one gsub regex?

why use "x = x[0..0].upcase << x[1..-1]", instead of "x[0]=x[0].upcase; x" ?

And why not use the already-written "titleize" function, which capitalizes the first letter of each word?

Frankly, I think your darling needs to be put out of its misery. :-)

Re:Please, kill your darlings by boxidea on Tuesday March 08, 2016 @02:52PM (#11248000)
> already-written "titleize" function

Which is a function provided by ActiveSupport. It's not part of Ruby itself.

Re:Please, kill your darlings by Kenji on Tuesday March 08, 2016 @12:29PM (#11246797)
I haven't written a single line of Ruby in my life and I guessed that it would do that. While I 100% agree to make code readable, maybe this darling was a bad example. And yes, a comment would help with the problem of not understanding it.