SlideShare une entreprise Scribd logo
1  sur  45
Télécharger pour lire hors ligne
Beyond null & more
Taming the monsters, the right way!
jayas@cisco.com
NullPointerException !!!
It must be some kind of race condition!!!
Functional and Reactive
Programming 101
for Java 7 Developers
What will you get?
● Get started with basic
Functional Programming
principles
● Reactive System Design
● Adopt in your day to day
work from now!
Functional Programming Intro
● Function
○ y = f(x)
● Functions as ‘First Class’ Citizens
○ Similar to values
○ Pass to / return from other functions
● Separate Data from Functions
○ Data is passed to functions
○ Data may be returned from functions
○ State Management outside of Functions
Programming with Functions
Functional Programming Facets
● Pure Functions
○ No Side Effects
○ Referential Transparency
● Immutable Data Structures
● Expressive Power
○ Ability to Reason - Equational Reasoning
○ Function Composition
■ f : b -> c , g : a -> b
■ f o g : a -> c
● f o g = f(g(x))
Pure Functions
● No side effects , including IO
○ For a set of inputs, always produce the same output
● Referential Transparency
○ Can ‘inline’ the function everywhere with no changes to the
behavior
○ Works with substitution model of computation
● Ensures Testability
Pure Functions
Is this a pure function?
func increment(int value ) {
return value + 1;
}
Pure Functions
How about this?
func increment(int value )
{
if(value < maxVal) {
return value + 1;
} else {
return maxVal;
}
}
func increment(int value, int maxVal )
{
if(value < maxVal) {
return value + 1;
} else {
return maxVal;
}
}
Pure Functions
..and this too?
func increment(int value ) {
int newValue = value +1;
localStorage.put(“curValue”,
newValue);
return newValue;
}
func increment(int value ) {
return {
“val” : value +1,
“task” : {
“task” : localStorage,
“key” : “curValue”,
“val” : value+1
}
};
}
Pure Functions
Ignore IO and let’s discuss this
func updatePwd(String pwd )
{
Node node = nodeFromExternal();
if(node.id == 2) {
invokeApi(node,pwd);
}
}
func updatePwd(String pwd , int id) {
Node node = nodeFromExternal();
if(id == node.id ) {
invokeApi(node,pwd);
}
}
Immutability
Reference to a data structure is *only* for reading from the data structure
Once you have a reference:
● Guarantees that no one will modify its contents
● Pass along to any number of threads without fear of ‘stepping on each
other’
○ Enables safe concurrency and parallelism
● Any updates to the data structure returns a new reference
Immutability : an Example
Defects (holds list of open defects)
val defectList = List(Defect(id="CSSid1234",age=48,engineer="jayas"),Defect(id="CSCid5678",age=12,engineer="otherone"))
defectList.foreach { defect =>
if (defect.age > 28) {
sendMsg(defect.engineer)
}
}
defectList.dropWhile { defect =>
defect.engineer == "jayas" }
Thread 1 Thread 2
val myDefectList =
Why Functional Programming
Pure Functions
● Guarantees Testability
● Easy Refactoring
● Reduce Defects
Immutable Data Structures
● Safe Concurrency and Parallelism
○ Eliminate concurrency / multi-threading defects
○ Enables lock free operations
● Ensures Testability
Why Functional Programming
Expressive Power
● Readability and Maintainability
● Adhering to mathematical laws and
principles
● Programming by whole values
○ Lesser Lines of Code , and thus
lesser the possibility of defects
More Info
● View “Why Functional Programming
Matters”
OOP
OO - in its Original Form
● State Encapsulation in Objects
● Message Passing for Communication
○ Objects to process messages sent to it
○ Respond by sending messages
● Not intended to use as Data
Containers/Holders
OO - as what we do for living
● Primarily a Data Holder
● No Message Passing - arbitrary retrieval and
update of data
● Imperative Programming to the core
○ Manipulating State
○ Instructing Computer on How to Do
● Hard to do Concurrency
Imperative Vs Functional
● Programming By Word Vs Whole Values
○ No word at a time processing
○ Work with entire value (data structures)
● How to Do Vs What To Do
○ Focus on business logic implementation
○ E.g: No instructions on how to traverse a list
● Less Code that Does more
○ Reduce Lines of Code
○ Reduce Possibility of Defects
for(int i=0; i< j; i++)
{
....
}
Whole Value Programming
map (A ->B) List<A> -> List<B>
● List of agents -> List of agent UserIds
flatMap (A -> List<B>) List<A> -> List<B>
● List of agents -> List of extension numbers
filter (A->Boolean) List<A> -> List<A>
● Get all supervisors from user list
foldl (acc x -> y) x List<x> -> y
● Total talk time from a list of call data records
Do a map, filter and fold it!
map a list of agents to list of strings (using Guava in Java 7)
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
Function<Agent, String> agentTransformer= new Function<Agent,String>() {
@Override
public String apply(Agent agent) {
return agent.getId();
}
};
List<String> agentIds = new ArrayList<String>(Collections2.transform(agents,agentTransformer));
Whole Value Programming
filter a list of agents to list of supervisors (using Guava in Java 7)
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
Predicate<Agent> supervisorFilter = new Predicate<Agent>() {
@Override
public boolean apply(Agent agent) {
return agent.role == Role.SUPERVISOR;
}
};
Collection<Agent> supervisors = Collections2.filter(agents, supervisorFilter);
List<String> supervisorIds = new ArrayList<String>(Collections2.transform(supervisors,agentTransformer));
Whole Value Programming
Strong Static Typing
● Type System and Compiler as First Line of defense
● Reduces dependency on Unit Tests
○ Most of the time, if it compiles, it will run
● Expressive Power By Type
○ e.g: Optional for indicating Value being present or not (vs returning null)
Strong Static Typing
Get rid of null, Now. (using Guava in Java 7)
import com.google.common.base.Optional;
class Agent {
Optional<String> agentAlias; // alias may not may not be set
public void setAlias(String alias) {
agentAlias = Optional.fromNullable(alias); // alias could be null, Java !!
}
public Optional<String> getAlias() {
return agentAlias; // returns Optional indicating that alias may or may not be there
}
}
Strong Static Typing
Indicate Error by Type, Later. ( in Scala)
def fetchDefectsFromDB() : Try[List[Defect]]= {
Try {
readFromDB// Get the list from DB
}
}
val defectList : Try[List[Defect]] = fetchDefectsFromDB()
defectList.map { defects => defects.foreach {defect => if (defect.age > 28) { sendMsg(defect.engineer)} } }
def fetchDefectsWhenDBDown() : Try[List[Defect]]= {
Failure(new Throwable(new IllegalStateException("DB Service is not in Running State)))
}
I am not sure.. What Can I do
Adopt these rules
● No nulls to indicate absence. Use Optional
○ Use it judiciously.
○ Do not use it purely to indicate an un-initialized internal state
○ Use it to indicate to other that data may or may not be present
● No loops . Use Collections2 utils
○ Simple for expression may be still efficient for simple iterations.
○ Use Guava if it avoids code duplication and does not cause performance overhead
● Make all method parameters final
○ Unfortunately there are no immutable collections in Java
I am not sure.. What Can I do
Adopt these rules
● No Explicit Multi threading / Concurrency
○ For state, in case concurrency is a need, ensure all messages to the object are processed in
sequence (yes, sequentially)
○ Use a single threaded executor to handle messages received by an object
● Question and Revisit Impure functions / methods
○ Strictly adhere to Single Responsibility Principle of OO
○ Write pure functions unless it’s unavoidable
○ It’s fine to have a class with all static methods, which are pure!
● Isolate State management and IO to one part of the system
○ Let 80 % of the implementation be pure and stateless
I am not sure.. What Can I do
Adopt these rules
● As Always, there are exceptions to all the rules , check with Architect.
● Performance , Maintainability and Readability are of most importance
○ Do not compromise on these
Learn Functional Programming
Haskell: https://github.com/data61/fp-course
Elm: https://www.elm-tutorial.org/en/
Stay Tuned for Elm training and other FP initiatives!
Architecture & Design
Reactive Programming
Corner cases are Corner Stones in Customer Experience
Design For Failure - Failures are Inevitable
Ensure testability at all levels - architecture, design, code
Reactive Systems Traits
Responsive
Message Driven
Resilient Elastic
Source: https://www.reactivemanifesto.org
Design & Architecture
Asynchronous Communication
● Message Passing across components
○ Asynchronous APIs another choice across systems
● Non Blocking IO, Lock Free Operations
○ Use Async Servlet (in Servlet 3.0) for web applications (Shindig is a perfect use case)
○ Use non blocking version of libraries, say http-client, jersey server and client
● Work on Futures and Future Compositions
○ Composition of Futures - not present by default in Java 7
○ Use ListenableFuture in Guava / CompletableFuture in Java 8
○ Evaluate and Use RxJava
Design & Architecture
Resiliency
● Build for failures
○ Failures are Inevitable
○ Architecture and Design should facilitate failure propagation and recovery
● Communicate Error & Recover from Failures
○ Let the user know
○ Provide error recovery option
● Retries for inter-component interactions / critical operations
● Location Transparency for distributed/ clustered systems
○ Distributed Cache usage does not rely on fetching cache entry from a particular node
Design & Architecture
Responsive
● Respond even when subsystems are down
○ Let the user know
● Provide degraded /lower set of functionalities than being totally unresponsive
● Have alternate option for dependent critical subsystems / external systems
Design & Architecture
Elastic
● Ability to scale up / down / out / in
○ Build services that caters to 400 users (CCX) , at the same time that can scale for 18000
users (CCE)
○ Utilize all the cores of CPU
○ Ability to add more machines to scale out
■ distributed data processing, location transparency for processing engines
Additional Learnings
Guava Wiki
RxJava
John Backus’s Turing Award Lecture
Learn Functional Programming in Haskell
Effects As Data - Talk
Thank You!

Contenu connexe

Tendances

Beyond PITS, Functional Principles for Software Architecture
Beyond PITS, Functional Principles for Software ArchitectureBeyond PITS, Functional Principles for Software Architecture
Beyond PITS, Functional Principles for Software ArchitectureJayaram Sankaranarayanan
 
The craft of meta programming on JVM
The craft of meta programming on JVMThe craft of meta programming on JVM
The craft of meta programming on JVMIgor Khotin
 
Categories for the Working C++ Programmer
Categories for the Working C++ ProgrammerCategories for the Working C++ Programmer
Categories for the Working C++ ProgrammerPlatonov Sergey
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIJörn Guy Süß JGS
 
User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4Shameer Ahmed Koya
 
11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class웅식 전
 
Teach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with ScalaTeach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with ScalaDamian Jureczko
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2Leonid Maslov
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions WebStackAcademy
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - OperatorsWebStackAcademy
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programmingNico Ludwig
 
TMPA-2015: A Need To Specify and Verify Standard Functions
TMPA-2015: A Need To Specify and Verify Standard FunctionsTMPA-2015: A Need To Specify and Verify Standard Functions
TMPA-2015: A Need To Specify and Verify Standard FunctionsIosif Itkin
 
Dev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingDev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingSvetlin Nakov
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and FunctionsJake Bond
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic FunctionsWebStackAcademy
 

Tendances (20)

Beyond PITS, Functional Principles for Software Architecture
Beyond PITS, Functional Principles for Software ArchitectureBeyond PITS, Functional Principles for Software Architecture
Beyond PITS, Functional Principles for Software Architecture
 
The craft of meta programming on JVM
The craft of meta programming on JVMThe craft of meta programming on JVM
The craft of meta programming on JVM
 
Categories for the Working C++ Programmer
Categories for the Working C++ ProgrammerCategories for the Working C++ Programmer
Categories for the Working C++ Programmer
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its API
 
Pointers & functions
Pointers &  functionsPointers &  functions
Pointers & functions
 
User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4
 
11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class
 
Teach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with ScalaTeach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with Scala
 
Composite types
Composite typesComposite types
Composite types
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Some basic FP concepts
Some basic FP conceptsSome basic FP concepts
Some basic FP concepts
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2
 
JavaScript operators
JavaScript operatorsJavaScript operators
JavaScript operators
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
TMPA-2015: A Need To Specify and Verify Standard Functions
TMPA-2015: A Need To Specify and Verify Standard FunctionsTMPA-2015: A Need To Specify and Verify Standard Functions
TMPA-2015: A Need To Specify and Verify Standard Functions
 
Dev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingDev Concepts: Functional Programming
Dev Concepts: Functional Programming
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
 

Similaire à Functional Programming 101 for Java 7 Developers

Java 8 - functional features
Java 8 - functional featuresJava 8 - functional features
Java 8 - functional featuresRafal Rybacki
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Ovidiu Farauanu
 
An Overview of SystemVerilog for Design and Verification
An Overview of SystemVerilog  for Design and VerificationAn Overview of SystemVerilog  for Design and Verification
An Overview of SystemVerilog for Design and VerificationKapilRaghunandanTrip
 
Sharable of qualities of clean code
Sharable of qualities of clean codeSharable of qualities of clean code
Sharable of qualities of clean codeEman Mohamed
 
Test strategies for data processing pipelines, v2.0
Test strategies for data processing pipelines, v2.0Test strategies for data processing pipelines, v2.0
Test strategies for data processing pipelines, v2.0Lars Albertsson
 
Understanding Implicits in Scala
Understanding Implicits in ScalaUnderstanding Implicits in Scala
Understanding Implicits in Scaladatamantra
 
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...Jitendra Bafna
 
Nitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptxNitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptxshivam460694
 
Robust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time ChecksRobust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time ChecksStoyan Nikolov
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answersAkash Gawali
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingRichardWarburton
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to Elixirbrien_wankel
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And AnswerJagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answerlavparmar007
 

Similaire à Functional Programming 101 for Java 7 Developers (20)

Java 8 - functional features
Java 8 - functional featuresJava 8 - functional features
Java 8 - functional features
 
Python for web security - beginner
Python for web security - beginnerPython for web security - beginner
Python for web security - beginner
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
 
An Overview of SystemVerilog for Design and Verification
An Overview of SystemVerilog  for Design and VerificationAn Overview of SystemVerilog  for Design and Verification
An Overview of SystemVerilog for Design and Verification
 
Sharable of qualities of clean code
Sharable of qualities of clean codeSharable of qualities of clean code
Sharable of qualities of clean code
 
Test strategies for data processing pipelines, v2.0
Test strategies for data processing pipelines, v2.0Test strategies for data processing pipelines, v2.0
Test strategies for data processing pipelines, v2.0
 
Async fun
Async funAsync fun
Async fun
 
Understanding Implicits in Scala
Understanding Implicits in ScalaUnderstanding Implicits in Scala
Understanding Implicits in Scala
 
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
 
Nitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptxNitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptx
 
Robust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time ChecksRobust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time Checks
 
Java 7 & 8 New Features
Java 7 & 8 New FeaturesJava 7 & 8 New Features
Java 7 & 8 New Features
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
Introduction to Java Programming Part 2
Introduction to Java Programming Part 2Introduction to Java Programming Part 2
Introduction to Java Programming Part 2
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to Elixir
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
Database programming
Database programmingDatabase programming
Database programming
 

Dernier

What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 

Dernier (20)

What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 

Functional Programming 101 for Java 7 Developers

  • 1. Beyond null & more Taming the monsters, the right way! jayas@cisco.com
  • 3. It must be some kind of race condition!!!
  • 4.
  • 5.
  • 6. Functional and Reactive Programming 101 for Java 7 Developers
  • 7. What will you get? ● Get started with basic Functional Programming principles ● Reactive System Design ● Adopt in your day to day work from now!
  • 8. Functional Programming Intro ● Function ○ y = f(x) ● Functions as ‘First Class’ Citizens ○ Similar to values ○ Pass to / return from other functions ● Separate Data from Functions ○ Data is passed to functions ○ Data may be returned from functions ○ State Management outside of Functions Programming with Functions
  • 9. Functional Programming Facets ● Pure Functions ○ No Side Effects ○ Referential Transparency ● Immutable Data Structures ● Expressive Power ○ Ability to Reason - Equational Reasoning ○ Function Composition ■ f : b -> c , g : a -> b ■ f o g : a -> c ● f o g = f(g(x))
  • 10. Pure Functions ● No side effects , including IO ○ For a set of inputs, always produce the same output ● Referential Transparency ○ Can ‘inline’ the function everywhere with no changes to the behavior ○ Works with substitution model of computation ● Ensures Testability
  • 11. Pure Functions Is this a pure function? func increment(int value ) { return value + 1; }
  • 12. Pure Functions How about this? func increment(int value ) { if(value < maxVal) { return value + 1; } else { return maxVal; } } func increment(int value, int maxVal ) { if(value < maxVal) { return value + 1; } else { return maxVal; } }
  • 13. Pure Functions ..and this too? func increment(int value ) { int newValue = value +1; localStorage.put(“curValue”, newValue); return newValue; } func increment(int value ) { return { “val” : value +1, “task” : { “task” : localStorage, “key” : “curValue”, “val” : value+1 } }; }
  • 14. Pure Functions Ignore IO and let’s discuss this func updatePwd(String pwd ) { Node node = nodeFromExternal(); if(node.id == 2) { invokeApi(node,pwd); } } func updatePwd(String pwd , int id) { Node node = nodeFromExternal(); if(id == node.id ) { invokeApi(node,pwd); } }
  • 15. Immutability Reference to a data structure is *only* for reading from the data structure Once you have a reference: ● Guarantees that no one will modify its contents ● Pass along to any number of threads without fear of ‘stepping on each other’ ○ Enables safe concurrency and parallelism ● Any updates to the data structure returns a new reference
  • 16. Immutability : an Example Defects (holds list of open defects) val defectList = List(Defect(id="CSSid1234",age=48,engineer="jayas"),Defect(id="CSCid5678",age=12,engineer="otherone")) defectList.foreach { defect => if (defect.age > 28) { sendMsg(defect.engineer) } } defectList.dropWhile { defect => defect.engineer == "jayas" } Thread 1 Thread 2 val myDefectList =
  • 17. Why Functional Programming Pure Functions ● Guarantees Testability ● Easy Refactoring ● Reduce Defects Immutable Data Structures ● Safe Concurrency and Parallelism ○ Eliminate concurrency / multi-threading defects ○ Enables lock free operations ● Ensures Testability
  • 18. Why Functional Programming Expressive Power ● Readability and Maintainability ● Adhering to mathematical laws and principles ● Programming by whole values ○ Lesser Lines of Code , and thus lesser the possibility of defects More Info ● View “Why Functional Programming Matters”
  • 19. OOP
  • 20. OO - in its Original Form ● State Encapsulation in Objects ● Message Passing for Communication ○ Objects to process messages sent to it ○ Respond by sending messages ● Not intended to use as Data Containers/Holders
  • 21. OO - as what we do for living ● Primarily a Data Holder ● No Message Passing - arbitrary retrieval and update of data ● Imperative Programming to the core ○ Manipulating State ○ Instructing Computer on How to Do ● Hard to do Concurrency
  • 22. Imperative Vs Functional ● Programming By Word Vs Whole Values ○ No word at a time processing ○ Work with entire value (data structures) ● How to Do Vs What To Do ○ Focus on business logic implementation ○ E.g: No instructions on how to traverse a list ● Less Code that Does more ○ Reduce Lines of Code ○ Reduce Possibility of Defects for(int i=0; i< j; i++) { .... }
  • 23. Whole Value Programming map (A ->B) List<A> -> List<B> ● List of agents -> List of agent UserIds flatMap (A -> List<B>) List<A> -> List<B> ● List of agents -> List of extension numbers filter (A->Boolean) List<A> -> List<A> ● Get all supervisors from user list foldl (acc x -> y) x List<x> -> y ● Total talk time from a list of call data records Do a map, filter and fold it!
  • 24. map a list of agents to list of strings (using Guava in Java 7) import com.google.common.base.Function; import com.google.common.collect.Collections2; Function<Agent, String> agentTransformer= new Function<Agent,String>() { @Override public String apply(Agent agent) { return agent.getId(); } }; List<String> agentIds = new ArrayList<String>(Collections2.transform(agents,agentTransformer)); Whole Value Programming
  • 25. filter a list of agents to list of supervisors (using Guava in Java 7) import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; Predicate<Agent> supervisorFilter = new Predicate<Agent>() { @Override public boolean apply(Agent agent) { return agent.role == Role.SUPERVISOR; } }; Collection<Agent> supervisors = Collections2.filter(agents, supervisorFilter); List<String> supervisorIds = new ArrayList<String>(Collections2.transform(supervisors,agentTransformer)); Whole Value Programming
  • 26. Strong Static Typing ● Type System and Compiler as First Line of defense ● Reduces dependency on Unit Tests ○ Most of the time, if it compiles, it will run ● Expressive Power By Type ○ e.g: Optional for indicating Value being present or not (vs returning null)
  • 27. Strong Static Typing Get rid of null, Now. (using Guava in Java 7) import com.google.common.base.Optional; class Agent { Optional<String> agentAlias; // alias may not may not be set public void setAlias(String alias) { agentAlias = Optional.fromNullable(alias); // alias could be null, Java !! } public Optional<String> getAlias() { return agentAlias; // returns Optional indicating that alias may or may not be there } }
  • 28. Strong Static Typing Indicate Error by Type, Later. ( in Scala) def fetchDefectsFromDB() : Try[List[Defect]]= { Try { readFromDB// Get the list from DB } } val defectList : Try[List[Defect]] = fetchDefectsFromDB() defectList.map { defects => defects.foreach {defect => if (defect.age > 28) { sendMsg(defect.engineer)} } } def fetchDefectsWhenDBDown() : Try[List[Defect]]= { Failure(new Throwable(new IllegalStateException("DB Service is not in Running State))) }
  • 29.
  • 30. I am not sure.. What Can I do Adopt these rules ● No nulls to indicate absence. Use Optional ○ Use it judiciously. ○ Do not use it purely to indicate an un-initialized internal state ○ Use it to indicate to other that data may or may not be present ● No loops . Use Collections2 utils ○ Simple for expression may be still efficient for simple iterations. ○ Use Guava if it avoids code duplication and does not cause performance overhead ● Make all method parameters final ○ Unfortunately there are no immutable collections in Java
  • 31. I am not sure.. What Can I do Adopt these rules ● No Explicit Multi threading / Concurrency ○ For state, in case concurrency is a need, ensure all messages to the object are processed in sequence (yes, sequentially) ○ Use a single threaded executor to handle messages received by an object ● Question and Revisit Impure functions / methods ○ Strictly adhere to Single Responsibility Principle of OO ○ Write pure functions unless it’s unavoidable ○ It’s fine to have a class with all static methods, which are pure! ● Isolate State management and IO to one part of the system ○ Let 80 % of the implementation be pure and stateless
  • 32. I am not sure.. What Can I do Adopt these rules ● As Always, there are exceptions to all the rules , check with Architect. ● Performance , Maintainability and Readability are of most importance ○ Do not compromise on these
  • 33. Learn Functional Programming Haskell: https://github.com/data61/fp-course Elm: https://www.elm-tutorial.org/en/ Stay Tuned for Elm training and other FP initiatives!
  • 35. Corner cases are Corner Stones in Customer Experience
  • 36. Design For Failure - Failures are Inevitable
  • 37. Ensure testability at all levels - architecture, design, code
  • 38. Reactive Systems Traits Responsive Message Driven Resilient Elastic Source: https://www.reactivemanifesto.org
  • 39. Design & Architecture Asynchronous Communication ● Message Passing across components ○ Asynchronous APIs another choice across systems ● Non Blocking IO, Lock Free Operations ○ Use Async Servlet (in Servlet 3.0) for web applications (Shindig is a perfect use case) ○ Use non blocking version of libraries, say http-client, jersey server and client ● Work on Futures and Future Compositions ○ Composition of Futures - not present by default in Java 7 ○ Use ListenableFuture in Guava / CompletableFuture in Java 8 ○ Evaluate and Use RxJava
  • 40. Design & Architecture Resiliency ● Build for failures ○ Failures are Inevitable ○ Architecture and Design should facilitate failure propagation and recovery ● Communicate Error & Recover from Failures ○ Let the user know ○ Provide error recovery option ● Retries for inter-component interactions / critical operations ● Location Transparency for distributed/ clustered systems ○ Distributed Cache usage does not rely on fetching cache entry from a particular node
  • 41. Design & Architecture Responsive ● Respond even when subsystems are down ○ Let the user know ● Provide degraded /lower set of functionalities than being totally unresponsive ● Have alternate option for dependent critical subsystems / external systems
  • 42. Design & Architecture Elastic ● Ability to scale up / down / out / in ○ Build services that caters to 400 users (CCX) , at the same time that can scale for 18000 users (CCE) ○ Utilize all the cores of CPU ○ Ability to add more machines to scale out ■ distributed data processing, location transparency for processing engines
  • 43.
  • 44. Additional Learnings Guava Wiki RxJava John Backus’s Turing Award Lecture Learn Functional Programming in Haskell Effects As Data - Talk