UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Your Thoughts? Renaming ContentView

Forums > 100 Days of SwiftUI

Not sure if this is an unpopular opinion. What do you think?

I am hoping when Paul updates 100 Days of SwiftUI videos he'll introduce the concept of renaming ContentView into something more appropriate for the application you are building.

To me, ContentView is the "FooBar" of examples. Many programming books would use the function name "FooBar" when trying to explain a concept. (This is back in the day when I actually bought hardcover programming books!) FooBar was always meant to mean, "This is a generic concept, replace FooBar with your actual intended function name. But for the purposes of this chapter, is just means generic function."

In most online SwiftUI videos, the presenters provide eloquent discussions how Swift functions names read like English sentences.

let singers = ["Taylor", "Paul", "George", "John"]  // An array of people who sing!
singers.sort(by:  > )  // See? sort by is meaningful. This is not FooBar.
singers.remove(at: 1)  //  this is clear.

Also there are many examples how struct and variable names should be descriptive of your intentions.

Yet, most online tutorials keep the default ContentView simply because that's what XCode provides when creating a new view.

I think we do a disservice to new programmers. Perhaps we should challenge them to think about what it is they want to see on the screen! They are designing a tip calculator, the view could be called TipView. If they are designing a screen to show a photo of a national park along with a description, perhaps the view should be called ParkView?

I just think we can raise the bar a bit and teach new programmers to avoid the default, ContentView.

Please share your thoughts below!

3      

I agree with you that ContentView should be renamed to something meaningful. If your app only has a single view, maybe it's fine to keep the default name, otherwise I would always rename it.

3      

It however you feel happy as ContentView tends to be the first view and most people would understand that. I have a project with near 5,000 lines of code (not including JSON files) and orginal I had different name for the first view but as progressed I actually ended up adding ContentView back in as a View that holds TabView and some other start up views (eg "Whats New Splash View" etc) which have nothing to do with the main part of the app, just "houskeeping" of the app status.

PS if you feel like you want to change it in the future from this, you can always right click on refactor name and change it. (and do not forget to change ContentView_Previews too).

3      

@Nikolaj @NigelGee. Thanks for your thoughts. I hope other developers chime in. While I agree that many new programmers can be comforted by this being the default, keeping the name seems to go against the messages @twostraws and many other SwiftUI instructors teach.

Think of how meaningful variable names are. Think of how Swifty we strive to make our function calls and parameter names! @twostraws hits these messages over and over. I see Nick Sarno of Swiftful Thinking change ContentView as one of his first steps when creating a new SwiftUI view.

To me, keeping a view named ContentView is similar giving a variable the name holdStuff or boxOfSomething. It's like naming a function "task1( input1: input2:) -> Output"

Sure, these work. But, like ContentView, these miss the mark at providing clarity.

4      

I usually go just with MyAppsNameStartView and it's clearly visible where's my entry point.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.