When to use private?

I watched Stanford 193p, and I had a question.

When the professor writes the code, he writes like this

struct GameView: View {
    var body: some View {
        gameBody
    }
    
    var gameBody: some View {
        Button("gameBody") {
            gameBodyFunc()
        }
    }
    
    private func gameBodyFunc() {
        
    }
}

Why is the function declared private but not the gameBody variable?

I am impressed with his coding style and want to learn it.

Accepted Answer

This is a basic object oriented design/programming thing. Related to design principles of information hiding, encapsulation and modular design.

The idea is to hide all implementation details in the class/struct and only expose minimal public interface to other elements in the software.

Apparently the var members of the struct are used from other structs/classes in the same package, but the private function should be used only by the GameView struct.

The principle is that everything that can be marked private should be. There is no risk: if it is not correct, compiler will tell and you will just remove private.

In this small example, it is likely that gameBody could be private.

When to use private?
 
 
Q