Dave L.
Software Engineer and Aspiring Software Craftsman
Passionate Software Engineer
& Aspiring Craftsman
Fullstack Developer at
Co Organizer of
david.losert@gmx.de
david.losert@virtual-identity.com
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Martin Fowler
“Clean code always looks like it was written by someone who cares.”
Michael Feathers
„You know you are working on clean code when each routine you read turns out to be pretty much what you expected.“
Ward Cunningham
Do you write your code like this...?
...or more like this?
=> CLEAN CODE SAVES TIME
=> CLEAN CODE IS BUSINESS CRITICAL
if((e.startDate <= new Date() || e.endDate >= new Date())) {
// do something
}
if(eventIsToday(e))) {
// do something
}
// ...
const today = new Date();
const eventIsToday = (event) =>
event.startDate <= today && event.endDate >= today
function eventHandler() {
if(!$(this).hasClass(('ext')){
$(this).animate({width: 120};
}
}
function enlargeLink() {
let linkElement = $(this);
if(!isExtended(linkElement)){
enlarge(linkElement);
}
}
const isExtended = (element) => element.hasClass('ext')
const enlarge = (element) => element.animate({width: 120})
function login(username, password) {
fetch('/login', {
method: post,
body: {
username,
password
}
}).then(() => {
window.location = '/app';
}).catch(() => {
window.location = '/error';
})
}
function login(username, password) {
requestToken(username, password)
.then(redirectToApp)
.catch(redirectToError);
}
const requestToken = (username, password) =>
fetch('/login', { method: 'POST', body: {username, password} });
const redirectToApp = () => window.location = '/app';
const redirectToError = () => window.location = '/error';
function enlargeOrShrink(button) {
if(button.hasClass('ext')) {
button.animate({width: 20, speed: 200, type: 'slide'});
} else {
button.animate({width: 120, speed: 200, type: 'slide'});
}
}
function enlargeOrShrink(button) {
if(button.hasClass('ext')) {
expandTo(button, 20);
} else {
expandTo(button, 120);
}
}
const expandTo = (element, width) =>
element.animate({width: width, speed: 200, type: 'slide'});
function enlargeOrShrink(button) {
let width = button.hasClass('ext') ? 20 : 120;
expandTo(button, width);
}
const expandTo = (element, width) =>
element.animate({width: width, speed: 200, type: 'slide'});
// If event is today
if((e.startDate <= new Date() || e.endDate >= new Date())) {
// do something
}
if(eventIsToday(e))) {
// do something
}
// ...
const today = new Date();
const eventIsToday = (event) =>
event.startDate <= today && event.endDate >= today
/**
* The following code filters the all documents of a project by
* providing the type as a argument to the signature.
* Test with unstrict operators as the type is a number and it can also be a number in a string
*/
function filterByDocType(project, docType) {
let allDocs = project.documents;
return allDocs.filter(doc => doc.type === docType);
}
Commit 8f2fee4 David Losert <david.losert@virtual-identity.com>:
Message: "Adjusted docType to always be a string"
From Version Control:
function login(username, password) {
fetch('/login', {
method: post,
body: {
username,
password
}
}).then(() => {
window.location = '/app';
}).catch(() => {
window.location = '/error';
})
}
function login(username, password) {
requestToken(username, password)
.then(redirectToApp)
.catch(redirectToError);
}
const requestToken = (username, password) =>
fetch('/login', { method: 'POST', body: {username, password} });
const redirectToApp = () => window.location = '/app';
const redirectToError = () => window.location = '/error';
Easy start:
More sophisticated:
By Dave L.
My first presentation on a meetup about my favorite topic: Clean Code!