TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

naming varibles, strucs, functions and the others

Forums > 100 Days of SwiftUI

Hi, Maybe this is a strange quiestion, not even allowed. But since I notice I struggle with it, I hope somebody can give some advice, tell how they do it. How do you give good names to the structs, classes, variables etc so you still know what is what if you look at your code after three weeks? I of course understand comments in the code help a lot. But giving logic names to everything also helps a lot. How longer the code gets how harder I find to create names.
In my current project, I posted a lot of the code in a other post https://www.hackingwithswift.com/forums/100-days-of-swiftui/day-47/22306 I have to many things that looks like the other that makes it hard to understand what I am doing. Especially since I'm still learning. At the same time I don't know other names to give that still will make sense.

2      

Hi @suzanne! You don't need to worry too much about it. It comes with time. I had the same issue. There is no one good rule for that, and every person will likely call things different ways. Just give names that are descriptive for youself and add comments if and where needed. E.g. like in your prevous post, look at possible names below.

struct HabitItem: Identifiable, Codable { // as this is distinct item we can call it this way 
    var id = UUID()
    var habitTitle: String // This will serve as a title of our habit
    var habitDescription: String // This is clear so no need to change
    var trackingDates: [String] // Or whatever is appropriate for you, as you save here the dates you track your habits

    mutating func addTrack(date: String) { // when you add you can say what you add there. e.g addTrackingDate()
        trackingDates.append(date)
    }

}

// Then you can continue

@Published var habitItems = [HabitItem]() {
        didSet {
            if var encoded = try? JSONEncoder().encode(habitItems) {
                UserDefaults.standard.set(encoded, forKey: "habitItems")
            }
        }
    }

@StateObject var viewModel = Habits()

// and when you call it in content it is more clear what is going on...

ForEach(viewModel.habitItem { habit in 
...

make it sound more like human language

@State private var title = ""
@State private var description = ""

// and then when you create the habit it will sound more like you speak

var habit = HabitItem(habitTitle: title, habitDiscription: description

so hope it will guide you a bit.

2      

Thank you.
I think the adding more commentes is very important. Often writing something is try and error. Often I think I will write the comments once it it working. But if it works I just want to continu and never write the comments.. Naming things are often also just something during testing. I often have something like "var blaat" during trying things and even almost forgot to change that the moment it worked.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.