I prefer the coding style below. (Of course, the code below is not long, but when the number of lines is long)
struct ContentView: View {
var body: some View {
VStack {
text
hello
}
}
var text: some View {
Text("text")
.padding()
}
var hello: some View {
Button("hello") {
print("hello")
}
}
}
But people seem to prefer the style below.(Not only when the number of lines of code is small, but also when it is long)
struct ContentView: View {
var body: some View {
VStack {
Text("text")
.padding()
Button("hello") {
print("hello")
}
}
}
}
Which coding style is more popular among the two?
As darkpaw said, in such simple example it is overkill.
But if the view (text) is more complex, that could make sense.
Problem is also the first code is not very readable. What is text ? what is hello. Really confusing.
So, when I need to split code in smaller chunks (which is a good practice),, I prefer to declare as Struct and give explicit names. And only do it when required (so it is useless for hello, unless code is really long.
struct ContentView: View {
var body: some View {
VStack {
MyTextView()
helloButton
}
}
struct MyTextView: View {
var body: some View {
HStack {
Text("text")
.padding()
Text("More")
// And more
}
}
}
var helloButton: some View {
Button("Hello") {
print("Hello")
// and a lot more code in tha action
}
}
}