public virtual void Method(IFactory arg) { arg.DoStuff(); }to Ruby's
def method arg arg.doStuff endIn this case, Ruby does away with ceremony mainly through dynamic typing. This frees the programmer from having to specify types in method arguments or returns. The downside to this approach, of course, is that you won’t find out about type errors until run time. While this problem can be mitigated through discipline and good testing, larger projects are arguably better off with static type checking—that is, unless you really need the flexibility that dynamic languages provide.
In fact, there are languages that manage to reduce ceremony without sacrificing static checking. Scala, for example, is a compiled, statically typed language that almost looks like Ruby if you squint hard enough. Scala is able to reduce the noise by leveraging such things as type inference and implicit returns. Consider the following example seen here:
def doubleValue(number : Int) : Int = { return number * 2 }By making the return implicit, it can be reduced to the following:
def doubleValue(number : Int) = { number * 2 }Finally, we can eliminate the braces since the function fits on a single line:
def doubleValue(number : Int) = number * 2Thus, languages like Scala are able to achieve the terseness of Ruby while maintaining the static type safety and performance of C#.
QED.
No comments:
Post a Comment