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
44
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
IF ... THEN ... ELSE
4 tips for write cleaner code
Nicola Sanitate
October 24, 2018
More Decks by Nicola Sanitate
See All by Nicola Sanitate
Angular - Introduction
nsanitate
0
60
Angular - Routing & Forms
nsanitate
0
75
Scratch: Programmare è un gioco da ragazzi
nsanitate
1
76
Principi SOLID - Le ricette dello zio Bob per un OOP da leccarsi i baffi
nsanitate
0
180
ApuliaSoft @ MathOnJob '18
nsanitate
0
45
ApuliaSoft @ Ingegneria del software '18
nsanitate
0
97
Modello ad attori - Da 700k a 40M e oltre di elaborazioni a settimana con il minimo sforzo
nsanitate
0
31
Angular - Routing & Forms
nsanitate
0
57
Angular - Introduction
nsanitate
0
67
Other Decks in Programming
See All in Programming
Swift愛好会100回記念 第1回を振り返る
jollyjoester
0
120
Go を使い始めて 2 ヶ月の学び / My first two months with Go
contour_gara
0
420
FastAPI の並行処理モデルを完全に理解する
hoto17296
9
3.7k
自分的「カンファレンスの楽しみ方」
syumai
0
190
KotlinConf Extended South Korea 2026 Keynote
l2hyunwoo
0
130
From 6 People Classroom Meetup to 100 People Regional Conference / FOSS4G Hiroshima 2026
furukawayasuto
0
100
AWS Step Functions 大規模並列の壁を越える / jaws-sonic-2026-niigata-step-functions
kasacchiful
PRO
0
340
MIZARU@SPAJAM2026 第二回予選
1901drama
0
100
30年振りにコンパイラの定数整数除算を改善した
herumi
9
4.4k
Flow は今どうなっているか
mizdra
PRO
0
900
バグを直したら useEffect が消えた
colorful12
3
830
LL言語やWebフレームワークのPostgreSQL対応 〜DBの機能がユーザーに届くまで〜
kentaroutakeda
0
130
Featured
See All Featured
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
430
The #1 spot is gone: here's how to win anyway
tamaranovitovic
3
1.2k
The SEO Collaboration Effect
kristinabergwall1
1
550
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.1k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
420
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
Paper Plane
katiecoart
PRO
2
53k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
Visualization
eitanlees
152
17k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
Faster Mobile Websites
deanohume
310
32k
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