SlideShare ist ein Scribd-Unternehmen logo
1 von 31
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)  } ,[object Object],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("")) 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

Weitere ähnliche Inhalte

Was ist angesagt?

JDK8 : parallel programming made (too ?) easy
JDK8 : parallel programming made (too ?) easyJDK8 : parallel programming made (too ?) easy
JDK8 : parallel programming made (too ?) easyJosé Paumard
 
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur..."How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...Fwdays
 
A Scala Corrections Library
A Scala Corrections LibraryA Scala Corrections Library
A Scala Corrections LibraryPaul Phillips
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoMuhammad Abdullah
 
A Re-Introduction to JavaScript
A Re-Introduction to JavaScriptA Re-Introduction to JavaScript
A Re-Introduction to JavaScriptSimon Willison
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersBartosz Kosarzycki
 
Scaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaScaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaOstap Andrusiv
 
Java 8 Streams & Collectors : the Leuven edition
Java 8 Streams & Collectors : the Leuven editionJava 8 Streams & Collectors : the Leuven edition
Java 8 Streams & Collectors : the Leuven editionJosé Paumard
 
Designing with Groovy Traits - Gr8Conf India
Designing with Groovy Traits - Gr8Conf IndiaDesigning with Groovy Traits - Gr8Conf India
Designing with Groovy Traits - Gr8Conf IndiaNaresha K
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoïc Descotte
 

Was ist angesagt? (20)

JDK8 : parallel programming made (too ?) easy
JDK8 : parallel programming made (too ?) easyJDK8 : parallel programming made (too ?) easy
JDK8 : parallel programming made (too ?) easy
 
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur..."How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Coffee script
Coffee scriptCoffee script
Coffee script
 
All about scala
All about scalaAll about scala
All about scala
 
A Scala Corrections Library
A Scala Corrections LibraryA Scala Corrections Library
A Scala Corrections Library
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demo
 
Google Guava
Google GuavaGoogle Guava
Google Guava
 
Beyond java8
Beyond java8Beyond java8
Beyond java8
 
Scala vs Ruby
Scala vs RubyScala vs Ruby
Scala vs Ruby
 
A Re-Introduction to JavaScript
A Re-Introduction to JavaScriptA Re-Introduction to JavaScript
A Re-Introduction to JavaScript
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
 
Comparing JVM languages
Comparing JVM languagesComparing JVM languages
Comparing JVM languages
 
Scala on Android
Scala on AndroidScala on Android
Scala on Android
 
The Xtext Grammar Language
The Xtext Grammar LanguageThe Xtext Grammar Language
The Xtext Grammar Language
 
Scaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaScaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with Scala
 
Java 8 Streams & Collectors : the Leuven edition
Java 8 Streams & Collectors : the Leuven editionJava 8 Streams & Collectors : the Leuven edition
Java 8 Streams & Collectors : the Leuven edition
 
Designing with Groovy Traits - Gr8Conf India
Designing with Groovy Traits - Gr8Conf IndiaDesigning with Groovy Traits - Gr8Conf India
Designing with Groovy Traits - Gr8Conf India
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 

Ähnlich wie Scala == Effective Java

1.1 motivation
1.1 motivation1.1 motivation
1.1 motivationwpgreenway
 
Features of Kotlin I find exciting
Features of Kotlin I find excitingFeatures of Kotlin I find exciting
Features of Kotlin I find excitingRobert MacLean
 
About java
About javaAbout java
About javaJay Xu
 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfNicholasflqStewartl
 
CodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodecamp Romania
 
can do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdfcan do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdfakshpatil4
 
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!Guilherme Carreiro
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)William Narmontas
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. ExperienceMike Fogus
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokusHamletDRC
 
5 Bullets to Scala Adoption
5 Bullets to Scala Adoption5 Bullets to Scala Adoption
5 Bullets to Scala AdoptionTomer Gabel
 
1.2 Scala Basics
1.2 Scala Basics1.2 Scala Basics
1.2 Scala Basicsretronym
 

Ähnlich wie Scala == Effective Java (20)

Scala introduction
Scala introductionScala introduction
Scala introduction
 
1.1 motivation
1.1 motivation1.1 motivation
1.1 motivation
 
1.1 motivation
1.1 motivation1.1 motivation
1.1 motivation
 
1.1 motivation
1.1 motivation1.1 motivation
1.1 motivation
 
1.1 motivation
1.1 motivation1.1 motivation
1.1 motivation
 
Features of Kotlin I find exciting
Features of Kotlin I find excitingFeatures of Kotlin I find exciting
Features of Kotlin I find exciting
 
About java
About javaAbout java
About java
 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdf
 
CodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical Groovy
 
can do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdfcan do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdf
 
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)
 
Fantom and Tales
Fantom and TalesFantom and Tales
Fantom and Tales
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. Experience
 
Java 8 Examples
Java 8 ExamplesJava 8 Examples
Java 8 Examples
 
Elegant objects
Elegant objectsElegant objects
Elegant objects
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
 
5 Bullets to Scala Adoption
5 Bullets to Scala Adoption5 Bullets to Scala Adoption
5 Bullets to Scala Adoption
 
C# 3.5 Features
C# 3.5 FeaturesC# 3.5 Features
C# 3.5 Features
 
1.2 Scala Basics
1.2 Scala Basics1.2 Scala Basics
1.2 Scala Basics
 

Mehr von Scalac

Applicative functors by Łukasz Marchewka
Applicative functors by Łukasz MarchewkaApplicative functors by Łukasz Marchewka
Applicative functors by Łukasz MarchewkaScalac
 
AWS Api Gateway by Łukasz Marchewka Scalacc
AWS Api Gateway by Łukasz Marchewka ScalaccAWS Api Gateway by Łukasz Marchewka Scalacc
AWS Api Gateway by Łukasz Marchewka ScalaccScalac
 
Do ECTL not ETL: the art and science of data cleansing in data pipelines by P...
Do ECTL not ETL: the art and science of data cleansing in data pipelines by P...Do ECTL not ETL: the art and science of data cleansing in data pipelines by P...
Do ECTL not ETL: the art and science of data cleansing in data pipelines by P...Scalac
 
React Hooks by Oleksandr Oleksiv Scalac
React Hooks by Oleksandr Oleksiv ScalacReact Hooks by Oleksandr Oleksiv Scalac
React Hooks by Oleksandr Oleksiv ScalacScalac
 
Introduction to Scala by Piotr Wiśniowski Scalac
Introduction to Scala by Piotr Wiśniowski ScalacIntroduction to Scala by Piotr Wiśniowski Scalac
Introduction to Scala by Piotr Wiśniowski ScalacScalac
 
ZIO actors by Mateusz Sokół Scalac
ZIO actors by Mateusz Sokół ScalacZIO actors by Mateusz Sokół Scalac
ZIO actors by Mateusz Sokół ScalacScalac
 
Why functional programming and category theory strongly matters - Piotr Parad...
Why functional programming and category theory strongly matters - Piotr Parad...Why functional programming and category theory strongly matters - Piotr Parad...
Why functional programming and category theory strongly matters - Piotr Parad...Scalac
 
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Scalac
 
How to write automated tests and don’t lose your mind by Dorian Sarnowski Scalac
How to write automated tests and don’t lose your mind by Dorian Sarnowski ScalacHow to write automated tests and don’t lose your mind by Dorian Sarnowski Scalac
How to write automated tests and don’t lose your mind by Dorian Sarnowski ScalacScalac
 
Do you have that Spark in your ECTL? by Piotr Sych Scalac
Do you have that Spark in your ECTL? by Piotr Sych ScalacDo you have that Spark in your ECTL? by Piotr Sych Scalac
Do you have that Spark in your ECTL? by Piotr Sych ScalacScalac
 
Can we automate the process of backlog prioritizing? by Adam Gadomski Scalac
Can we automate the process of backlog prioritizing? by Adam Gadomski ScalacCan we automate the process of backlog prioritizing? by Adam Gadomski Scalac
Can we automate the process of backlog prioritizing? by Adam Gadomski ScalacScalac
 
How to create the right sales funnel for your business? by Maciej Greń
How to create the right sales funnel for your business? by Maciej GreńHow to create the right sales funnel for your business? by Maciej Greń
How to create the right sales funnel for your business? by Maciej GreńScalac
 
ActorRef[Typed] by Andrzej Kopeć
ActorRef[Typed] by Andrzej KopećActorRef[Typed] by Andrzej Kopeć
ActorRef[Typed] by Andrzej KopećScalac
 
Liftweb
LiftwebLiftweb
LiftwebScalac
 

Mehr von Scalac (14)

Applicative functors by Łukasz Marchewka
Applicative functors by Łukasz MarchewkaApplicative functors by Łukasz Marchewka
Applicative functors by Łukasz Marchewka
 
AWS Api Gateway by Łukasz Marchewka Scalacc
AWS Api Gateway by Łukasz Marchewka ScalaccAWS Api Gateway by Łukasz Marchewka Scalacc
AWS Api Gateway by Łukasz Marchewka Scalacc
 
Do ECTL not ETL: the art and science of data cleansing in data pipelines by P...
Do ECTL not ETL: the art and science of data cleansing in data pipelines by P...Do ECTL not ETL: the art and science of data cleansing in data pipelines by P...
Do ECTL not ETL: the art and science of data cleansing in data pipelines by P...
 
React Hooks by Oleksandr Oleksiv Scalac
React Hooks by Oleksandr Oleksiv ScalacReact Hooks by Oleksandr Oleksiv Scalac
React Hooks by Oleksandr Oleksiv Scalac
 
Introduction to Scala by Piotr Wiśniowski Scalac
Introduction to Scala by Piotr Wiśniowski ScalacIntroduction to Scala by Piotr Wiśniowski Scalac
Introduction to Scala by Piotr Wiśniowski Scalac
 
ZIO actors by Mateusz Sokół Scalac
ZIO actors by Mateusz Sokół ScalacZIO actors by Mateusz Sokół Scalac
ZIO actors by Mateusz Sokół Scalac
 
Why functional programming and category theory strongly matters - Piotr Parad...
Why functional programming and category theory strongly matters - Piotr Parad...Why functional programming and category theory strongly matters - Piotr Parad...
Why functional programming and category theory strongly matters - Piotr Parad...
 
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...
 
How to write automated tests and don’t lose your mind by Dorian Sarnowski Scalac
How to write automated tests and don’t lose your mind by Dorian Sarnowski ScalacHow to write automated tests and don’t lose your mind by Dorian Sarnowski Scalac
How to write automated tests and don’t lose your mind by Dorian Sarnowski Scalac
 
Do you have that Spark in your ECTL? by Piotr Sych Scalac
Do you have that Spark in your ECTL? by Piotr Sych ScalacDo you have that Spark in your ECTL? by Piotr Sych Scalac
Do you have that Spark in your ECTL? by Piotr Sych Scalac
 
Can we automate the process of backlog prioritizing? by Adam Gadomski Scalac
Can we automate the process of backlog prioritizing? by Adam Gadomski ScalacCan we automate the process of backlog prioritizing? by Adam Gadomski Scalac
Can we automate the process of backlog prioritizing? by Adam Gadomski Scalac
 
How to create the right sales funnel for your business? by Maciej Greń
How to create the right sales funnel for your business? by Maciej GreńHow to create the right sales funnel for your business? by Maciej Greń
How to create the right sales funnel for your business? by Maciej Greń
 
ActorRef[Typed] by Andrzej Kopeć
ActorRef[Typed] by Andrzej KopećActorRef[Typed] by Andrzej Kopeć
ActorRef[Typed] by Andrzej Kopeć
 
Liftweb
LiftwebLiftweb
Liftweb
 

Kürzlich hochgeladen

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 

Kürzlich hochgeladen (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 

Scala == Effective Java

  • 1. Clean Code Scala Scala == Effective Java ?
  • 2.
  • 3.
  • 4.
  • 5. „Leave camp cleaner than you found it.” Uncle Bob
  • 6.
  • 7. 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)
  • 8. „Its harder to read the code than to write it” Joel Spolsky
  • 9. 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
  • 10. Java dev libraries Google Guava Apache Commons Spring Guice Scala !
  • 11. Why Scala ? More concise More powerfull Fun! Fast Becoming popular !
  • 12. Who uses Scala already
  • 13. 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; } }
  • 14. Class Parameters - Scala classPerson(var name: String, var age: Int)
  • 15. 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; }
  • 16. Scala case class caseclass Person(name: String, age: Int) Dostajemy za darmo: equals(), hashCode() oraz toString() oraz niezmienną klasę (immutable).
  • 17. 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 ???
  • 18. 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 // ... ???
  • 19.
  • 20. 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, ???");
  • 21. 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
  • 22. 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; }
  • 23. Parameter validation caseclass Person(name: String, age: Int) { // @elidable(ASSERTION)assert(age > 0, "Age < 0")// by name parameter }
  • 24. 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(); } }
  • 25. Working with arrays Scala val all: Array[Person] val (minors, adults) = all.partition(_.age<18)
  • 26. 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
  • 27. 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")
  • 28. 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("")) withNumbers.foreach(println) withNumbers.filter(!_.startsWith("0")).map(_.toUpperCase). sort(_.length < _.length).foreach(println)
  • 29. 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)) }
  • 30. Scala DI - Cake Pattern traitDirectory { self: PersonServicewithPersonDAO => }   classDirectoryComponentextends Directory withPersonServiceImplwithPersonDAOImpl { }
  • 31. Guice Minimize mutability Avoid static state @Nullable