visit
🚫
public getName(): string {
let result = "NO NAME FOUND";
if(this.name) {
result = this.name;
}
return "NO NAME FOUND";
}
✅
public getName(): string {
let result = "NO NAME FOUND";
if(this.name) {
result = this.name;
}
return result;
}
🚫
public getName(): string {
let result = "NO NAME FOUND";
if(this.name) {
result = this.name;
}
return result;
}
✅
export class User {
private readonly NAME_DEFAULT = "NO NAME FOUND";
private name;
public getName(): string {
let result = this.NAME_DEFAULT;
if(this.name) {
result = this.name;
}
return result;
}
}
pssst I tweet about code stuff all the time. If you have questions about how to level up your dev skills give me a follow
3) Keep the default clause last
A switch statement can contain a default clause to handle unexpected value. Considering you are most likely working collaboratively on a code base, it's important to follow conventions to ease communication.Considering the example below, just moving the default clause all the way to the bottom will already save a lot of frustration for your peers.🚫
import { Track } from "../models/Track";
export class BackgroundImage {
dimension: string;
url: string;
}
export class BackgroundImageService {
getBackgroundArt(track: Track): BackgroundImage {
switch(track.getGenre()) {
default:
return null;
case "hiphop":
const hipHopImage: BackgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/Qcl98B8Bk3I'};
return hipHopImage;
case "jazz":
const jazzImage : BackgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/dBWvUqBoOU8'};
return jazzImage;
case "rap":
const rapImage : BackgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/auq_QbyIA34'};
return rapImage;
case "country":
const countryImage: BackgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/RnFgs90NEHY'};
return countryImage;
}
}
}
✅
getBackgroundArt(track: Track): BackgroundImage {
switch(track.getGenre()) {
case "hiphop":
const hipHopImage: BackgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/Qcl98B8Bk3I'};
return hipHopImage;
case "jazz":
const jazzImage : BackgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/dBWvUqBoOU8'};
return jazzImage;
case "rap":
const rapImage : BackgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/auq_QbyIA34'};
return rapImage;
case "country":
const countryImage: BackgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/RnFgs90NEHY'};
return countryImage;
default:
return null;
}
}
🚫
getBackgroundArt(track: Track): BackgroundImage {
switch(track.getGenre()) {
case "hiphop":
const hipHopImage: BackgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/Qcl98B8Bk3I'};
return hipHopImage;
case "jazz":
const jazzImage : BackgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/dBWvUqBoOU8'};
return jazzImage;
case "rap":
const rapImage : BackgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/auq_QbyIA34'};
return rapImage;
case "country":
const countryImage: BackgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/RnFgs90NEHY'};
return countryImage;
default:
const defaultImage: BackgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/PDX_a_82obo'};
return defaultImage;
}
}
✅
getBackgroundArt(track: Track): BackgroundImage {
let backgroundImage: BackgroundImage;
switch(track.getGenre()) {
case "hiphop":
backgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/Qcl98B8Bk3I'};
break;
case "jazz":
backgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/dBWvUqBoOU8'};
break;
case "rap":
backgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/auq_QbyIA34'};
break;
case "country":
backgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/RnFgs90NEHY'};
break;
default:
backgroundImage = {dimension: 'small', url : '//unsplash.com/photos/PDX_a_82obo'};
}
return backgroundImage;
}
5) Forget the else, return if you can
An important concept in keeping code clean is cyclomatic complexity. Cyclomatic complexity is basically how many loops, conditionals or function calls do you have nested in our code. Really high cyclomatic complexity makes code nearly impossible to wrap your head around. You will definitely need a dozen rubber ducks if it gets out of control.Let's simplify the example from above by going from a switch statement to a tidy series of ifs.🚫🚫
getBackgroundArt(track: Track): BackgroundImage {
let backgroundImage: BackgroundImage;
switch(track.getGenre()) {
case "hiphop":
backgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/Qcl98B8Bk3I'};
break;
case "jazz":
backgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/dBWvUqBoOU8'};
break;
case "rap":
backgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/auq_QbyIA34'};
break;
case "country":
backgroundImage = {dimension: 'small', 'url': '//unsplash.com/photos/RnFgs90NEHY'};
break;
default:
backgroundImage = {dimension: 'small', url : '//unsplash.com/photos/PDX_a_82obo'};
}
return backgroundImage;
}
🚫
getBackgroundArt(track: Track): BackgroundImage {
let backgroundImage: BackgroundImage;
if(!track.getGenre()) {
backgroundImage = {dimension: BackgroundImageDimensions.small, url : this.DEFAULT_BACKGROUND_IMAGE_URL};
} else if (track.getGenre() == "hiphop") {
backgroundImage = {dimension: BackgroundImageDimensions.small, url: this.HIPHOP_BACKGROUND_IMAGE_URL};
} else if(track.getGenre() == "jazz") {
backgroundImage = {dimension: BackgroundImageDimensions.small, url: this.JAZZ_BACKGROUND_IMAGE_URL};
} else if(track.getGenre() == "rap") {
backgroundImage = {dimension: BackgroundImageDimensions.small, url: this.RAP_BACKGROUND_IMAGE_URL};
} else if(track.getGenre() == "country") {
backgroundImage = {dimension: BackgroundImageDimensions.small, url: this.COUNTRY_BACKGROUND_IMAGE_URL};
}
return backgroundImage;
}
✅
getBackgroundArt(track: Track): BackgroundImage {
let backgroundImage: BackgroundImage = {dimension: BackgroundImageDimensions.small, url : this.DEFAULT_BACKGROUND_IMAGE_URL};
if (track.getGenre() == "hiphop") {
return {dimension: BackgroundImageDimensions.small, url: this.HIPHOP_BACKGROUND_IMAGE_URL};
}
if(track.getGenre() == "jazz") {
return {dimension: BackgroundImageDimensions.small, url: this.JAZZ_BACKGROUND_IMAGE_URL};
}
if(track.getGenre() == "rap") {
return {dimension: BackgroundImageDimensions.small, url: this.RAP_BACKGROUND_IMAGE_URL};
}
if(track.getGenre() == "country") {
return {dimension: BackgroundImageDimensions.small, url: this.COUNTRY_BACKGROUND_IMAGE_URL};
}
return backgroundImage;
}