40
Experiences with languages other than Java 1 © Workingmouse Pty Ltd 2008. All rights reserved Experiences with Languages other than Java Kristian Domagala CITEC Technology Day 10 th October, 2008

Rails Scala Citec Presentation

Embed Size (px)

DESCRIPTION

In this presentation, Kristian gives an insight into his experiences of using Ruby on Rails and Scala for developing recent projects at Workingmouse. It is aimed at people from a Java background and assumes no prior knowledge of Ruby/Rails or Scala.The presentation is divided into two sections; Ruby on Rails and Scala. In each section, he gives a brief overview of the features and syntax of the languages before delving into the problems and benefits he discovered while using them. He finishes each section with a guide to the appropriateness of the languages and gives some tips on how to get started.

Citation preview

Page 1: Rails Scala Citec Presentation

Experiences with languages other than Java 1

© Workingmouse Pty Ltd 2008. All rights reserved

Experiences withLanguages other than Java

Kristian DomagalaCITEC Technology Day

10th October, 2008

Page 2: Rails Scala Citec Presentation

Experiences with languages other than Java 2

© Workingmouse Pty Ltd 2008. All rights reserved

Background● Technology

– Java/J2EE– Web client/server– WS*/XML/XSD

● Processes– Agile– TDD

Page 3: Rails Scala Citec Presentation

Experiences with languages other than Java 3

© Workingmouse Pty Ltd 2008. All rights reserved

What is Ruby?● Dynamically typed● Interpreted● Object Oriented● Supports common functional idioms● Meta-programming● Promotes testing to replace type safety

Page 4: Rails Scala Citec Presentation

Experiences with languages other than Java 4

© Workingmouse Pty Ltd 2008. All rights reserved

Object Orientedclass Person attr_reader :name def initialize(name,email) @name = name @email = email end def email @email endend

class Employee < Person def initialize(name,email,salary) super(name,email) @salary = salary end def increase_salary(pct) @salary *= pct endend

emp = Employee.new("Fred","[email protected]", 50000)puts emp.name + ": " + emp.emailemp.increase_salary(1.05)

Page 5: Rails Scala Citec Presentation

Experiences with languages other than Java 5

© Workingmouse Pty Ltd 2008. All rights reserved

Dynamic typingclass Business ... def email @email endend

class Person ... def email @email endend

def spam(obj) send_spam_to(obj.email)endspam(person)spam(business)

Page 6: Rails Scala Citec Presentation

Experiences with languages other than Java 6

© Workingmouse Pty Ltd 2008. All rights reserved

Mixins (multiple inheritance)

class Business include Payable attr_reader :account ...end

class Employee < Person include Payable attr_reader :account ...end

employee.pay(1000)business.pay(100)

module Payable def pay(amount) account.deposit(amount) endend

Page 7: Rails Scala Citec Presentation

Experiences with languages other than Java 7

© Workingmouse Pty Ltd 2008. All rights reserved

Meta-programming● Redefine class/method behaviour at runtime

– method_missing– eval

Page 8: Rails Scala Citec Presentation

Experiences with languages other than Java 8

© Workingmouse Pty Ltd 2008. All rights reserved

RSpecdescribe "Sending spam" do it "should send to the person's email address" do person_mock = mock("person") person_mock.should_receive(:email) .and_return("[email protected]") spammer.should_receive(:send_spam_to) .with("[email protected]") .and_return(true) spammer.spam(person_mock).should == true endend

Page 9: Rails Scala Citec Presentation

Experiences with languages other than Java 9

© Workingmouse Pty Ltd 2008. All rights reserved

What is Rails?● Web application framework featuring

– MVC– Active record– Template engine– Web server– Code generators

● Extensible through plugins● Open source since 2004

Page 10: Rails Scala Citec Presentation

Experiences with languages other than Java 10

© Workingmouse Pty Ltd 2008. All rights reserved

Convention over Configuration● Controllers

– public methods -> actions -> views● Model

– class-name -> table, attribute -> column– auto fields like id, created_at, version

● Minimal configuration required● Conventions can be overridden

Page 11: Rails Scala Citec Presentation

Experiences with languages other than Java 11

© Workingmouse Pty Ltd 2008. All rights reserved

Rails projects at Workingmouse

Page 12: Rails Scala Citec Presentation

Experiences with languages other than Java 12

© Workingmouse Pty Ltd 2008. All rights reserved

Rails projects at Workingmouse

Page 13: Rails Scala Citec Presentation

Experiences with languages other than Java 13

© Workingmouse Pty Ltd 2008. All rights reserved

Experiences● Code base not very discoverable

– Difficult to locate references and definitions– Unsure what effect code changes will have until

runtime

Page 14: Rails Scala Citec Presentation

Experiences with languages other than Java 14

© Workingmouse Pty Ltd 2008. All rights reserved

Experiences● Upgrades to framework/plug-ins risky

– Test coverage not always adequate● Introduction of plug-ins risky

– Can break assumptions on both sides

Page 15: Rails Scala Citec Presentation

Experiences with languages other than Java 15

© Workingmouse Pty Ltd 2008. All rights reserved

Experiences● Things aren't always what they appear to be

– Item.tags● Array of Tag?● Array of tag names?● Comma separated list of tag names?

Page 16: Rails Scala Citec Presentation

Experiences with languages other than Java 16

© Workingmouse Pty Ltd 2008. All rights reserved

Experiences● Few “best practices” universally agreed on

– Rails is very accessible– Strong, sometimes highly opinionated, community– Plethora of tutorials/blogs

● difficult to sieve through● not always good advice!

Page 17: Rails Scala Citec Presentation

Experiences with languages other than Java 17

© Workingmouse Pty Ltd 2008. All rights reserved

Common rebuttal● Not enough tests!

– Where do you stop?● TDD process doesn't catch all bugs

– Maintaining tests– Language doesn't force tests

● Code coverage tools not always adequate

Page 18: Rails Scala Citec Presentation

Experiences with languages other than Java 18

© Workingmouse Pty Ltd 2008. All rights reserved

Experiences● Still, able to get something up and running

quickly

Page 19: Rails Scala Citec Presentation

Experiences with languages other than Java 19

© Workingmouse Pty Ltd 2008. All rights reserved

IDE support● Eclipse/IDEA

– syntax highlighting– some code-completion– some search capabilities– limited to ability due to dynamic language features

● Interactive Ruby/Rails shells– Read Evaluate Print Loop capability

Page 20: Rails Scala Citec Presentation

Experiences with languages other than Java 20

© Workingmouse Pty Ltd 2008. All rights reserved

Appropriateness● Can be good for

– “green-fields” small project development– prototyping

● Had issues with– long running and non-trivial projects, especially

● maintenance● unfamiliarity of code

Page 21: Rails Scala Citec Presentation

Experiences with languages other than Java 21

© Workingmouse Pty Ltd 2008. All rights reserved

Tips for starting● Up front study & experimentation

– Books● Agile Web Development with Rails (tutorial)● The Rails Way (reference)

● Communication– Pairing/spending time with experienced people

● Research– Investigate basis of claims– Get both sides of the story

Page 22: Rails Scala Citec Presentation

Experiences with languages other than Java 22

© Workingmouse Pty Ltd 2008. All rights reserved

Questions?

Page 23: Rails Scala Citec Presentation

Experiences with languages other than Java 23

© Workingmouse Pty Ltd 2008. All rights reserved

What is Scala?● Statically typed● Object Oriented● Functional● Compiles to JVM byte code (.class files)● Open source

– First release in 2003– 2.0 released in 2006

Page 24: Rails Scala Citec Presentation

Experiences with languages other than Java 24

© Workingmouse Pty Ltd 2008. All rights reserved

Relation to Java● By design, Scala can do virtually everything that

Java can do– classes, methods, interfaces, statics

● no forced exception handling– call Java APIs

● Even the (arguably) bad stuff– nulls, down-casting, reflection

Page 25: Rails Scala Citec Presentation

Experiences with languages other than Java 25

© Workingmouse Pty Ltd 2008. All rights reserved

Exampleclass Person( val firstName:String, val lastName:String, val birthYear:Int, private var height: Int) {

val name = firstName + " " + lastName

def age(year: Int) = birthYear + year def debug { println(name + birthYear + height) }}

Page 26: Rails Scala Citec Presentation

Experiences with languages other than Java 26

© Workingmouse Pty Ltd 2008. All rights reserved

Language features● Type inferencer● First class functions and closures● XML literals● Type-safe tuples (allows multiple return values)● Traits (allows multiple inheritance)

Page 27: Rails Scala Citec Presentation

Experiences with languages other than Java 27

© Workingmouse Pty Ltd 2008. All rights reserved

XML literalsval bookElt = <book title="Scala for dummies" year="2008"> <author>Martin Odersky</author> <author>Tony Morris</author> </book>

println("Title: " + bookElt.attribute("title"))(bookElt \ "author").foreach{a => println(a)}

Page 28: Rails Scala Citec Presentation

Experiences with languages other than Java 28

© Workingmouse Pty Ltd 2008. All rights reserved

Type-safe tuplesdef nameAndYear(person:Person) = (person.name, person.birthYear)

val ny = nameAndYear(person)val firstInitial = ny._1.charAt(0)val genX = (1968 until 1979).contains(ny._2)

Page 29: Rails Scala Citec Presentation

Experiences with languages other than Java 29

© Workingmouse Pty Ltd 2008. All rights reserved

Multiple inheritance

class Business extends Payable { def account = ...}

class Employee extends Person with Payable { def account = ...}

employee.pay(1000)business.pay(100)

trait Payable { def account:Account def pay(amount:Int) { account.deposit(amount) }}

Page 30: Rails Scala Citec Presentation

Experiences with languages other than Java 30

© Workingmouse Pty Ltd 2008. All rights reserved

Type system● Useful for detecting common bugs

– Simple things like NullPointerExceptions– Through to enforcing concurrency constraints

● Constraints can be better implied with types● Removes some of the need for defensive

programming– fail fast often done at the compiler

Page 31: Rails Scala Citec Presentation

Experiences with languages other than Java 31

© Workingmouse Pty Ltd 2008. All rights reserved

Scala projects at Workingmouse● Scoodi Ads engine● Scoodi online help webapp● Slinky web application framework

Page 32: Rails Scala Citec Presentation

Experiences with languages other than Java 32

© Workingmouse Pty Ltd 2008. All rights reserved

Experiences● To get full benefit, need to change the way you

think● Functional programming concepts difficult to get

a grasp of, but very powerful once understood

Page 33: Rails Scala Citec Presentation

Experiences with languages other than Java 33

© Workingmouse Pty Ltd 2008. All rights reserved

Experiences● Easy to trace code definitions and usage

– Type system helps find dead code and prevents invalid changes and assumptions

● Sometimes difficult to get compiling– but more often than not, once it compiled it was

correct

Page 34: Rails Scala Citec Presentation

Experiences with languages other than Java 34

© Workingmouse Pty Ltd 2008. All rights reserved

Experiences● Scala to Java integration is practical

– sometimes more type annotations and down-casting required

● Java to Scala integration theoretically possible– practical implications can make it infeasible

Page 35: Rails Scala Citec Presentation

Experiences with languages other than Java 35

© Workingmouse Pty Ltd 2008. All rights reserved

Java integration exampleimport javax.servlet.http._

class MyServlet extends HttpServlet { override def service( req: HttpServletRequest, resp: HttpServletResponse) {

val name = req.getParameter("name") ... }}

Page 36: Rails Scala Citec Presentation

Experiences with languages other than Java 36

© Workingmouse Pty Ltd 2008. All rights reserved

IDE support● Eclipse/IDEA

– primitive support (syntax highlighting)– plug-ins actively developed (esp IDEA)

● buggy for our use● Interactive Scala shell

– REPL capability– Can also use for invoking Java libraries

Page 37: Rails Scala Citec Presentation

Experiences with languages other than Java 37

© Workingmouse Pty Ltd 2008. All rights reserved

Appropriateness● Can be used anywhere that Java is used● Added benefits

– better expressiveness with 1st class functions– safer code with type system and APIs that

encourage immutability– better rate of code reuse with higher kinds– less verbosity with syntax and inferencer

Page 38: Rails Scala Citec Presentation

Experiences with languages other than Java 38

© Workingmouse Pty Ltd 2008. All rights reserved

Tips for starting● Workingmouse Scala training course :-)● Start by imitating Java

– Explicitly specifying types will help most compilation issues

● Full benefit of functional aspects made easier by learning a pure functional language as well

Page 39: Rails Scala Citec Presentation

Experiences with languages other than Java 39

© Workingmouse Pty Ltd 2008. All rights reserved

Questions?

Page 40: Rails Scala Citec Presentation

Experiences with languages other than Java 40

© Workingmouse Pty Ltd 2008. All rights reserved

Thanks!

http://workingmouse.comhttp://wiki.workingmouse.com