SlideShare a Scribd company logo
1 of 20
Download to read offline
Karol Kalisz, Digital Supply Chain & Internet of Things
Writing Testable Code Inspired by Functional
Programming and Microservice Rules
INSPIRE
© 2017 SAP SE or an SAP affiliate company. All rights reserved. 2Internal
What you will learn today?
You will learn how to create better code with
concepts from functional programing and
microservices.
You will be inspired to be a better developer.
© 2017 SAP SE or an SAP affiliate company. All rights reserved. 3Internal
Concepts of functional programing that are valuable for testing
Pure functions
Purely functional functions (or expressions) have no side effects.
– Pure functions have several useful properties, many of which can be used to optimize the code.
First-class and higher-order functions
Higher-order functions can take other functions as arguments or return them as results – first-class
functions.
Strict and non-strict evaluation
Use of strict (eager) or non-strict (lazy) evaluation – concepts that refer to how function arguments
are processed when an expression is being evaluated.
Strong recursion
Iteration (looping) in functional languages is usually accomplished through recursion.
– Recursive functions invoke themselves, allowing an operation to be performed over and over until
the base case is reached.
https://en.wikipedia.org/wiki/Functional_programming
© 2017 SAP SE or an SAP affiliate company. All rights reserved. 4Internal
Concepts of functional programing that are valuable for testing
Pure functions
Purely functional functions (or expressions) have no side effects.
– Pure functions have several useful properties, many of which can be used to optimize the code.
First-class and higher-order functions
Higher-order functions can take other functions as arguments or return them as results – first-class
functions.
Strict and non-strict evaluation
Use of strict (eager) or non-strict (lazy) evaluation – concepts that refer to how function arguments
are processed when an expression is being evaluated.
Strong recursion
Iteration (looping) in functional languages is usually accomplished through recursion.
– Recursive functions invoke themselves, allowing an operation to be performed over and over until
the base case is reached.
https://en.wikipedia.org/wiki/Functional_programming
No side effects
Function
as argument
Better
performance
Less code
© 2017 SAP SE or an SAP affiliate company. All rights reserved. 5Internal
Concepts of microservices that are important for testing
Properties
The services are easy to replace:
– Services are organized around capabilities, such as the user-interface front-end,
recommendation, logistics, and billing.
– [Services can be implemented using different programming languages, databases,
hardware, and software environment, depending on what fits best.]
Architecture
Services should be small and the protocols should lightweight. This approach:
– Makes it easier to change and add functions and qualities to the system at any time
– Allows the architecture of an individual service to emerge through continuous refactoring
and therefore reduces the need for a large up-front design
https://en.wikipedia.org/wiki/Microservices
© 2017 SAP SE or an SAP affiliate company. All rights reserved. 6Internal
Concepts of microservices that are important for testing
Properties
The services are easy to replace:
– Services are organized around capabilities, such as the user-interface front-end,
recommendation, logistics, and billing.
– [Services can be implemented using different programming languages, databases,
hardware, and software environment, depending on what fits best.]
Architecture
Services should be small and the protocols should lightweight. This approach:
– Makes it easier to change and add functions and qualities to the system at any time
– Allows the architecture of an individual service to emerge through continuous refactoring
and therefore reduces the need for a large up-front design
https://en.wikipedia.org/wiki/Microservices
Structure
Flexibility
Testability
Maintainability
© 2017 SAP SE or an SAP affiliate company. All rights reserved. 7Internal
Using language specialties you can improve the way you code
Example based on JavaScript
What is challenging in JavaScript?
Parameters are untyped.
JavaScript Object Notation (JSON) is great as a data model:
– Good application programming interface (API) for accessing JSON objects,
serialization, deserialization, and cloning
– Easy programmatic access to any JSON object properties
– Easy binding to UI elements (such as SAPUI5)
A lot of Open Source is available for use:
– No compilation step is present; you cannot control any APIs from other modules.
Check your own programming language – you will find similar values.
© 2017 SAP SE or an SAP affiliate company. All rights reserved. 8Internal
Using language specialties you can improve the way you code
Example based on JavaScript
What is challenging in JavaScript?
Parameters are untyped.
JavaScript Object Notation (JSON) is great as a data model:
– Good application programming interface (API) for accessing JSON objects,
serialization, deserialization, and cloning
– Easy programmatic access to any JSON object properties
– Easy binding to UI elements (such as SAPUI5)
A lot of Open Source is available for use:
– No compilation step is present; you cannot control any APIs from other modules.
Check your own programming language – you will find similar values.
First-class
functions
Clear,
serializable
data model
Need for
better
tests
Let’s bring it together
Functional programming
Microservices
JavaScript
© 2017 SAP SE or an SAP affiliate company. All rights reserved. 10Internal
Five modelling rules to create better code
5
modelling
rules
- you can consider -
1Writeallfunctionslikemicroservices
Inputparametersareeitherprimitiveor
assuredtobeserializable.
Functionscanbeexecutedstateless.
Outputcanbeserializedaswell.
© 2017 SAP SE or an SAP affiliate company. All rights reserved. 11Internal
Five modelling rules to create better code
Write all functions like microservices
Input parameters are either primitive or
assured to be serializable
Functions can be executed stateless
Output can be serialized as well
2Writeallfunctionslikepurefunctions
Nocallstootherfunctionsinside
Allrequiredparametersandmethodspassedasinput
parameters
1
© 2017 SAP SE or an SAP affiliate company. All rights reserved. 12Internal
Five modelling rules to create better code
Write all functions like pure functions
No calls to other functions inside
All required parameters and methods passed
as input parameters
1Writeallfunctionslikemicroservices
Inputparametersareeitherprimitiveor
assuredtobeserializable.
Functionscanbeexecutedstateless.
Outputcanbeserializedaswell.
3Abstractrealobjectsassoonaspossible
Inuserinterface(UI)events,createanindependentdata
modelthatwillbeusedforthefunctionalprocessing.
Allparameterspassedtofunctionsmustbeserializable
intostring.
2
© 2017 SAP SE or an SAP affiliate company. All rights reserved. 13Internal
Five modelling rules to create better code
Abstract real objects as soon as possible
In user interface (UI) events, create an
independent data model that will be used
for the functional processing.
All parameters passed to functions must be
serializable
into string.
2Writeallfunctionslikepurefunctions
Nocallstootherfunctionsinside
Allrequiredparametersandmethodspassedasinput
parameters
4UseJSONasdatamodel
Canbeserializedanytime
CanbeusedforbindingtoSAPUI5elements
IseasytoaccessnativelyinJavaScriptcode
3
© 2017 SAP SE or an SAP affiliate company. All rights reserved. 14Internal
Five modelling rules to create better code
Use JSON as data model
Can be serialized any time
Can be used for binding to SAPUI5 elements
Is easy to access natively in JavaScript code
3Abstractrealobjectsassoonaspossible
Inuserinterface(UI)events,createanindependentdata
modelthatwillbeusedforthefunctionalprocessing.
Allparameterspassedtofunctionsmustbeserializable
intostring.
5Createlibrarieswithstatelesslogic
Ensuresnoaccesstoobjectsandotherfunctions
Meanseasiertestingoffunctions
Noneedtomockcomplexobjects
4
© 2017 SAP SE or an SAP affiliate company. All rights reserved. 15Internal
Five modelling rules to create better code
Create libraries with stateless logic
Ensures no access to objects and other
functions
Means easier testing of functions
No need to mock complex objects
4UseJSONasdatamodel
Canbeserializedanytime
CanbeusedforbindingtoSAPUI5elements
IseasytoaccessnativelyinJavaScriptcode
Usingtherulesallfunctionswillbereproducible
(idempotent)
Sameinputparametersalwaysthesameoutput.
Betterandstabletestsareeasiertocreate.
5
© 2017 SAP SE or an SAP affiliate company. All rights reserved. 16Internal
Five modelling rules to create better code
Using the rules all functions will be
reproducible (idempotent)
Same input parameters
always the same output.
Better and stable tests are easier to create
Caching for better performance is easier
5Createlibrarieswithstatelesslogic
Ensuresnoaccesstoobjectsandotherfunctions
Meanseasiertestingoffunctions
Noneedtomockcomplexobjects
1.2.3.4.5…
Let’s see the code
One function
applied rules
additional test code
JavaScript
© 2017 SAP SE or an SAP affiliate company. All rights reserved. 19Internal
Let’s be technical – if you follow the rules, you can serialize your
tests with automation
/* find a free id for an array using given prefix */
sap.scn.nextFreeArrayId = function (arrayToCheck, prefix, idProperty, f) {
if(sap.scn.tlog) {
sap.scn.tlog.fentry("nextFreeArrayId", arrayToCheck, prefix, idProperty);
}
var natNumber = 1; // start with ID 1
var newId = prefix + natNumber;
newId = f.createId(prefix, natNumber); // create new ID if f given
var indexN = 0; // loop on given array
for (indexN; indexN < arrayToCheck.length; indexN++) {
var colO = arrayToCheck[indexN]; // read object on actual index
if (colO[idProperty] == (prefix + newId)) { // compare ID property
indexN = -1; // reset loop
natNumber++; // try next ID
newId = prefix + natNumber;
newId = f.createId(prefix, natNumber); // create new ID if f given
}
}
if(sap.scn.tlog) {
sap.scn.tlog.fret("nextFreeArrayId", newId);
sap.scn.tlog.fexit("nextFreeArrayId", arrayToCheck, prefix, idProperty);
}
return newId;
};
Input parameters
Record function entry
Record function exit
Function code
How it works?
Input:
arrayToCheck
[0].id = ELEMENT_1
[1].id = ELEMENT_2
[2].id = ELEMENT_4
prefix
“ELEMENT_”
idProperty
“id”
Output:
“ELEMENT_3”
Input functions (modifiers)
If (f & f.createId)
If (f & f.createId)
Demo
© 2017 SAP SE or an SAP affiliate company. All rights reserved.
Thank you
Contact information:
Karol Kalisz
Project Expert - Development
Digital Supply Chain & IoT
Your Take Aways
Upgrade your Knowledge on Functional
Programming
Google “Functional Programming”
Check other self-learning options
Upgrade your Knowledge on Microservices
Google “Microservices”
Try it out
Start (or Continue) improving your code with
corresponding tests
Better code -> less troubles
Less troubles -> better cloud solutions!
Check SAP community events and learning
Google “SAP Inside Track”
Try out any Open SAP courses

More Related Content

Recently uploaded

📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱
📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱
📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱
@Chandigarh #call #Girls 9053900678 @Call #Girls in @Punjab 9053900678
 
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
nilamkumrai
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
 
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
nirzagarg
 
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
 
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls DubaiDubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
 
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
 
📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱
📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱
📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
 
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
 
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
 
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
 
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirt
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
 
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
 
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
 
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
 
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
 
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn
 

Featured (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

Sithh writing testable code

  • 1. Karol Kalisz, Digital Supply Chain & Internet of Things Writing Testable Code Inspired by Functional Programming and Microservice Rules INSPIRE
  • 2. © 2017 SAP SE or an SAP affiliate company. All rights reserved. 2Internal What you will learn today? You will learn how to create better code with concepts from functional programing and microservices. You will be inspired to be a better developer.
  • 3. © 2017 SAP SE or an SAP affiliate company. All rights reserved. 3Internal Concepts of functional programing that are valuable for testing Pure functions Purely functional functions (or expressions) have no side effects. – Pure functions have several useful properties, many of which can be used to optimize the code. First-class and higher-order functions Higher-order functions can take other functions as arguments or return them as results – first-class functions. Strict and non-strict evaluation Use of strict (eager) or non-strict (lazy) evaluation – concepts that refer to how function arguments are processed when an expression is being evaluated. Strong recursion Iteration (looping) in functional languages is usually accomplished through recursion. – Recursive functions invoke themselves, allowing an operation to be performed over and over until the base case is reached. https://en.wikipedia.org/wiki/Functional_programming
  • 4. © 2017 SAP SE or an SAP affiliate company. All rights reserved. 4Internal Concepts of functional programing that are valuable for testing Pure functions Purely functional functions (or expressions) have no side effects. – Pure functions have several useful properties, many of which can be used to optimize the code. First-class and higher-order functions Higher-order functions can take other functions as arguments or return them as results – first-class functions. Strict and non-strict evaluation Use of strict (eager) or non-strict (lazy) evaluation – concepts that refer to how function arguments are processed when an expression is being evaluated. Strong recursion Iteration (looping) in functional languages is usually accomplished through recursion. – Recursive functions invoke themselves, allowing an operation to be performed over and over until the base case is reached. https://en.wikipedia.org/wiki/Functional_programming No side effects Function as argument Better performance Less code
  • 5. © 2017 SAP SE or an SAP affiliate company. All rights reserved. 5Internal Concepts of microservices that are important for testing Properties The services are easy to replace: – Services are organized around capabilities, such as the user-interface front-end, recommendation, logistics, and billing. – [Services can be implemented using different programming languages, databases, hardware, and software environment, depending on what fits best.] Architecture Services should be small and the protocols should lightweight. This approach: – Makes it easier to change and add functions and qualities to the system at any time – Allows the architecture of an individual service to emerge through continuous refactoring and therefore reduces the need for a large up-front design https://en.wikipedia.org/wiki/Microservices
  • 6. © 2017 SAP SE or an SAP affiliate company. All rights reserved. 6Internal Concepts of microservices that are important for testing Properties The services are easy to replace: – Services are organized around capabilities, such as the user-interface front-end, recommendation, logistics, and billing. – [Services can be implemented using different programming languages, databases, hardware, and software environment, depending on what fits best.] Architecture Services should be small and the protocols should lightweight. This approach: – Makes it easier to change and add functions and qualities to the system at any time – Allows the architecture of an individual service to emerge through continuous refactoring and therefore reduces the need for a large up-front design https://en.wikipedia.org/wiki/Microservices Structure Flexibility Testability Maintainability
  • 7. © 2017 SAP SE or an SAP affiliate company. All rights reserved. 7Internal Using language specialties you can improve the way you code Example based on JavaScript What is challenging in JavaScript? Parameters are untyped. JavaScript Object Notation (JSON) is great as a data model: – Good application programming interface (API) for accessing JSON objects, serialization, deserialization, and cloning – Easy programmatic access to any JSON object properties – Easy binding to UI elements (such as SAPUI5) A lot of Open Source is available for use: – No compilation step is present; you cannot control any APIs from other modules. Check your own programming language – you will find similar values.
  • 8. © 2017 SAP SE or an SAP affiliate company. All rights reserved. 8Internal Using language specialties you can improve the way you code Example based on JavaScript What is challenging in JavaScript? Parameters are untyped. JavaScript Object Notation (JSON) is great as a data model: – Good application programming interface (API) for accessing JSON objects, serialization, deserialization, and cloning – Easy programmatic access to any JSON object properties – Easy binding to UI elements (such as SAPUI5) A lot of Open Source is available for use: – No compilation step is present; you cannot control any APIs from other modules. Check your own programming language – you will find similar values. First-class functions Clear, serializable data model Need for better tests
  • 9. Let’s bring it together Functional programming Microservices JavaScript
  • 10. © 2017 SAP SE or an SAP affiliate company. All rights reserved. 10Internal Five modelling rules to create better code 5 modelling rules - you can consider - 1Writeallfunctionslikemicroservices Inputparametersareeitherprimitiveor assuredtobeserializable. Functionscanbeexecutedstateless. Outputcanbeserializedaswell.
  • 11. © 2017 SAP SE or an SAP affiliate company. All rights reserved. 11Internal Five modelling rules to create better code Write all functions like microservices Input parameters are either primitive or assured to be serializable Functions can be executed stateless Output can be serialized as well 2Writeallfunctionslikepurefunctions Nocallstootherfunctionsinside Allrequiredparametersandmethodspassedasinput parameters 1
  • 12. © 2017 SAP SE or an SAP affiliate company. All rights reserved. 12Internal Five modelling rules to create better code Write all functions like pure functions No calls to other functions inside All required parameters and methods passed as input parameters 1Writeallfunctionslikemicroservices Inputparametersareeitherprimitiveor assuredtobeserializable. Functionscanbeexecutedstateless. Outputcanbeserializedaswell. 3Abstractrealobjectsassoonaspossible Inuserinterface(UI)events,createanindependentdata modelthatwillbeusedforthefunctionalprocessing. Allparameterspassedtofunctionsmustbeserializable intostring. 2
  • 13. © 2017 SAP SE or an SAP affiliate company. All rights reserved. 13Internal Five modelling rules to create better code Abstract real objects as soon as possible In user interface (UI) events, create an independent data model that will be used for the functional processing. All parameters passed to functions must be serializable into string. 2Writeallfunctionslikepurefunctions Nocallstootherfunctionsinside Allrequiredparametersandmethodspassedasinput parameters 4UseJSONasdatamodel Canbeserializedanytime CanbeusedforbindingtoSAPUI5elements IseasytoaccessnativelyinJavaScriptcode 3
  • 14. © 2017 SAP SE or an SAP affiliate company. All rights reserved. 14Internal Five modelling rules to create better code Use JSON as data model Can be serialized any time Can be used for binding to SAPUI5 elements Is easy to access natively in JavaScript code 3Abstractrealobjectsassoonaspossible Inuserinterface(UI)events,createanindependentdata modelthatwillbeusedforthefunctionalprocessing. Allparameterspassedtofunctionsmustbeserializable intostring. 5Createlibrarieswithstatelesslogic Ensuresnoaccesstoobjectsandotherfunctions Meanseasiertestingoffunctions Noneedtomockcomplexobjects 4
  • 15. © 2017 SAP SE or an SAP affiliate company. All rights reserved. 15Internal Five modelling rules to create better code Create libraries with stateless logic Ensures no access to objects and other functions Means easier testing of functions No need to mock complex objects 4UseJSONasdatamodel Canbeserializedanytime CanbeusedforbindingtoSAPUI5elements IseasytoaccessnativelyinJavaScriptcode Usingtherulesallfunctionswillbereproducible (idempotent) Sameinputparametersalwaysthesameoutput. Betterandstabletestsareeasiertocreate. 5
  • 16. © 2017 SAP SE or an SAP affiliate company. All rights reserved. 16Internal Five modelling rules to create better code Using the rules all functions will be reproducible (idempotent) Same input parameters always the same output. Better and stable tests are easier to create Caching for better performance is easier 5Createlibrarieswithstatelesslogic Ensuresnoaccesstoobjectsandotherfunctions Meanseasiertestingoffunctions Noneedtomockcomplexobjects 1.2.3.4.5…
  • 17. Let’s see the code One function applied rules additional test code JavaScript
  • 18. © 2017 SAP SE or an SAP affiliate company. All rights reserved. 19Internal Let’s be technical – if you follow the rules, you can serialize your tests with automation /* find a free id for an array using given prefix */ sap.scn.nextFreeArrayId = function (arrayToCheck, prefix, idProperty, f) { if(sap.scn.tlog) { sap.scn.tlog.fentry("nextFreeArrayId", arrayToCheck, prefix, idProperty); } var natNumber = 1; // start with ID 1 var newId = prefix + natNumber; newId = f.createId(prefix, natNumber); // create new ID if f given var indexN = 0; // loop on given array for (indexN; indexN < arrayToCheck.length; indexN++) { var colO = arrayToCheck[indexN]; // read object on actual index if (colO[idProperty] == (prefix + newId)) { // compare ID property indexN = -1; // reset loop natNumber++; // try next ID newId = prefix + natNumber; newId = f.createId(prefix, natNumber); // create new ID if f given } } if(sap.scn.tlog) { sap.scn.tlog.fret("nextFreeArrayId", newId); sap.scn.tlog.fexit("nextFreeArrayId", arrayToCheck, prefix, idProperty); } return newId; }; Input parameters Record function entry Record function exit Function code How it works? Input: arrayToCheck [0].id = ELEMENT_1 [1].id = ELEMENT_2 [2].id = ELEMENT_4 prefix “ELEMENT_” idProperty “id” Output: “ELEMENT_3” Input functions (modifiers) If (f & f.createId) If (f & f.createId)
  • 19. Demo
  • 20. © 2017 SAP SE or an SAP affiliate company. All rights reserved. Thank you Contact information: Karol Kalisz Project Expert - Development Digital Supply Chain & IoT Your Take Aways Upgrade your Knowledge on Functional Programming Google “Functional Programming” Check other self-learning options Upgrade your Knowledge on Microservices Google “Microservices” Try it out Start (or Continue) improving your code with corresponding tests Better code -> less troubles Less troubles -> better cloud solutions! Check SAP community events and learning Google “SAP Inside Track” Try out any Open SAP courses