The Artima Developer Community
Sponsored Link

Java Community News
Improving Code with Scala Patterns

5 replies on 1 page. Most recent reply: Dec 8, 2007 1:25 PM by James Watson

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Improving Code with Scala Patterns Posted: Dec 6, 2007 4:48 PM
Reply to this message Reply
Summary
One of the nicer benefits of Scala is its concise syntax. Luis Diego Fallas shows in a recent pair of articles how to substitute several lines of code with two short and elegant Scala idioms while also making the resulting code easier to understand.
Advertisement

Coding idioms, or patterns, often take up a significant part of a project's codebase. One such idiom is the Java try-catch-finally pattern used to ensure that access to some resource is properly relinquished, even if using the resource results in exceptions.

Another, more general, coding pattern that developers sometimes follow calls for wrapping an already existing library, possibly extending that library with new functionality without having to have access to, and recompile, the library source code.

Some languages afford more concise implementations of these, and other, coding patterns. Scala developers, for example, identified two Scala patterns that implement the above mentioned idioms. Luis Diego Fallas describes in Improving code using more Scala features how these patterns can result in shorter and, at the same time, easier-to-understand code.

The Loan pattern is a way to ensure resource disposal when the control leaves some scope. For this purpose closures are used.

The Pimp my library pattern, based on a Martin Odersky’s article, provides a way to extend existing class libraries with new operations without recompiling. New operations are added by defining a wrapper for specific classes.

Interestingly, Fellas illustrates these patterns in the course of implementing in Scala a code-hint for the NetBeans editor, for use when editing Ruby code with the IDE. Fellas' blog post also shows how such editor hints can be coded up in Scala with relatively few lines of code, and how Scala's compatibility with the Java runtime ensures that the resulting bytecodes run inside NetBeans.

What Scala coding idioms do you like the most? In general, what do you think about Scala's impact on programmer productivity?


Alex Blewitt

Posts: 44
Nickname: alblue
Registered: Apr, 2003

Re: Improving Code with Scala Patterns Posted: Dec 7, 2007 8:26 AM
Reply to this message Reply
There's also a slightly broader question; who's using scala? I think there's people playing with it at the moment, but patterns take time to evolve.

Plus, depending on where you are with Scala (and whether you've got any kind of functional background) a map/filter might be seen as an anti-pattern by procedural programmers but a pattern by functional ones.

Raoul Duke

Posts: 127
Nickname: raoulduke
Registered: Apr, 2006

Re: Improving Code with Scala Patterns Posted: Dec 7, 2007 4:17 PM
Reply to this message Reply
the Scala "loan pattern" is another nice example (along with the more grandiose Actors library) of how a language which supports extension is, i think, a happier thing than one which doesn't (Java). well, up to the point where somebody defines something really tweaky...

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Improving Code with Scala Patterns Posted: Dec 7, 2007 6:00 PM
Reply to this message Reply
> the Scala "loan pattern" is another nice example (along
> with the more grandiose Actors library) of how a language
> which supports extension is, i think, a happier thing than
> one which doesn't (Java). well, up to the point where
> somebody defines something really tweaky...

I'm not sure I understand. You can use the Loan pattern in Java, it's just a lot more verbose: interface + implementations. I actually used this approach to build a wrapper for JDBC that allows for some pretty sophisticated resource management and completely removes the need for managing them from the code doing the queries and updates.

Leo Lipelis

Posts: 111
Nickname: aeoo
Registered: Apr, 2006

Re: Improving Code with Scala Patterns Posted: Dec 7, 2007 6:40 PM
Reply to this message Reply
> I'm not sure I understand. You can use the Loan pattern
> in Java, it's just a lot more verbose: interface +
> implementations. I actually used this approach to build a
> wrapper for JDBC that allows for some pretty sophisticated
> resource management and completely removes the need for
> managing them from the code doing the queries and updates.

The laboriousness of a feature determines the threshold at which it will be used. If something is easy to use, it will be used often, and that's a good thing.

What is the point of any language? We can do all the same things in assembly. This is why we shouldn't trivialize advances that languages make in brevity and expressiveness.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Improving Code with Scala Patterns Posted: Dec 8, 2007 1:25 PM
Reply to this message Reply
> The laboriousness of a feature determines the threshold at
> which it will be used. If something is easy to use, it
> will be used often, and that's a good thing.

I agree. I didn't mean to imply anything different.

> What is the point of any language? We can do all the same
> things in assembly. This is why we shouldn't trivialize
> advances that languages make in brevity and expressiveness.

I didn't trivialize it. I said that "I'm not sure I understand" because I don't see how the loan pattern relates to Scala supporting extension. I think 'extension' in the context would refer to the implicit defs but I could be wrong, hence the "I'm not sure I understand" that prefaced my comment.

Just because I questioned a specific statement that one person made doesn't mean I'm questioning Scala.

Flat View: This topic has 5 replies on 1 page
Topic: Sun's Gregg Sporar on the New NetBeans 6.0 Code Editor Previous Topic   Next Topic Topic: Column-Oriented Databases

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use