Mohammad Umair Khan
Trainer, Mentor, Foo, Bar !
A code smell is a surface indication that usually corresponds to a deeper problem in system - Martin Fowler
Changing the structure of the code without changing its behaviour
Wherever there is a constant change in the code-base, refactoring is inevitable
Code smell
Code without smells
Refactoring
function calculateWeeklyStats() {
let startDate = new Date();
let endDate = new Date();
let numberOfDaysToAdd = 6;
endDate.setDate(startDate.getDate() + numberOfDaysToAdd);
// convert dates to UTC Timestamp (in milliseconds)
let startDateFormatted = startDate.valueOf();
let endDateFormatted = endDate.valueOf();
return statsRepository.where(startDateFormatted, endDateFormatted);
}
function calculateWeeklyStats() {
let startDate = new Date();
let endDate = new Date();
endDate.setDate(startDate.getDate() + 6);
return calculateStats(startDate, endDate);
}
function calculateMonthlyStats() {
let startDate = new Date();
let endDate = new Date();
endDate.setDate(startDate.getDate + 30);
return calcualteStats(startDate, endDate);
}
function calculateStats(startDate, endDate) {
return statsRepository.where(startDate.valueOf(), endDate.valueOf);
}
function calculateWeeklyStats() {
let startDate = new Date();
let endDate = new Date();
endDate.setDate(startDate.getDate() + 6);
return calculateStats(startDate, endDate);
}
function calculateMonthlyStats() {
let startDate = new Date();
let endDate = new Date();
endDate.setDate(startDate.getDate + 30);
return calcualteStats(startDate, endDate);
}
function calculateYearlyStats() {
let startDate = new Date();
let endDate = new Date();
endDate.setDate(startDate.getDate + 365);
return calcualteStats(startDate, endDate);
}
function calculateWeeklyStats() {
let startDate = new Date();
let endDate = startDate.addDays(6);
return calculateStats(startDate, endDate);
}
function calculateMonthlyStats() {
let startDate = new Date();
let endDate = startDate.addDays(30);
return calcualteStats(startDate, endDate);
}
function calculateYearlyStats() {
let startDate = new Date();
let endDate = startDate.addDays(365);
return calcualteStats(startDate, endDate);
}
// add to Date Prototype
Date.prototype.addDays(numberOfDays){
let clonedDate = new Date(this.getTime());
clonedDate.setDate(this.getDate() + numberOfDays);
return clonedDate;
}
public class Bank {
private double currency;
public double convertToUSD(){
return this.currency * 3200;
}
public double convertToGBP(){
return this.currency * 6400;
}
}
public class Currency {
private double value;
public Currency(double value) {
this.value = value;
}
public convertToUSD() {
return this.value * 3200;
}
}
public class Bank {
private Currency currency;
...
}
class AuthenticationService {
function isLoggedIn() {
return localStorage.getItem('loggedIn');
}
}
class SessionService {
function shouldRedirectToLogin() {
return localStorage.getItem('loggedIn');
}
}
class AuthenticationService {
constructor() {
this.allowRedirect = false;
}
function isLoggedIn() {
return localStorage.getItem('loggedIn');
}
function verifyToken() {
...
}
function isAdminUser() {
if(isLoggedIn) {
return this.allowRedirect;
} else {
return true;
}
}
}
function add(data) {
salesRepository.add(data);
let date = new Date();
console.log(`Operation: Add $(date)`);
}
function edit(data) {
salesRepository.edit(data);
let date = new Date();
console.log(`Operation: Edit $(date)`);
}
function delete(data) {
salesRepository.delete(data);
let date = new Date();
console.log(`Operation: Delete $(date)`);
}
function log(op) {
console.log(`Operation: $(op) $(new Date())`)
}
function add(data) {
salesRepository.add(data);
log('add');
}
function edit(data) {
salesRepository.edit(data);
log('edit');
}
function delete(data) {
salesRepository.delete(data);
log('delete');
}
// One liners
5 > b ? 3<b ? console.log(4) : console.log(2) : console.log(6);
// Multi-liner but readable
if ( 5 > b) {
if ( 3 < b) {
console.log(4)
} else {
console.log(2)
}
} else {
console.log(6)
}
By Mohammad Umair Khan