SlideShare uma empresa Scribd logo
1 de 31
Baixar para ler offline
Writing readable
✴ code
Clojure
Jiří Knesl
I am …
•

From functional programming I do
•
•

•

Clojure - Lispy, dynamic, server-side
LiveScript - Haskell-inspired, dynamic,clientside

Big fan of productivity (Agile&Scrum, GTD) and
quality (TDD, refactoring)
Left to right, up to bottom
•

Code which you read bottom-up right-to-left is
problematic (if you aren’t from culture which reads
bottom-up right-to-left)
!

•

#1 use -> or ->>

•

#2 use let
With threading macros you
can do A LOT
•

some-> some->>

•

Swiss arrows (https://github.com/rplevy/swiss-arrows)
•

-<> … <>

•

-<>> … <>

•

some-<> some-<>>

•

(and others)
Get rid of useless code:
Compose for the great good!
•

The reason I started functional programming is
to get rid of useless code, what was your
reason?

•

#1 Use partial application

•

#2 Use function composition
Partial application and
composition
•

Instead of (defn a [x] (b (c x))) do:

•

(def a (comp b c))

•

Instead of (fn [x] (+ 3 x)) or #(+ 3 %) do:

•

(partial + 3) ; shame it is not automatic as in Haskell, you would need only (+ 3)
Partial application and
composition
•

They combine together.

•

Instead of: (defn a [x] (b 11 (c 12 x)))

•

You can write:

•

(def a (comp (partial b 11) (partial c 12)))

•

(which is longer and not readable for some of you now but it helps in real
code especially after you get used to it - or if you are interested in Haskell)

•

In Haskell (and LiveScript):

•

a = (b 11) . (c 12)
Partial application and
composition example
Macros
Macros
Rule #1 of Macros:
Don’t  use macros
•

HOFs are extremely powerful

•

There is a lot of existing macros

•

Partial Application, Composition and HOFs take
you long way without ever touching macros

•

Don’t tell anybody, I have 1 macro on hundreds
of functions - and all my Clojure friends have
similar codebases
Rule #2 of Macros:
You can make language of your dreams

•

core.async is just group of macros

•

If you miss any feature, you can (and probably
should) implement it
•

But language design is hard (trust me,
I designed one)
•

You need special set of skills, it isn’t just add
operators, blocks here and there

•

Good syntax should improve code readability (or
at least shouldn’t make it worse)

•

Good syntax compose with the rest
Will you spot a mistake?
Will you spot a mistake?
There are two, three, four,
five … worlds!
•

While programming (especially in languages
with macros) you are not only language user
but language designer!

•

If you build business application, your language
will be different from programmer building math
application.
So there are many DSLs
•

Application is collection of languages
•

Validation, time, money, GUI testing, security,
schema & types checking

•

Validateur http://clojurevalidations.info/articles/
getting_started.html

•

Kerodon https://github.com/xeqi/kerodon

•

Clj-time https://github.com/seancorfield/clj-time
When designing language,
there are steps to follow:
1. Find and describe the terminology of your
domain
2. Use the terminology in your application
3. Find another relations (to other parts, check how
many times were ‘abstract’ constructs used)
4. Learn something (where did you do mistakes)
5. recurse (GOTO considered harmful)
Pure FP vs. Real World
•

In pure-FP everything is expression

•

In pure-FP you don’t need sequential calling of
functions

•

In pure-FP there’s no time

•

Non-pure programming needs some constructs
and your language design must follow it and
should make it readable.
Side-effects
•

You need sequences of calls (do (first) (second) …)

•

You will use for (again)

•

You will have problems with Clojure lazy sequences (doall to solve
all known problems)
•

•

(take, map, filter and others return lazy sequence and do nothing
automatically)

But, try (for [x some-collection] (save-to-table1 x) (save-to-anothertable x) (and-show x))
•

Hint: for expects only 1 expression (you must use (do…)) - which
I see as a wrong design decision
Side-effects
•

When designing your operators, think of it:
•

what if user needs more operations?

•

what about concurrency?

•

how it will look with other operators?

•

where is application state? how it changes?
Don’t Repeat

2
Yourself

•

See where you use similar code

•

Then decide if you want introduce new syntax
with macros
•

(But please, try to do it another way, for
example HOFs, existing macros etc.)
Cheatsheet
•

¡Remember, it cannot be done without refactoring!

•

Is everything named properly
•

does it use users terminology?

•

application is collection of DSLs

•

Can you read your code L-to-R up-to-bottom?

•

Aren’t there any unnecessary duplications?
Thanks!
•

Next step:

•

http://en.knesl.com

•

@jiriknesl (most of the tweets in Czech language
- sorry I will change it soon :-))

Mais conteúdo relacionado

Mais procurados

Why i love ruby than x
Why i love ruby than xWhy i love ruby than x
Why i love ruby than x
Samnang Chhun
 

Mais procurados (17)

2018-09 - F# and Fable
2018-09 - F# and Fable2018-09 - F# and Fable
2018-09 - F# and Fable
 
Flow control in Python
Flow control in PythonFlow control in Python
Flow control in Python
 
Modern Static Code Analysis in PHP
Modern Static Code Analysis in PHPModern Static Code Analysis in PHP
Modern Static Code Analysis in PHP
 
LogiLogicless UI prototyping with Node.js | SuperSpeaker@CodeCamp Iasi, 2014
LogiLogicless UI prototyping with Node.js | SuperSpeaker@CodeCamp Iasi, 2014LogiLogicless UI prototyping with Node.js | SuperSpeaker@CodeCamp Iasi, 2014
LogiLogicless UI prototyping with Node.js | SuperSpeaker@CodeCamp Iasi, 2014
 
EMPEX LA 2018 - Inclusion Starts with Docs
EMPEX LA 2018 - Inclusion Starts with DocsEMPEX LA 2018 - Inclusion Starts with Docs
EMPEX LA 2018 - Inclusion Starts with Docs
 
Build your own Language - Why and How?
Build your own Language - Why and How?Build your own Language - Why and How?
Build your own Language - Why and How?
 
Code Generation in Perl
Code Generation in PerlCode Generation in Perl
Code Generation in Perl
 
What is clooca
What is cloocaWhat is clooca
What is clooca
 
Break Free with Managed Functional Programming: An Introduction to F#
Break Free with Managed Functional Programming: An Introduction to F#Break Free with Managed Functional Programming: An Introduction to F#
Break Free with Managed Functional Programming: An Introduction to F#
 
Break Free with Managed Functional Programming: An Introduction to F#
Break Free with Managed Functional Programming: An Introduction to F#Break Free with Managed Functional Programming: An Introduction to F#
Break Free with Managed Functional Programming: An Introduction to F#
 
Why i love ruby than x
Why i love ruby than xWhy i love ruby than x
Why i love ruby than x
 
Beginning Java for .NET developers
Beginning Java for .NET developersBeginning Java for .NET developers
Beginning Java for .NET developers
 
Introduction to Functional Programming and Clojure
Introduction to Functional Programming and ClojureIntroduction to Functional Programming and Clojure
Introduction to Functional Programming and Clojure
 
Zero to Hero in Machine Learning with Keras
Zero to Hero in Machine Learning with KerasZero to Hero in Machine Learning with Keras
Zero to Hero in Machine Learning with Keras
 
Married to the Mob (programming)
Married to the Mob (programming)Married to the Mob (programming)
Married to the Mob (programming)
 
Mixing Plone and Django for explosive results
Mixing Plone and Django for explosive resultsMixing Plone and Django for explosive results
Mixing Plone and Django for explosive results
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 

Semelhante a Writing readable Clojure code

Programming Languages #devcon2013
Programming Languages #devcon2013Programming Languages #devcon2013
Programming Languages #devcon2013
Iván Montes
 
Python-unit -I.pptx
Python-unit -I.pptxPython-unit -I.pptx
Python-unit -I.pptx
crAmth
 

Semelhante a Writing readable Clojure code (20)

Go fundamentals
Go fundamentalsGo fundamentals
Go fundamentals
 
Presentation-1.pptx
Presentation-1.pptxPresentation-1.pptx
Presentation-1.pptx
 
Introduction to programming languages
Introduction to programming languagesIntroduction to programming languages
Introduction to programming languages
 
Introduction to programming - class 1
Introduction to programming - class 1Introduction to programming - class 1
Introduction to programming - class 1
 
Introduction to Computer Programming
Introduction to Computer ProgrammingIntroduction to Computer Programming
Introduction to Computer Programming
 
Are High Level Programming Languages for Multicore and Safety Critical Conver...
Are High Level Programming Languages for Multicore and Safety Critical Conver...Are High Level Programming Languages for Multicore and Safety Critical Conver...
Are High Level Programming Languages for Multicore and Safety Critical Conver...
 
Programming Languages #devcon2013
Programming Languages #devcon2013Programming Languages #devcon2013
Programming Languages #devcon2013
 
Introducing systems analysis, design & development Concepts
Introducing systems analysis, design & development ConceptsIntroducing systems analysis, design & development Concepts
Introducing systems analysis, design & development Concepts
 
Making A Game Engine Is Easier Than You Think
Making A Game Engine Is Easier Than You ThinkMaking A Game Engine Is Easier Than You Think
Making A Game Engine Is Easier Than You Think
 
Go programming language
Go programming languageGo programming language
Go programming language
 
Compilers.pptx
Compilers.pptxCompilers.pptx
Compilers.pptx
 
Intro To AOP
Intro To AOPIntro To AOP
Intro To AOP
 
Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1
 
Top 10 programming languages
Top 10 programming languagesTop 10 programming languages
Top 10 programming languages
 
Introduction to multicore .ppt
Introduction to multicore .pptIntroduction to multicore .ppt
Introduction to multicore .ppt
 
Vulnerability, exploit to metasploit
Vulnerability, exploit to metasploitVulnerability, exploit to metasploit
Vulnerability, exploit to metasploit
 
C++ programming languages lectures
C++ programming languages lectures C++ programming languages lectures
C++ programming languages lectures
 
Top programming Languages in software Industry companies
Top programming Languages in software Industry companiesTop programming Languages in software Industry companies
Top programming Languages in software Industry companies
 
Python-unit -I.pptx
Python-unit -I.pptxPython-unit -I.pptx
Python-unit -I.pptx
 
Rails tools
Rails toolsRails tools
Rails tools
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

Writing readable Clojure code

  • 2. I am … • From functional programming I do • • • Clojure - Lispy, dynamic, server-side LiveScript - Haskell-inspired, dynamic,clientside Big fan of productivity (Agile&Scrum, GTD) and quality (TDD, refactoring)
  • 3. Left to right, up to bottom • Code which you read bottom-up right-to-left is problematic (if you aren’t from culture which reads bottom-up right-to-left) ! • #1 use -> or ->> • #2 use let
  • 4.
  • 5.
  • 6.
  • 7. With threading macros you can do A LOT • some-> some->> • Swiss arrows (https://github.com/rplevy/swiss-arrows) • -<> … <> • -<>> … <> • some-<> some-<>> • (and others)
  • 8. Get rid of useless code: Compose for the great good! • The reason I started functional programming is to get rid of useless code, what was your reason? • #1 Use partial application • #2 Use function composition
  • 9. Partial application and composition • Instead of (defn a [x] (b (c x))) do: • (def a (comp b c)) • Instead of (fn [x] (+ 3 x)) or #(+ 3 %) do: • (partial + 3) ; shame it is not automatic as in Haskell, you would need only (+ 3)
  • 10. Partial application and composition • They combine together. • Instead of: (defn a [x] (b 11 (c 12 x))) • You can write: • (def a (comp (partial b 11) (partial c 12))) • (which is longer and not readable for some of you now but it helps in real code especially after you get used to it - or if you are interested in Haskell) • In Haskell (and LiveScript): • a = (b 11) . (c 12)
  • 14. Rule #1 of Macros: Don’t  use macros • HOFs are extremely powerful • There is a lot of existing macros • Partial Application, Composition and HOFs take you long way without ever touching macros • Don’t tell anybody, I have 1 macro on hundreds of functions - and all my Clojure friends have similar codebases
  • 15. Rule #2 of Macros: You can make language of your dreams • core.async is just group of macros • If you miss any feature, you can (and probably should) implement it
  • 16. • But language design is hard (trust me, I designed one) • You need special set of skills, it isn’t just add operators, blocks here and there • Good syntax should improve code readability (or at least shouldn’t make it worse) • Good syntax compose with the rest
  • 17. Will you spot a mistake?
  • 18. Will you spot a mistake?
  • 19.
  • 20.
  • 21. There are two, three, four, five … worlds! • While programming (especially in languages with macros) you are not only language user but language designer! • If you build business application, your language will be different from programmer building math application.
  • 22. So there are many DSLs • Application is collection of languages • Validation, time, money, GUI testing, security, schema & types checking • Validateur http://clojurevalidations.info/articles/ getting_started.html • Kerodon https://github.com/xeqi/kerodon • Clj-time https://github.com/seancorfield/clj-time
  • 23. When designing language, there are steps to follow: 1. Find and describe the terminology of your domain 2. Use the terminology in your application 3. Find another relations (to other parts, check how many times were ‘abstract’ constructs used) 4. Learn something (where did you do mistakes) 5. recurse (GOTO considered harmful)
  • 24. Pure FP vs. Real World • In pure-FP everything is expression • In pure-FP you don’t need sequential calling of functions • In pure-FP there’s no time • Non-pure programming needs some constructs and your language design must follow it and should make it readable.
  • 25. Side-effects • You need sequences of calls (do (first) (second) …) • You will use for (again) • You will have problems with Clojure lazy sequences (doall to solve all known problems) • • (take, map, filter and others return lazy sequence and do nothing automatically) But, try (for [x some-collection] (save-to-table1 x) (save-to-anothertable x) (and-show x)) • Hint: for expects only 1 expression (you must use (do…)) - which I see as a wrong design decision
  • 26. Side-effects • When designing your operators, think of it: • what if user needs more operations? • what about concurrency? • how it will look with other operators? • where is application state? how it changes?
  • 27. Don’t Repeat 2 Yourself • See where you use similar code • Then decide if you want introduce new syntax with macros • (But please, try to do it another way, for example HOFs, existing macros etc.)
  • 28.
  • 29.
  • 30. Cheatsheet • ¡Remember, it cannot be done without refactoring! • Is everything named properly • does it use users terminology? • application is collection of DSLs • Can you read your code L-to-R up-to-bottom? • Aren’t there any unnecessary duplications?
  • 31. Thanks! • Next step: • http://en.knesl.com • @jiriknesl (most of the tweets in Czech language - sorry I will change it soon :-))