Wednesday, April 09, 2025

A Conversation with ChatGPT

Recently, I had a very good conversation with ChatGPT about a topic that often goes unnoticed: how to name a variable in a Swift class in my iOS app. This isn't the kind of question you typically search for on Google or post on StackOverflow (where you might have to wait a day or longer for a response). Yet, I was stuck trying to find a meaningful name—one that clearly conveys its purpose and is easy to recognize later during maintenance.

My Initial Question

ChatGPT came to mind. I explained why I needed this variable, and with some context, I hoped it would suggest a good name. Here's my original question:

I have a main view with some tab bars, such as Edit, Tap, and Report. The main view is the starting point. I would like to define an enum type to indicate where the starting point is, for example, TabBarStatus. I also like to define a variable for the status. What names should I use for the enum and variable? The variable will be used in subsequent views to find out where the start view is.

Less than a second later, ChatGPT responded with a thoughtful explanation and code examples.

For your use case, where you want to track which tab was the starting point (Edit, Tap, Report, etc.), defining an enum and a variable to hold that state is a great approach. Here's a naming suggestion that is clear, consistent with Swift/iOS conventions, and scalable:

🧩 Enum Name

Use a descriptive enum name that reflects the context:

  1. enum MainTab {
  2.  case edit
  3.  case tap
  4.  case report
  5. }

Alternative names:

  • StartTab
  • MainTabType
  • TabBarStatus (your suggestion, but slightly less Swift-like)

Swift style prefers concise, noun-based enum names like MainTab or StartTab.

🧩 Variable Name

For a variable that stores the starting tab, here are a few clean suggestions:

  • startingTab: MainTab
  • originTab: MainTab
  • entryTab: MainTab
  • launchTab: MainTab
  • startFromTab: MainTab (if used in routing context)
✅ Example Usage
  1. enum MainTab {
  2.  case edit
  3.  case tap
  4.  case report
  5. }
  6. struct ViewModel {
  7.  var startingTab: MainTab
  8. }

My Follow-Up

Just like talking to a senior developer, I continued the conversation and explained more about the context and how this variable would be used:

Good. Another question I need your advice on. All main view controllers and subsequent view controllers (VCs) are based on a base view controller, BaseViewController: UIViewController. All the subsequent VCs are presented as detail views, modal views, etc. I would like to define a public variable in BaseViewController, and its value is set in the main view controllers. I think this var should be static, instead of an instance variable. The reason is that there is only one starting tab. Right?

I was hoping ChatGPT could offer a better design based on my explanation. To my surprise, ChatGPT not only understood my design but also laid out a solid analysis of how the design could be improved.

ChatGPT's instant reply

Great question—and you're thinking in the right direction! Let's break it down and figure out the best approach.


🧠 Your Goal

You want all view controllers (which inherit from BaseViewController) to know where the user started (i.e., which main tab). There's only one starting tab per app session, and this info is set by the main view controller.


✅ Static vs Instance Variable
  • Static variable (static var or class var) is shared across all instances of BaseViewController. Perfect for global, app-wide state.
  • Instance variable (var) is unique to each instance, so you'd need to pass it manually to every view controller—probably not what you want here.

👉 Since there's only one starting tab, and you want all view controllers to access the same value, a static variable is the right choice.


✅ Suggested Implementation
  1. class BaseViewController: UIViewController {
  2.  // Default value, set from main controller
  3.  static var startingTab: MainTab = .tap
  4. }

Then, in your main tab view controllers (e.g. EditViewController, TapViewController, etc.), you set it like this:

  1. override func viewDidLoad() {
  2.  super.viewDidLoad()
  3.  BaseViewController.startingTab = .edit  // or .tap or .report
  4. }

And any other view controller can read it like:

  1. let origin = BaseViewController.startingTab


✅ Optional: Make It a Computed Property (if you want read-only from others)
  1. class BaseViewController: UIViewController {
  2.  private static var _startingTab: MainTab = .tap
  3.  public class var startingTab: MainTab {
  4.    get { return _startingTab }
  5.    set { _startingTab = newValue }
  6.  }
  7. }

Let me know if you also want to persist this value (e.g. between launches) or just keep it in-memory for one app session.

A Better Design

During this very natural and productive conversation, three concepts caught my attention: state, persistence, and computed property. They suddenly lit up my thinking like a flash of inspiration. A new design came to mind, and I shared it with ChatGPT:

There may be some cases where a subsequent view is not inherited from BaseViewController. I think it may be better to define a static class, such as MainTabStatus (or maybe a better name?). Within the class, a private var _startingTab and a public computed property startingTab can be used to manage the starting tab status. This class can be extended to include mechanisms to store the status in memory or user settings (persisting across app launches). This helper class can then be accessed from any view controller, view, or class to retrieve the starting point status.

Very quickly, I got feeback from ChatGPT.

Yes, that’s a great idea—and a cleaner and more modular design! Creating a dedicated MainTabStatus (or similarly named) helper class decouples the logic from your view controllers entirely, making it more flexible, easier to test, and extendable for future needs (like persistence).


✅ Suggested Design
  1. enum MainTab {
  2.  case edit
  3.  case tap
  4.  case report
  5. }
  6. final class MainTabStatus {
  7.  private static var _startingTab: MainTab = .tap  // default value
  8.  public static var startingTab: MainTab {
  9.    get { return _startingTab }
  10.    set { _startingTab = newValue }
  11.  }
  12.  // Optional: Save to UserDefaults if needed
  13.  public static func persist() {
  14.    UserDefaults.standard.set(_startingTab.rawValue, forKey: "StartingTab")
  15.  }
  16.  public static func restore() {
  17.    if let raw = UserDefaults.standard.string(forKey: "StartingTab"),
  18.       let tab = MainTab(rawValue: raw) {
  19.          _startingTab = tab
  20.    }
  21.  }
  22. }

🔧 For the UserDefaults part to work, you'd make MainTab conform to RawRepresentable (usually with String), like this:

✅ Suggested Design
  1. enum MainTab : String {
  2.  case edit
  3.  case tap
  4.  case report
  5. }

✅ Usage in Any View Controller

Set at startup:

  1. MainTabStatus.startingTab = .edit

Read anywhere:

  1. let origin = MainTabStatus.startingTab

Restore from UserDefaults (if persisted):

  1. MainTabStatus.restore()

🧠 Alternative Names for MainTabStatus

If you want something shorter or more Swift-y:

  • AppNavigationContext
  • StartContext
  • OriginTab
  • TabTracker
  • MainTabTracker

MainTabStatus is already clear, so it's a solid choice.


Let me know if you'd like this helper to also handle notifications (e.g., broadcast when tab changes), or if you want a sample using @AppStorage (SwiftUI), too!

Wow! What a great set of suggestions from ChatGPT—starting from a simple question about naming a variable, and evolving into a much better and scalable design.

Other Blogs

0 comments: