Modern Programming
Languages - Overview
Ayman Mahfouz
July 2018
With Kotlin as an Example
Biography
- VP of Engineering at Webalo (webalo.com)
- Los Angeles based
- Branch in Alexandria
- UX4IIoT
- 20+ years of software development
- PhD. in software engineering
- BSc. Computer Science - Alexandria 1997
Programming Languages
Pascal C C++
Java
Python C# Objective-C ActionScript
Scala Ruby Octave
JavaScript
Go TypeScript Kotlin
BASIC
Swift Groovy
Why Many Languages Exist?
Optimized for different objectives
● Productivity
● Readability
● Portability
● Performance
○ Compile-time
○ Runtime
Some Notable Languages
Released By Notes
Groovy 2007 Pivotal Concise, easy-to learn, Java-interoperable, scripting
language for the JVM.
Go 2009 Google High-speed compilation compared to C , built-in
support for concurrency.
Swift 2014 Apple Many safety and syntax improvements to replace
Objective-C.
Kotlin 2016 JetBrains Designed to be “better than Java” - Interoperable for
incremental migration.
Kotlin Arrives!
● Paid little attention until Google I/O announcement.
● Android Studio Canary 3.
● Immediately fell in love
○ Strong Typing
○ Brevity
○ Expressiveness
○ Interoperability
○ Growing support
● Starts from where others ended (Java, Python, Go, Swift)
● One app built on github.
● Still learning.
Brevity
Null Safety
Conditionals
Smart Cast
Lambdas
Collections
Features we will go through:
Brevity
In Java, one would normally write this:
With my background, I translated this into Kotlin verbatim:
Not idiomatic but still
shorter! Can you spot
the omissions?
● No ;
● No “new”
● “override” is a modifier
● “:” not “implements”
● “public” is default
● Return type
In Java one would typically write code like this:
Brevity
Using “Single Expression Function” syntax
Pass a lambda expression to a function that accepts a single-method interface
Brevity
Omit parameter type
Omit parameter name as well
Eliminate left hand side completely
Lambda expression only argument so we can eliminate the parenthesis
Omit parameter type
Brevity - From Java to Kotlin
to Kotlin ...
From Java ...
We will go through:
Brevity
Null Safety
Conditionals
Smart Cast
Lambdas
Collections
Null Safety - Explicit handling
The dreaded NPE
Handle explicitly
Null Safety - Java Optionals
Handle via Java 8 Optionals - very verbose!
Handle by convention:
- Special member naming (IfAny)
- Null checks in constructor
- Hand-coded
- Annotations
- Aspects
Or let it throw! (pun intended)
Null Safety - Safe Navigation
Kotlin safe navigation operator
Compile-time language-enforced null safety
Forced init
Force null guard
Allowing late init
Conscious NPE
We will go through:
Brevity
Null Safety
Conditionals
Smart Cast
Lambdas
Collections
Conditionals
I cringe when I see if statements that look like this
Use the ternary operator please!
Conditionals - Expressions
In Kotlin, if statement is an expression
Combined with single expression function shorthand
So you can write a function like this
Conditionals - when
I hate “Switch” statements!
But I love “when” expressions!
1. No annoying “break” or even “case”
2. Match against any type
3. Switch variable can be of any type
4. The “else” section is mandatory
We will go through:
Brevity
Null Safety
Conditionals
Smart Cast
Lambdas
Collections
Smart Cast
In Java explicit cast is needed
In Kotlin Smart Cast takes place automatically
Smart Cast
In Java
Smart Cast
In Kotlin
Smart Cast - Nullables
Automatic cast to non-null
We will go through:
Brevity
Null Safety
Conditionals
Smart Cast
Lambdas
Collections
Lambdas
Think of it as a “function literal”
The function “Array” produces an array by applying a lambda
Lambdas - Higher-order Functions
Higher-order functions: functions that take other functions as arguments
Lambdas - Closures
Closures: lambdas can access variables in their outer scope
We will go through:
Brevity
Null Safety
Conditionals
Smart Cast
Lambdas
Collections
Collections
With Lambdas
it
Collections - Streaming
Chaining lambda operations
Collections - Immutability
In Java both mutable and immutable collections implement the same interface
Kotlin uses different interfaces - Compile error
What about the others?
● Brevity
○ No semicolons
○ Optional “return”
○ Variable types
○ Omit parens in a call
○ Default imports
● Null Safety
● Closures
● Most of other Kotlin features
● And more
○ Parameterized Strings
Groovy
def list = [1, 2, 3]
list.each { println it }
person?.address.?city
def name = ‘World’
println “Hello $name!”
Swift
● Brevity
○ No semicolons
○ Implicit variable types
● Null Safety
● Closures
● Ranges
● Switch
○ Any type
○ Not just equality
○ No fallthrough
● Other
○ Parameterized Strings
○ Dynamic Extension
for i in 0..3 { println(“i”) }
extension Double {
c2f -> Double() {
return self * 9 / 5 + 32
}
}
…
let c = 100.0
print(c.c2f())
result, error := callSomeFunction()
Go
● Brevity
○ No semicolons
○ Implicit variable types
○ (But must use braces sometimes)
● Null Safety
● Switch
○ Non-constants
○ (But same type)
● Closures
● Multiple return values
● Missing some features
○ e.g. Inheritance
if i % 2 == 0 {
// even
} else {
// odd
}
Comparison Matrix
JVM GC Classes No ; Type
Inference
Safe
Null
If Expr Rich
Switch
Closure Variable
Arguments
Collection
Streaming
Dynamic
Extension
Tuples
Kotlin
✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓
Groovy
✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓
Swift
✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓
Go
✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓
Thank You!
Ayman Mahfouz
amahfouz@gmail.com
www.linkedin.com/in/amahfouz/
amahfouz.wordpress.com
github.com/amahfouz
amahfouz.com
AymanMahfouz.com

Modern Programming Languages - An overview

  • 1.
    Modern Programming Languages -Overview Ayman Mahfouz July 2018 With Kotlin as an Example
  • 2.
    Biography - VP ofEngineering at Webalo (webalo.com) - Los Angeles based - Branch in Alexandria - UX4IIoT - 20+ years of software development - PhD. in software engineering - BSc. Computer Science - Alexandria 1997
  • 3.
    Programming Languages Pascal CC++ Java Python C# Objective-C ActionScript Scala Ruby Octave JavaScript Go TypeScript Kotlin BASIC Swift Groovy
  • 4.
    Why Many LanguagesExist? Optimized for different objectives ● Productivity ● Readability ● Portability ● Performance ○ Compile-time ○ Runtime
  • 5.
    Some Notable Languages ReleasedBy Notes Groovy 2007 Pivotal Concise, easy-to learn, Java-interoperable, scripting language for the JVM. Go 2009 Google High-speed compilation compared to C , built-in support for concurrency. Swift 2014 Apple Many safety and syntax improvements to replace Objective-C. Kotlin 2016 JetBrains Designed to be “better than Java” - Interoperable for incremental migration.
  • 6.
    Kotlin Arrives! ● Paidlittle attention until Google I/O announcement. ● Android Studio Canary 3. ● Immediately fell in love ○ Strong Typing ○ Brevity ○ Expressiveness ○ Interoperability ○ Growing support ● Starts from where others ended (Java, Python, Go, Swift) ● One app built on github. ● Still learning.
  • 7.
  • 8.
    Brevity In Java, onewould normally write this: With my background, I translated this into Kotlin verbatim: Not idiomatic but still shorter! Can you spot the omissions? ● No ; ● No “new” ● “override” is a modifier ● “:” not “implements” ● “public” is default ● Return type In Java one would typically write code like this:
  • 9.
    Brevity Using “Single ExpressionFunction” syntax Pass a lambda expression to a function that accepts a single-method interface
  • 10.
    Brevity Omit parameter type Omitparameter name as well Eliminate left hand side completely Lambda expression only argument so we can eliminate the parenthesis Omit parameter type
  • 11.
    Brevity - FromJava to Kotlin to Kotlin ... From Java ...
  • 12.
    We will gothrough: Brevity Null Safety Conditionals Smart Cast Lambdas Collections
  • 13.
    Null Safety -Explicit handling The dreaded NPE Handle explicitly
  • 14.
    Null Safety -Java Optionals Handle via Java 8 Optionals - very verbose! Handle by convention: - Special member naming (IfAny) - Null checks in constructor - Hand-coded - Annotations - Aspects Or let it throw! (pun intended)
  • 15.
    Null Safety -Safe Navigation Kotlin safe navigation operator Compile-time language-enforced null safety Forced init Force null guard Allowing late init Conscious NPE
  • 16.
    We will gothrough: Brevity Null Safety Conditionals Smart Cast Lambdas Collections
  • 17.
    Conditionals I cringe whenI see if statements that look like this Use the ternary operator please!
  • 18.
    Conditionals - Expressions InKotlin, if statement is an expression Combined with single expression function shorthand So you can write a function like this
  • 19.
    Conditionals - when Ihate “Switch” statements! But I love “when” expressions! 1. No annoying “break” or even “case” 2. Match against any type 3. Switch variable can be of any type 4. The “else” section is mandatory
  • 20.
    We will gothrough: Brevity Null Safety Conditionals Smart Cast Lambdas Collections
  • 21.
    Smart Cast In Javaexplicit cast is needed In Kotlin Smart Cast takes place automatically
  • 22.
  • 23.
  • 24.
    Smart Cast -Nullables Automatic cast to non-null
  • 25.
    We will gothrough: Brevity Null Safety Conditionals Smart Cast Lambdas Collections
  • 26.
    Lambdas Think of itas a “function literal” The function “Array” produces an array by applying a lambda
  • 27.
    Lambdas - Higher-orderFunctions Higher-order functions: functions that take other functions as arguments
  • 28.
    Lambdas - Closures Closures:lambdas can access variables in their outer scope
  • 29.
    We will gothrough: Brevity Null Safety Conditionals Smart Cast Lambdas Collections
  • 30.
  • 31.
  • 32.
    Collections - Immutability InJava both mutable and immutable collections implement the same interface Kotlin uses different interfaces - Compile error
  • 33.
  • 34.
    ● Brevity ○ Nosemicolons ○ Optional “return” ○ Variable types ○ Omit parens in a call ○ Default imports ● Null Safety ● Closures ● Most of other Kotlin features ● And more ○ Parameterized Strings Groovy def list = [1, 2, 3] list.each { println it } person?.address.?city def name = ‘World’ println “Hello $name!”
  • 35.
    Swift ● Brevity ○ Nosemicolons ○ Implicit variable types ● Null Safety ● Closures ● Ranges ● Switch ○ Any type ○ Not just equality ○ No fallthrough ● Other ○ Parameterized Strings ○ Dynamic Extension for i in 0..3 { println(“i”) } extension Double { c2f -> Double() { return self * 9 / 5 + 32 } } … let c = 100.0 print(c.c2f())
  • 36.
    result, error :=callSomeFunction() Go ● Brevity ○ No semicolons ○ Implicit variable types ○ (But must use braces sometimes) ● Null Safety ● Switch ○ Non-constants ○ (But same type) ● Closures ● Multiple return values ● Missing some features ○ e.g. Inheritance if i % 2 == 0 { // even } else { // odd }
  • 37.
    Comparison Matrix JVM GCClasses No ; Type Inference Safe Null If Expr Rich Switch Closure Variable Arguments Collection Streaming Dynamic Extension Tuples Kotlin ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ Groovy ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ Swift ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ Go ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓
  • 38.