Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
30
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
53
Angular - Routing & Forms
nsanitate
0
58
Scratch: Programmare è un gioco da ragazzi
nsanitate
1
66
Principi SOLID - Le ricette dello zio Bob per un OOP da leccarsi i baffi
nsanitate
0
160
ApuliaSoft @ MathOnJob '18
nsanitate
0
36
ApuliaSoft @ Ingegneria del software '18
nsanitate
0
81
Modello ad attori - Da 700k a 40M e oltre di elaborazioni a settimana con il minimo sforzo
nsanitate
0
26
Angular - Routing & Forms
nsanitate
0
45
Angular - Introduction
nsanitate
0
59
Other Decks in Programming
See All in Programming
【CA.ai #3】Google ADKを活用したAI Agent開発と運用知見
harappa80
0
260
S3 VectorsとStrands Agentsを利用したAgentic RAGシステムの構築
tosuri13
5
270
Querying Design System デザインシステムの意思決定を支える構造検索
ikumatadokoro
1
1.2k
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
5
1.3k
テストやOSS開発に役立つSetup PHP Action
matsuo_atsushi
0
140
Reactive Thinking with Signals and the new Resource API
manfredsteyer
PRO
0
160
【CA.ai #3】ワークフローから見直すAIエージェント — 必要な場面と“選ばない”判断
satoaoaka
0
210
AWS CDKの推しポイントN選
akihisaikeda
1
240
これだけで丸わかり!LangChain v1.0 アップデートまとめ
os1ma
6
1.3k
認証・認可の基本を学ぼう前編
kouyuume
0
150
WebRTC と Rust と8K 60fps
tnoho
2
1.9k
Google Antigravity and Vibe Coding: Agentic Development Guide
mickey_kubo
2
130
Featured
See All Featured
Balancing Empowerment & Direction
lara
5
780
Designing for humans not robots
tammielis
254
26k
Bash Introduction
62gerente
615
210k
4 Signs Your Business is Dying
shpigford
186
22k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Rails Girls Zürich Keynote
gr2m
95
14k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.3k
Typedesign – Prime Four
hannesfritz
42
2.9k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
285
14k
What's in a price? How to price your products and services
michaelherold
246
12k
Practical Orchestrator
shlominoach
190
11k
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