Scala == Effective Java
Upcoming SlideShare
Loading in...5
×
 

Scala == Effective Java

on

  • 2,794 views

Presentation about clean code Scala with counter examples in Java.

Presentation about clean code Scala with counter examples in Java.
Live recording is here http://livestre.am/A1SW (Polish)

Statistics

Views

Total Views
2,794
Views on SlideShare
2,708
Embed Views
86

Actions

Likes
3
Downloads
33
Comments
0

5 Embeds 86

https://www.linkedin.com 62
http://www.linkedin.com 9
https://twitter.com 6
http://tedwon.com 5
http://115.68.2.182 4

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Scala == Effective Java Scala == Effective Java Presentation Transcript

  • Clean Code Scala
    Scala == Effective Java ?
  • „Leave camp cleaner
    than you found it.”
    Uncle Bob
  • Why do we write bad code ???
    Rush (duck tape programmer)
    Laziness
    Careless
    We don’t know how good code looks like
    Lack of resources
    We like to do new things (create)
  • „Its harder to read the
    code
    than to write it”
    Joel Spolsky
  • How to improve work quality ?
    Dont give developers money rewardsbigger reward == worse performance
    Let them work on what they like and how they like (Google, Atlassian, Facebook)
    Allow and encourage self improvement
  • Java dev libraries
    Google Guava
    Apache Commons
    Spring
    Guice
    Scala !
  • Why Scala ?
    More concise
    More powerfull
    Fun!
    Fast
    Becoming popular !
  • Who uses Scala already
  • Class Parameters - Java
    publicclass Person {
    private String name;
    privateintage;
    publicPerson(String name, int age) {
    this.name = name;
    this.age = age;
    }
    public String getName() {
    returnname;
    }
    publicvoid setName(String name) {
    this.name = name;
    }
    publicint getAge() {
    returnage;
    }
    publicvoid setAge(int age) {
    this.age = age;
    }
    }
  • Class Parameters - Scala
    classPerson(var name: String, var age: Int)
  • publicclass Person {
    private String name;
    privateintage;
    public Person(String name, int age) {
    this.name = name;
    this.age = age;
    }
    public String getName() {
    returnname;
    }
    publicvoidsetName(String name) {
    this.name = name;
    }
    publicintgetAge() {
    returnage;
    }
    publicvoidsetAge(int age) {
    this.age = age;
    }
    @Override
    public String toString() {
    returnString.format("Person: %s age: %s", name, age);
    }
    @Override
    publicinthashCode() {
    inthashCode = 0;
    for(char c: name.toCharArray()) {
    hashCode += c;
    }
    return 11 * hashCode + age;
    }
    @Override
    publicboolean equals(Object other) {
    if(other == null) returnfalse;
    if(other instanceof Person) {
    Person person = (Person)other;
    returnperson.name.equals(name) && person.age == age;
    }
    returnfalse;
    }
  • Scala case class
    caseclass Person(name: String, age: Int)
    Dostajemy za darmo: equals(), hashCode() oraz toString() oraz niezmienną klasę (immutable).
  • To equal or not to equal ?
      Person p = new Person("Jan Kowalski", 30);
    Set<Person> set = new HashSet<Person>();
    set.add(p);
    System.out.println(set.contains(p));
    // true
    p.setAge(p.getAge()+1);
    System.out.println(set.contains(p));
    // false
    ... WTF ???
  • To equal or not to equal ?
    Iterator<Person> it = set.iterator();
    booleancontainedP = false;
    while (it.hasNext()) {
    Person nextP = it.next();
    if (nextP.equals(p)) {
    containedP = true;
    break;
    }
    }
    System.out.println(containedP);
    // true
    // ... ???
  • Scala Case Class
    classPerson(val name: String, val age: Int)
    objectPerson {
    def apply(name: String, age: Int) = newPerson(name, age)
    // defhashCode(): Int
    // deftoString(): String
    // def equals(other: Object): Boolean
    defunapply(p: Person): Option[(String, Int)]=Some(p.name, p.age)
    }
    • Extractor
    case
  • Java – working with Person
    Object x = new Person("Bill Clinton", 64);
    if(x instanceof Person) {
    Person p = (Person)x;
    System.out.println(„Person name: "+p.getName());
    } else {
    System.out.println("Not a person");
    }
    x = "Lukasz Kuczera";
    if(x instanceof Person) {
    Person p = (Person)x;
    System.out.println("hello "+p.getName());
    } elseif(x instanceof String) {
    String s = (String)x;
    if(s.equals("Bill Clinton"))
    System.out.println("Hello Bill");
    else System.out.println("hello: "+s);
    } else System.out.println("err, ???");
  • Scala – Pattern Matching
    var x: Any = Person("Lukasz", 28);
    x match {
    case Person(name, age) => println("Person name: "+name);
    case _ => println("Not a person")
    }
    x = "Lukasz Kuczera"
    x match {
    case Person(name, age) => println("Person name: "+name)
    case"Bill Clinton" => println("hello Bill")
    case s: String => println("hello "+s)
    case _ => "err, ???"
    }
    Person name: Lukasz
    hello Lukasz Kuczera
  • Parameter validation
    publicclass Person {
    private String name;
    privateintage;
    publicPerson(String name, int age) {
    if(name == null) {
    thrownew NullPointerException();
    }
    if(age < 0) {
    thrownew IllegalArgumentException("Age < 0")
    }
    this.name = name;
    this.age = age;
    }
  • Parameter validation
    caseclass Person(name: String, age: Int) {
    // @elidable(ASSERTION)assert(age > 0, "Age < 0")// by name parameter
    }
  • Working with arrays
    Java
    publicclass Partition {
    Person[] all;
    Person[] adults;
    Person[] minors;
    {
    ArrayList<Person> minorsList = new ArrayList<Person>();
    ArrayList<Person> adultsList = new ArrayList<Person>();
    for(int i=0; i<all.length; i++ ) {
    (all[i].age<18 ? adultsList: minorsList).add(all[i]);
    }
    minors = (Person[]) minorsList.toArray();
    adults = (Person[]) adultsList.toArray();
    }
    }
  • Working with arrays
    Scala
    val all: Array[Person]
    val (minors, adults) = all.partition(_.age<18)
  • Null’s – Java
    Map<String, String> capitals =
    new HashMap<String, String>();
    capitals.put("Poland", "Warsaw");
    System.out.println(capitals.get("Polska").trim());
    Exception in thread "main" java.lang.NullPointerException
  • Null’s - Scala
    val capitals = Map("Poland" -> "Warsaw");
    val capitalOption: Option[String] = capitals.get("Polska")
    capitalOption match {
    case Some(value) => println(value)
    case None => println("Not found")
    case _ =>
    }
    if(capitalOption.isDefined) println(capitalOption.get)
    println(capitalOption getOrElse "Not found")
  • Scala IO == Java + Commons IO
    vallines = Source.fromFile("d:scalaMobyDick.txt").getLines;
    valwithNumbers = lines.foldLeft(List(""))((l,s) => (s.length+" "+s)::l)
    println(withNumbers.mkString("n"))
    withNumbers.foreach(println)
    withNumbers.filter(!_.startsWith("0")).map(_.toUpperCase).
    sort(_.length < _.length).foreach(println)
  • Scala DI - Cake Pattern
    traitPersonService {
    deffindByAge(age: Int): Person
    }
     
    traitPersonServiceImplextendsPersonService {
    deffindByAge(age: Int): Person = new Person("", 12)
    }
     
     
    traitPersonDAO {
    defgetAll(): Seq[Person]
    }
     
    traitPersonDAOImplextendsPersonDAO {
    defgetAll(): Seq[Person] = List(new Person("", 12))
    }
  • Scala DI - Cake Pattern
    traitDirectory {
    self: PersonServicewithPersonDAO =>
    }
     
    classDirectoryComponentextends Directory withPersonServiceImplwithPersonDAOImpl {
    }
  • Guice
    Minimize mutability
    Avoid static state
    @Nullable