visit
//🚫
getBackgroundArt(track: Track): BackgroundImage {
let backgroundImage: BackgroundImage;
if(!track.getGenre()) {
backgroundImage = {dimension: BackgroundImageDimensions.small, url : this.DEFAULT_BACKGROUND_IMAGE_URL};
} if (track.getGenre() == "hiphop") {
backgroundImage = {dimension: BackgroundImageDimensions.small, url: this.HIPHOP_BACKGROUND_IMAGE_URL};
} if(track.getGenre() == "jazz") {
backgroundImage = {dimension: BackgroundImageDimensions.small, url: this.JAZZ_BACKGROUND_IMAGE_URL};
} if(track.getGenre() == "rap") {
backgroundImage = {dimension: BackgroundImageDimensions.small, url: this.RAP_BACKGROUND_IMAGE_URL};
} if(track.getGenre() == "country") {
backgroundImage = {dimension: BackgroundImageDimensions.small, url: this.COUNTRY_BACKGROUND_IMAGE_URL};
}
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;
}
//🚫
export class Track {
private name;
private genre;
private length;
private backgroundImage : string | undefined;
private album: string | undefined;
private url: string | undefined;
}
✅
export class Track {
private name;
private genre;
private length;
private backgroundImage? : string;
private album?: string;
private url?: string;
}
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
A dead store is when you assign a value to a variable but is then re-assigned without actually using the original value. Calculating or setting a value, without actually using it is at best a waste of resources, and at worst a bug in our code. For the following examples, let’s assume we have an array of music tracks and we want to calculate the total runtime of all the songs.
A little added bonus in the following example, is the use of the reduce function to get our value.
//🚫
public getLength(): number {
let totalLength = 0;
totalLength = this.tracks.reduce((count, track) => count + track.getLength(),0);
//convert to minutes
return totalLength;
}
//✅
public getLength(): number {
const totalLength = this.tracks.reduce((count, track) => count + track.getLength(),0);
// do some formatting later
return totalLength;
}
//🚫
public deleteTrack(index: number) {
// out of bound check
if(!(index <= this.tracks.length)) {
return;
}
this.tracks.splice(index,1);
}
//✅
public deleteTrack(index: number) {
// out of bound check
if(index >= this.tracks.length) {
return;
}
this.tracks.splice(index,1);
}
//🚫
createConnection({
type: 'sqlite',
database: path.resolve(__dirname, '..')+'/data/musicnow.sqlite',
entities: [
__dirname+'/models/*.ts'
],
});
//✅
createConnection({
type: 'sqlite',
database: `${path.resolve(__dirname, '..')}/data/musicnow.sqlite`,
entities: [
`${__dirname}/models/*.ts`
],
});