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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Nicola Sanitate
October 24, 2018
Programming
42
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
58
Angular - Routing & Forms
nsanitate
0
72
Scratch: Programmare è un gioco da ragazzi
nsanitate
1
72
Principi SOLID - Le ricette dello zio Bob per un OOP da leccarsi i baffi
nsanitate
0
170
ApuliaSoft @ MathOnJob '18
nsanitate
0
41
ApuliaSoft @ Ingegneria del software '18
nsanitate
0
92
Modello ad attori - Da 700k a 40M e oltre di elaborazioni a settimana con il minimo sforzo
nsanitate
0
29
Angular - Routing & Forms
nsanitate
0
50
Angular - Introduction
nsanitate
0
65
Other Decks in Programming
See All in Programming
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
220
Oxlintのカスタムルールの現況
syumai
5
1k
さぁV100、メモリをお食べ・・・
nilpe
0
130
Oxcを導入して開発体験が向上した話
yug1224
4
290
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
1.4k
LLM Plugin for Node-REDの利用方法と開発について
404background
0
160
セキュリティの専門家じゃなくてもできる。「セキュリティ意識」をアップデートして サプライチェーン攻撃への耐性を高めよう。
tk3fftk
5
630
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
210
Lemonade + Foundry Toolkit でお手軽アプリ開発
seosoft
1
310
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
13
3.5k
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
110
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
150
Featured
See All Featured
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
300
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
180
Paper Plane
katiecoart
PRO
1
51k
How GitHub (no longer) Works
holman
316
150k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
600
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
280
Building AI with AI
inesmontani
PRO
1
1.1k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.6k
Music & Morning Musume
bryan
47
7.2k
Tell your own story through comics
letsgokoyo
1
950
Building the Perfect Custom Keyboard
takai
2
780
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