NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

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!

1      

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.

1      

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).

1      

@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.

2      

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

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

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

Reply to this topic…

You need to create an account or log in to reply.

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.