<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Henrik Forsberg</title>
    <description>The latest articles on DEV Community by Henrik Forsberg (@forsberg_dev).</description>
    <link>https://dev.to/forsberg_dev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F55398%2F72608c4b-5d73-485f-8615-678f9990ca4d.jpeg</url>
      <title>DEV Community: Henrik Forsberg</title>
      <link>https://dev.to/forsberg_dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/forsberg_dev"/>
    <language>en</language>
    <item>
      <title>Enhancing Readability: The Power of MARK Comments in Swift Development</title>
      <dc:creator>Henrik Forsberg</dc:creator>
      <pubDate>Tue, 12 Dec 2023 00:00:00 +0000</pubDate>
      <link>https://dev.to/forsberg_dev/enhancing-readability-the-power-of-mark-comments-in-swift-development-5bnj</link>
      <guid>https://dev.to/forsberg_dev/enhancing-readability-the-power-of-mark-comments-in-swift-development-5bnj</guid>
      <description>&lt;p&gt;When attempting to maintain our codebase it’s a good idea to have a clear and consistent way of structuring our code so it remains readable and easy to navigate. This becomes increasingly important as our codebase grows larger in size. There are several approaches and tools available for achieving this but the use of MARK comments is the one I want to highlight with this post.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a MARK comment and how do we use them?
&lt;/h2&gt;

&lt;p&gt;MARK comments - also known as "MARK lines" - are comments with special powers that are interpreted by Xcode a little differently compared to regular comments. They serve as a kind of labels within our code to help us organize it and keep it easy to navigate and readable in both our code and Xcode's jump bar and minimap. There are three different types we can use to annotate our code; &lt;code&gt;MARK&lt;/code&gt;, &lt;code&gt;TODO&lt;/code&gt; and &lt;code&gt;FIXME&lt;/code&gt;. Let's take a closer look at each one.&lt;/p&gt;

&lt;h3&gt;
  
  
  MARK
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;MARK&lt;/code&gt; type is probably the most commonly used type of MARK comments (hence the name). With it, we can add a label, a dividing line or both to annotate sections of our files and make them more organized.&lt;/p&gt;

&lt;h4&gt;
  
  
  MARK with only label
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// MARK: Properties&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;myConstant1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;myVariable1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This adds an icon and the label "Properties" to the jump bar: &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e4HeECB3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-label.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e4HeECB3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-label.png" alt="MARK comment with only label" width="390" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  MARK with only divider
&lt;/h4&gt;

&lt;p&gt;Sometimes we may just want to compartmentalize our code to simply make it clear that a chunk of code is separate, without adding labels. For this we can add a divider with the same &lt;code&gt;// MARK:&lt;/code&gt; followed by a dash:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;methodBelongingToFirstChunk&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="c1"&gt;// MARK: -&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;methodBelongingToAnotherChunk&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As expected, this shows a dividing line in the jump bar: &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vZRxSIkC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-divider.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vZRxSIkC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-divider.png" alt="MARK comment with only label" width="479" height="123"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  MARK with both divider and label
&lt;/h4&gt;

&lt;p&gt;As mentioned we can easily add both a divider and a label.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// MARK: - Private Methods&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;myPrivateMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;myOtherPrivateMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the jump bar, this places a divider before the label: &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--crkYE67P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-divider-and-label.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--crkYE67P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-divider-and-label.png" alt="MARK comment with only label" width="478" height="165"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The placement of the dash determines where the divider ends up so if we prefer the divider to be placed below the label we can simply put the dash at the end of the comment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// MARK: Private Methods -&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;myPrivateMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;myOtherPrivateMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VeKExig3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-divider-and-label-after.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VeKExig3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-divider-and-label-after.png" alt="MARK comment with only label" width="479" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When looking at the minimap, MARK comments provide a clear overview of the file: &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BXCAo68r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-minimap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BXCAo68r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-minimap.png" alt="MARK comment with only label" width="276" height="358"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Compared to not using MARK comments: &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NhOFq4di--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-no-mark.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NhOFq4di--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-no-mark.png" alt="MARK comment with only label" width="274" height="304"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  TODO and FIXME
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;TODO&lt;/code&gt; and &lt;code&gt;FIXME&lt;/code&gt; types work exactly the same (even with dashes) but display their own respective icons to the jump bar. Keep in mind, however, that these comments do not show on the minimap.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;myMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// TODO: Add implementation here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KJOYRn8x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-todo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KJOYRn8x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-todo.png" alt="MARK comment with only label" width="478" height="99"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;myOtherMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// FIXME: Make this make sense        &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JM9FAJgD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-fixme.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JM9FAJgD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://forsberg.dev/images/mark-fixme.png" alt="MARK comment with only label" width="477" height="103"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Structuring our code is always a good idea, especially when the files grow large. Keep in mind, though, that large files could be indicators that we need to refactor our code to split up the responsibilities into smaller ones and keep it more modular and more easily maintained. We should also not go overboard and start putting MARK comments on every other line, that would just clutter our files and defeat their purpose. Ultimately - as with so many things - it’s up to you and your team to find the right balance of how to use them.&lt;/p&gt;

&lt;p&gt;Personally I try to avoid merging code containing &lt;code&gt;FIXME&lt;/code&gt; or &lt;code&gt;TODO&lt;/code&gt; comments, as my experience is that they tend to be easily forgotten. If you find yourself in a situation where you &lt;em&gt;really&lt;/em&gt; need to merge your unfinished code (we all know it happens sometimes) with these types of comments, I highly recommend using a &lt;code&gt;#warning("")&lt;/code&gt; statement instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;myMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cp"&gt;#warning("TODO: Add implementation here")&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes the compiler help remind you about their existence by putting a warning about it in the Issues navigator.&lt;/p&gt;

</description>
      <category>swift</category>
      <category>readability</category>
      <category>xcode</category>
    </item>
  </channel>
</rss>
