Introduction to Scala

Posted by rob on July 31, 2009

I gave an Introduction to Scala talk yesterday for the software team I am a member of. I have included here the slides and code examples used in the presentation.

Presentation

Code Examples

Hello World

package org.breidecker.scalaexamples

object HelloWorld {
def main(args : Array[String]) : Unit = {
println("Hello World!")
}
}

// Notes:
// 1. main method is required to run
// 2. ": Unit =" is optional
// 3. Unit in Scala's is similar to Java's void



Hello World #2

package org.breidecker.scalaexamples

object HelloWorld2 extends Application {
println("Hello World!")
}

// Notes:
// 1. Application provides the main method
// 2. The println statement is in the object's constructor



Accessors

package org.breidecker.scalaexamples

object Accessors {
def main(args : Array[String]) {
val person = new Person;
person.firstName = ""   // Try different values here
person.lastName = "Smith"
println("Hello " + person.fullName + ".")
}

class Person {
private var theFirstName = ""
var lastName = ""

/* Overide the first name getter. */
def firstName = theFirstName.toUpperCase

/* Override the first name setter. */
def firstName_=(firstName : String) {
if (firstName != null && !firstName.trim.isEmpty) {
theFirstName = firstName
} else {
throw new IllegalArgumentException("First Name must contain a value.")
}
}

def fullName() : String = (firstName + " " + lastName).trim
}
}

// Notes:
// 1. The firstName property on the Person class is providing accessor method for the property
// 2. The instance variable for firstName had to be renamed to avoid a name conflict with the getter method
// 3. The lastName property is being referenced in the main method  with its default accessors provided by Scala



List Comprehension

package org.breidecker.scalaexamples

object ListComprehension {
def main(args : Array[String]) {
for (val color <- Colors.ALL_COLORS) {
println(color.name)
}
}

/* This is an immutable class */
class Color(newName : String) {
val name = newName
}

/* This is a Scala singleton object */
object Colors {
/* These are Scala constants. */
val blue = new Color("blue")
val green = new Color("green")
val red = new Color("red")
val yellow = new Color("yellow")
val ALL_COLORS = List(blue, green, red, yellow)
}
}

// Notes:
// 1. This example simply shows how to iterate over a list of values in Scala



Twitter Client

package org.breidecker.scalaexamples

import java.net._
import scala.xml._

object TwitterClient {
def main(args: Array[String]) {
val screenName = "robbr"    // Follow me! Try another Twitter name
val url =
new URL("http://twitter.com/users/show.xml?screen_name=" +
screenName)
val conn = url.openConnection
val xml = XML.load(conn.getInputStream)
val status = (xml\"status"\"text").text
println(screenName + ": " + status)
}
}

// Notes:
// 1. Scala uses an underscore instead of an asterisk for its package wildcard character
// 2. Java's network package is imported
// 3. Scala's built-in XML library is used
// 4. This example makes a URL request to Twitter for the current screen name
// 5. It then uses an XQuery like statement to reference status text

Straightforward and Simple

Posted by Rob on April 21, 2007

Emma

I have to agree with Seth on this one. Emma’s Web site is really nice and a good model for a B2B Web site. The Web site’s design is simple (yet contemporary), the artwork is nice and the words are straightforward and easy to understand. They even have up front pricing. Notice the lack of fancy widgets?

Microsoft’s Multicolored Bar Codes

Posted by Rob on April 16, 2007

Microsoft Bar Code

I find these innovative, multicolored bar codes from Microsoft very interesting to look at. Color is never something I thought of associating with bar codes. It will be interesting to see if these bar codes catch on and are used for products outside of Microsoft’s initial scope.

Pick Your Java Poison: XML or Annotations?

Posted by Rob on January 22, 2007

Support for annotations is one of the biggest new features in Java 5. Since annotations can eliminate or greatly reduce the need to use XML for expressing metadata and configuration data, naturally a debate has arisen as to which technology is better. Some believe that XML is evil and should never have been invented. Others believe that annotations are a passing fad and were only added to Java because C# had them first. Ruby on Rails has also put a lot of pressure on the Java community to make it easier configure Java applications, with its “convention over configuration mantra”. I’ve been using annotations (as well as Java 5), for about six or seven months now, so by no means am I an expert. I thought it would be interesting to research the topic and develop a list of when to and not to use annotations and XML.

When to use annotations:

  1. You despise XML and are looking for new ways to express your metadata.
  2. Your metadata is related to a single class or method.
  3. Your metadata is fairly static and will not need to change at runtime.
  4. You want to see your metadata encapsulated inside of your objects.
  5. You are looking to take advantage of special annotation features supported by the JVM.
  6. You are looking for ways to access metadata within your Java code using native language commands.
  7. For specifying default metadata.
  8. Several of the popular frameworks are using annotations and you want try them out yourself. :)

When to use XML:

  1. You are happy with XML and are not looking for a new way to express metadata.
  2. You think that the addition of intelligent defaults to many of the most popular frameworks is good enough to warrant the continued use of XML.
  3. Your metadata is related to multiple classes.
  4. Your metadata is not related to a class or a method at all, but to a higher level of the application and therefore aren’t a good solution for what you are doing.
  5. Your metadata will change often. Configuration data is a good example of this.
  6. Your metadata needs multiple implementations.
  7. You want to see all of your metadata defined in one location.
  8. Your metadata needs to change at runtime.

Unless you are dead set against using annotations at all, then I suspect you will end up using some combination of XML and annotations to support your metadata and configuration needs. EJB3 is a good example of this. EJB3 uses annotations to set nearly all of its features where it makes sense, and XML can be used to provide alternate implementations and overrides if necessary or when annotations aren’t appropriate.

Has anyone reading this has written any guidelines or standards on the use of XML vs annotations? Have an opinion on this topic? Please leave a comment to this post. This would be a great topic for sharing ideas.

Update 1/24/2007: I just added a couple of new items to list for default metadata and metadata that needs to change at runtime. Thanks for the comment Alex.

Dueling Revitalization

Posted by Rob on September 29, 2006

If you been to downtown St. Louis, MO in the last couple of years, you surely would have noticed the amazing revitalization which is underway. Since it has been a couple of years since I have been to Kansas City, MO, I was unaware that they have a tremendous revitalization effort under way as well. Both Missouri cities are finalists for a World Leadership Award in the Urban Renewal category. The other two finalists include Calcutta, India and Manchester, England.

Urban Renewal Award

One such renewal effort I am aware of in downtown St. Louis is the Park Pacific development. The Mopac Building, as Park Pacific was once named, is the former home to the Missouri Pacific and Union Pacific Railroads. I once worked in this building back in the 1990’s. Check out the plans for this development. They are quite amazing. I have a friend whom has purchased a loft in this development, so I am looking forward to receiving an invitation to drop by once the development is complete.

Park Pacific

Very Cool New MP3 Player

Posted by Rob on September 01, 2006

SanDisk Sansa
The new SanDisk Sansa looks awesome. For $250 you get 8GB of storage and as a bonus you get a FM tuner and voice recorder.
SanDisk Sansa

New Motorola KRZR

Posted by Rob on September 01, 2006

Motorola has released photos and information on their new upcoming phone based on the popular RAZR. The phone includes a camera and music player.I love the design of this phone. It is small, stylish and packs in a lot of cool features. I can’t wait to make this my next phone.

Motorola KRZR

Motorola KRZR

Five Ways To Identify Increasing Software Complexity

Posted by Rob on September 01, 2006

Sometimes the key to simplifying software can be the ability to recognize when the software has become too complex. Below is a list of things I look for in projects that I work on to determine if they are becoming increasingly complex.

  1. More bugs: The more complex software becomes, the harder it becomes to test and maintain. This will certainly result in more bugs.
  2. More issues: When software becomes complex it also becomes harder to use, thus resulting in more support calls.
  3. Less usage: If software becomes too hard to use, customers may stop using the software to the extent they were using it before. Sometimes they will stop using it altogether.
  4. Less productivity: When software becomes harder to use, user as well as developers will get less accomplished.
  5. Less understanding: When software becomes more complex, programmers, analysts, testers and users may have trouble themselves understanding the software. This one is more difficult to determine. You’ll have to keep your ear to ground for this one. Less understanding is very bad, as it not only will result in all of the above, but will also result in a disconnect between what the software does and what it was originally intended to do.

Let’s Keep Things Simple

Posted by Rob on August 30, 2006

“Everything should be made as simple as possible, but not simpler.” -Albert Einstein

Even though I see this Einstein quote used all over the place, I thought it would be fitting to start this blog off with it. This is one of my favorite quotes of all time. I wish software designers would keep this quote in mind more often.