SlideShare a Scribd company logo
Writing Simple, Readable and
Robust Code: Examples in
Java, Clojure, and Go
freiheit.com technologies - Hamburg, September 30, 2015
code.talks 2015
© freiheit.com 2
Hi! I am Stefan Richter. I started programming on an Apple ][ in
1983. Since 1998, I run my own software company, which is
specialized in large Internet systems.
WHO AM I?
Working as a professional
programmer
Running my own company
20102005199819951990198719831966
Apple II,
Pascal,
C64,
6502
VAX, Unix,
C,
4GLs,
Smalltalk,
Lisp,
Scheme
Unix, C,
4 GLs,
(Windows NT, VB (!))
Linux, Java, Python, Ruby,
Objective-C, C/C++, Common Lisp, Clojure,
JavaScript, Go
...
Future
University studies and work as a
programmer in research institutes
SO LET’S GET STARTED
AND HAVE SOME FUN! :)
According to Wikipedia a domain-
specific language (DSL) is a
“... programming language or
specification language dedicated to
a particular problem domain [...].“
© freiheit.com 4
I guess, we find DSLs so compelling
because sooner or later every
programmer dreams of creating
his/her own programming language. ;)
© freiheit.com 5
Let’s take an example from a person
you all know: Martin Fowler.
In 2005, he posted an article on his
homepage about domain-specific
languages ...
© freiheit.com 6
© freiheit.com 7
Fowler creates a DSL to read files in a fixed-length
format and to convert each line into an object. This is
the file format and the mapping spec:
8© freiheit.com
He uses C#, but as C# is more or less
a Java clone you will agree that the
Java code would look almost exactly
the same.
© freiheit.com 9
Line by line, he reads the file and dispatches the
conversion to a strategy class.
10© freiheit.com
The strategy class is parameterized with the type
code and the target class ...
11© freiheit.com
The individual fields in each line are read by a
FieldExtractor class ...
12© freiheit.com
To process the line, the strategy creates the target
class and uses the extractors to get the field data ...
13© freiheit.com
The FieldExtractor pulls out the data and sets the
correct field in the target class by using reflection ...
14© freiheit.com
Now you have an API that can be configured at
compile time ...
15© freiheit.com
If you would like to do this in a declarative style, you
need an XML configuration ... (arrggh)
16© freiheit.com
Fowler’s example shows about 70 lines
of code (LOC). But the code is not
complete. I guess, the complete example
would be around 200 LOC Java.
© freiheit.com 17
HMM. SO MUCH CODE
FOR A SIMPLE PROBLEM.
Ideally, you would like to have something like this
as a mini DSL:
19© freiheit.com
The Common Lisp Community picked
up Fowler’s article, because he
mentioned Lisp favorably.
© freiheit.com 20
Rainer Joswig then proposed this DSL in comp.
lang.lisp:
21© freiheit.com
His solution is a 12 LOC Lisp macro …,
22© freiheit.com
... which expands to this code:
Let’s examine how macros work. We’ll
use the Common Lisp code as an
example. It’s very close to how macros
look like in Clojure! I’ll show you later! :)
© freiheit.com 23
• Basically, a Lisp macro is a code generator. But you can’t compare Lisp macros with C macros or any other
macro or template system.
• Lisp programs are trees of expressions. Code is data. Data is code.
• This is why a Lisp program can modify itself.
• In a nutshell - “How to read Lisp macros“:
• Macros don’t evaluate their parameters.
• The backtick (`) says: „Don’t evaluate the following code.“
• The comma (,) says: „Evaluate the following code and replace it with the result.“
• Then: Evaluate all resulting code together.
• Read Peter Seibel’s „Practical Common Lisp“ to learn more ...
24© freiheit.com
• defclass creates a new CLOS class. In CLOS methods are defined independently of the class definition, but
getter/setter can be defined implicitly.
• Methods are specialized on their parameters. So each method can be specialized to more than one class! Think about
this for a moment ...
• You can even specialize a method depending on what exact value is passed as a parameter. So you can have special
methods for specific parameter values. In this example, parse-line-for-class is called, when you pass a class with the
name “service-call“. (Which in turn is generated by this macro, too).
• This method then takes the class, instantiates it and sets (side effect) its member variables by taking the format
specification and using a substring function to extract the right bits and pieces from the input line. (This is not pure
functional style, but this is how you use Common Lisp with CLOS.)
25© freiheit.com
To read from a file and apply the mappings
Rainer Joswig then wrote this code:
26© freiheit.com
Obviously, the Common Lisp version
works almost the same as
Martin Fowler’s C# version, with much
less code. Rainer Joswig’s solution is
functionally complete.
Let’s do this in Clojure now!
© freiheit.com 27
One thing: Some people in the Clojure
community say that you should not use
macros. You will soon see why.
But I disagree! Macros are one of the
reasons why you should favor
Lisp-like languages over other languages.
© freiheit.com 28
NOW THAT YOU ARE
EXPERTS ON COMMON
LISP MACRO, I WILL SHOW
YOU THE FIRST CLOJURE
VERSION STRAIGHT AWAY!
30© freiheit.com
• We create a list of the field names because we need them two times in the code.
• defrecord creates a Clojure data structure with the look and feel of a hash table.
• But it is much more: Under the hood, this is a Java class! And even more!
• This is why you need a constructor function to create a “record“. I generate the name of the constructor function by
appending “.“ to the record name.
• I create a multimethod, which is specialized on the value of the mapping parameter. So Clojure “knows“, which method
to call based on the input ...
• Then, I create a list of all values from the input line (in the same order as their field names) and apply this list to the
constructor of the record, which is returned as the function value.
To make it complete: defmethod needs a defmulti
to provide a dispatching function.
31© freiheit.com
• Meaning: Take the first four characters of the value of the parameter “line“ (which should be a java.lang.
String) and then call the multimethod, which is specialized on this value.
• (Note: Of course, you have to handle the cases: What happens when you have an empty line, an
unknown mapping or a comment line. But I leave this out here to keep the example simple, even though
this is just five lines more including another defmethod, which handles all these :default cases).
32© freiheit.com
• In a nutshell “How to read Clojure macros“:
• The backtick (`) says: „Don‘t evaluate the following code.“
• The tilde (~) says: „Evaluate the following code and replace it with the result.“
• ~@ means: “Evaluate the following code and replace it with the result, but don’t put parentheses around the result.“
(This is called „splicing“.)
• The hash sign (#) at the end of a variable is used to automatically create unique variable names in the code created by
the macro. This is called auto-gensyms. This way you can prevent name clashes with call parameters.
Finally, this Clojure macro expands to the following
code. Can you see the automatically generated
variable names?
33© freiheit.com
Now we can define mappings as follows:
34© freiheit.com
Actually, the
leading 0 doesn’t
work here.
Leading zeros
are reserved for
octal numbers.
LET US BUILD
SOMETHING TO
CONVENIENTLY USE
THESE MAPPINGS!
This is a classic with-macro that you often find in
the Common Lisp world.
36© freiheit.com
• It reads the file identified by file name line by line.
• It calls our multimethod parse line for each line.
• It binds the result to a variable that you can hand over to the macro (binding).
• And only if parsing the line has a result (no empty line, no comment line or unknown binding), it executes the body,
meaning the code that you can hand over to this macro, which can contain the variable, which contains the binding.
• Then it returns a list of the results.
• Sounds difficult, but with the example on the next page you will find it to be very simple.
In this example, we just extract the customer name
from each line.
37© freiheit.com
• For each valid line in the file specified by file name (*testdata* contains the file name as a string) a Clojure record is
created.
• Each result is bound in this case to the symbol/variable “obj“.
• In the body code you can use this symbol/variable to access this Clojure record.
• In this case, we use it to extract the customer name from the record.
• with-mappings then returns a list of the results.
Isn’t this awesome?! We have just
defined two new programming
constructs and added them to our
programming language: defmapping,
with-mappings.
© freiheit.com 38
The complete example is about 30 LOC
of Clojure. You don’t need any special
tools (Language Workbench). It’s all
right there in the Clojure language.
And you just need Emacs to unleash
this power ...
© freiheit.com 39
Now I can extend this without breaking
the existing code.
© freiheit.com 40
Let’s add a serializer to create
JSON from the data ...
© freiheit.com 41
We define a protocol and add an implementation to
the record definition.
42© freiheit.com
Now we can convert the data to JSON.
43© freiheit.com
And we can even change the serialize method
„in place“ and on the fly.
© freiheit.com GOTO Conference Copenhagen, 2011
“LISP ISN’T A LANGUAGE,
IT’S A BUILDING
MATERIAL.“
- ALAN KAY
And Clojure gives you powerful
abstractions that you won’t find in
any other programming language.
Some stuff is only possible in a
Lisp-like language ... with all these
parenthesis. ;)
© freiheit.com 45
© freiheit.com GOTO Conference Copenhagen, 2011
IT IS COOL. I LOVE THIS.
BUT IS THIS REALLY
SIMPLE?
LET’S DO THIS IN GO!
© freiheit.com GOTO Conference Copenhagen, 2011
LIVE CODING
THANK YOU
freiheit.com technologies
Budapester Str. 45
20359 Hamburg, Germany
kontakt@freiheit.com
T +49 40 890584 0
F +49 40 890584 20
Hackers wanted.
Join us:
jobs@freiheit.com

More Related Content

What's hot (20)

DOC
Typescript Basics
Manikandan [M M K]
 
PPTX
TypeScript - Silver Bullet for the Full-stack Developers
Rutenis Turcinas
 
PPTX
Kubernetes Internals
Shimi Bandiel
 
PPT
LLVM
guest3e5046
 
PPTX
Should i Go there
Shimi Bandiel
 
PPTX
Neodev
Shimi Bandiel
 
PPTX
Introduction to TypeScript
KeithMurgic
 
PDF
TypeScript 2 in action
Alexander Rusakov
 
PPTX
Getting started with typescript
C...L, NESPRESSO, WAFAASSURANCE, SOFRECOM ORANGE
 
PPTX
Typescript
Nikhil Thomas
 
PDF
New c sharp4_features_part_iv
Nico Ludwig
 
PDF
TypeScript - An Introduction
NexThoughts Technologies
 
PDF
Clang: More than just a C/C++ Compiler
Samsung Open Source Group
 
ODP
Getting started with typescript and angular 2
Knoldus Inc.
 
PDF
Blocks & GCD
rsebbe
 
PDF
Typescript for the programmers who like javascript
Andrei Sebastian Cîmpean
 
PPTX
Type script - advanced usage and practices
Iwan van der Kleijn
 
PDF
Introduction to TypeScript by Winston Levi
Winston Levi
 
PDF
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Codemotion
 
PDF
TypeScript: coding JavaScript without the pain
Sander Mak (@Sander_Mak)
 
Typescript Basics
Manikandan [M M K]
 
TypeScript - Silver Bullet for the Full-stack Developers
Rutenis Turcinas
 
Kubernetes Internals
Shimi Bandiel
 
Should i Go there
Shimi Bandiel
 
Introduction to TypeScript
KeithMurgic
 
TypeScript 2 in action
Alexander Rusakov
 
Getting started with typescript
C...L, NESPRESSO, WAFAASSURANCE, SOFRECOM ORANGE
 
Typescript
Nikhil Thomas
 
New c sharp4_features_part_iv
Nico Ludwig
 
TypeScript - An Introduction
NexThoughts Technologies
 
Clang: More than just a C/C++ Compiler
Samsung Open Source Group
 
Getting started with typescript and angular 2
Knoldus Inc.
 
Blocks & GCD
rsebbe
 
Typescript for the programmers who like javascript
Andrei Sebastian Cîmpean
 
Type script - advanced usage and practices
Iwan van der Kleijn
 
Introduction to TypeScript by Winston Levi
Winston Levi
 
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Codemotion
 
TypeScript: coding JavaScript without the pain
Sander Mak (@Sander_Mak)
 

Viewers also liked (20)

PPT
01 Java Is Architecture Neutral
rajuglobal
 
DOCX
Best practices to optimize code and build robust and scalable web applications
dheerajpiet
 
PPTX
High Performance Data Analytics with Java on Large Multicore HPC Clusters
Saliya Ekanayake
 
PDF
Clojure Macros Workshop: LambdaJam 2013 / CUFP 2013
Leonardo Borges
 
PDF
A Dive Into Clojure
Carlo Sciolla
 
PDF
不自然なcar/ナチュラルにconsして
mitsutaka mimura
 
ODP
Clojure: Practical functional approach on JVM
sunng87
 
PDF
Patterns
David Nolen
 
PPT
A little exercise with clojure macro
Zehua Liu
 
PDF
Writing Macros
RueiCi Wang
 
PDF
Macros in Clojure
sohta
 
PDF
入門ClojureScript
sohta
 
PPTX
Ecom trend
Huy Dang
 
PDF
Continuation Passing Style and Macros in Clojure - Jan 2012
Leonardo Borges
 
PPTX
Clojure的魅力
dennis zhuang
 
PPTX
Clojure概览
dennis zhuang
 
PDF
Niels Leenheer - Weird browsers - code.talks 2015
AboutYouGmbH
 
PDF
Clojureシンタックスハイライター開発から考えるこれからのlispに必要なもの
sohta
 
PPTX
Introduction to JAVA
javatrainingonline
 
PPSX
Core java lessons
vivek shah
 
01 Java Is Architecture Neutral
rajuglobal
 
Best practices to optimize code and build robust and scalable web applications
dheerajpiet
 
High Performance Data Analytics with Java on Large Multicore HPC Clusters
Saliya Ekanayake
 
Clojure Macros Workshop: LambdaJam 2013 / CUFP 2013
Leonardo Borges
 
A Dive Into Clojure
Carlo Sciolla
 
不自然なcar/ナチュラルにconsして
mitsutaka mimura
 
Clojure: Practical functional approach on JVM
sunng87
 
Patterns
David Nolen
 
A little exercise with clojure macro
Zehua Liu
 
Writing Macros
RueiCi Wang
 
Macros in Clojure
sohta
 
入門ClojureScript
sohta
 
Ecom trend
Huy Dang
 
Continuation Passing Style and Macros in Clojure - Jan 2012
Leonardo Borges
 
Clojure的魅力
dennis zhuang
 
Clojure概览
dennis zhuang
 
Niels Leenheer - Weird browsers - code.talks 2015
AboutYouGmbH
 
Clojureシンタックスハイライター開発から考えるこれからのlispに必要なもの
sohta
 
Introduction to JAVA
javatrainingonline
 
Core java lessons
vivek shah
 
Ad

Similar to Stefan Richter - Writing simple, readable and robust code: Examples in Java, Clojure, and Go - code.talks 2015 (20)

KEY
Exciting JavaScript - Part II
Eugene Lazutkin
 
PPTX
What`s New in Java 8
Mohsen Zainalpour
 
PDF
JUG Münster 2014 - Code Recommenders & Codetrails - Wissenstransfer 2.0
Marcel Bruch
 
PDF
Core JavaScript
Lilia Sfaxi
 
PDF
Javascript pdf for beginners easy levell
SakshamGupta957136
 
PPTX
Introduction Of C++
Sangharsh agarwal
 
PPTX
introduction to server-side scripting
Amirul Shafeeq
 
PDF
Getting started with CATIA V5 Macros
Emmett Ross
 
PDF
Programming in Java: Getting Started
Martin Chapman
 
PPTX
JAVASCRIPT PPT [Autosaved].pptx
AchieversIT
 
PPTX
Full Stack Online Course in Marathahalli| AchieversIT
AchieversIT
 
PDF
The Ring programming language version 1.5.2 book - Part 176 of 181
Mahmoud Samir Fayed
 
PDF
New c sharp4_features_part_vi
Nico Ludwig
 
PDF
The Ring programming language version 1.5.4 book - Part 180 of 185
Mahmoud Samir Fayed
 
PPT
Jacarashed-1746968053-300050282-Java.ppt
DilipDas70
 
PPTX
Looping and switch cases
MeoRamos
 
KEY
In-depth look at the Flex compiler and HFCD
Stop Coding
 
PDF
JavaScript Miller Columns
Jonathan Fine
 
PDF
64-bit Loki
PVS-Studio
 
Exciting JavaScript - Part II
Eugene Lazutkin
 
What`s New in Java 8
Mohsen Zainalpour
 
JUG Münster 2014 - Code Recommenders & Codetrails - Wissenstransfer 2.0
Marcel Bruch
 
Core JavaScript
Lilia Sfaxi
 
Javascript pdf for beginners easy levell
SakshamGupta957136
 
Introduction Of C++
Sangharsh agarwal
 
introduction to server-side scripting
Amirul Shafeeq
 
Getting started with CATIA V5 Macros
Emmett Ross
 
Programming in Java: Getting Started
Martin Chapman
 
JAVASCRIPT PPT [Autosaved].pptx
AchieversIT
 
Full Stack Online Course in Marathahalli| AchieversIT
AchieversIT
 
The Ring programming language version 1.5.2 book - Part 176 of 181
Mahmoud Samir Fayed
 
New c sharp4_features_part_vi
Nico Ludwig
 
The Ring programming language version 1.5.4 book - Part 180 of 185
Mahmoud Samir Fayed
 
Jacarashed-1746968053-300050282-Java.ppt
DilipDas70
 
Looping and switch cases
MeoRamos
 
In-depth look at the Flex compiler and HFCD
Stop Coding
 
JavaScript Miller Columns
Jonathan Fine
 
64-bit Loki
PVS-Studio
 
Ad

More from AboutYouGmbH (20)

PDF
Tech talk 01.06.2017
AboutYouGmbH
 
PDF
Retention Strategies in Mobile E-Commerce
AboutYouGmbH
 
PDF
Rethinking Fashion E-Commerce
AboutYouGmbH
 
PDF
ABOUT YOU get on board
AboutYouGmbH
 
PDF
Lars Jankowfsky - Learn or Die - code.talks 2015
AboutYouGmbH
 
PDF
Dr. Jeremias Rößler - Wenn Affen Testen - Das Ende der Bananensoftware - code...
AboutYouGmbH
 
PDF
Zeljko Kvesic - Scrum in verteilten Teams / Agil über die Landesgrenzen - cod...
AboutYouGmbH
 
PDF
Uwe Friedrichsen - CRDT und mehr - über extreme Verfügbarkeit und selbstheile...
AboutYouGmbH
 
PDF
Kai Voigt - Big Data zum Anfassen - code.talks 2015
AboutYouGmbH
 
PDF
Dr. Andreas Lattner - Aufsetzen skalierbarer Prognose- und Analysedienste mit...
AboutYouGmbH
 
PDF
Marcel Hild - Spryker (e)commerce framework als Alternative zu traditioneller...
AboutYouGmbH
 
PDF
Wolfram Kriesing - EcmaScript6 for real - code.talks 2015
AboutYouGmbH
 
PDF
Stefanie Grewenig & Johannes Thönes - Internet ausdrucken mit JavaScript - c...
AboutYouGmbH
 
PDF
Alex Korotkikh - From 0 to N: Lessons Learned - code.talks 2015
AboutYouGmbH
 
PDF
Christian Haider & Helge Nowak - Mehr Demokratie durch Haushaltstransparenz ...
AboutYouGmbH
 
PDF
Bernhard Wick - appserver.io - code.talks 2015
AboutYouGmbH
 
PDF
Moritz Siuts & Robert von Massow - Data Pipeline mit Apache Kafka - code.tal...
AboutYouGmbH
 
PDF
Carina Bittihn & Linda Dettmann - Same Same but Different - code.talks 2015
AboutYouGmbH
 
PDF
Dr. Florian Krause - Der Kunde im Fokus: Personalisierte Aussteuerung von Inh...
AboutYouGmbH
 
PDF
Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t...
AboutYouGmbH
 
Tech talk 01.06.2017
AboutYouGmbH
 
Retention Strategies in Mobile E-Commerce
AboutYouGmbH
 
Rethinking Fashion E-Commerce
AboutYouGmbH
 
ABOUT YOU get on board
AboutYouGmbH
 
Lars Jankowfsky - Learn or Die - code.talks 2015
AboutYouGmbH
 
Dr. Jeremias Rößler - Wenn Affen Testen - Das Ende der Bananensoftware - code...
AboutYouGmbH
 
Zeljko Kvesic - Scrum in verteilten Teams / Agil über die Landesgrenzen - cod...
AboutYouGmbH
 
Uwe Friedrichsen - CRDT und mehr - über extreme Verfügbarkeit und selbstheile...
AboutYouGmbH
 
Kai Voigt - Big Data zum Anfassen - code.talks 2015
AboutYouGmbH
 
Dr. Andreas Lattner - Aufsetzen skalierbarer Prognose- und Analysedienste mit...
AboutYouGmbH
 
Marcel Hild - Spryker (e)commerce framework als Alternative zu traditioneller...
AboutYouGmbH
 
Wolfram Kriesing - EcmaScript6 for real - code.talks 2015
AboutYouGmbH
 
Stefanie Grewenig & Johannes Thönes - Internet ausdrucken mit JavaScript - c...
AboutYouGmbH
 
Alex Korotkikh - From 0 to N: Lessons Learned - code.talks 2015
AboutYouGmbH
 
Christian Haider & Helge Nowak - Mehr Demokratie durch Haushaltstransparenz ...
AboutYouGmbH
 
Bernhard Wick - appserver.io - code.talks 2015
AboutYouGmbH
 
Moritz Siuts & Robert von Massow - Data Pipeline mit Apache Kafka - code.tal...
AboutYouGmbH
 
Carina Bittihn & Linda Dettmann - Same Same but Different - code.talks 2015
AboutYouGmbH
 
Dr. Florian Krause - Der Kunde im Fokus: Personalisierte Aussteuerung von Inh...
AboutYouGmbH
 
Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t...
AboutYouGmbH
 

Recently uploaded (20)

PPTX
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
PDF
FutureCon Seattle 2025 Presentation Slides - You Had One Job
Suzanne Aldrich
 
PDF
Empowering Local Language Email with IDN & EAI – Powered by XgenPlus
XgenPlus Technologies
 
PDF
The Internet - By the numbers, presented at npNOG 11
APNIC
 
PDF
Strategic Plan New and Completed Templeted
alvi932317
 
PPTX
PHIPA-Compliant Web Hosting in Toronto: What Healthcare Providers Must Know
steve198109
 
PDF
AI security AI security AI security AI security
elite44
 
PPTX
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
PDF
Boardroom AI: The Next 10 Moves | Cerebraix Talent Tech
ssuser73bdb11
 
DOCX
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
PDF
google promotion services in Delhi, India
Digital Web Future
 
PDF
BRKAPP-1102 - Proactive Network and Application Monitoring.pdf
fcesargonca
 
PPTX
原版一样(LHU毕业证书)英国利物浦希望大学毕业证办理方法
Taqyea
 
PDF
Cleaning up your RPKI invalids, presented at PacNOG 35
APNIC
 
PPTX
原版一样(毕业证书)法国蒙彼利埃大学毕业证文凭复刻
Taqyea
 
PPTX
美国电子毕业证帕克大学电子版成绩单UMCP学费发票办理学历认证
Taqyea
 
PDF
The Hidden Benefits of Outsourcing IT Hardware Procurement for Small Businesses
Carley Cramer
 
PPTX
Meloniusk_Communication_Template_best.pptx
howesix147
 
PPTX
Networking_Essentials_version_3.0_-_Module_5.pptx
ryan622010
 
PPTX
Networking_Essentials_version_3.0_-_Module_3.pptx
ryan622010
 
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
FutureCon Seattle 2025 Presentation Slides - You Had One Job
Suzanne Aldrich
 
Empowering Local Language Email with IDN & EAI – Powered by XgenPlus
XgenPlus Technologies
 
The Internet - By the numbers, presented at npNOG 11
APNIC
 
Strategic Plan New and Completed Templeted
alvi932317
 
PHIPA-Compliant Web Hosting in Toronto: What Healthcare Providers Must Know
steve198109
 
AI security AI security AI security AI security
elite44
 
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
Boardroom AI: The Next 10 Moves | Cerebraix Talent Tech
ssuser73bdb11
 
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
google promotion services in Delhi, India
Digital Web Future
 
BRKAPP-1102 - Proactive Network and Application Monitoring.pdf
fcesargonca
 
原版一样(LHU毕业证书)英国利物浦希望大学毕业证办理方法
Taqyea
 
Cleaning up your RPKI invalids, presented at PacNOG 35
APNIC
 
原版一样(毕业证书)法国蒙彼利埃大学毕业证文凭复刻
Taqyea
 
美国电子毕业证帕克大学电子版成绩单UMCP学费发票办理学历认证
Taqyea
 
The Hidden Benefits of Outsourcing IT Hardware Procurement for Small Businesses
Carley Cramer
 
Meloniusk_Communication_Template_best.pptx
howesix147
 
Networking_Essentials_version_3.0_-_Module_5.pptx
ryan622010
 
Networking_Essentials_version_3.0_-_Module_3.pptx
ryan622010
 

Stefan Richter - Writing simple, readable and robust code: Examples in Java, Clojure, and Go - code.talks 2015

  • 1. Writing Simple, Readable and Robust Code: Examples in Java, Clojure, and Go freiheit.com technologies - Hamburg, September 30, 2015 code.talks 2015
  • 2. © freiheit.com 2 Hi! I am Stefan Richter. I started programming on an Apple ][ in 1983. Since 1998, I run my own software company, which is specialized in large Internet systems. WHO AM I? Working as a professional programmer Running my own company 20102005199819951990198719831966 Apple II, Pascal, C64, 6502 VAX, Unix, C, 4GLs, Smalltalk, Lisp, Scheme Unix, C, 4 GLs, (Windows NT, VB (!)) Linux, Java, Python, Ruby, Objective-C, C/C++, Common Lisp, Clojure, JavaScript, Go ... Future University studies and work as a programmer in research institutes
  • 3. SO LET’S GET STARTED AND HAVE SOME FUN! :)
  • 4. According to Wikipedia a domain- specific language (DSL) is a “... programming language or specification language dedicated to a particular problem domain [...].“ © freiheit.com 4
  • 5. I guess, we find DSLs so compelling because sooner or later every programmer dreams of creating his/her own programming language. ;) © freiheit.com 5
  • 6. Let’s take an example from a person you all know: Martin Fowler. In 2005, he posted an article on his homepage about domain-specific languages ... © freiheit.com 6
  • 8. Fowler creates a DSL to read files in a fixed-length format and to convert each line into an object. This is the file format and the mapping spec: 8© freiheit.com
  • 9. He uses C#, but as C# is more or less a Java clone you will agree that the Java code would look almost exactly the same. © freiheit.com 9
  • 10. Line by line, he reads the file and dispatches the conversion to a strategy class. 10© freiheit.com
  • 11. The strategy class is parameterized with the type code and the target class ... 11© freiheit.com
  • 12. The individual fields in each line are read by a FieldExtractor class ... 12© freiheit.com
  • 13. To process the line, the strategy creates the target class and uses the extractors to get the field data ... 13© freiheit.com
  • 14. The FieldExtractor pulls out the data and sets the correct field in the target class by using reflection ... 14© freiheit.com
  • 15. Now you have an API that can be configured at compile time ... 15© freiheit.com
  • 16. If you would like to do this in a declarative style, you need an XML configuration ... (arrggh) 16© freiheit.com
  • 17. Fowler’s example shows about 70 lines of code (LOC). But the code is not complete. I guess, the complete example would be around 200 LOC Java. © freiheit.com 17
  • 18. HMM. SO MUCH CODE FOR A SIMPLE PROBLEM.
  • 19. Ideally, you would like to have something like this as a mini DSL: 19© freiheit.com
  • 20. The Common Lisp Community picked up Fowler’s article, because he mentioned Lisp favorably. © freiheit.com 20
  • 21. Rainer Joswig then proposed this DSL in comp. lang.lisp: 21© freiheit.com
  • 22. His solution is a 12 LOC Lisp macro …, 22© freiheit.com ... which expands to this code:
  • 23. Let’s examine how macros work. We’ll use the Common Lisp code as an example. It’s very close to how macros look like in Clojure! I’ll show you later! :) © freiheit.com 23
  • 24. • Basically, a Lisp macro is a code generator. But you can’t compare Lisp macros with C macros or any other macro or template system. • Lisp programs are trees of expressions. Code is data. Data is code. • This is why a Lisp program can modify itself. • In a nutshell - “How to read Lisp macros“: • Macros don’t evaluate their parameters. • The backtick (`) says: „Don’t evaluate the following code.“ • The comma (,) says: „Evaluate the following code and replace it with the result.“ • Then: Evaluate all resulting code together. • Read Peter Seibel’s „Practical Common Lisp“ to learn more ... 24© freiheit.com
  • 25. • defclass creates a new CLOS class. In CLOS methods are defined independently of the class definition, but getter/setter can be defined implicitly. • Methods are specialized on their parameters. So each method can be specialized to more than one class! Think about this for a moment ... • You can even specialize a method depending on what exact value is passed as a parameter. So you can have special methods for specific parameter values. In this example, parse-line-for-class is called, when you pass a class with the name “service-call“. (Which in turn is generated by this macro, too). • This method then takes the class, instantiates it and sets (side effect) its member variables by taking the format specification and using a substring function to extract the right bits and pieces from the input line. (This is not pure functional style, but this is how you use Common Lisp with CLOS.) 25© freiheit.com
  • 26. To read from a file and apply the mappings Rainer Joswig then wrote this code: 26© freiheit.com
  • 27. Obviously, the Common Lisp version works almost the same as Martin Fowler’s C# version, with much less code. Rainer Joswig’s solution is functionally complete. Let’s do this in Clojure now! © freiheit.com 27
  • 28. One thing: Some people in the Clojure community say that you should not use macros. You will soon see why. But I disagree! Macros are one of the reasons why you should favor Lisp-like languages over other languages. © freiheit.com 28
  • 29. NOW THAT YOU ARE EXPERTS ON COMMON LISP MACRO, I WILL SHOW YOU THE FIRST CLOJURE VERSION STRAIGHT AWAY!
  • 30. 30© freiheit.com • We create a list of the field names because we need them two times in the code. • defrecord creates a Clojure data structure with the look and feel of a hash table. • But it is much more: Under the hood, this is a Java class! And even more! • This is why you need a constructor function to create a “record“. I generate the name of the constructor function by appending “.“ to the record name. • I create a multimethod, which is specialized on the value of the mapping parameter. So Clojure “knows“, which method to call based on the input ... • Then, I create a list of all values from the input line (in the same order as their field names) and apply this list to the constructor of the record, which is returned as the function value.
  • 31. To make it complete: defmethod needs a defmulti to provide a dispatching function. 31© freiheit.com • Meaning: Take the first four characters of the value of the parameter “line“ (which should be a java.lang. String) and then call the multimethod, which is specialized on this value. • (Note: Of course, you have to handle the cases: What happens when you have an empty line, an unknown mapping or a comment line. But I leave this out here to keep the example simple, even though this is just five lines more including another defmethod, which handles all these :default cases).
  • 32. 32© freiheit.com • In a nutshell “How to read Clojure macros“: • The backtick (`) says: „Don‘t evaluate the following code.“ • The tilde (~) says: „Evaluate the following code and replace it with the result.“ • ~@ means: “Evaluate the following code and replace it with the result, but don’t put parentheses around the result.“ (This is called „splicing“.) • The hash sign (#) at the end of a variable is used to automatically create unique variable names in the code created by the macro. This is called auto-gensyms. This way you can prevent name clashes with call parameters.
  • 33. Finally, this Clojure macro expands to the following code. Can you see the automatically generated variable names? 33© freiheit.com
  • 34. Now we can define mappings as follows: 34© freiheit.com Actually, the leading 0 doesn’t work here. Leading zeros are reserved for octal numbers.
  • 35. LET US BUILD SOMETHING TO CONVENIENTLY USE THESE MAPPINGS!
  • 36. This is a classic with-macro that you often find in the Common Lisp world. 36© freiheit.com • It reads the file identified by file name line by line. • It calls our multimethod parse line for each line. • It binds the result to a variable that you can hand over to the macro (binding). • And only if parsing the line has a result (no empty line, no comment line or unknown binding), it executes the body, meaning the code that you can hand over to this macro, which can contain the variable, which contains the binding. • Then it returns a list of the results. • Sounds difficult, but with the example on the next page you will find it to be very simple.
  • 37. In this example, we just extract the customer name from each line. 37© freiheit.com • For each valid line in the file specified by file name (*testdata* contains the file name as a string) a Clojure record is created. • Each result is bound in this case to the symbol/variable “obj“. • In the body code you can use this symbol/variable to access this Clojure record. • In this case, we use it to extract the customer name from the record. • with-mappings then returns a list of the results.
  • 38. Isn’t this awesome?! We have just defined two new programming constructs and added them to our programming language: defmapping, with-mappings. © freiheit.com 38
  • 39. The complete example is about 30 LOC of Clojure. You don’t need any special tools (Language Workbench). It’s all right there in the Clojure language. And you just need Emacs to unleash this power ... © freiheit.com 39
  • 40. Now I can extend this without breaking the existing code. © freiheit.com 40
  • 41. Let’s add a serializer to create JSON from the data ... © freiheit.com 41
  • 42. We define a protocol and add an implementation to the record definition. 42© freiheit.com
  • 43. Now we can convert the data to JSON. 43© freiheit.com And we can even change the serialize method „in place“ and on the fly.
  • 44. © freiheit.com GOTO Conference Copenhagen, 2011 “LISP ISN’T A LANGUAGE, IT’S A BUILDING MATERIAL.“ - ALAN KAY
  • 45. And Clojure gives you powerful abstractions that you won’t find in any other programming language. Some stuff is only possible in a Lisp-like language ... with all these parenthesis. ;) © freiheit.com 45
  • 46. © freiheit.com GOTO Conference Copenhagen, 2011 IT IS COOL. I LOVE THIS. BUT IS THIS REALLY SIMPLE? LET’S DO THIS IN GO!
  • 47. © freiheit.com GOTO Conference Copenhagen, 2011 LIVE CODING
  • 48. THANK YOU freiheit.com technologies Budapester Str. 45 20359 Hamburg, Germany [email protected] T +49 40 890584 0 F +49 40 890584 20