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
Jan 28, 2015
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
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
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
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)
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)
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
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
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
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
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
Experiences with languages other than Java 11
© Workingmouse Pty Ltd 2008. All rights reserved
Rails projects at Workingmouse
Experiences with languages other than Java 12
© Workingmouse Pty Ltd 2008. All rights reserved
Rails projects at Workingmouse
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
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
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?
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!
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
Experiences with languages other than Java 18
© Workingmouse Pty Ltd 2008. All rights reserved
Experiences● Still, able to get something up and running
quickly
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
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
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
Experiences with languages other than Java 22
© Workingmouse Pty Ltd 2008. All rights reserved
Questions?
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
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
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) }}
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)
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)}
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)
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) }}
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
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
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
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
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
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") ... }}
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
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
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
Experiences with languages other than Java 39
© Workingmouse Pty Ltd 2008. All rights reserved
Questions?
Experiences with languages other than Java 40
© Workingmouse Pty Ltd 2008. All rights reserved
Thanks!
http://workingmouse.comhttp://wiki.workingmouse.com