{"id":2596,"date":"2026-02-05T05:34:52","date_gmt":"2026-02-05T05:34:52","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2596"},"modified":"2026-02-05T05:35:08","modified_gmt":"2026-02-05T05:35:08","slug":"chapter-8-swift-comments","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-8-swift-comments\/","title":{"rendered":"Chapter 8: Swift Comments"},"content":{"rendered":"<h3 dir=\"auto\">1. Why do we write comments at all?<\/h3>\n<p dir=\"auto\">Comments are <strong>notes for humans<\/strong> \u2014 the compiler completely ignores them.<\/p>\n<p dir=\"auto\">Main reasons people write comments:<\/p>\n<ul dir=\"auto\">\n<li>Explain <strong>why<\/strong> something is done (not just what)<\/li>\n<li>Document <strong>public API<\/strong> so others (or future you) understand how to use it<\/li>\n<li>Temporarily disable code during debugging<\/li>\n<li>Add <strong>legal notices<\/strong> \/ license \/ copyright<\/li>\n<li>Mark <strong>sections<\/strong> in long files<\/li>\n<li>Leave <strong>TODOs<\/strong>, <strong>FIXMEs<\/strong>, <strong>warnings<\/strong><\/li>\n<\/ul>\n<h3 dir=\"auto\">2. The two main types of comments in Swift<\/h3>\n<h4 dir=\"auto\">Type A: Single-line comment \u2014 \/\/<\/h4>\n<p dir=\"auto\">Most common style \u2014 used everywhere.<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ This is a single-line comment\r\n\r\nlet name = \"Priya\"          \/\/ inline comment after code\r\n\r\n\/\/ You can write as many lines as you want\r\n\/\/ each starting with \/\/<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Very common patterns:<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ MARK: - View Life Cycle\r\noverride func viewDidLoad() {\r\n    super.viewDidLoad()\r\n    \/\/ Do any additional setup after loading the view.\r\n}\r\n\r\n\/\/ Configuration\r\nprivate func setupUI() {\r\n    \/\/ Colors\r\n    backgroundColor = .systemBackground\r\n    \r\n    \/\/ Fonts\r\n    titleLabel.font = .preferredFont(forTextStyle: .headline)\r\n    \r\n    \/\/ Layout constraints\r\n    NSLayoutConstraint.activate([\r\n        \/\/ ...\r\n    ])\r\n}\r\n\r\n\/\/ TODO: Handle offline mode later\r\n\/\/ FIXME: This calculation is wrong on leap years\r\n\/\/ WARNING: Do not call this on main thread!<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Type B: Multi-line comment \u2014 \/* &#8230; *\/<\/h4>\n<p dir=\"auto\">Used less often, but still important in specific cases.<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/*\r\nThis is a multi-line comment.\r\nIt can span several lines.\r\nVery useful for longer explanations.\r\n*\/<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Realistic examples where people actually use \/* *\/:<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/*\r\n    JSON response structure from API:\r\n    {\r\n        \"user\": {\r\n            \"id\": 1234,\r\n            \"name\": \"Aarav\",\r\n            \"email\": \"aarav@example.com\"\r\n        },\r\n        \"token\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...\"\r\n    }\r\n*\/<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/* \r\n   \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\r\n   \u2551              IMPORTANT NOTICE              \u2551\r\n   \u2551                                            \u2551\r\n   \u2551   Never commit real API keys to git!       \u2551\r\n   \u2551   Use environment variables or Secrets.    \u2551\r\n   \u255a\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255d\r\n*\/<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">3. Special comment styles that modern Swift developers love<\/h3>\n<h4 dir=\"auto\">3.1 \/\/ MARK: \u2013 Section headers (very very common in Xcode)<\/h4>\n<p dir=\"auto\">Xcode shows these in the jump bar (the dropdown above the editor).<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ MARK: - Properties\r\n\r\nprivate let apiClient = APIClient()\r\nprivate var userSession: UserSession?\r\n\r\n\/\/ MARK: - Public Methods\r\n\r\nfunc login(email: String, password: String) async throws {\r\n    \/\/ ...\r\n}\r\n\r\n\/\/ MARK: - Private Helpers\r\n\r\nprivate func validateInput() -&gt; Bool {\r\n    \/\/ ...\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">You can also add a separator line:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ MARK: - View Life Cycle -----------------------------------<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">3.2 Documentation comments \u2014 \/\/\/ (super important!)<\/h4>\n<p dir=\"auto\">These generate <strong>beautiful documentation<\/strong> when you Option-click a symbol in Xcode.<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/\/ Calculates the total price including tax.\r\n\/\/\/\r\n\/\/\/ - Parameters:\r\n\/\/\/   - subtotal: The price before tax (must be positive)\r\n\/\/\/   - taxRate: Tax rate as a percentage (0...100)\r\n\/\/\/ - Returns: The final amount to pay\r\n\/\/\/ - Throws: `PricingError.negativeSubtotal` if subtotal is negative\r\nfunc calculateTotal(subtotal: Double, taxRate: Double) throws -&gt; Double {\r\n    guard subtotal &gt;= 0 else {\r\n        throw PricingError.negativeSubtotal\r\n    }\r\n    \r\n    let tax = subtotal * (taxRate \/ 100)\r\n    return subtotal + tax\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">When someone Option-clicks this function in Xcode, they see:<\/p>\n<ul dir=\"auto\">\n<li>Summary<\/li>\n<li>Parameters table<\/li>\n<li>Returns<\/li>\n<li>Throws<\/li>\n<\/ul>\n<p dir=\"auto\">Very professional \u2014 almost every public function in good libraries uses \/\/\/.<\/p>\n<h4 dir=\"auto\">3.3 \/** &#8230; *\/ \u2014 old-style doc comments (still works, less common now)<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/**\r\n Calculates the area of a circle.\r\n \r\n - parameter radius: The radius of the circle\r\n - returns: Area in square units\r\n *\/\r\nfunc circleArea(radius: Double) -&gt; Double {\r\n    return .pi * radius * radius\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Most people now prefer \/\/\/ because it&#8217;s cleaner.<\/p>\n<h3 dir=\"auto\">4. Good habits &amp; bad habits (real developer advice)<\/h3>\n<p dir=\"auto\"><strong>Good habits:<\/strong><\/p>\n<ul dir=\"auto\">\n<li>Explain <strong>WHY<\/strong>, not just <strong>WHAT<\/strong> Bad: \/\/ add 1 to counter Good: \/\/ increment retry counter after failed network attempt<\/li>\n<li>Keep comments <strong>up to date<\/strong> \u2014 delete or fix outdated ones<\/li>\n<li>Use \/\/ MARK: to organize long files<\/li>\n<li>Use \/\/\/ for <strong>public<\/strong> functions, types, properties<\/li>\n<li>Use \/\/ TODO: \/ \/\/ FIXME: \/ \/\/ NOTE: consistently<\/li>\n<\/ul>\n<p dir=\"auto\"><strong>Bad habits (please avoid):<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ bad: useless comment\r\nlet x = 10                  \/\/ set x to 10\r\n\r\n\/\/ bad: comment that just repeats the code\r\nuser.age = user.age + 1     \/\/ increase age by 1\r\n\r\n\/\/ bad: lying comment (very dangerous!)\r\nlet isAdmin = true          \/\/ only for testing<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">5. Quick reference table \u2013 when to use each style<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"sm\">Style<\/th>\n<th data-col-size=\"md\">Syntax<\/th>\n<th data-col-size=\"xl\">Best used for<\/th>\n<th data-col-size=\"lg\">Frequency in modern code<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"sm\">\/\/<\/td>\n<td data-col-size=\"md\">Single line<\/td>\n<td data-col-size=\"xl\">inline notes, temporary disable, TODO\/FIXME<\/td>\n<td data-col-size=\"lg\">\u2605\u2605\u2605\u2605\u2605 (most common)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">\/\/ MARK:<\/td>\n<td data-col-size=\"md\">Section header<\/td>\n<td data-col-size=\"xl\">organizing code in files (shows in Xcode jump bar)<\/td>\n<td data-col-size=\"lg\">\u2605\u2605\u2605\u2605\u2605<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">\/\/\/<\/td>\n<td data-col-size=\"md\">Documentation<\/td>\n<td data-col-size=\"xl\">public API, functions, structs, properties<\/td>\n<td data-col-size=\"lg\">\u2605\u2605\u2605\u2605 (very important)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">\/* &#8230; *\/<\/td>\n<td data-col-size=\"md\">Multi-line block<\/td>\n<td data-col-size=\"xl\">long explanations, ASCII art notices, JSON examples<\/td>\n<td data-col-size=\"lg\">\u2605\u2605 (occasional)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">\/** &#8230; *\/<\/td>\n<td data-col-size=\"md\">Old doc block<\/td>\n<td data-col-size=\"xl\">legacy code or some open-source projects<\/td>\n<td data-col-size=\"lg\">\u2605 (rare now)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">6. Small realistic examples you can copy-paste<\/h3>\n<p dir=\"auto\"><strong>Example 1 \u2013 Clean function documentation<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/\/ Formats a phone number into international style.\r\n\/\/\/\r\n\/\/\/ Example: \"9876543210\" \u2192 \"+91 98765 43210\"\r\n\/\/\/\r\n\/\/\/ - Parameter rawNumber: Raw Indian phone number (10 digits)\r\n\/\/\/ - Returns: Formatted string or nil if invalid\r\nfunc formatIndianPhoneNumber(_ rawNumber: String) -&gt; String? {\r\n    \/\/ Remove all non-digits\r\n    let digits = rawNumber.filter { $0.isNumber }\r\n    \r\n    \/\/ Check length\r\n    guard digits.count == 10 else { return nil }\r\n    \r\n    \/\/ Format: +91 XXXXX XXXXX\r\n    let prefix = \"+91 \"\r\n    let part1 = String(digits.prefix(5))\r\n    let part2 = String(digits.dropFirst(5))\r\n    \r\n    return prefix + part1 + \" \" + part2\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Example 2 \u2013 Organized struct<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ MARK: - Models\r\n\r\nstruct UserProfile {\r\n    \/\/ MARK: - Properties\r\n    \r\n    let id: UUID\r\n    var name: String\r\n    var email: String?\r\n    \r\n    \/\/ MARK: - Computed Properties\r\n    \r\n    var displayName: String {\r\n        name.trimmingCharacters(in: .whitespacesAndNewlines)\r\n    }\r\n    \r\n    \/\/ MARK: - Initialization\r\n    \r\n    init(id: UUID = UUID(), name: String, email: String? = nil) {\r\n        self.id = id\r\n        self.name = name\r\n        self.email = email\r\n    }\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Would you like to continue with:<\/p>\n<ul dir=\"auto\">\n<li>Writing <strong>good documentation comments<\/strong> in more detail<\/li>\n<li>Using <strong>TODO \/ FIXME \/ MARK<\/strong> effectively<\/li>\n<li>Comment styles in <strong>SwiftUI<\/strong> vs <strong>UIKit<\/strong> vs <strong>server-side Swift<\/strong><\/li>\n<li>How to <strong>generate documentation<\/strong> from comments (DocC)<\/li>\n<li>Or move to another topic (variables, functions, printing, etc.)<\/li>\n<\/ul>\n<p dir=\"auto\">Just tell me what you\u2019d like next \u2014 we\u2019ll keep the same detailed, teacher-like style \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Why do we write comments at all? Comments are notes for humans \u2014 the compiler completely ignores them. Main reasons people write comments: Explain why something is done (not just what) Document public&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[],"class_list":["post-2596","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/comments?post=2596"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2596\/revisions"}],"predecessor-version":[{"id":2597,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2596\/revisions\/2597"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}