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
Try a little tenderness
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
masylum
July 01, 2013
0
94
Try a little tenderness
masylum
July 01, 2013
Tweet
Share
More Decks by masylum
See All by masylum
IPFS
masylum
0
190
REST Clients Nordic APIs
masylum
3
230
REST clients
masylum
2
200
Building the best tech-team
masylum
4
210
I love async but I can't code like this
masylum
4
530
Featured
See All Featured
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
170
From π to Pie charts
rasagy
0
120
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.3k
The agentic SEO stack - context over prompts
schlessera
0
640
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.7k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
96
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.9k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
170
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
110
So, you think you're a good person
axbom
PRO
2
1.9k
Utilizing Notion as your number one productivity tool
mfonobong
3
220
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.4k
Transcript
Coding is a fucking mess, big programs are complex and
troublesome... follow Ottis advice and Try a little tenderness
Who am I?
Pau Ramon CTO at Teambox ! @masylum github.com/masylum
This talk is about good code style principles
Why caring about style?
Development is not the bottleneck, maintenance is
Code may be ugly, but is what you stare at
all day
Mind your API Expose only what you need
Mind your API var obj = {}; obj.pi = 3.14;
obj.circumference = function (r) { return num * this.pi; };
Mind your API var obj = {}; var pi =
3.14; obj.circumference = function (r) { return r * a; };
Be verbose Good naming
Be verbose function avg(xs) { var s = i =
0; var l = xs.length; for(; i < l; i++;){ s += parseInt(xs[i]); } return s / l; }
Be verbose function average(numbers) { var sum = i =
0; var length = numbers.length; for(; i < length; i++;){ sum += parseInt(numbers[i]); } return sum / length; }
Explicit over Implicit Magic is for magicians
Explicit over Implicit function Dog() { this.speed = 4; }
require(‘run’); var dog = new Dog(); dog.run();
Explicit over Implicit function Dog() { this.speed = 4; }
Dog.prototype.run = require(‘run’); var dog = new Dog(); dog.run();
Careful with DRY It creates dependencies
Careful with DRY function truncateName(el) { var attr = el.type
=== ‘user’ ? ‘fullName’ : ‘name’; return el[attr].substring(0, 40); }
Careful with DRY function truncate(string) { return string.substring(0, 40); }
truncate(user.fullName); truncate(project.name);
Lines of code syndrome The myth of less is more
Lines of code syndrome function isUrgent(id) { if (task =
Task.find(id) && task.urgent) return true; return false; }
Lines of code syndrome function isUrgent(id) { var task =
Task.find(id); if (task && task.urgent) { return true; } return false; }
Self explanatory code Avoid inline comments
Self explanatory code function removeUser(id) { // find user var
user = User.find(id); ... // remove tasks return user.tasks.remove(); }
Self explanatory code function removeUser(id) { var user = findUser(id);
return removeTasks(user); }
Write contracts Document your API
Write contracts // removes the user function removeUser(id) ...
Write contracts /** * Removes the user and all his
* assigned tasks * * @param {Number} id - user id * @return {Boolean} - did work? */ function removeUser(id) ...
Beware of nesting Flow control complexity
Beware of nesting function isComplete(task) { var comment = task.comment;
if (comment) if (comment.status === 3) return true; return task.isResolved(); }
Beware of nesting function isComplete(task) { var comment = task.comment;
if (comment) { return comment.isComplete(); } return task.isResolved(); }
Beware of nesting setTimeout(function () { setTimeout(function () { console.log(‘B’);
}, 10); }, 20);
Beware of nesting ! function secondMethod() { console.log(‘B’); } function
firstMethod() { setTimeout(secondMethod, 10); } setTimeout(firstMethod, 20);
Use tools They are better than you
Use tools • Have static analyzers to detect bad code
practices and syntax errors • Use tools that can provide metrics • Integrate your tools with your editor • Integrate your tools with your process
Use tools • Javascript: JSHint • Ruby: Cane, Rubocop •
IDE • SAAS: Codeclimate
Avoid broken windows With metrics and processes
Avoid broken windows • Before pushing: Git hooks • Before
merging a branch: Travis • Before deploying a release: Jenkins