Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • Ruby long method chain - Is it best practice?
    I enjoy chaining methods together when I write code, but my question is: Do others enjoy reading it? Is it hard to understand? Is it clearer to create multiple lines, each with its own purpose? Here's an example of a method I created using lots of chaining:
    def NumberAddition(str)
      str.split(/[^0-9]/).delete_if { |elem| elem.empty? }.map! { |str_num| str_num.to_i }.inject(:+)
    end
    
    matiasfernandez posted this question on 10/16/15 | ruby, method
    Answers
  • +
  • 1
  • -
  • There is no real definitive answer to your question. Each programmer will read/write code at ease in accordance with their own individual styles. You'll NEVER please everyone; and, there's always a critic. It's not my intention to be cynical. I applaud your empathetic tendencies. Empathy is an important business tool. I digress. Like I mentioned, It;s going to come down to individual preferences. If you're writing code for yourself or for a passion project that only you or one or two others will be working on, then you should feel free to write in whatever white-spacing works best for you.. --I'd strongly recommend that you leave comment clues by some of the more cryptic chains of code for future reference--- On the other hand, I'm sure you're familiar with the styling conventions laid out in the Ruby Style Guide (github.com/bbatsov/ruby-style-guide). If you writing code that you intend to post for others to use it's important to follow these standards. If for no other reason than for that brave new programmer out there just learning the ropes. It's possible that they could benefit from your code. But their likely to become overwhelmed/intimidated if the code doesn't follow the conventions they've learned. Look into the settings of the text editor software you are using to code. Many have the ability to customize how code is to be automatically expanded /collapsed as you type. I hope this helped. I know it was a little all over the place. ;-) Good luck, and Be well.
    Log in to write an answer.