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
27
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
50
Angular - Routing & Forms
nsanitate
0
54
Scratch: Programmare è un gioco da ragazzi
nsanitate
1
62
Principi SOLID - Le ricette dello zio Bob per un OOP da leccarsi i baffi
nsanitate
0
140
ApuliaSoft @ MathOnJob '18
nsanitate
0
30
ApuliaSoft @ Ingegneria del software '18
nsanitate
0
77
Modello ad attori - Da 700k a 40M e oltre di elaborazioni a settimana con il minimo sforzo
nsanitate
0
23
Angular - Routing & Forms
nsanitate
0
42
Angular - Introduction
nsanitate
0
58
Other Decks in Programming
See All in Programming
月刊 競技プログラミングをお仕事に役立てるには
terryu16
1
1.3k
Внедряем бюджетирование, или Как сделать хорошо?
lamodatech
0
980
動作確認やテストで漏れがちな観点3選
starfish719
5
870
自分ひとりから始められる生産性向上の取り組み #でぃーぷらすオオサカ
irof
8
2.2k
Оптимизируем производительность блока Казначейство
lamodatech
0
990
shadcn/uiを使ってReactでの開発を加速させよう!
lef237
0
390
Immutable ActiveRecord
megane42
0
120
functionalなアプローチで動的要素を排除する
ryopeko
1
950
混沌とした例外処理とエラー監視に秩序をもたらす
morihirok
18
3.3k
Java Webフレームワークの現状 / java web framework at burikaigi
kishida
9
2k
Fibonacci Function Gallery - Part 2
philipschwarz
PRO
0
230
Linux && Docker 研修/Linux && Docker training
forrep
22
4.1k
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
40
2.5k
Designing for Performance
lara
604
68k
4 Signs Your Business is Dying
shpigford
182
22k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Facilitating Awesome Meetings
lara
51
6.2k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
3k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
52k
KATA
mclloyd
29
14k
Building Adaptive Systems
keathley
39
2.4k
We Have a Design System, Now What?
morganepeng
51
7.4k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Making Projects Easy
brettharned
116
6k
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