<?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>Forem: Kaww</title>
    <description>The latest articles on Forem by Kaww (@kaww).</description>
    <link>https://forem.com/kaww</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%2F924454%2F331f4f6e-2901-4193-9031-b8606cdd4fdf.jpg</url>
      <title>Forem: Kaww</title>
      <link>https://forem.com/kaww</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kaww"/>
    <language>en</language>
    <item>
      <title>Clean Code - Improve your code comments</title>
      <dc:creator>Kaww</dc:creator>
      <pubDate>Mon, 01 May 2023 12:46:07 +0000</pubDate>
      <link>https://forem.com/kaww/clean-code-improve-your-code-comments-blh</link>
      <guid>https://forem.com/kaww/clean-code-improve-your-code-comments-blh</guid>
      <description>&lt;p&gt;Robert C. Martin, often called Uncle Bob, is an American software engineer well known for his books on &lt;strong&gt;Clean Code&lt;/strong&gt; and &lt;strong&gt;Clean Architecture&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This article is one of a series that I am writing as an introduction to Clean Code. It’s made of notes that I wrote while watching a series of video lessons that Robert gave about clean code. This one will focus on tips about code comments, from the second episode.&lt;/p&gt;

&lt;p&gt;You can find the &lt;a href="https://www.youtube.com/watch?v=7EmboKQH8lM&amp;amp;list=PLmmYSbUCWJ4x1GO839azG_BBw8rkh-zOj"&gt;6 episodes' Youtube playlist here&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Code comments
&lt;/h1&gt;

&lt;p&gt;What’s the purpose of a comment? The purpose of a comment is to explain the purpose of code if the code can’t explain its own purpose.&lt;/p&gt;

&lt;p&gt;Code can explain its purpose by itself. But it did not used to be the case: Fortran for instance have only 6 chars names for variables.&lt;/p&gt;

&lt;p&gt;Today's languages are remarkably rich: we can have very long names, we have structs, classes, namespaces, etc. We have all the tools that we need to write code that explains itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Not all comments are bad
&lt;/h2&gt;

&lt;p&gt;Nothing can be quite so helpful as a good comment.&lt;/p&gt;

&lt;p&gt;Nothing can be quite so obscuring as a bad comment.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;→ Comments are not “pure good”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Every comment you write is a failure
&lt;/h2&gt;

&lt;p&gt;The proper use of comments is to compensate for our failure to express ourselves in code. And then because we technically can express ourselves in code, we don’t need comments.&lt;/p&gt;

&lt;p&gt;Every use of a comment represents a failure. So don’t comment first, try everything else then comment as a last resort.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;→ When you write bad code, don’t comment it, clean it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The right colour of comments
&lt;/h2&gt;

&lt;p&gt;Comments lie. Not intentionally, but as the code change, most of the time they don’t, because they are often ignored, and no one maintains them.&lt;/p&gt;

&lt;p&gt;Comments are often in a soft and invisible colour. Change IDE settings to paint comments in bright red, so that they will be very visible. Also, this being quite ugly, it may help you to avoid writing useless comments.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;→ Change IDE settings to paint comments in &lt;strong&gt;bright red&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Examples of bad comments
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Comments that explain complex conditions or loop statements. Instead, use explanatory variables.&lt;br&gt;&lt;br&gt;
Read more about explanatory variables &lt;a href="https://kaww.hashnode.dev/what-is-clean-code"&gt;in the previous article&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Comments that reference code elsewhere (ex: &lt;code&gt;\\ at this moment, {other code} should have been called&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Redundant comments that takes more time to read than time to understand the code itself.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Noise comments. For instance, comments explaining stuff that developers already know, or comments that have been copy-pasted with mistakes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mandatory comments. When you force developers to write comments you get stupid and useless comments. For instance, documentation comments that list all parameters of a function. Documentation tools are already smart enough to do that.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Examples of acceptable comments
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Copyrights or Legal boilerplate code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When a name is not clear but it’s named on purpose (ex: when using a design pattern or a framework), you can comment to give more context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Informative comment: date format, regular expression, etc. But this is a &lt;strong&gt;double edge knife&lt;/strong&gt;: this is very helpful while you update the comment when the code change (if not, the comment will be lying).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Warning of consequences: a function that takes time to run, or that is not thread-safe, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;TODO&lt;/code&gt; comments: a &lt;code&gt;TODO&lt;/code&gt; means “&lt;em&gt;don’t do&lt;/em&gt;”. You can write &lt;code&gt;TODO&lt;/code&gt; comments but don’t commit them to Git: they have to be done. If not, they will become “don’t do” comments. A much better option if you can’t do a thing now, is to use the ticket system of your project (Jira, Notion, Trello, …) and create a technical task.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Amplification: when something is clear enough but it needs to stand out.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Documentation markups ONLY if made for external APIs. It’s acceptable for public things that are gonna be exposed to external developers. If you’re writing code that only the team is gonna see, you don't need comments, because the team knows the system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Commented out code. If a code is not needed, remove it. You can comment code while working on something, but don’t commit it to Git. And if you are scared to lose some code, don't forget that it will stay in the Git history.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Wrap up
&lt;/h1&gt;

&lt;p&gt;Here were my few notes on writing good comments from Uncle Bob’s lesson on Clean Code.&lt;/p&gt;

&lt;p&gt;You can find my other notes on the next lessons here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://kaww.hashnode.dev/what-is-clean-code"&gt;Lesson 1: What is Clean Code?&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://kaww.hashnode.dev/clean-code-function-and-variable-names"&gt;Lesson 2: Function and variable names.&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;I hope this article has been helpful to you. If you have any questions or feedback about this article, don’t hesitate to contact me on&lt;/em&gt; &lt;a href="https://twitter.com/kaww"&gt;&lt;strong&gt;&lt;em&gt;Twitter&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;em&gt;!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Clean Code - Function and variable names</title>
      <dc:creator>Kaww</dc:creator>
      <pubDate>Mon, 01 May 2023 12:43:31 +0000</pubDate>
      <link>https://forem.com/kaww/clean-code-function-and-variable-names-3ohl</link>
      <guid>https://forem.com/kaww/clean-code-function-and-variable-names-3ohl</guid>
      <description>&lt;p&gt;Robert C. Martin, often called Uncle Bob, is an American software engineer well known for his books on &lt;strong&gt;Clean Code&lt;/strong&gt; and &lt;strong&gt;Clean Architecture&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This article is one of a series that I am writing as an introduction to Clean Code. It’s made of notes that I wrote while watching a series of video lessons that Robert gave about clean code. This one will focus on &lt;strong&gt;tips for function and variable names&lt;/strong&gt;, from the first and second episodes.&lt;/p&gt;

&lt;p&gt;You can find the &lt;a href="https://www.youtube.com/watch?v=7EmboKQH8lM&amp;amp;list=PLmmYSbUCWJ4x1GO839azG_BBw8rkh-zOj"&gt;6 episodes' Youtube playlist here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Names
&lt;/h2&gt;

&lt;p&gt;Names are everywhere: files, directories, programs, classes, variables, methods, arguments,&lt;/p&gt;

&lt;p&gt;We then have to be good at naming!&lt;/p&gt;

&lt;p&gt;Most of the naming rules in Bob’s videos are from &lt;a href="https://exelearning.org/wiki/OttingersNaming/"&gt;Tim Ottinger’s rules for variable and class naming&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function names should be verbs
&lt;/h2&gt;

&lt;p&gt;You don’t want nouns as function names. Function names should be verbs because functions do things.&lt;/p&gt;

&lt;p&gt;Functions names should explain clearly what it’s doing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;→ Prefer using &lt;strong&gt;verbs&lt;/strong&gt; for function names.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Explanatory variables
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://kaww.hashnode.dev/what-is-clean-code"&gt;Described in the previous article.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Some variables should exist and be named only to explain a statement. You remove the complexity of a statement (an &lt;code&gt;if&lt;/code&gt; for instance) by explaining it with the name of the variable.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;→ Extract complex statements into well-named variables.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Reveal your intent
&lt;/h2&gt;

&lt;p&gt;A name that requires a comment is a bad name. The name of a variable should tell us the significance of what the variable contains.&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;// BAD&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;d&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="c1"&gt;// elapsed time in days&lt;/span&gt;

&lt;span class="c1"&gt;// GOOD&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;elasedTimeInDays&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Size of names
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Rules for a &lt;strong&gt;variable&lt;/strong&gt; name size: a variable name should be proportional to the size of the scope that contains it :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the scope is very small, a single letter is fine (ex: for loops with &lt;code&gt;i&lt;/code&gt; and &lt;code&gt;j&lt;/code&gt; variables, you don’t want them to be any longer).&lt;/li&gt;
&lt;li&gt;Inside &lt;code&gt;if&lt;/code&gt; statement, loops or small functions, it should be very short, like a word.&lt;/li&gt;
&lt;li&gt;In a class, instance variables have a larger scope, it should be longer, like two words.&lt;/li&gt;
&lt;li&gt;Global variables have a huge scope, it should be very long to be specific.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example: a variable storing an animation duration in a very short and specific method can be named &lt;code&gt;duration&lt;/code&gt; because the context already explains it. But the same variable in a global scope should be more detailed, like &lt;code&gt;headerBounceAnimationDuration&lt;/code&gt;.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Rules for a &lt;strong&gt;function&lt;/strong&gt; name size: the exact opposite. The bigger the scope, the smaller the name.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We would not want to call the &lt;code&gt;open&lt;/code&gt; function if it was called &lt;code&gt;openFileAndThrowExceptionIfNotFound&lt;/code&gt;. We want a large scope function name to shrink because we are going to call it more, and it must be dealing with a high level of abstraction.&lt;/li&gt;
&lt;li&gt;An instance function of a class should be a bit more specific, it will probably have a slightly longer name.&lt;/li&gt;
&lt;li&gt;A private function called by a public function will have a longer name (see &lt;a href="https://kaww.hashnode.dev/what-is-clean-code#heading-the-good-size-for-a-function"&gt;this article on clean code about extracting code&lt;/a&gt;). Then a private function called by a private function will have an even longer name…&lt;/li&gt;
&lt;li&gt;Names get longer because functions become more precise. It does something very tiny and precise.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Rules for a &lt;strong&gt;class&lt;/strong&gt; name size: same as functions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Public classes have one-word names.&lt;/li&gt;
&lt;li&gt;Subclasses have longer names.&lt;/li&gt;
&lt;li&gt;Inner classes have even longer names…&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;→ For &lt;strong&gt;variables&lt;/strong&gt;, the smaller the scope, the smaller the name.&lt;/p&gt;

&lt;p&gt;→ For &lt;strong&gt;functions&lt;/strong&gt; and &lt;strong&gt;classes&lt;/strong&gt;, the bigger the scope, the smaller the name.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  A good system of names
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;getThem&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;list1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;]()&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is not complicated, but it’s not explicit. It reveals no intent.&lt;br&gt;&lt;br&gt;
The names do not explicitly reveal the context of the problem being solved.&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;func&lt;/span&gt; &lt;span class="nf"&gt;getFlaggedCells&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;flaggedCells&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;]()&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;cell&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;gameBoardCells&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cell&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kt"&gt;CELL_STATUS&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;FLAGGED&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;flaggedCells&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cell&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;flaggedCells&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here only the names have changed. This naming system makes it easier to understand what the function does.&lt;/p&gt;

&lt;p&gt;It also tells what program contains the function by giving context: this program is probably a game, a Minesweeper.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;→ A good system of names tells you about the function you’re working on, but also about the entire context of the system.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Disambiguate
&lt;/h2&gt;

&lt;p&gt;Code is full of optical illusions, and you have to fight against that.&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;func&lt;/span&gt; &lt;span class="kt"&gt;XYZControllerForEfficientHandlingOfStrings&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;StringController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="kt"&gt;XYXControllerForEfficientStorageOfStrings&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;StringController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Would you pick the right function when using the code completion of your IDE?&lt;br&gt;&lt;br&gt;
Sometimes symbols can differ by only a few pixels.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;→ Use the right names to disambiguate the situation.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Parameters with numbers
&lt;/h2&gt;

&lt;p&gt;Do not use parameters with numbers, those are confusing:&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;func&lt;/span&gt; &lt;span class="nf"&gt;copyChars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;str1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;str2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, name the parameters to clarify the intent:&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;func&lt;/span&gt; &lt;span class="nf"&gt;copyChars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;→ Instead of adding numbers to parameters, &lt;strong&gt;name them by their roles&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Use Pronounceable names&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;if you can’t pronounce it, you can’t discuss it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Wrap up
&lt;/h1&gt;

&lt;p&gt;Here were my few notes on naming from Uncle Bob’s lesson on Clean Code.&lt;/p&gt;

&lt;p&gt;You can find my other notes on Uncle Bob's lessons here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://kaww.hashnode.dev/what-is-clean-code"&gt;Lesson 1: What is Clean Code?&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://kaww.hashnode.dev/clean-code-improve-your-code-comments"&gt;Lesson 2: Improve your code comments&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;I hope this article has been helpful to you. If you have any questions or feedback about this article, don’t hesitate to contact me on&lt;/em&gt; &lt;a href="https://twitter.com/kaww"&gt;&lt;strong&gt;&lt;em&gt;Twitter&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;em&gt;!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>architecture</category>
    </item>
    <item>
      <title>What is Clean Code?</title>
      <dc:creator>Kaww</dc:creator>
      <pubDate>Mon, 01 May 2023 12:39:51 +0000</pubDate>
      <link>https://forem.com/kaww/what-is-clean-code-4i1n</link>
      <guid>https://forem.com/kaww/what-is-clean-code-4i1n</guid>
      <description>&lt;p&gt;Robert C. Martin, often called Uncle Bob, is an American software engineer well known for his books on &lt;strong&gt;Clean Code&lt;/strong&gt; and &lt;strong&gt;Clean Architecture&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This article is the first of a series that I am writing as an introduction to Clean Code. It’s made of notes that I wrote while watching a series of video lessons that Robert gave about clean code. This one will focus on the first episode, which is an introduction to Clean Code through a few concepts that are important to know as a software engineer.&lt;/p&gt;

&lt;p&gt;You can find the &lt;a href="https://www.youtube.com/watch?v=7EmboKQH8lM&amp;amp;list=PLmmYSbUCWJ4x1GO839azG_BBw8rkh-zOj"&gt;6 episodes' Youtube playlist here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quotes from his friends
&lt;/h2&gt;

&lt;p&gt;Here are some quotes about Clean Code that Robert’s friends gave to him.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bjarne Stroustrup (C++):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;“&lt;em&gt;I like my code to be elegant and efficient&lt;/em&gt;”.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;”&lt;em&gt;Clean code does one thing well&lt;/em&gt;”&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Grady Booch (Object Oriented Software Engineering, UML, …):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“&lt;em&gt;Clean code is simple and direct. Clean code reads like well-written prose…&lt;/em&gt;”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Uncle Bob (Clean Code, Agile Software Development, …):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;“&lt;em&gt;You must write code that other people that maintain. If you hand me code that works perfectly but I can’t understand it, as soon as the requirements changes, that code is useless. But if you give me code that doesn’t work but I can understand it, I can make it work.&lt;/em&gt;”&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;“&lt;em&gt;It's much more important to write code that your peers can understand, than that the computer can understand&lt;/em&gt;”&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Ward Cunningham (Wiki concept inventor):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“&lt;em&gt;You know you are working on clean code when each routine you read turns out to be pretty much what you expected...&lt;/em&gt;”&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why is Clean Code important?
&lt;/h2&gt;

&lt;p&gt;Society works with software.&lt;/p&gt;

&lt;p&gt;In our society today, it’s impossible to go more than 60 seconds without interacting with a software system: you can’t use a phone, watch TV, wash your clothes, you can’t buy or sell anything, …&lt;/p&gt;

&lt;p&gt;Even your car can’t work without software: GPS, entertainment system, engine, and brakes. When you press on the breaks, there is code.&lt;/p&gt;

&lt;p&gt;And who writes that code, that’s going to decide whether or not to stop the car when you push on the break? We write that code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why are programmers so slow?
&lt;/h2&gt;

&lt;p&gt;Software teams want to go fast:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;They write a lot of features fast at a beginning of a new project, so they make a mess. And as the mess grows they get slower.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So they need to hire more people to keep going fast. The old code teaches the new people, which continues to make a mess.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We are slow because we make a mess.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;→ To work fast, work well. You are not done when the code works, you are done when it’s clean! The only way to go fast is to go well.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Function names
&lt;/h2&gt;

&lt;p&gt;You don’t want nouns as function names. Function names should be verbs because functions do things.&lt;/p&gt;

&lt;p&gt;Functions names should explain clearly what it’s doing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;→ Read more on &lt;a href="https://kaww.hashnode.dev/clean-code-function-and-variable-names"&gt;my article on methods and variable naming&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Polite code: don’t be rude!
&lt;/h2&gt;

&lt;p&gt;You have to care about developers that will read your code. So you have to make your code easy to read and understand. This is polite.&lt;/p&gt;

&lt;h2&gt;
  
  
  The code should read like an article
&lt;/h2&gt;

&lt;p&gt;When reading the first methods names of a class (for instance the public methods) and their few lines of code, you should be able to understand in a few seconds what this code is about.&lt;/p&gt;

&lt;p&gt;Then if you’re interested and want to go deeper into the implementation, you can continue with the next methods (for instance the private ones). You should not have to read all the code to understand what a class is doing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;→ Start with short high-level code. Code’s level is lower as you dive deeper into it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It allows the reader to exit early.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The good size for a function
&lt;/h2&gt;

&lt;p&gt;The good size for a function is when you can’t extract any other function from it.&lt;br&gt;&lt;br&gt;
This can be a bit extreme but the principle is still the same: a function should read easily.&lt;/p&gt;

&lt;p&gt;So if when reading you alternate between high-level code and low-level code (like doing complex manipulations on a string and then calling a global method with this string), there is probably code that you should extract.&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;// high level code: publishing an article&lt;/span&gt;
&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;publishArticle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;rawText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;markdown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;convertToMarkdown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;rawText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rawText&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uploadMarkdownText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;markdown&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;markdown&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;convertToMarkdown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;rawText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;MarkDownObject&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// low level code: text conversion&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;uploadMarkdownText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;markdown&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;markdown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// low level code: network call&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;→ Every line of code in a method should be on the same level of abstraction, and that level should start right below the function name.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Explanatory variables
&lt;/h2&gt;

&lt;p&gt;This is the same principle about code readability but in a deeper scope. Some code can contain complex statements that should be extracted into variables.&lt;/p&gt;

&lt;p&gt;Reading those variable names may help the reader to understand the code faster.&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;// complexe if statemment&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isAdmin&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"france"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;job&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"developer"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// explanatory variable&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;isAdminOrFrenchDev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isAdmin&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"france"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;job&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"developer"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;isAdminOrFrenchDev&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;→ Use explanatory variables. Their only purpose is to &lt;strong&gt;explain&lt;/strong&gt; what its content is. It extracts code complexity to a “&lt;em&gt;well-written prose”&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Number of arguments in a function
&lt;/h2&gt;

&lt;p&gt;Reading a long list of coma-separated arguments is rude!&lt;br&gt;&lt;br&gt;
You probably never need to pass more than 2 or 3 things as arguments.&lt;/p&gt;

&lt;p&gt;If you need more, like 6 things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Those arguments are probably so tightly linked to each other that they should be an object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Or probably some code can be extracted into 2 separate methods that only need 3 arguments each.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;→ Keep the number of arguments down to 2 or 3. If you need more use other strategies: create objects, extract code, …&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Don’t pass booleans as arguments
&lt;/h2&gt;

&lt;p&gt;A function that takes a boolean as an argument will probably contain an &lt;code&gt;if&lt;/code&gt; statement, that ends up with two branches in that function, for the &lt;code&gt;if&lt;/code&gt; and the &lt;code&gt;else&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Why not just separate them into 2 different functions? Call the one in the &lt;code&gt;true&lt;/code&gt; case, and call the other in the &lt;code&gt;else&lt;/code&gt; case.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;→ Boolean arguments are often useless and can be replaced by directly calling specific methods.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Avoid switch statements
&lt;/h2&gt;

&lt;p&gt;Let’s say we have a switch statement that switches on a &lt;code&gt;Shape&lt;/code&gt; enum that declares &lt;code&gt;Circles&lt;/code&gt;, &lt;code&gt;Squares&lt;/code&gt;, etc.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;How many switch statements do we have in our application? As many as we want to draw a shape, rotate a shape, move a shape, …&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now what happens when you create a new type of &lt;code&gt;Shape&lt;/code&gt;? You have to go through the all code to find those switch statements to add a new case each time. &lt;strong&gt;This is fragile!&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What we want is &lt;strong&gt;polymorphism&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We create a &lt;strong&gt;base class&lt;/strong&gt; &lt;code&gt;Shape&lt;/code&gt; (or an interface).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then we create all the &lt;strong&gt;subclasses&lt;/strong&gt; we need (&lt;code&gt;Circle&lt;/code&gt;, &lt;code&gt;Square&lt;/code&gt;, …) and put those &lt;code&gt;draw()&lt;/code&gt;, &lt;code&gt;rotate()&lt;/code&gt; and move methods in it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now what changes when we add a new &lt;code&gt;Shape&lt;/code&gt;? We just have to create a new subclass. Nothing else change in our system.&lt;br&gt;&lt;br&gt;
&lt;em&gt;Note: This is the “open-closed principle” (the “O” of the “SOLID” principles), which means you can add new functionalities to the system, without changing the existing code.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;→ If you write the same switch statement multiple times, you should probably use &lt;strong&gt;polymorphism&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Side effects
&lt;/h2&gt;

&lt;p&gt;What is a side effect? It’s a change in the state of the system. If you call a function that causes the system to change state, that function has a side effect.&lt;/p&gt;

&lt;p&gt;For instance, some functions come in pairs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;File management with open and close.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Memory management with allocation and free.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Semaphores with lock and release.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those functions cause side effects. If we forget to call the second function the state of the system stay changed.&lt;/p&gt;

&lt;p&gt;A solution to prevent that can be to wrap those pairs of functions in a new one that doesn't have side effects.&lt;/p&gt;

&lt;p&gt;Here is an example of a custom high-level open method in Swift that prevent side effects.&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;func&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;lambda&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;File&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// low level open method from the system&lt;/span&gt;
    &lt;span class="nf"&gt;lambda&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// low level close method from the system&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// calling this function doesn't cause any side effect&lt;/span&gt;
&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* use the file... */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;→ Write code that has as few side effects as possible.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Command and query separation
&lt;/h2&gt;

&lt;p&gt;Commands are methods that change the state of the system. Therefore they return &lt;code&gt;Void&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
And by convention, a method that returns a value doesn’t have side effects.&lt;/p&gt;

&lt;p&gt;Then when you see a function that returns a value, you know that is safe to call, it will leave the system in the same state.&lt;/p&gt;

&lt;p&gt;If you need to return an error, instead use exceptions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;→ Follow this convention to write more consistent code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  DRY: Don’t Repeat Yourself
&lt;/h2&gt;

&lt;p&gt;We don’t like duplicated code. And we don’t like code that is kind of duplicated code.&lt;/p&gt;

&lt;p&gt;If you repeat yourself, prefer placing that code into functions. And if the repeated code changes a bit sometimes, you may want to use some sort of argument in your function to make it more flexible.&lt;/p&gt;

&lt;p&gt;But what happens if it’s not the code that is repeated but the code structure, like loops?&lt;/p&gt;

&lt;p&gt;For instance, let's say you have some sort of complex configuration data structure, and to work with it you have a big nested bunch of loops and if statements, then you finally get to the end node and you’ve got some processing code.&lt;/p&gt;

&lt;p&gt;And then you see that same loop repeating inside different parts of the system.&lt;/p&gt;

&lt;p&gt;Here again, you can use lambdas. You can put that looping structure into a function that takes a lambda as argument and then you pass the processing code into the lambda when calling that function. You can take inspiration from the way functions like &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;flatMap&lt;/code&gt; pr &lt;code&gt;compactMap&lt;/code&gt; works.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;→ Don’t repeat yourself.&lt;br&gt;&lt;br&gt;
Use &lt;strong&gt;functions&lt;/strong&gt; to centralise repeated code.&lt;br&gt;&lt;br&gt;
Use &lt;strong&gt;lambdas&lt;/strong&gt; to centralise repeated code structures like complex loops.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Wrap up
&lt;/h1&gt;

&lt;p&gt;Here were my few notes on this first lesson on Clean Code.&lt;/p&gt;

&lt;p&gt;You can find my other notes on Uncle Bob's lessons here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Lesson 2&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://kaww.hashnode.dev/clean-code-function-and-variable-names"&gt;Functions and variable names&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://kaww.hashnode.dev/clean-code-improve-your-code-comments"&gt;Improve your code comments&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;I hope this article has been helpful to you. If you have any questions or feedback about this article, don’t hesitate to contact me on&lt;/em&gt; &lt;a href="https://twitter.com/kaww"&gt;&lt;strong&gt;&lt;em&gt;Twitter&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;em&gt;!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>architecture</category>
    </item>
    <item>
      <title>The Observer Design Pattern in Swift</title>
      <dc:creator>Kaww</dc:creator>
      <pubDate>Wed, 08 Mar 2023 17:31:47 +0000</pubDate>
      <link>https://forem.com/kaww/the-observer-design-pattern-in-swift-2ee3</link>
      <guid>https://forem.com/kaww/the-observer-design-pattern-in-swift-2ee3</guid>
      <description>&lt;p&gt;In this article, we will learn what is the Observer design pattern and how to use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The use case
&lt;/h2&gt;

&lt;p&gt;Let’s say we have a weather station that regularly takes measures of temperature and humidity:&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;struct&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Equatable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;humidity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;WeatherStation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Timer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;lastMeasurement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Timer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scheduledTimer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;withTimeInterval&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;repeats&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;block&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;weak&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;measureWeather&lt;/span&gt;&lt;span class="p"&gt;()&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;measureWeather&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Simulate new data&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;newData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;in&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nv"&gt;humidity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;in&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;newData&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;lastMeasurement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lastMeasurement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newData&lt;/span&gt;
        &lt;span class="p"&gt;}&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;And two screens that are connected to the station, one displaying temperature, the other one displaying humidity:&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;struct&lt;/span&gt; &lt;span class="kt"&gt;TemperatureScreen&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Temperature is now &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;HumidityScreen&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;humidity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Humidity is now &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;humidity&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&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;→ How would we allow the several screens to update their information when the weather changes?&lt;/p&gt;

&lt;h2&gt;
  
  
  Polling the data?
&lt;/h2&gt;

&lt;p&gt;One solution that can come to mind would be that a screen can regularly ask the weather station for new data, for instance, every 100 milliseconds.&lt;/p&gt;

&lt;p&gt;Unfortunately, this won’t be very efficient. Here is why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the weather doesn’t change during a minute, all screens would ask a lot of times the weather station for nothing, wasting energy.&lt;/li&gt;
&lt;li&gt;The more screens you have, the more the weather station will receive simultaneous requests, which risks to overload the station.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So instead of making the screens asking the new data, let’s try to see the solution in the other direction: what if it’s the weather station's responsibility to inform the screens of new data?&lt;/p&gt;

&lt;h2&gt;
  
  
  The observer pattern
&lt;/h2&gt;

&lt;p&gt;The observer pattern is based on two elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The observable (also called the subject): the object that is observed, the weather station.&lt;/li&gt;
&lt;li&gt;The observers (also called subscribers): they observe the observable. The screens observe the weather station.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The responsibility of the observable is to keep a list of its observers and notify them if needed. Then, the observers can register or unregister themselves from the observable.&lt;/p&gt;

&lt;p&gt;We can see that like a newsletter: a person can subscribe to a newsletter, and the newsletter system will notify each subscriber when new information is available.&lt;/p&gt;

&lt;p&gt;Let’s see what the Swift code would look like.&lt;/p&gt;

&lt;h2&gt;
  
  
  The code
&lt;/h2&gt;

&lt;p&gt;First, we have to define the two protocols: the observable and the observer&lt;/p&gt;

&lt;h3&gt;
  
  
  Observable
&lt;/h3&gt;

&lt;p&gt;The observable is composed of:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A list of subscribers, of the type &lt;code&gt;WeatherStationSubscriber&lt;/code&gt; (we will come to that).&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;register&lt;/code&gt; method, that we can call to add subscribers. An &lt;code&gt;unregister&lt;/code&gt; method should also be added to stop a subscription.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;notify&lt;/code&gt; method that will be used to notify the subscribers.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;protocol&lt;/span&gt; &lt;span class="kt"&gt;WeatherStationObservable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 1&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;subscribers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;WeatherStationSubscriber&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// 2&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;subscriber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="kt"&gt;WeatherStationSubscriber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// 3&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&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;Let’s apply that to our weather station:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We add the &lt;code&gt;WeatherStationObservable&lt;/code&gt; protocol conformance to our &lt;code&gt;WeatherStation&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We declare the list of subscribers.&lt;/li&gt;
&lt;li&gt;We implement the &lt;code&gt;register&lt;/code&gt; method, by adding the new subscriber to the list of subscribers. Note that you may want to prevent the same observer to subscribe multiple times, by checking before if the subscriber is already in the &lt;code&gt;subscribers&lt;/code&gt;, or by using a Set instead of a list.&lt;/li&gt;
&lt;li&gt;Finally, we implement the &lt;code&gt;notify&lt;/code&gt; method: we just have to iterate through each of the subscribers and notify them with the new data.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;WeatherStation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherStationObservable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;

    &lt;span class="c1"&gt;// previous code ...&lt;/span&gt;

    &lt;span class="c1"&gt;// 2&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;subscribers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;WeatherStationSubscriber&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="c1"&gt;// 3&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;subscriber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherStationSubscriber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;subscribers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subscriber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// 4&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;subscriber&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;subscribers&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// TODO: notify subscriber here !&lt;/span&gt;
        &lt;span class="p"&gt;}&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;Now we just have to modify the &lt;code&gt;measureWeather&lt;/code&gt; method to call the &lt;code&gt;notify&lt;/code&gt; method:&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;measureWeather&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Simulate new data&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;newData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;in&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nv"&gt;humidity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;in&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;newData&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;lastMeasurement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lastMeasurement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newData&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;)&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;h3&gt;
  
  
  Observer
&lt;/h3&gt;

&lt;p&gt;The observer (here we call it subscriber) is even easier: it just has to implement a method that can be called by the subscriber to notify it when new data is available:&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;protocol&lt;/span&gt; &lt;span class="kt"&gt;WeatherStationSubscriber&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;onNotified&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&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;Let’s apply that to one of our screens:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here the &lt;code&gt;onNotified&lt;/code&gt; method will just send the temperature information to the display method.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;TemperatureScreen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherStationSubscriber&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// Previous code...&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;onNotified&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;newData&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;)&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;h3&gt;
  
  
  Usage
&lt;/h3&gt;

&lt;p&gt;Now let’s see how to use that in practice:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We create the weather station.&lt;/li&gt;
&lt;li&gt;We create the screens.&lt;/li&gt;
&lt;li&gt;We register the screens to the weather station.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;weatherStation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;WeatherStation&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;temperatureScreen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;TemperatureScreen&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;humidityScreen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;HumidityScreen&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;span class="n"&gt;weatherStation&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temperatureScreen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;weatherStation&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;humidityScreen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The final code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Foundation&lt;/span&gt;

&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Equatable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;humidity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;protocol&lt;/span&gt; &lt;span class="kt"&gt;WeatherStationObservable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;subscribers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;WeatherStationSubscriber&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;subscriber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="kt"&gt;WeatherStationSubscriber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;protocol&lt;/span&gt; &lt;span class="kt"&gt;WeatherStationSubscriber&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;onNotified&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;WeatherStation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherStationObservable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Timer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;lastMeasurement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;subscribers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;WeatherStationSubscriber&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Timer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scheduledTimer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;withTimeInterval&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;repeats&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;block&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;weak&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;measureWeather&lt;/span&gt;&lt;span class="p"&gt;()&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;measureWeather&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Measuring..."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;newData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;in&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nv"&gt;humidity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;in&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;newData&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;lastMeasurement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lastMeasurement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newData&lt;/span&gt;
            &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;subscriber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherStationSubscriber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;subscribers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subscriber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;subscriber&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;subscribers&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;subscriber&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onNotified&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;TemperatureScreen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherStationSubscriber&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;onNotified&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;newData&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Temperature is now &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;HumidityScreen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherStationSubscriber&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;onNotified&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;humidity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;newData&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;humidity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;humidity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Humidity is now &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;humidity&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;weatherStation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;WeatherStation&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;temperatureScreen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;TemperatureScreen&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;humidityScreen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;HumidityScreen&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;weatherStation&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temperatureScreen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;weatherStation&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;humidityScreen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we run that code in a playground, we can see each time the weather station gets new data, it will notify the screens that will display the new weather information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Measuring...
Temperature is now 7
Humidity is now 3

Measuring...
Temperature is now 1
Humidity is now 9

Measuring...
Temperature is now 4
Humidity is now 8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Wrap up
&lt;/h2&gt;

&lt;p&gt;In this article, we learned what is the observer design pattern and how to use it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I hope this article has been helpful to you. If you have any questions or feedback about this article, don’t hesitate to contact me on&lt;/em&gt; &lt;em&gt;**&lt;a href="https://twitter.com/kaww"&gt;Twitter&lt;/a&gt;&lt;/em&gt;&lt;em&gt;!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>design</category>
    </item>
    <item>
      <title>How to create an example app for your SPM package</title>
      <dc:creator>Kaww</dc:creator>
      <pubDate>Wed, 08 Mar 2023 12:57:55 +0000</pubDate>
      <link>https://forem.com/kaww/how-to-create-an-example-app-for-your-spm-package-4icm</link>
      <guid>https://forem.com/kaww/how-to-create-an-example-app-for-your-spm-package-4icm</guid>
      <description>&lt;p&gt;When working on a Swift package, it may be quite difficult to test that the code we are writing works properly.&lt;/p&gt;

&lt;p&gt;Beyond writing unit tests, an easy way to test a package would be to configure a demo project right inside the repository. This has non-negligible advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It allows us to write at the same place the package code and the demo code without any friction.&lt;/li&gt;
&lt;li&gt;It allows us to configure several targets, for instance, an iOS and macOS demo apps.&lt;/li&gt;
&lt;li&gt;It serves as documentation code with usage examples for other developers that will be able to browse the code on Github.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, we will see how to add and configure a demo app project for an SPM package.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create the demo project
&lt;/h2&gt;

&lt;p&gt;First, we need a Swift package. If you don’t have any and want to learn how to create one, check out my article “&lt;strong&gt;&lt;a href="https://kaww.hashnode.dev/how-to-create-an-spm-package"&gt;How to create an SPM package with SwiftUI&lt;/a&gt;&lt;/strong&gt;”.&lt;/p&gt;

&lt;p&gt;Now we will create the demo app project. In Xcode, go to &lt;code&gt;File &amp;gt; New &amp;gt; Project...&lt;/code&gt;then choose for instance an iOS app. By convention, I will name it as my package name suffixed by “demo”. Here, “MyPackageDemo”. Some also suffix it "Example".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fYkulESD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngtykktq98zr6a4d752i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fYkulESD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngtykktq98zr6a4d752i.png" alt="Package creation" width="800" height="521"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then save the project at the root location of the Swift package:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qDH9RLon--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fkl2kwfwxkgei78rx1zl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qDH9RLon--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fkl2kwfwxkgei78rx1zl.png" alt="Package save location" width="800" height="521"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Close Xcode windows, then go to the new Xcode project directory and open the &lt;code&gt;.xcodeproj&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure the demo project with the package
&lt;/h2&gt;

&lt;p&gt;The trick here is to import the SPM package by referencing its location on the disk, instead of importing it from GitHub.&lt;/p&gt;

&lt;p&gt;To do so, go to the main project file then go to &lt;code&gt;Project &amp;gt; Your project &amp;gt; Package Dependencies&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OQ6itVE---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x8v25p5z40ejjyiwv8zg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OQ6itVE---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x8v25p5z40ejjyiwv8zg.png" alt="Package Dependencies" width="800" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Press the “+” button and select “Add local”. The Finder opens, go to the root location of the SPM package, where the &lt;code&gt;Package.swift&lt;/code&gt; file is (and that now also contains the demo project folder).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OQGxL3gp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxcfzay769n5qiuo4wou.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OQGxL3gp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxcfzay769n5qiuo4wou.png" alt="Add package locally" width="800" height="509"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The SPM package is now imported, we can see it in the file explorer section.&lt;/p&gt;

&lt;p&gt;The final step will be to link the package’s library to our demo project target. To do so, go to the main project file then go to &lt;code&gt;Targets &amp;gt; Your project target &amp;gt; General&lt;/code&gt;. Scroll down to find the section &lt;code&gt;Frameworks, Libraries, and Embedded Content&lt;/code&gt; then click the “+” button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kWWuV6_c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pgjfvdqbc6be41cjod6m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kWWuV6_c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pgjfvdqbc6be41cjod6m.png" alt="Frameworks, Libraries, and Embedded Content" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A window opens, find the SPM package and select the library icon.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yyCU7bkA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yemd6heqp0au95rptcg5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yyCU7bkA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yemd6heqp0au95rptcg5.png" alt="Library selection" width="447" height="162"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All good! The project is fully configured, it's now possible to compile both the package code and the demo app, simply by changing the build target on Xcode’s top bar.&lt;/p&gt;

&lt;p&gt;We can now use our package's code in our demo app's code and push all that on the package’s GitHub repository.&lt;/p&gt;

&lt;p&gt;Note that if you want to work in that configuration, you need to open the &lt;code&gt;.xcodeproj&lt;/code&gt; file with Xcode. And if you just want to open the SPM files you can still open Xcode at the package's root location, but you won’t be able to run the demo app this way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap up
&lt;/h2&gt;

&lt;p&gt;In this article, we learned how to create and configure a demo app project for an SPM package to simplify its development.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I hope this article has been helpful to you. If you have any questions or feedback about this article, don’t hesitate to contact me on&lt;/em&gt; &lt;em&gt;**&lt;a href="https://twitter.com/kaww"&gt;Twitter&lt;/a&gt;&lt;/em&gt;&lt;em&gt;!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>swift</category>
      <category>swiftui</category>
      <category>xcode</category>
      <category>github</category>
    </item>
    <item>
      <title>How to create an SPM package with SwiftUI</title>
      <dc:creator>Kaww</dc:creator>
      <pubDate>Wed, 08 Mar 2023 12:43:31 +0000</pubDate>
      <link>https://forem.com/kaww/how-to-create-an-spm-package-with-swiftui-k49</link>
      <guid>https://forem.com/kaww/how-to-create-an-spm-package-with-swiftui-k49</guid>
      <description>&lt;p&gt;Swift packages are reusable components of Swift, Objective-C, Objective-C++, C, or C++ code that we can import into any Xcode project.&lt;/p&gt;

&lt;p&gt;In this article we will see:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to create our Swift package containing reusable SwiftUI code&lt;/li&gt;
&lt;li&gt;How to make it available for other developers&lt;/li&gt;
&lt;li&gt;How to add our new package to an Xcode project&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Create our package
&lt;/h2&gt;

&lt;p&gt;In Xcode, go to &lt;code&gt;File &amp;gt; New &amp;gt; Package…&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Name the package, select a destination to save the project and click the “create” button. The package is created.&lt;/p&gt;

&lt;p&gt;The package contains several default files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;README.md&lt;/code&gt; is a markdown file that will serve as documentation for other developers.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Package.swift&lt;/code&gt; contains the configuration of the package.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MyPackage.swift&lt;/code&gt; is the default generated file. We won’t use it as we will create another one.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MyPackageTests.swift&lt;/code&gt; is also generated by default and is made for unit testing for the package. We will not cover that part today.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tHeRWl5V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a33ygggk90s6r56mccn6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tHeRWl5V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a33ygggk90s6r56mccn6.png" alt="xcode explorer" width="265" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure the package
&lt;/h3&gt;

&lt;p&gt;We can see that the file contains some information about the package, like its &lt;strong&gt;name&lt;/strong&gt;, its &lt;strong&gt;products&lt;/strong&gt; (the parts that other developers will import into their projects) and the &lt;strong&gt;targets&lt;/strong&gt; for compilation. It also contains a &lt;strong&gt;dependencies&lt;/strong&gt; section if the package needs to import other Swift packages.&lt;/p&gt;

&lt;p&gt;As SwiftUI only runs on iOS 13 or later (also macOS but we will not cover that), we need to add this requirement to our package.&lt;/p&gt;

&lt;p&gt;Add the following property under the &lt;code&gt;name&lt;/code&gt; property:&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="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;package&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Package&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="c1"&gt;//    name: ...&lt;/span&gt;
    &lt;span class="nv"&gt;platforms&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;iOS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;v13&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adding our code
&lt;/h3&gt;

&lt;p&gt;Create a new file next to MyPackage.swift file to write our SwiftUI code. For this tutorial, we will use this code:&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;import&lt;/span&gt; &lt;span class="kt"&gt;SwiftUI&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;MyPackageView&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;some&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello world in MyPackage!"&lt;/span&gt;&lt;span class="p"&gt;)&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;Note that all here needs to be marked as public. If not, nothing will be visible for developers that will import your package.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📝  While developing a package, it may be useful to be able to run the code we are writing on a demo app.&lt;br&gt;
Check my article &lt;a href="https://kaww.hashnode.dev/how-to-create-an-example-app-for-your-spm-package"&gt;How to create an example app for your SPM package&lt;/a&gt;!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Make the package available for other developers
&lt;/h2&gt;

&lt;p&gt;To make this new package available for other developers, we will need to host it somewhere. Here we will use &lt;strong&gt;&lt;a href="http://github.com/"&gt;Github.com&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;On Github, create a new &lt;strong&gt;repository&lt;/strong&gt;. Add a name and use all the default settings. Just make sure the repository is &lt;strong&gt;public&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now open the terminal at the location of the project directory and enter the following commands:&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="n"&gt;git&lt;/span&gt; &lt;span class="kd"&gt;init&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;all&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;commit&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="s"&gt;"first commit"&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;branch&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kt"&gt;M&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;remote&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt; &lt;span class="kt"&gt;YOUR&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kt"&gt;PACKAGE&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kt"&gt;URL&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;push&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;→ Here you need to replace the &lt;code&gt;YOUR-PACKAGE-URL&lt;/code&gt; by the URL of the Github repository. In my case it’s &lt;code&gt;https://github.com/Kaww/MyPackage&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The package should now be available!&lt;/p&gt;

&lt;h2&gt;
  
  
  Import the package to an Xcode project
&lt;/h2&gt;

&lt;p&gt;Now we may want to use our new package in an Xcode project. Let’s see how to import it.&lt;/p&gt;

&lt;p&gt;Open an Xcode project, go to the main project file then go to &lt;code&gt;Project &amp;gt; Your project &amp;gt; Package Dependencies&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CcatJ1fa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bv3lwdt7koyqpmhrdk3q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CcatJ1fa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bv3lwdt7koyqpmhrdk3q.png" alt="Package Dependencies" width="800" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Press the “+” button and enter your package Github URL in the search bar.&lt;/p&gt;

&lt;p&gt;We can specify a dependency rule like a minimum version number for the package (if we added a release in the Github repository). Here we will use the code available on the &lt;code&gt;main&lt;/code&gt; git branch.&lt;/p&gt;

&lt;p&gt;Then press “Add Package”.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z0ty7XAC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hmspkk4720gh6zunrym1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z0ty7XAC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hmspkk4720gh6zunrym1.png" alt="Add Package" width="800" height="504"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that your package appeared on the file explorer under the “Package Dependencies” section.&lt;/p&gt;

&lt;p&gt;In a SwiftUI view, we can now import the package and call the view we created:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add &lt;code&gt;import MyPackage&lt;/code&gt; on the top of the file.&lt;/li&gt;
&lt;li&gt;In the view, add &lt;code&gt;MyPackageView()&lt;/code&gt; to call the package’s view.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G5Wefr_h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/981efw8j4zbcmnilvdnh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G5Wefr_h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/981efw8j4zbcmnilvdnh.png" alt="Xcode with new coode" width="800" height="504"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that’s it!&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap up
&lt;/h2&gt;

&lt;p&gt;In this article, we learned how to create a Swift package and what are the important files in it.&lt;/p&gt;

&lt;p&gt;Then we learned how to push it on Github to make it visible and available for other developers.&lt;/p&gt;

&lt;p&gt;Finally, we’ve seen how to import the package as a dependency for another project to use its code.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I hope this article has been helpful to you. If you have any questions or feedback about this article, don’t hesitate to contact me on&lt;/em&gt; &lt;em&gt;**&lt;a href="https://twitter.com/kaww"&gt;Twitter&lt;/a&gt;&lt;/em&gt;&lt;em&gt;!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>swift</category>
      <category>swiftui</category>
      <category>xcode</category>
      <category>github</category>
    </item>
  </channel>
</rss>
