Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
IF ... THEN ... ELSE
Search
Nicola Sanitate
October 24, 2018
Programming
0
24
IF ... THEN ... ELSE
4 tips for write cleaner code
Nicola Sanitate
October 24, 2018
Tweet
Share
More Decks by Nicola Sanitate
See All by Nicola Sanitate
Angular - Introduction
nsanitate
0
38
Angular - Routing & Forms
nsanitate
0
47
Scratch: Programmare è un gioco da ragazzi
nsanitate
1
56
Principi SOLID - Le ricette dello zio Bob per un OOP da leccarsi i baffi
nsanitate
0
130
ApuliaSoft @ MathOnJob '18
nsanitate
0
27
ApuliaSoft @ Ingegneria del software '18
nsanitate
0
68
Modello ad attori - Da 700k a 40M e oltre di elaborazioni a settimana con il minimo sforzo
nsanitate
0
19
Angular - Routing & Forms
nsanitate
0
37
Angular - Introduction
nsanitate
0
56
Other Decks in Programming
See All in Programming
障害対応を起点としたもっといい開発と運用のサイクル作りのためにできること / Hatena Enginner Seminar #29
polamjag
0
440
WebGLで始める コンピュータグラフィックス入門
heller77
0
360
チーム立ち上げにAWSを活用したらClaudeさんに褒められた話
mkdev10
1
110
SIMD Parallel Programming with the Vector API
josepaumard
0
250
初心者のためのRubyKaigi入門/RubyKaigi Introduction
a_matsuda
10
1.8k
Webアプリをできるだけコードを手書きしないで作ってみる
tomokusaba
2
230
Criando a Woovi em uma semana
daniloab
0
110
Native Federation: The Future of Micro Frontends in Angular
manfredsteyer
PRO
0
140
Fast JSX: Don't clone props object #28768
yossydev
1
230
CDKコントリビュートの最初の壁を越えよう! -簡単issueの見つけ方-
badmintoncryer
3
390
Balkan Ruby 2024 — How and why to run SQLite on Rails in production
fractaledmind
0
110
Productivity is Messing Around and Having Fun
hollycummins
1
140
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
266
26k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
358
22k
StorybookのUI Testing Handbookを読んだ
zakiyama
13
4.6k
The Illustrated Children's Guide to Kubernetes
chrisshort
32
47k
How To Stay Up To Date on Web Technology
chriscoyier
782
250k
The Invisible Side of Design
smashingmag
294
49k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
80
44k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
501
140k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
23
1.6k
KATA
mclloyd
16
12k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
18
7k
The Straight Up "How To Draw Better" Workshop
denniskardys
228
130k
Transcript
IF … THEN … ELSE Come sopravvivere alle avverse condizioni
e vivere una vita più felice
4 consigli per scrivere codice più pulito
100% Utile anche per linguaggi che non siano JavaScript
1. Multiple criteria
function test(fruit) { if (fruit == 'apple' || fruit ==
'strawberry') console.log('red'); }
Array.includes
function test(fruit) { const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries'];
if (redFruits.includes(fruit)) console.log('red'); }
2. Nesting
function test(fruit, quantity) { const redFruits = ['apple', 'strawberry', 'cherry',
'cranberries']; if (fruit) { if (redFruits.includes(fruit)) { console.log('red'); if (quantity > 10) console.log('big quantity'); } } else { throw new Error('No fruit!'); } }
Return Early
function test(fruit, quantity) { const redFruits = ['apple', 'strawberry', 'cherry',
'cranberries']; if (!fruit) throw new Error('No fruit!'); if (!redFruits.includes(fruit)) return; console.log('red'); if (quantity > 10) console.log('big quantity'); }
3. Switch
function test(color) { switch (color) { case 'red': return ['apple',
'strawberry']; case 'yellow': return ['banana', 'pineapple']; case 'purple': return ['grape', 'plum']; default: return []; } }
Object Literal (Map)
const fruitColor = { red: ['apple', 'strawberry'], yellow: ['banana', 'pineapple'],
purple: ['grape', 'plum'] }; function test(color) { return fruitColor[color] || []; }
4. Partial criteria
const fruits = [ { name: 'apple', color: 'red' },
{ name: 'banana', color: 'yellow' } ]; function test() { let isAllRed = true; for (let f of fruits) { if (f.color == 'red') { isAllRed = false; break; } } console.log(isAllRed); }
Array.every (Array.some)
const fruits = [ { name: 'apple', color: 'red' },
{ name: 'banana', color: 'yellow' } ]; function test() { const isAllRed = fruits.every(f => f.color == 'red'); console.log(isAllRed); }
I linguaggi offrono più di quello che pensiamo
Studiamo Grazie per l’attenzione