Clean Code Scala<br />Scala == Effective Java ?<br />
„Leave camp cleaner <br />than you found it.”<br />Uncle Bob<br />
Why do we write bad code ???<br />Rush (duck tape programmer)<br />Laziness<br />Careless<br />We don’t know how good code...
„Its harder to read the <br />code <br />than to write it”<br />Joel Spolsky<br />
How to improve work quality ?<br />Dont give developers money rewardsbigger reward == worse performance<br />Let them work...
Java dev libraries<br />Google Guava<br />Apache Commons<br />Spring<br />Guice<br />Scala !<br />
Why Scala ?<br />More concise<br />More powerfull<br />Fun!<br />Fast<br />Becoming popular !<br />
Who uses Scala already<br />
Class Parameters - Java<br />publicclass Person {<br />	private String name;<br />	privateintage;<br />publicPerson(String...
Class Parameters - Scala<br />classPerson(var name: String, var age: Int)<br />
publicclass Person {<br />private String name;<br />privateintage;<br />public Person(String name, int age) {<br />this.na...
Scala case class<br />caseclass Person(name: String, age: Int)<br />Dostajemy za darmo: equals(), hashCode() oraz toString...
To equal or not to equal ?<br /> 		Person p = new Person("Jan Kowalski", 30);<br />		Set<Person> set = new HashSet<Person>...
To equal or not to equal ?<br />Iterator<Person> it = set.iterator();<br />booleancontainedP = false;<br />while (it.hasNe...
Scala Case Class<br />classPerson(val name: String, val age: Int)<br />objectPerson {<br />def apply(name: String, age: In...
Java – working with Person<br />Object x = new Person("Bill Clinton", 64);<br />if(x instanceof Person) {<br />	Person p =...
Scala – Pattern Matching<br />var x: Any = Person("Lukasz", 28);<br />	x match {<br />case Person(name, age) => println("P...
Parameter validation<br />publicclass Person {<br />	private String name;<br />	privateintage;<br />publicPerson(String na...
Parameter validation<br />caseclass Person(name: String, age: Int) {<br />// @elidable(ASSERTION)assert(age > 0, "Age < 0"...
Working with arrays<br />Java<br />publicclass Partition {<br />Person[] all;<br />Person[] adults;<br />Person[] minors;<...
Working with arrays<br />Scala<br />val all: Array[Person]<br />val (minors, adults) = all.partition(_.age<18)<br />
Null’s – Java<br />Map<String, String> capitals = <br />new HashMap<String, String>();<br />capitals.put("Poland", "Warsaw...
Null’s - Scala<br />  val capitals = Map("Poland" -> "Warsaw");<br />val capitalOption: Option[String] = capitals.get("Pol...
Scala IO == Java + Commons IO<br />vallines = Source.fromFile("d:scalaMobyDick.txt").getLines;<br />valwithNumbers = lines...
Scala DI - Cake Pattern<br />traitPersonService {<br />deffindByAge(age: Int): Person<br />}<br /> <br />traitPersonServic...
Scala DI - Cake Pattern<br />traitDirectory {<br />	self: PersonServicewithPersonDAO =><br />}<br /> <br />classDirectoryC...
Guice<br />Minimize mutability<br />Avoid static state<br />@Nullable<br />
Upcoming SlideShare
Loading in...5
×

Scala == Effective Java

2,893

Published on

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

Published in: Technology, News & Politics
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,893
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
36
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Transcript of "Scala == Effective Java"

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

    Clipping is a handy way to collect important slides you want to go back to later.

×