{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "@id": "https://www.vidyasource.com/blog/idiom-savant-java-ruby/",
  "url": "https://www.vidyasource.com/blog/idiom-savant-java-ruby/",
  "mainEntityOfPage": "https://www.vidyasource.com/blog/idiom-savant-java-ruby/",
  "headline": "Idiom Savant",
  "description": "Idioms are just as important in programming languages as they are in spoken languages.",
  "datePublished": "2014-01-21T00:00:00.000Z",
  "author": {
    "@type": "Person",
    "name": "Neil Chaudhuri",
    "jobTitle": "President",
    "url": "https://www.linkedin.com/in/neil-chaudhuri/",
    "sameAs": "https://www.linkedin.com/in/neil-chaudhuri/"
  },
  "publisher": {
    "@id": "https://www.vidyasource.com/#organization"
  },
  "image": "https://www.vidyasource.com/img/blog/programming.jpg",
  "keywords": [
    "Java",
    "Ruby",
    "Software Engineering",
    "Programming"
  ],
  "articleBody": "Idioms are a big deal in any language. They represent deep knowledge beyond syntax and grammar, a feel for getting the\nmost out of the language.\n\nFor example, when non-English speakers become fluent in English, they usually speak in a “textbook” way. One might\nsee a celebrity receive a light punishment for a major crime and say, “That individual most assuredly did not receive a\npunishment commensurate with the gravity of the crime.” Meanwhile, a native English speaker might say instead, “Dude!\nReally? That’s a slap on the wrist!” What the non-native English speaker says is formal and correct, but it doesn’t\nreflect a deep understanding of the language or comfort with its idiosyncrasies.\n\nIdioms are just as important in programming languages.\n\nWhen I first started writing Ruby code, it was after many years of writing Java\ncode. It showed. It looked like Ruby code written by a Java guy.\nIt still worked, but it didn’t give me a feel for how Ruby works. Worse yet, I wasn’t taking advantage of the cool\nfeatures Ruby has that Java doesn’t.\n\nHere is a Ruby function written in a non-idiomatic way.\n\n~~~ruby\ndef process(name)\n  if name.include?(' ')\n    return name.gsub(' ', '').downcase\n  else\n    return name.upcase\n  end\nend\n~~~\n\nExcept for the *def*, which is more reminiscent of Groovy, this function looks very Java. It takes a string parameter\n(or so we assume since Ruby is dynamic) and checks if it contains a space. If it does, return the string in lowercase\nwith the space removed. If not, return the string in uppercase. Contrived but straightforward. And very Java-looking.\n\nCompare that to the idiomatic Ruby equivalent\n\n~~~ruby\ndef process(name)\n  return name.upcase unless name.include? ' '\n  name.gsub(' ', '').downcase\nend\n~~~\n\nNotice a few differences:\n\n* The *unless* keyword in Ruby to indicate “if not”\n* The condition on the same line (which you can do with *if* or *unless*)\n* No parentheses necessary around the parameter to *include?*\n* A second return statement unnecessary since the last line is returned\n\nAs you can see, idiomatic Ruby is more concise and leverages core language features that are pretty cool and absent from\nJava.\n\nNow check out a very Java-looking loop\n\n~~~ruby\nfor i in 1..100\n  puts i\nend\n~~~\n\nThis loops 100 times and prints out the current number. It looks very familiar to a Java developer.\n\nCompare that to the idiomatic Ruby equivalent\n\n~~~ruby\n100.times.each do |i|\n  puts i\nend\n~~~\n\nNotice the differences:\n\n* Underscore syntax for the function name rather than the lower camel case of Java\n* The *[times](http://www.ruby-doc.org/core-2.1.0/Integer.html#method-i-times)* method called on *100* because it is an\nobject--not a primitive like in Java\n* The *each* method called on the *times* enumerator\n\nIn this case, idiomatic Ruby is not more concise. But as [Russ Olsen](https://twitter.com/russolsen) points out in *[Eloquent Ruby](http://eloquentruby.com/)*,\nthe foremost authority on idiomatic Ruby,\nthe Java-like loop syntax is actually syntactic sugar for the idiomatic loop syntax. The Ruby community prefers the idiomatic\nsyntax. So should you.\n\nThese are simple examples. I didn’t even get into the idiomatic Ruby awesomeness at your disposal with collections thanks to\nits support for functional programming constructs\n(even though [Ruby is not a functional language](http://stackoverflow.com/questions/159797/is-ruby-a-functional-language)).\n\nI’m not saying Ruby idioms are superior to Java idioms. You simply should write idiomatic code for whichever language\nyou’re using. A Ruby developer moving to Java would have to learn to use lower camel case, *if* blocks, and so on.\n\nIdiomatic code is critical for several reasons. It will often be more concise than the alternative. You will take\nadvantage of the powerful features that have presumably made the language popular. You will also become a student of languages\nand understand the pros and cons of [static vs. dynamic](http://stackoverflow.com/questions/125367/dynamic-type-languages-versus-static-type-languages),\n[imperative vs. functional](http://stackoverflow.com/questions/602444/what-is-functional-declarative-and-imperative-programming),\n*etc*., which will make it easier to learn new languages and stay ahead in technology. Finally,\nyour colleagues at work will be more likely to understand and maintain your code. That needs to be a priority for you if you want to be a\ntruly professional developer and team player.\n\nKeep idioms in mind as you become an expert polyglot developer. You will write better code and prove very\nmarketable."
}