BEST PRACTICES
FOR 

QUALITY CODE
SERCAN DEĞİRMENCİ
AGENDA
• BASICS
• IN-DEEP
• EXAMPLE
GLOSSARY
There are two hard things in
computer science: cache
invalidation, naming things,
and off-by-one errors
UNIX
PHILO
SOPHY
that do one thing
and do it well.
to work together.
to handle text streams.
Write programs
Fundamental Theorem of
Software Engineering
We can solve any problem by
introducing an extra level of
indirection.
ONE and ONLY
SOURCEOF
TRUTH
Any fool can write code that a
computer can understand. Good
programmers write code that
humans can understand
Martin Fowler
NAMING THINGS
FOLLOW THE STYLE GUIDE
• JS: https://github.com/airbnb/javascript
• C++: https://google.github.io/
styleguide/cppguide.html
• Java: https://google.github.io/
styleguide/javaguide.html
NAMING CONVENTIONS
• CamelCase
• camelCase
• snake_case
FUNCTION NAMES
• Function names should be verb
• DoThing, MakeAbc, GetAbc, SetHello
• Property names should be noun and adj
GET-SET
• GET does not get input
• SET gets single input
get hello() {
return this._h;
}
set hello(value) {
this._h = value
}
var center: Int {
get {
return 0
}
set(newValue) {
}
}
const games = await api.games();
// TODO(burak): fix name
this.getGamesData(games);
const categories = await api.categories();
// TODO(burak): fix name
this.getCategoriesData(categories,this.state
.games);
NAME AND CONTEXT
• Name and context of func/property/class
have to be consistent and meaningful
UNIX PHILOSOPHY
FUNCTIONS/CLASSES
SHOULD DO ONE THING
PACKAGES/MODULES
SHOULD DO ONE THING
SOURCE OF TRUTH
STRING IS YOUR
WORST ENEMY
NO MAGICAL
NUMBERS
Magical Number

Will be Broken
ABSTRACTION
openVerbConjunction(wordObj) {
this.conjArr = [];
let language = this.$scope.global.language;
if (language == "tr") {
CONSTANT.CONJTYPE[language].forEach(c => {
let conjable = {
title: this.conj.conjTurkish(wordObj.title, c),
tence: c
};
this.conjArr.push(conjable);
});
} else if (language == "en") {
CONSTANT.CONJTYPE[language].forEach(c => {
let conjable = {
title: this.conj.conjEnglish(wordObj.title, c),
tence: c
};
this.conjArr.push(conjable);
});
}
document.getElementById("verbConj").style.display = "block";
}
Before
openVerbConjunction(wordObj) {
this.conjArr = [];
this.conj.conjtype.forEach(c => {
let conjable = {
title: this.conj.conjVerb(wordObj.title, c),
tence: c
};
this.conjArr.push(conjable);
});
document.getElementById("verbConj").style.display = "block";
}
After
protocol RandomNumberGenerator {
func random() -> Double
}
type RandomNumberGenerator interface{
Random() float64
}
interface RandomNumberGenerator{
double Random();
}
class RandomNumberGenerator {
virtual double random() = 0;
}
Abstraction on different programming languages
WRAPPER
• Wrap original object
• Add new functionalities
export class Category {
constructor(obj) {
this.c = obj;
this._color = null;
this._games = [];
}
locale(lang) {
for (const o of this.c.locales) {
if (o.language === lang) {
return o;
}
}
const dl = this.defaultLanguage;
if (typeof dl === 'string') {
for (const o of this.c.locales) {
if (o.language === dl) {
return o;
}
}
}
return this.c.locales[0];
}
get color() {
if (this._color !== null) {
return this._color;
}
if (typeof this.c.labels.color === 'string
this._color = this.c.labels.color;
} else {
this._color =
colors[Math.floor(Math.random() * colors.lengt
}
return this._color;
}
THE TWELVE-FACTOR APP
https://12factor.net/
• USE GIT
• USE NPM, YARN
• STORE CONFIG IN THE ENVIRONMENT
• MAKE THE APP STATELESS
Don’t use hard-coded values

Best Practices for Quality Code