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 trialWill Matthews
8,127 PointsWhy are we defining the MusicLibrary and Playlist structs in two separate files?
I'm confused by why we're defining these two structs in separate files? I can understand moving data model code out of the controller code. But given that the definition of Playlist is tightly integrated with the dictionary elements defined in MusicLibrary, why not define the two structs in the same file?
4 Answers
Madhav Sharma
4,081 PointsThe MusicLibrary struct contains the details for ALL the playlists. So when accessing the playlists we would have one more layer of a struct to go through.
Creating a separate struct allows our ViewController to access a struct with only the relevant data. This allows your code to be more broken up in stages.
Will Matthews
8,127 PointsThanks for the reply Madhav. I can totally appreciate why we're defining things in two different structs, but what I'm curious about is why these two structs are set up in separate files. From what I can figure, there's very little portability of the Playlist struct without the prior definition of this particular MusicLibrary struct. So in my opinion, it would make more sense to include them both in the same file rather than to store them in two different files, no?
Madhav Sharma
4,081 PointsSorry I misread your question.
You can see that you can still fully use the Playlist struct without including the MusicLibrary file. So, I assume that separating them allows you to in certain cases restrict your access to MusicLibrary, and two separate files grants you that luxury.
Will Matthews
8,127 PointsI actually don't think you can use this particular Playlist struct as it is defined without our MusicLibrary struct file. It won't be able to initialize. That why I think in this particular case, I'd feel more comfortable defining the two in the same file.
Eduardo Scodino
5,083 PointsI think the structs were separated in two files just because of MVC best practices.
Will Matthews
8,127 PointsI guess what I'm asking is how is this a best practice? I totally understand why you want to store the data model apart from the view controller code. No problem there. But why is each individual struct stored in two different files when this Playlist struct cannot be initialized without the definition of this MusicLibrary struct. Why would they not be stored together since the Playlist can't be used without MusicLibrary? ie. Create a file called MusicDataModel or some-such-thing and the store both in there?