Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Why are variables being named after specific songs?

At the end of the video when creating the playlist in app.js, why are the variables being named after specific songs in the global table? Is that the correct way to do it?

var playlist = new Playlist();

var hereComesTheSun = new Song("Here Comes the Sun", "The Beatles", "2:54");
var walkingOnSunshine = new Song("Walking on Sunshine", "Katrina and the Waves", "3:43");

playlist.add(hereComesTheSun);
playlist.add(walkingOnSunshine);

Thinking about it logically there will be more songs and some may have longer or even odd names names making for less simple and sexy code. Would it not make more sense to go with a more generic variable standard for this list that can represent any song added?

Consider having to add something like this to be worked with more than once inside of your coding:

var mmmMmmMmmMmm = new Song("Mmm Mmm Mmm Mmm", "Crash Test Dummies", "4:02")

My approach would be different and this may come from my past with databases and having a consistent pattern, nomenclature or abbreviation for everything where each one represens a variation of a variable)

var track1003 = new Song("Say It", "Tory Lanez", "3:56");  // naming convention:   ' "track" + id" ' [beginning at 0001]  OR  
var trackFour = new Song("Let It Be Me", "Ray Lamontagne", "4:13");  // naming convention: ' "track" +  "number" ' [spelled out if under 10 songs] OR
var sId3005 = new Song ("This Womans Work", "Maxwell", "3:58"); // naming convention: 'songid' = sid   OR
var song1008 =  new Song("Mmm Mmm Mmm Mmm", "Crash Test Dummies", "4:02"); naming convention: ' "song" +  "####" ' [song + id]

playlist.add(track1003);
playlist.add(trackFour);
playlist.add(sId3005);
playlist.add(song1008);

I know that variables can be what you need them to be but I'm wondering if there is an advantage here in some way or just personal choice. This may be obvious in later videos just curious.

Also it looks like these are on the global table is that the correct location for them?

1 Answer

Hi NiKole, you can name your variables as you want - well, not exactly, there are certain rules (your variable name can't start with a number and so on), but it is a good practise to give them names that have a meaning so anyone looking at your code can immediately see what certain variable holds as a value. And in this particular example each song is an object and it seems that each object is added to playlist so it might be easier to navigate through songs if they are named by the song name rather than random string.