Print

Code Watch: Two years with Scala



Larry O Brien
Email
October 22, 2012 —  (Page 1 of 3)
For the past two years, my day-to-day programming language has been Scala. Scala is a hybrid language that combines the approaches of object-orientation and functional programming. You can treat Scala as a "Java.next" with minimal changes in your object-oriented mindset (the only thing that springs to mind is that Scala's "companion objects" might seem like unnecessarily clunky replacements of Java's "static" functionality). Even just as a drop-in replacement, I think most programmers would prefer Scala to Java: type inference saves finger-typing, and fields and class structure are a little clearer.

Scala is also a functional programming language. I've talked about functional programming many times over the years, but it continues to have an undeserved air of mystery around it. The low bar for a language to be "functional" is the support of functions as "first-class constructs." Essentially, wherever you can have a value (on the right-hand of assignments, as the input or output of functions, etc.), you can have instead an anonymous block of code (if you like Greek, you can call them "lambda functions"). This turns out to be so convenient that every mainstream language either already supports or is moving toward such support; every language is, or will be soon, a "functional programming" language.

The greatest practical benefit of first-class functions is that, multiple times per day, instead of writing a for loop that iterates over elements of a collection to transform, filter or accumulate something, you just pass a small function to common collection-class functions such as (in the Scala world), map, filter and foldLeft. I cannot imagine a programmer not preferring the use of such functions: They are both clearer and more concise than loops.

But first-class functions do not guarantee understandable code, as anyone who's tried to maintain a complex JavaScript Web page (with all those element.click(function(){}) tangles of first-class event handlers) can attest. Instead, the major task-by-task benefit of functional programming is not a language feature, but the philosophy of "lack of mutable state."



Related Search Term(s): functional programming, Scala

Pages 1 2 3 


Share this link: http://sdt.bz/37094
 
Most Read  Latest News  Resources

close
NEXT ARTICLE
Code Watch: A paradigm for your thoughts
Functional programming still has many hurdles to get over, but its imminence is undeniable Read More...
 
 
 




News on Monday  more>>
Android Developer News  more>>
SharePoint Tech Report  more>>
Big Data TechReport  more>>

   
 
 

 


Download Current Issue
MAY 2013 PDF ISSUE

Need Back Issues?
DOWNLOAD HERE

Want to subscribe?