Posts Tagged ‘Lift’

Scala’s simple strategy to reduce debugging costs: “Some” and “None”

Sunday, August 9th, 2009

Null Pointer Exceptions (NPEs) are most common runtime errors in Java (more than 500.000 results in Google). Most Java libraries are interspersed with methods returning null if the computation could not be finished. Invoking method calls on null raises Null Pointer Exceptions.

The best practice in Java to avoid such errors is a test against null.

if(aObject == null) {...

or pass the null test to another function.

For example, Instead of writing:

if(aResultString.equals(“literal”)) {
...

you write:

if(“literal”.equals(aResultString)) {
...

More than eleven years ago (beginning with the java era) a data type existed, avoiding the NPE problem:

data  Maybe a =  Nothing | Just a deriving (Eq, Ord, Read, Show)

A Haskell one-liner ported to Java could have prevented thousands of errors.

Why?

You cannot forget to test against None (unlike the null check) because this would raise a compile time error. Based on Haskells Maybe-Type, Scala defines the Some and None Type with:

final case class Some[+A](x: A) extends Option[A]
...

and

case object None extends Option[Nothing]
...

To test an Option Type against None you write:

val result =  a getOrElse b..

If a is Some(_) than result is _. If result is None, than result is b. The value of result is therefore always defined!

Runtime Errors on my News Page Kungle.de so far

To Keep it short:
None

Running a Website With Scala and Lift

Wednesday, July 1st, 2009

Scala is a modern, statically typed language. Its bytecode is Java-VM compatible and the design is influenced by languages like Haskell or Erlang.

Lift is a Web Framework written in Scala. Lift applications follow the View First pattern. The View First pattern defines a complete separation of presentation and logic.

Although with long experience in business, writing my first web application in Scala/Lift wasn’t an easy task. The combination of functional and object-oriented programming paradigms enables a wide range of new possibilities to compose your source code. It may take a while to understand wiki examples or library documentation, but writing your application in Scala has crucial advantages.

  • Less code:
    The elegant Type System and the modern Control Flow Structures reduce your code size dramatically.
  • A manageable toolchain:
    Every new tool in your development environment means a new dependency in your application. Every update can cause trouble. To configure these tools you often implant new plugins in your ide which in turn complicates the build process. Typically you stop updating your development environment at a certain project stage.
    In contrary, you can build your Lift applications with vi. In less than 100 lines of code you can create a complete web application!

The result is a fast and flexible development cycle.

You can visit my site at kungle.de. It is an application to identify relevant news. The news rating is statistically calculated by an adaptive network and updated every 30 minutes.

References:
Scala homepage: http://www.scala-lang.org/
Lift homepage: http://liftweb.net/
Introduction “View First Pattern”: http://wiki.liftweb.net/index.php/Lift_View_First