Hacker News new | ask | show | jobs
by JoeAltmaier 3752 days ago
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'.

2 comments

As I said, until it hits some kind of edge case. Again, I'm not trying very hard here, but this oneliner doesn't give the expected output if your sentence has a string like "æsir".

  irb> @sentence = "\u00e6sir are gods"
  => "æsir are gods"

  irb> @sentence = @sentence.split(' ').map!{|x| x = x[0..0].upcase << x[1..-1]}.join(' ')
  => "æsir Are Gods"
(Expected output being "Æsir Are Gods")

If you want to understand why this is failing, the code I gave above will make it way simpler. Of course, viewpoints differ – you might claim that troubleshooting it is only a job for "professionals".

Its all a matter of what gets the algorithm into your head most clearly. If extra names and steps do it for you, then cool. But at some point extra names get in the way - they can suggest the wrong thing (what was intended but not what is actually happening). That's how troubleshooting goes astray.
Code should be written in the easiest to understand form that both works and provides the necessary performance characteristics. Often that is the form that "newbies" also find easy to understand, for obvious reasons.

I've used Ruby somewhat actively for almost 10 years now and I still hate it when people barf out a bunch of Ruby-specific shorthand and think they're clever for doing so. Write it in the simplest form that works. I don't care if it takes 10 more keystrokes.

I've seen people write fizzbuzz with lambdas and such thinking that it would signal they're super-serious Ruby all-stars, but all it really does is let me know I should keep my distance, because they're going to be writing needlessly convoluted code.