<?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: Dave Cridland</title>
    <description>The latest articles on Forem by Dave Cridland (@dwd).</description>
    <link>https://forem.com/dwd</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%2F3541%2F25c31f07-e569-4ef6-a9ec-1aeea6d36266.jpg</url>
      <title>Forem: Dave Cridland</title>
      <link>https://forem.com/dwd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dwd"/>
    <language>en</language>
    <item>
      <title>It's always DNS</title>
      <dc:creator>Dave Cridland</dc:creator>
      <pubDate>Sun, 09 Nov 2025 13:48:57 +0000</pubDate>
      <link>https://forem.com/dwd/its-always-dns-4f56</link>
      <guid>https://forem.com/dwd/its-always-dns-4f56</guid>
      <description>&lt;h1&gt;
  
  
  It is always DNS... Sometimes
&lt;/h1&gt;

&lt;p&gt;It might seem as if the DNS, or the "Domain Name System", is highly unreliable. A major AWS outage was traced to an incorrectly set-up DNS entry, and the old hands (and new hands) in the industry all smiled knowingly and said "it's always DNS", even though it was really the DynamoDB global table names, the DNS was working perfectly, and nobody else would have been using that DNS entry directly.&lt;/p&gt;

&lt;p&gt;The DNS isn't really the cause of many outages and problems - but it is involved in lots of them, and has privacy leaks and performance issues. And this is because it is usually so reliable we use it for literally everything, and use it with an intensity that results in us pouring information into it as well as relying totally on the information we get back.&lt;/p&gt;

&lt;p&gt;In this post, I'll explain why we have the DNS, how it works, how to make it faster, and how to make it more private. And how it goes wrong.&lt;/p&gt;

&lt;p&gt;And also I'll prove, beyond a shadow of a doubt, that my servers are faster than Google's.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Gentleman's Primer In DNS
&lt;/h3&gt;

&lt;p&gt;So let's remind ourselves about the Domain Name System. The Internet operates on addresses - originally NCP, then IPv4, and then more recently, often IPv6. These are magic numbers - we tend to think of IPv4 addresses as four 8-bit numbers, but really it's a single 32-bit number, and IPv6 addresses are a giant 128-bit number. But thanks to Vint Cerf's epiphany on the back of a napkin, they're split at multiple points to form subnet addresses, which makes routing - the decision of where to send the packets - really easy (and fast!).&lt;/p&gt;

&lt;p&gt;Many of us probably remember half a dozen IPv4 addresses without thinking. We're in the habit of typing &lt;code&gt;127.0.0.1&lt;/code&gt; instead of &lt;code&gt;localhost&lt;/code&gt;, and these days having to train ourselves the other way lest we miss out on &lt;code&gt;::1&lt;/code&gt;, the IPv6 version. If you've a static IPv4 address from your ISP, you might memorize that.&lt;/p&gt;

&lt;p&gt;But you probably don't memorise all the possible IP addresses for Google, for example - instead you want to type &lt;code&gt;www.google.com&lt;/code&gt; and get back the address.&lt;/p&gt;

&lt;p&gt;This was originally solved on the Internet by the simple method of having a text file containing the names and their addresses, and sharing this around via email or whatever. Convenient, but it turns out this didn't scale well - though it still exists as &lt;code&gt;/etc/hosts&lt;/code&gt; on UNIX systems as a fallback.&lt;/p&gt;

&lt;p&gt;And, moreover, you might want to know more than just the address - you might want to know how to send email there, or where the XMPP server is, or what settings to use for TLS, and so on - making a single text file get fairly complicated.&lt;/p&gt;

&lt;p&gt;What we needed was a distributed database.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Lady's Primer in the DNS
&lt;/h3&gt;

&lt;p&gt;First things first: because it's "The Domain Name System", real experts call it "The DNS". Top tip to make yourself look like you know what you're talking about, that.&lt;/p&gt;

&lt;p&gt;Domain names are a sequence of labels, each followed by a dot. Each label is - normally - lower case alphanumeric characters, plus '-'. Note that at a low level, the DNS never handles Unicode, but from a purely technical standpoint, it can have labels with anything at all in (including, wildly, dots and spaces).&lt;/p&gt;

&lt;p&gt;The DNS has the concept of a "zone" - something you might normally call a "domain", though it's slightly different. Zones contain records - the only mandatory records are the "Start Of Authority" (SOA) record, which lists settings for the zone, and the "Name Server" (NS) records, which list the nameservers. Those are given as fully qualified domain names themselves, which does give us an interesting chicken and egg problem. These records will have a key which will be the same domain name as the name of the zone.&lt;/p&gt;

&lt;p&gt;NS records with a different domain name essentially state that the listed nameservers will answer queries for this other domain (as another zone).&lt;/p&gt;

&lt;p&gt;A and AAAA records list the IPv4 and IPv6 addresses for the label. There's other record types too - I'm going to skip over them, but they're often quite important.&lt;/p&gt;

&lt;p&gt;To query the DNS from first principles, you'll need a list of the Root Servers. We can get one just by using &lt;code&gt;dig&lt;/code&gt;, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dig &lt;span class="nb"&gt;.&lt;/span&gt; NS
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is asking for the nameservers for the domain name ".". Remember I said it was labels followed by a dot? Google's domain is really "google.com.", but we skip the last dot usually. Putting it there - on some programs at least - tells the program not to look on our local domains.&lt;/p&gt;

&lt;p&gt;The response will be something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; &amp;lt;&amp;lt;&amp;gt;&amp;gt; DiG 9.18.39-0ubuntu0.24.04.2-Ubuntu &amp;lt;&amp;lt;&amp;gt;&amp;gt; . NS
;; global options: +cmd
;; Got answer:
;; -&amp;gt;&amp;gt;HEADER&amp;lt;&amp;lt;- opcode: QUERY, status: NOERROR, id: 54773
;; flags: qr rd ra; QUERY: 1, ANSWER: 13, AUTHORITY: 0, ADDITIONAL: 27

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;.              IN  NS

;; ANSWER SECTION:
.           309030  IN  NS  e.root-servers.net.
.           309030  IN  NS  h.root-servers.net.
.           309030  IN  NS  a.root-servers.net.
.           309030  IN  NS  j.root-servers.net.
.           309030  IN  NS  b.root-servers.net.
.           309030  IN  NS  l.root-servers.net.
.           309030  IN  NS  c.root-servers.net.
.           309030  IN  NS  g.root-servers.net.
.           309030  IN  NS  d.root-servers.net.
.           309030  IN  NS  f.root-servers.net.
.           309030  IN  NS  k.root-servers.net.
.           309030  IN  NS  i.root-servers.net.
.           309030  IN  NS  m.root-servers.net.

;; ADDITIONAL SECTION:
a.root-servers.net. 309030  IN  A   198.41.0.4
b.root-servers.net. 309030  IN  A   170.247.170.2
c.root-servers.net. 309030  IN  A   192.33.4.12
d.root-servers.net. 309030  IN  A   199.7.91.13
e.root-servers.net. 309030  IN  A   192.203.230.10
f.root-servers.net. 309030  IN  A   192.5.5.241
g.root-servers.net. 309030  IN  A   192.112.36.4
h.root-servers.net. 309030  IN  A   198.97.190.53
i.root-servers.net. 309030  IN  A   192.36.148.17
j.root-servers.net. 309030  IN  A   192.58.128.30
k.root-servers.net. 309030  IN  A   193.0.14.129
l.root-servers.net. 309030  IN  A   199.7.83.42
m.root-servers.net. 309030  IN  A   202.12.27.33
a.root-servers.net. 309030  IN  AAAA    2001:503:ba3e::2:30
b.root-servers.net. 309030  IN  AAAA    2801:1b8:10::b
c.root-servers.net. 309030  IN  AAAA    2001:500:2::c
d.root-servers.net. 309030  IN  AAAA    2001:500:2d::d
e.root-servers.net. 309030  IN  AAAA    2001:500:a8::e
f.root-servers.net. 309030  IN  AAAA    2001:500:2f::f
g.root-servers.net. 309030  IN  AAAA    2001:500:12::d0d
h.root-servers.net. 309030  IN  AAAA    2001:500:1::53
i.root-servers.net. 309030  IN  AAAA    2001:7fe::53
j.root-servers.net. 309030  IN  AAAA    2001:503:c27::2:30
k.root-servers.net. 309030  IN  AAAA    2001:7fd::1
l.root-servers.net. 309030  IN  AAAA    2001:500:9f::42
m.root-servers.net. 309030  IN  AAAA    2001:dc3::35

;; Query time: 5 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
;; WHEN: Sat Nov 08 17:14:21 GMT 2025
;; MSG SIZE  rcvd: 811
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The actual answer to what we asked is in the "Answer Section" - which is fair enough, really. It tells us that there's a set of Root Servers numbered - okay, lettered - from &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;m&lt;/code&gt;. You'll notice that in the answer, they're jumbled up - they're all equal, and if &lt;code&gt;a&lt;/code&gt; were always listed first, it'd risk getting all the traffic.&lt;/p&gt;

&lt;p&gt;However, since this just gives us domain names, and we need to use the DNS to look those up, this would be fairly useless - so instead of just that, it gives us "Additional" records which it thinks we will find useful. This includes all the address records, in this case, which is just as well. These are called "Glue Records", and are usually very helpful. And that, right there, was dramatic foreshadowing, that was.&lt;/p&gt;

&lt;p&gt;Glue Records are needed because the only way to look up &lt;code&gt;x.root-servers.net. A&lt;/code&gt; would be by knowing where &lt;code&gt;root-servers.net. NS&lt;/code&gt; was, and we don't know that without knowing the addresses for the root servers to ask where to find &lt;code&gt;net. NS&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can't just ask the root servers to look everything up for you, though, because there's two kinds of DNS server.&lt;/p&gt;

&lt;p&gt;Authoritative Servers actually hold the data for a particular zone - the Root Servers are the Authoritative Servers for ".". They will typically only provide answers for the zone they're authoritative for. (There might be a "primary" and several "secondaries" for a zone, but that's a detail that doesn't matter to us - they're all authoritative and you can't tell where the data is actually sourced from).&lt;/p&gt;

&lt;p&gt;Recursive Servers (also known as Resolvers) don't hold authoritative data at all, they're just there to make our lives easier and faster. These will have that list above statically stored - remember that before the DNS, we had to share text files with all the servers' names in? Now we just share the root server list around. It's not as big.&lt;/p&gt;

&lt;p&gt;However, Authoritative Servers do answer queries for subdomains (or "delegated zones"), at least by handing over the NS records of the delegated zone.&lt;/p&gt;

&lt;p&gt;So when we type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dig www.google.com. A +aaonly +norecurse @&amp;lt;ip address &lt;span class="k"&gt;for &lt;/span&gt;root server&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;... we get back the servers not for Google, but for "com.". We can then pick one of those, and ask again, and we'll get Google's nameservers (via a Glue Record in "com.", by the way), and finally, by asking them, we'll get our address.&lt;/p&gt;

&lt;p&gt;(Asides: Why did I elide the IP address there, despite listing them all above? Because nobody wants every person reading this to hit the same root server. No really. Why did I include the + flags? To stop the root server operating in recursive mode - which it won't, but it might cause additional logging for those folks running them. Why am I querying for an IPv4 record over IPv6? Well, no reason, really.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dig www.google.com +aaonly +norecurse A @2001:4860:4802:34::a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;.. fill finally give us:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; QUESTION SECTION:
;www.google.com.            IN  A

;; ANSWER SECTION:
www.google.com.     300 IN  A   142.250.140.105
www.google.com.     300 IN  A   142.250.140.147
www.google.com.     300 IN  A   142.250.140.106
www.google.com.     300 IN  A   142.250.140.99
www.google.com.     300 IN  A   142.250.140.104
www.google.com.     300 IN  A   142.250.140.103

;; Query time: 37 msec
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's all great whelp it takes 37ms for each query and how many queries?&lt;/p&gt;

&lt;p&gt;This is too slow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cache and Carry
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Cache and Grab
&lt;/h3&gt;

&lt;p&gt;Recursive nameservers do two things for us - they'll handle the multiple queries so we only need to do one, and they'll also cache the request for us too.&lt;/p&gt;

&lt;p&gt;The numbers (300 in the last response) tell us the "Time To Live", or "TTL", on the record. Google is telling me that I can cache this one for 300 seconds (5 minutes). Since all the records have this, I can drop the number of queries down to, well, none.&lt;/p&gt;

&lt;p&gt;Our client machine can also cache, but typically a recursive resolver runs at your ISP, and will cache for all its customers.&lt;/p&gt;

&lt;p&gt;This means the DNS can be really much faster. My ISP will return the result in (typically) 18-20ms, because it'll be fully cached. If it isn't, it'll take closer to 27-30ms.&lt;/p&gt;

&lt;p&gt;These are so useful that they're part of the connection data you'll get as you connect. Your phone or laptop uses DHCP; your router will get the same via PPP options. Finding your local resolver means getting a faster DNS service - and thus a faster connection.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cache is King
&lt;/h3&gt;

&lt;p&gt;To make things even faster, you can (remarkably easily) run your own recursive nameserver at home, on a Raspberry Pi or similar. I run two, in fact, on "real" Linux machines, because I am perfectly normal and in no way stupidly nerdy. These will return me answer (cached, of course) in about 1-2ms.&lt;/p&gt;

&lt;p&gt;If only we could measure this over months and have graphs. (I do. Of course I do).&lt;/p&gt;

&lt;p&gt;Here's my ISP's DNS server (or one of them), for the past 10 days:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5d6169rlupcddf0cpivv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5d6169rlupcddf0cpivv.png" alt="ISP Resolver, past 10 days" width="697" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And here's mine:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fveyty0wuftknhvoe7hlr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fveyty0wuftknhvoe7hlr.png" alt="My Resolver, past 10 days" width="697" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And for comparison here's Google's 8.8.8.8 public DNS resolvers:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuwbwq35kkfyaqzdv6wx7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuwbwq35kkfyaqzdv6wx7.png" alt="Google 8.8.8.8, past 10 days" width="697" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I include this as definitive proof that my servers are faster than Google's, incidentally. Honestly, sometimes I'm such a genius I even impress myself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Speed, Reliability, and Privacy
&lt;/h2&gt;

&lt;p&gt;Almost every connection to any service will involve, as a first step, a DNS lookup. In fact, the only exception I can think of is the DNS root server queries themselves.&lt;/p&gt;

&lt;p&gt;That means - quite obviously - speed is crucial. That 20 (or 2) millisecond DNS tax is paid for every single connection.&lt;/p&gt;

&lt;p&gt;It means reliability is crucial. If the DNS fails, then of course the connection cannot continue.&lt;/p&gt;

&lt;p&gt;And DNS can fail - of course it can - in numerous ways.&lt;/p&gt;

&lt;h2&gt;
  
  
  How can I fail thee? Let me count the ways
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Bad Answers
&lt;/h3&gt;

&lt;p&gt;It goes without saying - but I'll say it anyway of course - that if the data in the authoritative DNS servers is wrong, you'll get wrong answers, and things won't work.&lt;/p&gt;

&lt;p&gt;If a lookup for &lt;code&gt;www.google.com.&lt;/code&gt; gave me the IPv4 address of &lt;code&gt;127.0.0.1&lt;/code&gt;, it's going to be game over. This is the kind of failure that brought down AWS, incidentally - everything worked perfectly, but the wrong DNS records were put in, and thus the wrong ones came out.&lt;/p&gt;

&lt;p&gt;Obviously "It's always DNS" applies here, but really, it was Terraform or Cloud Formation or something.&lt;/p&gt;

&lt;p&gt;A more common case is where the service is intentionally moving, and the caching of DNS records causes resolvers to provide the old address. (Fix: lower TTLs the day before a move!)&lt;/p&gt;

&lt;h3&gt;
  
  
  Bad Glue
&lt;/h3&gt;

&lt;p&gt;Oh, wait, I foreshadowed this! I hope you were paying attention.&lt;/p&gt;

&lt;p&gt;Glue Records are vital for locating nameservers when the nameservers' names are in the zone they're serving. But - and you'll like this pun, I promise - when glue records are missing or incorrect, things can come unstuck.&lt;/p&gt;

&lt;p&gt;A classic case is where a nameserver moves address - this is so rare that the precise things needed are sometimes forgotten, like not only updating the authoritative records but the glue as well.&lt;/p&gt;

&lt;p&gt;Suddenly the entire zone drops offline - but only for those resolvers using the wrong glue address.&lt;/p&gt;

&lt;p&gt;It's maddeningly difficult to diagnose such cases because any resolver with the NS records cached might never see the problem, only "new" traffic will fail, and even then perhaps only half of it.&lt;/p&gt;

&lt;h3&gt;
  
  
  DNS Server Offline
&lt;/h3&gt;

&lt;p&gt;Obviously if there's no DNS server listening at the right IP address, you won't get a response. DNS operates (at least usually) over UDP, so packets get lost, and we have to retry. If nothing's there we'll retry a lot.&lt;/p&gt;

&lt;p&gt;Even if the server is present, but the link is lossy, this can add a substantial amount of time to the lookup - and that will add up surprisingly quickly into a delay that an actual human will notice as being "a slow site".&lt;/p&gt;

&lt;h3&gt;
  
  
  "Lame" Delegation
&lt;/h3&gt;

&lt;p&gt;Maybe there is a server that responds, but maybe it says it's not the authoritative server the parent said it was? These are known as "Lame" delegations, and might be down to bad answers in the parent, or bad glue.&lt;/p&gt;

&lt;h2&gt;
  
  
  And Privacy?
&lt;/h2&gt;

&lt;p&gt;Privacy is a bigger problem than just the DNS, but DNS servers are a key part of the problem.&lt;/p&gt;

&lt;p&gt;If every site you visit gets looked up in DNS, then the DNS server you're using gets every site you visit.&lt;/p&gt;

&lt;p&gt;When that's your ISP (at least in sane jurisdictions), that's most likely fine. You have a contract with your ISP, and in most sane jurisdictions they'd have to tell you, at least, if they were gathering the data (and generally they'd have to ask your permission).&lt;/p&gt;

&lt;p&gt;But if it's a rando WiFi in a café? Yeah, they should still ask, but honestly, who knows?&lt;/p&gt;

&lt;h3&gt;
  
  
  DoT and DoH
&lt;/h3&gt;

&lt;p&gt;DoT, or DNS over TLS, allows you to directly use a particular DNS provider over a TLS encrypted connection. This is good for privacy, but comes with a cost - the provider isn't local to you, and will be slower.&lt;/p&gt;

&lt;p&gt;DoH is much the same, but uses HTTPS rather than simply running the DNS protocol directly over TLS - it's useful for cases where other protocols may be blocked at the firewall, or where you want to minimize the likelihood that someone can tell it's DNS traffic rather than just another website (but they can tell anyway).&lt;/p&gt;

&lt;p&gt;There are privacy-focused providers, such as Quad9, and there's also Google. As ever, encryption doesn't solve problems, it just moves them, and a DoT/DoH provider like Quad9 or Google could choose to log all your queries just like a nefarious café owner.&lt;/p&gt;

&lt;p&gt;At home, you're likely paying a "tax" of around 5ms for using these by my measurements - so I'd recommend not to bother unless you genuinely cannot trust your ISP. But when out of the house, you probably want to, so they're worth considering for mobile devices and laptops you use on arbitrary WiFis.&lt;/p&gt;

&lt;p&gt;I do not understand why ISPs don't offer DoT, DoH, and indeed full-blown VPN access for their customers. If you're an ISP reading this, this is a free product idea and I'd love you to do it.&lt;/p&gt;

&lt;h3&gt;
  
  
  DNSSEC
&lt;/h3&gt;

&lt;p&gt;A parallel security issue with the DNS (in general, but particularly problematic in rando WiFi situations) is that the operator of a DNS service - whether or not it's using TLS - can simply fake the answers, redirecting you to a site under their control.&lt;/p&gt;

&lt;p&gt;Assuming you're using TLS (usually via HTTPS) this isn't as big a problem as you'd think, surprisingly, but they can generate a lot of confusion. And if a Certificate Authority gets duped into handing out a certificate, it becomes a nasty issue.&lt;/p&gt;

&lt;p&gt;DNSSEC allows each individual record to be signed, which means that even when the records are cached, they're still trustworthy.&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTPS
&lt;/h3&gt;

&lt;p&gt;HTTPS is - of course - a key part of network security and privacy, but I'm not going into it in this article at all. This, right here, is the only mention of SNI, and ECH, and I'm not even expanding them.&lt;/p&gt;

&lt;p&gt;Sorry. I'll write something else on these later!&lt;/p&gt;

&lt;h2&gt;
  
  
  It really is always the DNS, sort of.
&lt;/h2&gt;

&lt;p&gt;The DNS is a crucial, critical, and central part of the Internet. While it's not quite a single point of failure, DNS problems have an outsized effect on the rest of the system.&lt;/p&gt;

&lt;p&gt;I remember back in around 1997 or so - 17th July 1997, I looked it up - every root server dropped offline (except &lt;code&gt;k&lt;/code&gt;), and caused mass disruption for several hours. Just 10 machines offline and the Internet - as a whole - "went down". These days there's a couple more root servers, and besides, each one of those IP addresses goes to several - perhaps hundreds - of actual servers. I don't think any root server has been offline since.&lt;/p&gt;

&lt;p&gt;And even today, relatively isolated DNS problems - or even just problems with the data in the DNS - can result in much of AWS dropping offline for hours.&lt;/p&gt;

&lt;p&gt;But ultimately, most of the "it's always DNS" problems - including that AWS outage - turn out to have root causes outside of DNS. The average DNS server looks at 99.999% uptime and considers it amateurish. Really, it's always ARP, or Terraform, or a digger. That mass root server outage in 1997? A US exchange point, MAE-East, had a bad configuration update to a router and went offline. The servers themselves were actually fine.&lt;/p&gt;

&lt;p&gt;It's just that DNS will often be the first thing you'll notice go as a result of any outage at all - and the knock-on effects will be massive.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>devops</category>
      <category>networking</category>
    </item>
    <item>
      <title>Nearly Offline Revocation Status Checks for JWT</title>
      <dc:creator>Dave Cridland</dc:creator>
      <pubDate>Tue, 04 Jun 2024 08:13:44 +0000</pubDate>
      <link>https://forem.com/dwd/nearly-offline-revocation-status-checks-for-jwt-2j86</link>
      <guid>https://forem.com/dwd/nearly-offline-revocation-status-checks-for-jwt-2j86</guid>
      <description>&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;The industry standard approach for a token authentication system is to use a JWT with a limited lifetime - the "id token" - with service endpoints, forcing a stronger reauthentication with a central auth system periodically.&lt;/p&gt;

&lt;p&gt;These id tokens are verified "offline" - meaning the central auth system is not contacted. This is a useful property as it allows a significantly improved response time for the endpoint. If the central auth system is located only on one side of the Atlantic, for example, then online verification would lead to around 200ms of additional service endpoint latency on the other side.&lt;/p&gt;

&lt;p&gt;If these tokens are leaked, however, this means there is no way to signal this - the id token remains irrevocably valid until it expires. Adding an online revocation check would eradicate all the benefits of using the system in the first place.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Solution
&lt;/h2&gt;

&lt;p&gt;Firstly, every id token should have a unique identifier associated with it. There is no harm in these being eventually reused, as long as they are reused only well after the original token has been expired. I call this the revocation id.&lt;/p&gt;

&lt;p&gt;If a token is to be marked as revoked - either automatically via taint failures or explicitly - this revocation id is added to a list (perhaps a database table).&lt;/p&gt;

&lt;p&gt;A new central auth endpoint can now check if a token has been revoked - an online revocation check that we want to avoid if at all possible.&lt;/p&gt;

&lt;p&gt;Next, I propose adding a shared Bloom filter to the system, maintained by the central auth system. It can be broadcast out on change to service entities via publish-subscribe (eg, Redis) or simply fetched on a short cadence (say, once a minute, or even less).&lt;/p&gt;

&lt;p&gt;A Bloom filter is a probabalistic mechaninism for optimising the decision of whether a value is in a set, so by distributing just the Bloom filter values, this obviates the need to check most tokens in an online revocation check.&lt;/p&gt;

&lt;p&gt;Bloom filters work by checking if the hash of some value has been binary  ORed into a value - multiple hashes are used to reduce the probability of a false positive. For example, if we used SHA-256 and SHA-512, we could check like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Pseudocode, really
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;in_bloom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bloom&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;revocation_id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;revocation_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;bloom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sha256&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nf"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;revocation_id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;sha512&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;revocation_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;bloom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sha512&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nf"&gt;sha512&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;revocation_id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I propose using, as the hash algorithms, a set of HMAC-SHA-256. By using a single algorithm, adding an additional HMAC key is trivial in code, and allows this to be tweaked by operational experience.&lt;/p&gt;

&lt;p&gt;This means the Bloom filter will be a short array of 256-bit long binary values. One might be sufficient for smaller systems; I don't think it'd need to be higher than four ever, though measure measure measure.&lt;/p&gt;

&lt;p&gt;Finally, to reduce computational load, I suggest calculating the HMAC-SHA-256 values of the revocation id and placing this in the JWT; this essentially trades CPU time for JWT size.&lt;/p&gt;

&lt;h2&gt;
  
  
  Result
&lt;/h2&gt;

&lt;p&gt;At this point, you have a recovable token with a very low revocation latency, but effective offline revocation checks (and the advantages in latency this gives you).&lt;/p&gt;

</description>
      <category>security</category>
    </item>
    <item>
      <title>Subroutines</title>
      <dc:creator>Dave Cridland</dc:creator>
      <pubDate>Thu, 18 Mar 2021 20:16:56 +0000</pubDate>
      <link>https://forem.com/dwd/subroutines-21gj</link>
      <guid>https://forem.com/dwd/subroutines-21gj</guid>
      <description>&lt;h2&gt;
  
  
  A Unit Of Code
&lt;/h2&gt;

&lt;p&gt;A subroutine is a callable unit of code. &lt;/p&gt;

&lt;p&gt;It may surprise you to find that not all languages name their subroutines "functions". Pascal - not that anyone writes Pascal anymore - distinguished between "Procedures" and "Functions". The latter always returned a value, the former could not. Other languages, like BASIC, stuck with "subroutine", giving us &lt;code&gt;GOSUB&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But whatever the name, the key thing is that you can call a function as many times as you like, and from the caller's perspective, it's just like an operator or statement. When it completes, execution picks up where it was called.&lt;/p&gt;

&lt;p&gt;Sometimes these functions return a value. Sometimes they accept values - called "parameters" or "arguments".&lt;/p&gt;

&lt;p&gt;They usually have a name - a function identifier - but sometimes the name is more complex than just a simple name.&lt;/p&gt;

&lt;p&gt;This is a deep dive into functions, how they work, and what to do with them.&lt;/p&gt;

&lt;h2&gt;
  
  
  The low level
&lt;/h2&gt;

&lt;p&gt;At a low level, in languages like C, something like this happens on a function call:&lt;/p&gt;

&lt;p&gt;First, the caller puts the arguments somewhere the function code can find them. Next, it places a hidden argument of where the function was called from - a Program Counter value or equivalent.&lt;/p&gt;

&lt;p&gt;Then the actual call occurs, and execution moves from the call site to the function body. Most CPUs actually provide an instruction for this and the later return, which will handle the Program Counter storage for you.&lt;/p&gt;

&lt;p&gt;The function then does its stuff, getting the function arguments, processing them, and calculating a return value if any. Then finally, it returns.&lt;/p&gt;

&lt;p&gt;The return process is the reverse of the calling process - the return value is placed somewhere, and the Program Counter is restored. Execution then continues from where it left off at the call site.&lt;/p&gt;

&lt;p&gt;In general, the place where the function call arguments, return values, and local variables are placed is called a "stack frame". This naturally gives a variable scope for the function, and a clean lifetime for any values created during the function call.&lt;/p&gt;

&lt;p&gt;Each call adds a new stack frame to the end, and each return removes it again. In a lot of languages, the program simply terminates once the stack is empty of frames. Too many stack frames will fill the stack and cause a fatal error.&lt;/p&gt;

&lt;p&gt;Even where languages don't use actual stack frames, this terminology remains - hence we talk about "the call stack", "stack traces", and so on in all languages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Call me by my name, oh, call me by my value...
&lt;/h2&gt;

&lt;p&gt;In a language like C, a copy of the variable or expression is placed in the stack frame. This means that any change to the function argument within the function won't propagate back to the caller:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;called&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&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;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;caller&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;called&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// c picks up the return value here.&lt;/span&gt;
  &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// b is left unchanged; we passed a copy.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is known as "call by value".&lt;/p&gt;

&lt;p&gt;Because C has reference types - types which hold a reference to some other value, rather than the value itself - we can also pass in the reference by value, giving the function the same reference, and allowing it to use the same value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;called&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="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// a is a "pointer to int", a reference type.&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// "*a" dereferences, reaching the value.&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;caller&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;called&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Pass a reference to b, not b's value.&lt;/span&gt;
  &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// As before.&lt;/span&gt;
  &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This time, we've changed the value.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This behaviour is called "call by reference", and it allows a function to manipulate the values passed into it.&lt;/p&gt;

&lt;p&gt;Some languages - including Javascript, Python, and several others - implicitly use reference types in many (or even all) cases. This means you'll always end up with functions able to manipulate the value of objects unexpectedly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;oo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;oo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fn2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ii&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;ii&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;ii&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;o&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Implicitly call by reference.&lt;/span&gt;
&lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 1, because fn changed it.&lt;/span&gt;
&lt;span class="nf"&gt;fn2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns 2&lt;/span&gt;
&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// still 0, because primitives are passed by value.&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are other possibilities - Swift has in-out parameters giving you "call by value-result", but in practice these are generally doing "call by reference" underneath so you needn't pay that much attention. "Call by reference" is, of course, really "call by value" with a fake moustache and a reference type, but the distinction is important.&lt;/p&gt;

&lt;h2&gt;
  
  
  Returning a Value
&lt;/h2&gt;

&lt;p&gt;When a function returns a value, the distinction between returning a value or a reference can be extremely important.&lt;/p&gt;

&lt;p&gt;In C, all reference types are explicit, but also the local variables are likely to vanish - returning a reference to a local variable gives you a dangling reference, which will cause some impressive crashes (or worse).&lt;/p&gt;

&lt;p&gt;But you can still return a reference to some value that isn't a local one.&lt;/p&gt;

&lt;p&gt;In other languages where objects are always referenced, then the language takes care of this for you. Examples here include JavaScript, but also Python and others.&lt;/p&gt;

&lt;h2&gt;
  
  
  Returning some Values
&lt;/h2&gt;

&lt;p&gt;Usually, you can only return a single value from a function, but there are two solutions to this limitation.&lt;/p&gt;

&lt;p&gt;Firstly, you can return some aggregate type. A typical Python idiom is to use a tuple, and then unpack the tuple at the call site, all of which can be done transparently:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Tuple&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;A string&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;


&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In other languages, you might need a record type or an array.&lt;/p&gt;

&lt;p&gt;JavaScript allows you to do something broadly similar to the Python case with restructuring and other shorthands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;A string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The alternative is a solution we've already touched upon - call by reference allows the function to provide the results by manipulating the arguments. This is often used by C for this purpose - there's an idiom involving passing reference types to reference types in order to get back a reference to a newly created value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;
&lt;span class="n"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;create&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="n"&gt;f&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="n"&gt;f&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="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(...);&lt;/span&gt; &lt;span class="c1"&gt;// Allocate memory&lt;/span&gt;
  &lt;span class="c1"&gt;// Initialize (*f).&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Dereference twice to get to the actual int...&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;caller&lt;/span&gt;&lt;span class="p"&gt;()&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="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Pointer to nothing.&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;f&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// True at this point.&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;Don't worry too much about the syntax there (and I accept that double-pointers like that are confusing).&lt;/p&gt;

&lt;p&gt;While this deliberate manipulation of arguments seems painfully complicated, it's actually very useful, and is how - in practice - most object methods work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Not Returning Values
&lt;/h2&gt;

&lt;p&gt;Most modern languages have chosen to unify functions and procedures. C did this by having a special non-type, &lt;code&gt;void&lt;/code&gt;, which cannot have any value. A function "returning void" actually returns nothing, and an attempt to assign the return value gives a syntax error.&lt;/p&gt;

&lt;p&gt;JavaScript and Python always return a value, however - it's just that it might be a special placeholder value. JavaScript uses &lt;code&gt;undefined&lt;/code&gt; here (both a primitive type and a value), whereas Python uses &lt;code&gt;None&lt;/code&gt; (the sole possible value of the type &lt;code&gt;NoneType&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The distinction isn't that confusing in practice, but it does mean that in both cases, you can still assign the return value, though it's not likely to be useful - and might be an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Naming and signatures
&lt;/h2&gt;

&lt;p&gt;When we call a function, the compiler or interpreter needs to do several things.&lt;/p&gt;

&lt;p&gt;First, it needs to find the function declaration. Functions are much like variables - indeed, in many languages they are variables. As such, they are declared somewhere, and in most languages that declaration will also include a definition - in other words, the function's declaration includes the function body containing the actual code. In C and C++, the declaration and definition are usually distinct.&lt;/p&gt;

&lt;p&gt;Secondly, in a static typed language, it will need to examine the types involved.&lt;/p&gt;

&lt;p&gt;Functions have a return type, and each argument has a type as well - in a dynamic typed language these aren't present.&lt;/p&gt;

&lt;p&gt;The arguments you're using, and the way you store the return value, will have to be resolved against the function arguments. In static typed languages, this might result in implicit conversions. Many languages also have optional arguments, which have defaults when omitted.&lt;/p&gt;

&lt;p&gt;These details - the types, arguments, defaults and so on - are called the function signature. In a dynamically typed language, the signatures are of course vastly simpler - really, just the name and the "arity", or number of arguments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overloading
&lt;/h2&gt;

&lt;p&gt;Some languages provide overloading, where a single function name may have multiple signatures, and the language is free to pick the one that suits best. These are typically picked by name first, then number of arguments, and finally argument types. The obvious exemplar language is C++:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;called&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"I was called with "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;called&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"I was called with "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;caller&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;called&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;called&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"10"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;called&lt;/code&gt; here has multiple declarations with distinct types, and each declaration also has a definition, or "implementation". If you're seeing a common interface with multiple implementations and thinking "polymorphism", you're not wrong.&lt;/p&gt;

&lt;p&gt;Overloading gets a bad rap in some quarters but used well it's amazingly useful - in the code above, we're saving inefficient conversions and adding flexibility for the caller. But if we'd done something entirely different between the two overloads, that'd be very confusing.&lt;/p&gt;

&lt;p&gt;Functional languages often allow overloading based on more than just types - certain values, and the "shape" of the data, can be used to overload too.&lt;/p&gt;

&lt;p&gt;For example, here's a bit of Erlang which - if I've got this right - will run different implementations of the function depending on whether the array passed in is empty or not, eventually counting the members of the array in a wonderfully pointless and inefficient way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight erlang"&gt;&lt;code&gt;
&lt;span class="nf"&gt;array_count&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
  &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;array_count&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt; &lt;span class="nv"&gt;S&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nv"&gt;R&lt;/span&gt; &lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
  &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;array_count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;R&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript does not do overloading - but with a little effort you can do it yourself using a "dispatch function" pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;caller_number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Number variant&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;caller_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;String variant&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;caller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;arg&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&lt;/span&gt;&lt;span class="dl"&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="nf"&gt;caller_number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;caller_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Convert to string&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;TypeScript &lt;em&gt;does&lt;/em&gt; do overloading, but only with the signatures, and not the implementation. To the above, we'd prepend something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;caller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;caller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this is not true overloading, just a way to tell TypeScript how to manage the static typing involved.&lt;/p&gt;

&lt;h2&gt;
  
  
  Operators
&lt;/h2&gt;

&lt;p&gt;Operators are functions, too, of a sort.&lt;/p&gt;

&lt;p&gt;In some languages - like C - the operators represent purely mathematical operations which roughly correspond to machine code instructions - they'll never get compiled into calls like a traditional function call. Nevertheless, they possess many of the same attributes as a function.&lt;/p&gt;

&lt;p&gt;They have a name, such as &lt;code&gt;+&lt;/code&gt;. They have some arguments, which have types. They return a value, which, too, has a type.&lt;/p&gt;

&lt;p&gt;In higher-level languages, they're often heavily overloaded. Look at this JavaScript, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;World!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Concatenates the strings.&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Adds the numbers.&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some languages, like Python and C++, allow you to write your own special functions which are then used in overload lookup. For example, in C++ we could write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&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;r&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;This would then allow two strings to be concatenated just like JavaScript. In fact, C++ has done this for us anyway in the standard library - but unlike JavaScript this is some "ordinary" C++ code in the library (and you can go read it if you like).&lt;/p&gt;

&lt;h2&gt;
  
  
  Variables
&lt;/h2&gt;

&lt;p&gt;And just as operators can be functions, it turns out that functions can be variables, in turn - or at least, you can keep a function in a variable and pass it around.&lt;/p&gt;

&lt;p&gt;In the venerable C, this is done by treating the function name as a variable holding the memory address of the function implementation. The type of the variable is the function signature, sans name.&lt;/p&gt;

&lt;p&gt;JavaScript makes this simpler, as do a lot of languages, by having what amounts to a function literal. When we define a function, we're just defining a variable holding the function, a bit like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&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="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&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;Recent JavaScript has a simplified form (which has a few limitations):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is particularly helpful for using small anonymous functions as arguments to other functions, like &lt;code&gt;filter&lt;/code&gt; or &lt;code&gt;map&lt;/code&gt;. In these cases, such functions are normally known as "lambda functions", or simply "lambdas". Most modern languages have them, though they often have some limitations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Functional Programming
&lt;/h2&gt;

&lt;p&gt;Of course, I've managed an entire article on functions and barely mentioned functional programming.&lt;/p&gt;

&lt;p&gt;But that's because functional programming isn't about functions as in subroutines, but functions as in lambda calculus. Functional techniques can be (and often should be) used in any language, and modern languages capable of "procedural programming" can comfortably handle most of these.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Functions are the way we break code down into manageable, and reusable, units. Different languages provide different capabilities, like overloading, and they inherit features like static typing from their variables, too.&lt;/p&gt;

&lt;p&gt;A firm idea of how functions work and operate is important - likely if you're reading this you knew a lot of it already, but I hope this has helped settle things a bit.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Types</title>
      <dc:creator>Dave Cridland</dc:creator>
      <pubDate>Thu, 18 Mar 2021 12:42:48 +0000</pubDate>
      <link>https://forem.com/dwd/types-28ol</link>
      <guid>https://forem.com/dwd/types-28ol</guid>
      <description>&lt;h2&gt;
  
  
  Type
&lt;/h2&gt;

&lt;p&gt;Data isn't just bits. You'll have numbers, strings, and more in your code. A "type" is metadata used as a way of indicating what sort of data you have, and how it's going to be used. Passing data of the wrong type into a function is generally going to make things go badly wrong, so keeping tabs on this is important.&lt;/p&gt;

&lt;p&gt;You knew this already - but this is a deep dive into types, and I'd make this a series if I actually knew how, along with The Variable, and probably more to come.&lt;/p&gt;

&lt;h2&gt;
  
  
  O, say can you C?
&lt;/h2&gt;

&lt;p&gt;Yeah, so I know I tagged this with JavaScript. But first, I'm going to have to talk about C.&lt;/p&gt;

&lt;p&gt;For several decades, even across different types of CPU, all machines have used a flat memory model with a single address system for both code and data, with every byte being 8 bits (though we often read them as a group of bytes up to 64 bits).&lt;/p&gt;

&lt;p&gt;This means that just looking at a particular memory location in isolation, there's no real way to tell if something is an integer of 80, or a 'P' character, or (for IA32) the opcode for &lt;code&gt;PUSH EAX&lt;/code&gt; - the bits in memory are the same. An assembly programmer must simply remember where they had put what, and what it was for. But as symbolic languages came into vogue, remembering became the job of the language.&lt;/p&gt;

&lt;p&gt;C is a thin veneer of symbolic language over ASM. There are variations which are even closer - &lt;code&gt;C--&lt;/code&gt; for example - but C casually hands the programmer raw memory addresses and their contents.&lt;/p&gt;

&lt;p&gt;Types in C are essentially reminders to the programmer about what they decided to use a variable for. Sometimes, they're not even reminders:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'P'&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This compiles without error or warning!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;C has just five basic types (counting &lt;code&gt;bool&lt;/code&gt;, a recent addition) and three are just integers (including &lt;code&gt;char&lt;/code&gt;, which is normally used for character data). It supplements these with an address type (a "pointer") that is itself typed, a special "void" type, a "struct" type for building up records, and some modifiers to alter the width (ie, number of bytes).&lt;/p&gt;

&lt;p&gt;Thanks to (mostly) Claude Shannon, we know we can take these few types and process any information at all. Strings, in C, are just arrays of &lt;code&gt;char&lt;/code&gt; type integers treated as characters, for example - yet C does not have an actual string type at all.&lt;/p&gt;

&lt;p&gt;You can switch between several types at will in case you change your mind on what sort of data you meant, or how you want to treat it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;
&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'P'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sc"&gt;'Q'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Well of course it does.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most languages we use these days have a stricter view on what types mean, but fundamentally it's still about remembering what sort of data you have, and what you're meant to do with it. The distinction is who must remember - you or the computer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Variable type or data type?
&lt;/h2&gt;

&lt;p&gt;In C, the type of a value is only defined by the type used in the variable declaration you're using the manipulate the data, rather than the value itself. This "weak typing" provides the programmer with much opportunity for exciting errors. Getting the type wrong at runtime means hard-to-find bugs, crashes, or worse - many security exploits are based on treating the same data as different types at different times.&lt;/p&gt;

&lt;p&gt;This is, surprisingly, the same for C++ as well, despite its stronger typing - though C++ makes such mistakes much harder.&lt;/p&gt;

&lt;p&gt;In most modern languages, the data type is part of the value in some way - and sometimes not part of the variable declaration at all.&lt;/p&gt;

&lt;p&gt;So in weak typing, the type is bound to the identifier, and in strong typing, it's bound to the value - or even better, both.&lt;/p&gt;

&lt;p&gt;Note that there is no actual definition of "weak typing" versus "strong typing" - or rather, there are many. This one is mine.&lt;/p&gt;

&lt;p&gt;In JavaScript, a variable name might reference a string one moment, and later an integer - but either way the program will "know" at runtime, because the type is bound to the value. This is known as "dynamic typing".&lt;/p&gt;

&lt;p&gt;But this is confusing, both for the programmer (ie, you) and for the tooling. It's much easier to catch all sort of errors if the type is also bound to the variable declaration - a technique known as "static analysis", which a C compiler will give you for free.&lt;/p&gt;

&lt;p&gt;So there's a trend (particularly in imperative languages like JavaScript) to ensure a variable only ever references one type of data. This is known as "static typing", and so C is a "static typed" language with weak types, whereas Python and Javascript are "dynamic typed" languages with strong types. Typescript gives you static, strong types, and Python's type annotations give you much of static typing as well - both are actually dynamic typed at runtime though.&lt;/p&gt;

&lt;p&gt;The crucial thing is that whether the data is typed via the variable or intrinsically within the value, there is &lt;em&gt;always&lt;/em&gt; a type - you cannot have untyped languages beyond assembly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Type coercion and conversion
&lt;/h2&gt;

&lt;p&gt;While C is relaxed about types, there are times you want to explicitly change the type of data. One case is where you have an untyped memory address pointer - denoted as &lt;code&gt;void *&lt;/code&gt; - and you want to tell the compiler (and your future self) that you're going to store and access some specific type (characters, perhaps).&lt;/p&gt;

&lt;p&gt;This is done by "casting", a form of type coercion, where we decide as programmers that we know better than the compiler. Broadly speaking, we do not, so type coercion is considered a Bad Thing.&lt;/p&gt;

&lt;p&gt;In most cases, type coercion will not change the actual data at all - though in others it will truncate it, often violently.&lt;/p&gt;

&lt;p&gt;In TypeScript, we can do it by using "as", like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;my_foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_a_thing&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a pure coercion - no runtime checks are involved, we're simply overriding the static typing.&lt;/p&gt;

&lt;p&gt;Type conversion, on the other hand, creates an entirely new value of the requested type. Converting an integer to a string might render it in characters, for example. Conversion is always safe from the point of view of correctness, though implicit conversions the language does for you automatically can take you by surprise. Avoiding implicit conversion therefore becomes useful in languages which are particularly over-enthusiastic about conversions, and these languages typically have a &lt;code&gt;===&lt;/code&gt; operator and similar.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true!&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All the above fail when used with &lt;code&gt;===&lt;/code&gt; instead of &lt;code&gt;==&lt;/code&gt;. The string conversions to (or from) numeric strings into boolean values are particularly surprising.&lt;/p&gt;

&lt;p&gt;But the &lt;code&gt;===&lt;/code&gt; will not save you in all cases, since implicit conversions happen all over the place:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But note that this is &lt;em&gt;not&lt;/em&gt; coercion - this is an implicit type conversion.&lt;/p&gt;

&lt;p&gt;Another definition for a strongly typed language is that it won't allow coercion, only conversion (but note that TypeScript allows both, and by my definition is strongly typed).&lt;/p&gt;

&lt;h2&gt;
  
  
  Structure of Record
&lt;/h2&gt;

&lt;p&gt;C's &lt;code&gt;struct&lt;/code&gt; builds up composite types, which are types themselves. C++ builds on this further, and gives us &lt;code&gt;class&lt;/code&gt;, JavaScript gives us objects, and Typescript brings them formal type definitions with &lt;code&gt;interface&lt;/code&gt;. Other languages will give you other kinds of "record types".&lt;/p&gt;

&lt;p&gt;In all cases, a record has a list of "fields", which themselves have names, types, and values. In languages where we can treat the resulting record definition as a type in all respects, these are often called "user defined types", or "UDT" for short.&lt;/p&gt;

&lt;p&gt;You may note I've not mentioned methods here - but this is an article about types, and types alone. Object orientation is another matter, for another article. That said, classes are often the same as a "dumb" record type.&lt;/p&gt;

&lt;p&gt;JavaScript is a bit weird on this, mind - the type of any object, of any class, is "object", yet classes can and do exist.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;oo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="err"&gt;{};
&lt;/span&gt;&lt;span class="nc"&gt;ooo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;oo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;oo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// "function"&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;ooo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// "object"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Types and Shapes
&lt;/h2&gt;

&lt;p&gt;Some languages - particularly functional ones - tend not to care so much about types beyond the level that C does, but do worry about shape.&lt;/p&gt;

&lt;p&gt;So if a data structure has "the right bits", then it can be treated interchangeably with a particular type.&lt;/p&gt;

&lt;p&gt;JavaScript's history means that a lot of this practice resonates with TypeScript, and you'll see echoes of it throughout the language design. Other attempted to introduce formal typing into JavaScript went even further along this line of thought.&lt;/p&gt;

&lt;p&gt;If you look at, say, Erlang, you can treat different values as distinct types, too - this can be astoundingly useful. So, a record with a "foo" field of "bar" can be treated as a different type to one with a field of "baz" - and we can do this even when other times, we'll treat them the same.&lt;/p&gt;

&lt;h2&gt;
  
  
  Plain Old Data
&lt;/h2&gt;

&lt;p&gt;In some languages, not all types are equal. In C++, there's a concept called "POD types", for "Plain Old Data", for example. These are unlike more complex classes and are just the C value types (bool, char, int, float, double and their relations).&lt;/p&gt;

&lt;p&gt;JavaScript has "primitive" types; &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;string&lt;/code&gt; and so on. These are broadly similar to C++'s POD types. In the case of JavaScript, this is made hellishly confusing because there's both &lt;code&gt;string&lt;/code&gt; (a primitive type) and &lt;code&gt;String&lt;/code&gt; (a global object you can make instances of).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;A string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;s2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;A string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// "string"&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// "object"&lt;/span&gt;
&lt;span class="nx"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true - same value&lt;/span&gt;
&lt;span class="nx"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// false - different types&lt;/span&gt;
&lt;span class="nx"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;s2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true - `+` operator converted to primitive&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Types underpin everything else in programming. Because they're so fundamental to how we make computers anything more than giant calculators, gaining a solid understanding of types is a crucial step on the path from hobbyist to seasoned professional.&lt;/p&gt;

&lt;p&gt;Getting types wrong, at any stage, yields pain, bugs, extra work, and catastrophic failures.&lt;/p&gt;

&lt;p&gt;Static typing will help you, and the tools, to find these errors before you run the code. Strong typing helps catch these cleanly at runtime. But implicit conversions and the easily misused coercion can still bite you, even if you're using the &lt;code&gt;===&lt;/code&gt; operator.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Variable</title>
      <dc:creator>Dave Cridland</dc:creator>
      <pubDate>Sat, 13 Mar 2021 18:41:03 +0000</pubDate>
      <link>https://forem.com/dwd/the-variable-9l5</link>
      <guid>https://forem.com/dwd/the-variable-9l5</guid>
      <description>&lt;h2&gt;
  
  
  A Rose By Any Other Name
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's some code. It's JavaScript, but it might as well be any of a dozen (or more) other languages. Your challenge? Point to the variable.&lt;/p&gt;

&lt;p&gt;It seems easy, except that just because I've asked you, you're thinking this might be a trick question. And it sort of is.&lt;/p&gt;

&lt;p&gt;Let's start with the things that are not the variable for certain.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; is a form of declaration. It's definitely not a variable, but it does cause a new variable to be created.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;=&lt;/code&gt; is an operator, in this case it might be the assignment operator - but might also be an initialization operator, or even a match operator, in other languages. It's causing the variable, newly declared by &lt;code&gt;let&lt;/code&gt;, to be created with a particular value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;1 + 1&lt;/code&gt; is an expression, providing that value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;a&lt;/code&gt; is what we generally call a variable. But really, it's a name. In some languages (notably C) a variable name always points to a unique value - you cannot have one variable with two names for it - and this is still technically true in C++, which really tries very hard to muddy the waters. In others, including Javascript and Python, many names can point to the same value. In most languages (possibly all) you can have values with no names at all - and if you think this is esoteric, just link of an array: one name covering lots of values.&lt;/p&gt;

&lt;p&gt;So in some senses the variable doesn't exist in the source code at all. It is a value, held somewhere in the computer's memory, and the name merely references it - together, they make up the variable.&lt;/p&gt;

&lt;p&gt;"Memory" here is a nebulous term. It might be that this is an actual memory location, but it could also be a CPU register. Either way, the value might change over time, and the location might move, but the identity of the value never does.&lt;/p&gt;

&lt;h2&gt;
  
  
  By thy name I bind thee ...
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;phrase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;phrase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Goodbye!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;phrase&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Prints "Goodbye!"&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we've actually done in the first code is create a variable, initialize it with a value, and finally bind it to a name.&lt;/p&gt;

&lt;p&gt;Javascript allows us to later bind the variable to a new name. In this little snippet, we've bound the variable to &lt;code&gt;b&lt;/code&gt; as well. Changing the variable's value does just that - the change is visible through both bound names.&lt;/p&gt;

&lt;p&gt;We could also do other things, like rebinding the name to a different variable. Somewhat confusingly, Javascript does this using the same assignment operator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;phrase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;phrase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Goodbye!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;phrase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;What?&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;phrase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This one.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;--&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;phrase&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Prints "What?"&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the line marked with an arrow, we're not changing the variable (like we do in the line above), we're rebinding &lt;code&gt;a&lt;/code&gt;. This doesn't occur with, say, a number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Prints 1 0&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is so confusing that Javascript provides an alternate declaration keyword, &lt;code&gt;const&lt;/code&gt;, which prevents rebinding. In Java, this would be &lt;code&gt;final&lt;/code&gt;. It also makes numbers and other "primitive types" constant, like the &lt;code&gt;const&lt;/code&gt; keyword in C or C++.&lt;/p&gt;

&lt;p&gt;It's as if the designers of Javascript, faced with a confusing capability, decided to make it more confusing.&lt;/p&gt;

&lt;h2&gt;
  
  
  ... to my service unto death
&lt;/h2&gt;

&lt;p&gt;Values have a lifetime, whereas names have a scope. These two are often (but not always) interlinked.&lt;/p&gt;

&lt;p&gt;While the value exists, it takes up a chunk of the memory for the program (whereas names need not). The program can, if it has a reference to the value, read and change it.&lt;/p&gt;

&lt;p&gt;While the name is "in scope", the program source can use that name - once it's "out of scope" it will cause a syntax error.&lt;/p&gt;

&lt;p&gt;Javascript is, once more, odd here - so let's ignore it and pick the (surprisingly) simpler C.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="cm"&gt;/* Some stuff here */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In C, a variable name exists from the point of its declaration until the end of the block (the brace-enclosed statements). In earlier versions of C, variables had to be defined at the top of the block, but that was easy to work around since a block can be used anywhere a single statement can be (it's how &lt;code&gt;if&lt;/code&gt; statements work, for example), so if you needed to, you could nest a block. Modern C allows you to declare the variable anywhere.&lt;/p&gt;

&lt;p&gt;When the block is exited, the name falls out of scope and cannot be used anymore, and the value is instantly destroyed, its memory freed for use by something else.&lt;/p&gt;

&lt;p&gt;C++ makes this a bit more explicit, since if the value is an object, special methods are called when the value is created (the "constructor") and when it is destroyed (the "destructor"). This means you can trivially see when an object is destroyed, and actually do something.&lt;/p&gt;

&lt;p&gt;These values and variables - called "automatic variables" in C - are created on the program stack. You can create values with a different lifetime by creating them on the heap, but if you do this, you take responsibility for their lifetime entirely - the program will never destroy them unless you specifically ask it to. Equally, you don't create these values with a name - you'll instead get the memory location back (a kind of number, at least usually), and have to store that in turn as a more traditional variable somewhere.&lt;/p&gt;

&lt;p&gt;Many languages prefer not to make the destruction explicit in the same way - these are known as "garbage collection" languages. Java, Python, and Javascript are all like this - objects are created by the programmer explicitly, as normal, but the language itself decides when you're no longer using them. This usually happens automatically for the programmer (which is nice) but can occasionally be confused by circular references and other problems.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;friend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;friend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;friend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;friend&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Which cup is the ball under?&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, &lt;code&gt;a&lt;/code&gt; references a value which references another value which references itself. Deciding when these values can be discarded is tricky.&lt;/p&gt;

&lt;p&gt;But for the most part, this usually "just works".&lt;/p&gt;

&lt;p&gt;In the vast majority of languages, scope works in the same way - "local" variable names created within a function are visible from the point of declaration through to the end of the function. C's nested blocks mean that some names have a reduced sub-scope of that function. Calling another function creates a new, empty scope - the variable names from the caller's scope are not visible to the callee.&lt;/p&gt;

&lt;p&gt;Global variables - names created outside of a function - are "in scope" to everything, and since anything might change them unexpectedly, it's best to avoid these. Many languages have a module scope as well which behaves similarly.&lt;/p&gt;

&lt;p&gt;Member variables - more properly called "object fields" - are only in scope inside the methods for that object.&lt;/p&gt;

&lt;p&gt;Javascript is complex here, since the scope depends on how they're declared.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Implicit declaration&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Explicit declaration&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Let&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Const&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; both operate the same way for scope, which is largely the same way as C as described above.&lt;/p&gt;

&lt;p&gt;A minor difference here is that Javascript "hoists" the name creation (but not the value creation) to the beginning of the block. This is primarily of importance for the interview question, "What is Javascript variable hoisting?", and is otherwise pointless and confusing.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var&lt;/code&gt;, though, creates a new variable name - which is dutifully hoisted to the beginning of the scope - but which is visible through the entire function. This is pretty weird.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;call_me&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// aa actually created here.&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Caller start:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;aa&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;aa&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;aa&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;aa&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;--&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Caller end:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;aa&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;call_me&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might think that the line marked with an arrow declares a new variable - but it doesn't, it just assigns the existing one a new value.&lt;/p&gt;

&lt;p&gt;This behaviour is vital for, again, interview questions. Just use &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can also define a variable implicitly, by just assigning a value to the name. What this actually does, though, is define a new global variable (or module/file scope variable, strictly) - even if you're in a function. This is probably not what you expected to happen. Try this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;call_me_too&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;bb&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;bb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Weird, huh?&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bb&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;bb&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;call_me_too&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bb&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A summary
&lt;/h2&gt;

&lt;p&gt;The moral of the story is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;const&lt;/code&gt; - if you can - or &lt;code&gt;let&lt;/code&gt; - if you can't.&lt;/li&gt;
&lt;li&gt;Thank ESLint for finding this kind of stuff for you.&lt;/li&gt;
&lt;li&gt;Anything else is for answering interview questions.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Honest Security</title>
      <dc:creator>Dave Cridland</dc:creator>
      <pubDate>Tue, 08 Dec 2020 17:32:49 +0000</pubDate>
      <link>https://forem.com/dwd/honest-security-3bj9</link>
      <guid>https://forem.com/dwd/honest-security-3bj9</guid>
      <description>&lt;h2&gt;
  
  
  Honestly?
&lt;/h2&gt;

&lt;p&gt;Not that long ago, I was in a company working heavily in cybersecurity.&lt;/p&gt;

&lt;p&gt;One day, I started as usual, by opening my company-provided MacBook, and went to read the day's announcements. I'd just started to read—&lt;/p&gt;

&lt;p&gt;The screen blinked off.&lt;/p&gt;

&lt;p&gt;Surprised, I nudged the mouse, and sure enough, the screen came to life again, with a password prompt. Odd. I logged back in, found my place and started to—&lt;/p&gt;

&lt;p&gt;The screen blinked off &lt;em&gt;again&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;What the heck?&lt;/p&gt;

&lt;h2&gt;
  
  
  The User as the Problem
&lt;/h2&gt;

&lt;p&gt;Device Management solutions are pretty awful things. They enforce some arcane policy by changing your settings, usually without telling you. You, the user, have no control. In our case, we were a consultancy literally filled with experts in the cybersecurity industry, yet our laptops were working against us.&lt;/p&gt;

&lt;p&gt;It was simply infuriating. In this case, a bug in the device management solution had meant that in enforcing a screen timeout, it enforced a one minute screen timeout.&lt;/p&gt;

&lt;p&gt;This meant that we were unable to work without gently moving the mouse near constantly. Several of us gave up, and downloaded the source for an open source app that caused the mouse to "jiggle" when left alone, and defeated the errant software.&lt;/p&gt;

&lt;p&gt;If you think we were wrong, just bear in mind that we frequently had to give presentations to key customers. Having to change slides at least once a minute would be a challenging presentation style.&lt;/p&gt;

&lt;p&gt;But fundamentally, this situation arose because in the security world, the user is not trusted or involved. They are seen as part of the problem - not part of the solution. Surely, in our case at least, our team mates were an asset?&lt;/p&gt;

&lt;p&gt;In fact, aren't the staff always the front line for any organisation's security posture and device health?&lt;/p&gt;

&lt;h2&gt;
  
  
  The Insider Threat
&lt;/h2&gt;

&lt;p&gt;All too many cybersecurity firms - those with impressive front pages with pictures of green-lit, hoodie-wearing hackers - like to talk about The Insider Threat. In capitals, just like that.&lt;/p&gt;

&lt;p&gt;What they tend not to note is that the insider threat - while very real - comprises almost totally of people making honest mistakes. Trying to prevent mistakes by enforcing that the mistakes cannot be made has two problems. First, it is very complex - and, as we saw, prone to error. Secondly, it often damages the productivity of employees.&lt;/p&gt;

&lt;p&gt;Surely the best way to reduce errors like this is by inclusion and education - turning your staff into a security asset, rather than a liability?&lt;/p&gt;

&lt;p&gt;Surely security should be more than saying "No"?&lt;/p&gt;

&lt;p&gt;Plenty of security experts have already found, for example, that the best way to reduce the effectiveness of phishing attacks is to send phishing attacks to users periodically, gamifying the task of spotting and avoiding them.&lt;/p&gt;

&lt;p&gt;After all, this protects not only their corporate email, but their personal email, too - and you can bet that a clever attacker will target that, too. By involving users in their own security, therefore, you are protecting areas that enforcement could never hope to cover.&lt;/p&gt;

&lt;h2&gt;
  
  
  Working from Home - and back again
&lt;/h2&gt;

&lt;p&gt;As "Bring Your Own Device" and working from home builds momentum, the lines between corporate security and personal security blur to an unprecedented degree.&lt;/p&gt;

&lt;p&gt;Just as we don't want our employers to gather information on our home lives systematically, we obviously don't want them to gather information on our personal devices without our understanding and consent.&lt;/p&gt;

&lt;p&gt;For companies with staff in Europe, California, and other places around the world, this is a matter of more than idle concern. The GDPR makes gathering personal data without consent illegal. Perhaps worse, it requires companies to provide the data they do collect back to the user on demand.&lt;/p&gt;

&lt;p&gt;Clearly, then, the old model of blind draconian enforcement isn't sustainable, even if it were desirable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Security Leadership
&lt;/h2&gt;

&lt;p&gt;What's needed is a model of corporate security that works in the best - and most effective - traditions of leadership. As security leaders, we should draw our users with us, rather than trying to corral and drive them from behind.&lt;/p&gt;

&lt;p&gt;We need to reset the relationship users have with security. We can transform it into a positive force for not only the risk management of the company, but the personal safety of those we work with.&lt;/p&gt;

&lt;p&gt;This will make our users happier - and perhaps even more productive. But it will also reduce the risks from security failures to the company as a whole.&lt;/p&gt;

&lt;h2&gt;
  
  
  Honest Security
&lt;/h2&gt;

&lt;p&gt;Thoughts like these are behind the emergence of a new model of corporate security - "&lt;a href="https://honest.security/" rel="noopener noreferrer"&gt;Honest Security&lt;/a&gt;". Built around concepts like consent, transparency, and inclusional security practice, the intent is to reverse the adversarial posture of security versus user.&lt;/p&gt;

&lt;p&gt;I am not, I admit, the least cynical person on the planet. In the cybersecurity world, there's plenty to be cynical about, after all. I'm fully expecting a series of companies to jump on this bandwagon in name only.&lt;/p&gt;

&lt;p&gt;But if the outcome is that security becomes less of a barrier and more of an enabler, I'm all for it. If this is a buzzword, it's a buzzword to watch.&lt;/p&gt;

</description>
      <category>security</category>
      <category>honestsecurity</category>
    </item>
    <item>
      <title>Efficient INSERT MULTIPLE with Postgres</title>
      <dc:creator>Dave Cridland</dc:creator>
      <pubDate>Fri, 17 Jul 2020 22:37:26 +0000</pubDate>
      <link>https://forem.com/dwd/efficient-insert-multiple-with-postgres-3f06</link>
      <guid>https://forem.com/dwd/efficient-insert-multiple-with-postgres-3f06</guid>
      <description>&lt;h2&gt;
  
  
  Foreword
&lt;/h2&gt;

&lt;p&gt;I'd like to apologise for the lack of cover art. In my defence, I did search for a suitable image to illustrate "multiple insertion", and I feel it important to warn anyone else &lt;em&gt;never&lt;/em&gt; to do the same.&lt;/p&gt;

&lt;h2&gt;
  
  
  Look Familiar?
&lt;/h2&gt;

&lt;p&gt;Do you have code that looks like this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;new_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
  INSERT INTO foo(blah, wibble)
  VALUES($1, $2)
  RETURNING id&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;blah&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;wibble&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
  INSERT INTO other(foo_id, floogle)
  VALUES($1, $2)&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;new_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Maybe it's more complicated, maybe it has SELECT statements and all sorts.&lt;/p&gt;

&lt;p&gt;But it works. So why am I going to suggest changing it?&lt;/p&gt;

&lt;h2&gt;
  
  
  Round trips
&lt;/h2&gt;

&lt;p&gt;The moment we introduce a network connection, we end up with two key parameters for performance. There's "How long something takes to execute", and "How far is it away". The former we have some control over; we can write things more efficiently, add database indexing, boost instance sizes and whatever else needs doing.&lt;/p&gt;

&lt;p&gt;The latter, though, we have little control over. If the round-trip time to the database is 5ms, then that code above will take a minimum of 10ms, no matter how fast the database and app service is.&lt;/p&gt;

&lt;p&gt;There's other posts that will take you through the delights of query optimisation - I hope, and if not, then nag me and I'll write one.&lt;/p&gt;

&lt;p&gt;What we're going to do is make the round-trips go away - and maybe make things a bit faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  INSERT MULTIPLE
&lt;/h2&gt;

&lt;p&gt;Sadly, there's no such thing as &lt;code&gt;INSERT MULTIPLE&lt;/code&gt;. You can put multiple rows into a single table (&lt;code&gt;INSERT ... SELECT ...&lt;/code&gt;), but not a row into multiple tables.&lt;/p&gt;

&lt;p&gt;You could use a trigger here - that will absolutely work, but I find triggers that are beyond really trivial cases to be astonishingly difficult to test and debug.&lt;/p&gt;

&lt;p&gt;Instead, we need a technique called a "Modifying Common Table Expression", or "Modifying CTE".&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Table Expressions
&lt;/h2&gt;

&lt;p&gt;Normally, any sign of &lt;code&gt;WITH&lt;/code&gt; in a query has my hackles up. It's generally the sign of an overcomplex query on a schema not designed to service it.&lt;/p&gt;

&lt;p&gt;At their simplest, they look like a rephrasing of a sub-select JOIN:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;thing&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;blah&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;wibble&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%ibble'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;floogle&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;thing&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;foo_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the magic is that the &lt;code&gt;WITH&lt;/code&gt; causes the query to be a fully-named table - a Common Table Expression - so you could use it multiple times, or even within a second Common Table Expression.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;thing&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;blah&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;wibble&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%ibble'&lt;/span&gt;
&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="n"&gt;other_thing&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;foo_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;floogle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;blah&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;
  &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;thing&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;foo_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;foo_id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;other_thing&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;table_i_havent_mentioned_before&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;other_thing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The query optimizer will be clever here, and knows it needs&lt;code&gt;thing&lt;/code&gt; before &lt;code&gt;other_thing&lt;/code&gt; can be ready. But in practise, it'll also treat the entire query as a single SELECT, so this isn't that exciting.&lt;/p&gt;

&lt;p&gt;But what if we don't use a SELECT in the CTE, but an INSERT, UPDATE, or DELETE?&lt;/p&gt;

&lt;h2&gt;
  
  
  Modifying CTE
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;step_one&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blah&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wibble&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;VALUES&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foo_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;floogle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;step_one&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This does the same as the (pseudo) Python at the beginning.&lt;/p&gt;

&lt;p&gt;Postgres will run step_one first, as the outer statement depends on it, and then run the outer statement with the result - just as the Python did before it. Only this time, it's all in a single round-trip.&lt;/p&gt;

&lt;p&gt;Let's try something more complicated - how about four statements?&lt;/p&gt;

&lt;h2&gt;
  
  
  MOAR Modifying CTE
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;new_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
  INSERT INTO foo(blah, wibble)
  VALUES($1, $2)
  RETURNING id&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;blah&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;wibble&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;other_new_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
  INSERT INTO bar(blook)
  VALUES($1)
  RETURNING id&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;blah&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;wibble&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
  INSERT INTO other(foo_id, floogle, bar_id)
  VALUES($1, $2, $3)&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;new_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other_new_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we have some Python that executes two statements, one after the other, and inserts the new ids from both in a second table.&lt;/p&gt;

&lt;p&gt;In Python, we have to run one after the other - you can't run statements concurrently in a single transaction - so 3 round-trips, 3 statement executions. (Worse actually, since the transaction cost an additional 2 round-trips, for a total of 5.)&lt;/p&gt;

&lt;p&gt;But as a modifying CTE statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;step_one&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blah&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wibble&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;VALUES&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;step_two&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blook&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;VALUES&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foo_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;floogle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bar_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&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;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;step_one&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;step_two&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This uses just one round-trip - but also, step_one and step_two are executed concurrently, because they don't depend on each other... so only two statement execution times.&lt;/p&gt;

&lt;p&gt;Also, it's a single statement, so you no longer need a transaction for isolation.&lt;/p&gt;

&lt;p&gt;I admit, though, that the parameters are getting hard to track. Hey, I know what would solve this:&lt;/p&gt;

&lt;h2&gt;
  
  
  EVEN MOAR CTE!
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;my_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blah&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wibble&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;blook&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;floogle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;BIGINT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;step_one&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blah&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wibble&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blah&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wibble&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;my_data&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;
  &lt;span class="n"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;step_two&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blook&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blook&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;my_data&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;
  &lt;span class="n"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foo_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;floogle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bar_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;floogle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;step_one&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;step_two&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_data&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look! Variables! (Sorta!)&lt;/p&gt;

&lt;p&gt;Note that I've used a SQL cast to ensure my floogle value is the correct BIGINT type, though normally the libraries get these right for you - but for timestamps and things, it might get hard to figure out.&lt;/p&gt;

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

&lt;p&gt;Modifying Common Table Expressions are powerful tools for making a range of common patterns involve a lot less back-and-forth between the application service and your database.&lt;/p&gt;

&lt;p&gt;Not only that, but despite the arcane syntax, they can often be faster, and with a little effort, easier to read. They're also easier to debug than triggers.&lt;/p&gt;

&lt;p&gt;Have fun!&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>postgres</category>
    </item>
    <item>
      <title>Smart Pointers in C++</title>
      <dc:creator>Dave Cridland</dc:creator>
      <pubDate>Sun, 12 Jul 2020 14:59:27 +0000</pubDate>
      <link>https://forem.com/dwd/smart-pointers-in-c-31e8</link>
      <guid>https://forem.com/dwd/smart-pointers-in-c-31e8</guid>
      <description>&lt;h2&gt;
  
  
  Smart Pointers
&lt;/h2&gt;

&lt;p&gt;Smart Pointers are clever little things. Understanding them is key to writing solid C++, and this article aims to take you on an ever deeper dive into them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resource Acquisition Is Initialisation
&lt;/h2&gt;

&lt;p&gt;The golden rule of C++ is that the compiler will do a lot of work for you as long as you put your resource acquisition - particularly memory - into a constructor somehow, and ensure the resource is cleaned up by a destructor.&lt;/p&gt;

&lt;p&gt;The C++ compiler will then ensure that your resource, whatever it is, is always cleaned up when you exit the variable's scope - whether that was reaching the end of the scope normally, returning from a function call, having the parent object destroyed, or having an exception thrown.&lt;/p&gt;

&lt;p&gt;The technique of doing so is called Resource Acquisition Is Initialisation, or RAII for short. If you only know one thing about C++ best practises, it should be this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Memory
&lt;/h2&gt;

&lt;p&gt;So it's something of a surprise that ordinary pointers don't do this - instead, "bare pointers" do no clean-up at all. That makes code like this risky:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;p&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;We have to explicitly delete the object we created, and should the output generate an exception, we'll lose track of that memory entirely.&lt;/p&gt;

&lt;p&gt;This is a contrived example, of course - it's trivial to just use a stack object here, instead - but non-contrived examples are more difficult, so we'll stick with this.&lt;/p&gt;

&lt;h2&gt;
  
  
  A (Too) Trivial Smart Pointer
&lt;/h2&gt;

&lt;p&gt;Just tracking the memory is quite easy. We'll grab the pointer into an object at the earliest opportunity, and delete the object on destruction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;smart_string_pointer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;smart_string_pointer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="n"&gt;smart_string_pointer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;nullptr&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="n"&gt;smart_string_pointer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;m_ptr&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;So far, so good. I've defined the pointer (but not the string it points to) as const, to indicate that we don't actually want to change it.&lt;/p&gt;

&lt;p&gt;We can use this like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;smart_string_pointer&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="c1"&gt;// Erm?&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OK, so we can't get the pointer, which makes this a bit useless. Luckily, C++ gives us operator overloading to help here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m_ptr&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 can just use it like a normal pointer - if it's a const smart pointer, than the object is automatically const too.&lt;/p&gt;

&lt;p&gt;There's a similar overload available to us for the arrow operator too - we do that in the same way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;smart_string_pointer&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// p-&amp;gt;~smart_string_pointer() called here and cleans up.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this still has problems - the C++ compiler is going to be simply too helpful here, and create us a copy constructor, a move constructor, and assignment operators - all of which will copy the pointer. When the other object is destroyed, that means we'll have a pointer which has already been deleted.&lt;/p&gt;

&lt;p&gt;When we try to delete that a second time, we touch on what the standard calls &lt;em&gt;undefined behaviour&lt;/em&gt;, and that generally means the program crashes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;smart_string_pointer&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;smart_string_pointer&lt;/span&gt; &lt;span class="n"&gt;another_ptr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Works!&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// another_ptr-&amp;gt;~smart_string_pointer() called, deletes object.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// ptr-&amp;gt;~smart_string_pointer() called, crash!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're going to need to solve this. And that means deciding what to do when we try to copy (or move) a smart pointer.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Short Interlude About Templates
&lt;/h2&gt;

&lt;p&gt;I don't really want to have to make a new smart pointer type for every different type I'm pointing to. That means making it generic, by using templates.&lt;/p&gt;

&lt;p&gt;People think templates are complicated, and that's really not so. All a template is is just some code where there's a variable that contains a type.&lt;/p&gt;

&lt;p&gt;We use a different syntax for these because they're handled at compile-time, not runtime, but beyond a slightly unfamiliar syntax, that's it.&lt;/p&gt;

&lt;p&gt;So let's make this smart pointer we have nicely generic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;
&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;smart_ptr&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="k"&gt;explicit&lt;/span&gt; &lt;span class="n"&gt;smart_ptr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="n"&gt;smart_ptr&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;-&amp;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;m_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&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;m_ptr&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="n"&gt;smart_ptr&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;m_ptr&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;I've included the arrow operators this time, and also made the pointer constructor &lt;code&gt;explicit&lt;/code&gt;, which prevents it being used in object conversions we don't ask for.&lt;/p&gt;

&lt;p&gt;OK? Let's move on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Move It!
&lt;/h2&gt;

&lt;p&gt;If we intend that a smart_ptr's object can be moved into another one - useful for being able to return them from functions, for example - we can do that by overloading the move constructor and assignment operators:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nf"&gt;smart_ptr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;smart_ptr&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;smart_ptr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;smart_ptr&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;swap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;smart_ptr&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;smart_ptr&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;smart_ptr&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;m_ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;swap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;m_ptr&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;So, we need to delete any pointer we have, copy the new pointer from the smart_ptr being moved, and then set the smart_ptr's one to nullptr.&lt;/p&gt;

&lt;p&gt;You'll see I'm not doing that, quite - instead I set my own pointer to nullptr and swap them, since this is a little safer.&lt;/p&gt;

&lt;p&gt;Also, I've told the compiler not to generate implicit copy functions.&lt;/p&gt;

&lt;p&gt;If we do this, we should also rename it, and then, with regret, throw it away - what we have there is a &lt;code&gt;std::unique_ptr&lt;/code&gt;, and it's a certainty that the one that comes with your compiler will be better written.&lt;/p&gt;

&lt;h2&gt;
  
  
  Copy It!
&lt;/h2&gt;

&lt;p&gt;If instead we want to be able to have lots of these smart pointers, all pointing at the same object, and copy them about happily, we're going to need to do something more clever.&lt;/p&gt;

&lt;p&gt;Because we're going to need to know when to delete the object, we'll need to track how many of these smart pointers exist - only when the last one is destroyed do we delete the pointer.&lt;/p&gt;

&lt;p&gt;Moreover, they'll need to all share the same counter.&lt;/p&gt;

&lt;p&gt;That's a job for yet another pointer... Let's look at just the simple constructor and destructor cases:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;shared_ptr&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;m_counter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="n"&gt;shared_ptr&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="k"&gt;explicit&lt;/span&gt; &lt;span class="nf"&gt;shared_ptr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="n"&gt;shared_ptr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shared_ptr&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;m_counter&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_counter&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m_counter&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="n"&gt;shared_ptr&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m_counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;m_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;m_counter&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, when we bring a new object under the control of this shared pointer, we create a counter to go alongside it. Whenever we copy it, we increment the counter. Whenever we get destroyed, we decrement it - if it falls to 0, there are no remaining shared_ptr instances pointing at the same pointer, so we also delete both object and counter.&lt;/p&gt;

&lt;h2&gt;
  
  
  A confession
&lt;/h2&gt;

&lt;p&gt;And that code, above, doesn't entirely work, and isn't very good anyway.&lt;/p&gt;

&lt;p&gt;The reasons are many and varied, and mostly subtle. When allocating the counter, for example, we might encounter an exception and then leak the original pointer.&lt;/p&gt;

&lt;p&gt;Luckily, the fix is trivial - if you need this kind of behaviour, just use &lt;code&gt;std::shared_ptr&lt;/code&gt;, which has a host of additional features.&lt;/p&gt;

&lt;h2&gt;
  
  
  And one more thing.
&lt;/h2&gt;

&lt;p&gt;A particular challenge left is the initial object creation. Exceptions thrown at the wrong moment can still leak memory, and we don't want that.&lt;/p&gt;

&lt;p&gt;The standard library includes a couple of useful helper functions for this. &lt;code&gt;std::make_unique&lt;/code&gt; creates (and returns) a &lt;code&gt;std::unique_ptr&lt;/code&gt; with the object you need initialized, and &lt;code&gt;std::make_shared&lt;/code&gt; does the same for &lt;code&gt;std::shared_ptr&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The arguments are the same as the constructors of the object you want, so you can do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&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;Or:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_shared&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;point&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&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;Happy smart pointering!&lt;/p&gt;

</description>
      <category>cpp</category>
    </item>
    <item>
      <title>Why my code looks nicer than yours</title>
      <dc:creator>Dave Cridland</dc:creator>
      <pubDate>Wed, 08 Apr 2020 19:30:33 +0000</pubDate>
      <link>https://forem.com/dwd/why-my-code-looks-nicer-than-yours-3lnd</link>
      <guid>https://forem.com/dwd/why-my-code-looks-nicer-than-yours-3lnd</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;If I wrote this post entirely in a
monospaced font, you'd probably
stop reading very quickly and
wonder what possessed me to
spend actual effort trying
to make my post less
pleasant to
read.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we won't even begin to ask why the line length was so much narrower than the space available. I mean... Why?&lt;/p&gt;

&lt;p&gt;But the truth of it is that, as programmers, we spend much of our working life painfully reading vast amounts of text that have been formatted and presented in the same way.&lt;/p&gt;

&lt;p&gt;Well, you do. I, on the other hand, do not.&lt;/p&gt;

&lt;p&gt;For I have a secret.&lt;/p&gt;

&lt;p&gt;Every time I setup a code editor, I change the font to a proportional one - usually Deja Vu Sans.&lt;/p&gt;

&lt;p&gt;And every time a colleague sees me do that, they stare.&lt;/p&gt;

&lt;p&gt;"You can't write code in a proportional font!" they cry.&lt;/p&gt;

&lt;p&gt;I ask them why not.&lt;/p&gt;

&lt;p&gt;"Because it won't line up!" is the answer.&lt;/p&gt;

&lt;p&gt;But you know what? It really does line up very well indeed - because unless you're using some really esoteric languages, you're only ever aligning the beginning of the line, and that means counting space characters. Even in a proportional font, leading space characters have a consistent fixed width.&lt;/p&gt;

&lt;p&gt;"It won't look right!"&lt;/p&gt;

&lt;p&gt;Oh, but it does. I admit that it takes a little getting used to - looking at code in something like &lt;code&gt;Courier&lt;/code&gt; is so ingrained into our psyche that code rendered anything like normal text does look a little weird at first. But after spending a bit of time with it, you realise a few things.&lt;/p&gt;

&lt;p&gt;Firstly, it's easier to read. And as a programmer, we read code far more than we write it. The reason why books, magazines, and almost everything else is laid out in proportional fonts is because they're easier to read like that - and we surely want our code to be pleasant to read?&lt;/p&gt;

&lt;p&gt;Typographical errors are easier to spot too, because the odd letter combinations actually jar a bit on the eyes.&lt;/p&gt;

&lt;p&gt;Secondly, proportional fonts fit a lot more on the page. Horizontally, it's very obvious - you'll overflow you mandated maximal line lengths (don't get me started) with ease, and your primary difficulty will be guessing whereabouts the line ought to end.&lt;/p&gt;

&lt;p&gt;Even vertically, a proportional font can fit a bit more in, because the font designs can be considerably clearer at smaller sizes.&lt;/p&gt;

&lt;p&gt;But enough of this - you're all convinced it'll look terrible.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbrrifydn509bdt9ox226.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbrrifydn509bdt9ox226.png" alt="Proportional Javascript" width="521" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Javascript (actually the window-size library), looking pretty.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fevrnph07xqawe74mhqch.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fevrnph07xqawe74mhqch.png" alt="Proportional C++" width="550" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;C++ (this time Metre) - did you even know it could look this beautiful?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwdw0645cpdf11pghixmv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwdw0645cpdf11pghixmv.png" alt="Proportional Erlang" width="636" height="239"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Erlang, looking... OK, it still looks ugly. But less ugly. And I'll bet you spot the spelling error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F45wevuwwf28d4dae87qq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F45wevuwwf28d4dae87qq.png" alt="Proportional Python" width="457" height="324"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even Python, where the strict layout actually matters to the code, looks great in proportional font.&lt;/p&gt;

&lt;p&gt;So do give it a go. You never know, you might like it.&lt;/p&gt;

&lt;p&gt;The only irony is that this post, of course, was carefully crafted in monospace...&lt;/p&gt;

</description>
      <category>programming</category>
    </item>
    <item>
      <title>Slow Data</title>
      <dc:creator>Dave Cridland</dc:creator>
      <pubDate>Mon, 06 Jan 2020 22:54:29 +0000</pubDate>
      <link>https://forem.com/dwd/slow-data-do1</link>
      <guid>https://forem.com/dwd/slow-data-do1</guid>
      <description>&lt;p&gt;A fair amount of my career has involved some pretty appalling networks. I don't mean just because I live in rural Wales - my broadband is actually pretty good - but because I work in "Critical Messaging", and that tends to get deployed where the network is basically awful.&lt;/p&gt;

&lt;p&gt;As a result, I giggle a bit when people start to talk about making their app work effectively "even on mobile", because mobile, to me, is an awful lot of bandwidth.&lt;/p&gt;

&lt;p&gt;Instead, I've spent time on satellite connectivity, and even HF radio. Satellites only go as slow as modems did when I first used the Internet, mind, but HF radio goes really, really slow. Just as bad, but in other ways, our app is deployed into hospitals, which seem almost designed to block network signals.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Slow is Slow?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffgs8hax6e9jf3xojtc6e.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffgs8hax6e9jf3xojtc6e.jpg" alt="A Snail" width="800" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Networks can be bad in any one of three ways. They can be low bandwidth - in fact, most people would expect a slow network to be slow because of bandwidth more than anything else.&lt;/p&gt;

&lt;p&gt;Or they can be slow because of high latency. Satellite connectivity used to be relatively high bandwidth, compared to home dial-up, but very high latency. You'll typically have come across this as a "high ping".&lt;/p&gt;

&lt;p&gt;Finally, they can just be unreliable. WiFi blackspots and so on can be really frustrating if you're trying to keep a connection up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Low Bandwidth
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnq8eczzptqzgo5rqk132.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnq8eczzptqzgo5rqk132.jpg" alt="Slow Network" width="570" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Bandwidth is how fast data can be sent - actually how many individually identifiable atoms of information can be sent each second.&lt;/p&gt;

&lt;p&gt;In general, low bandwidth is the easiest problem to deal with. Just send less data.&lt;/p&gt;

&lt;p&gt;In general, the actual number of bytes on the wire (I can be all proper and call them "octets" if you like) doesn't really matter, because there's signficant overhead from TCP and IP headers in general. But keeping your data into as few packets as possible does make quite a difference.&lt;/p&gt;

&lt;p&gt;Ultimately, though, if you've a lot of data to send (for example, a photograph), you'll just have to send it.&lt;/p&gt;

&lt;p&gt;In HTTP, it's worth taking a look at the size of requests and responses. Typically, most request and response bodies are far smaller than the headers that go along with them - HTTP/2 helps a lot here by removing repeated headers - this "amortizes" the cost of headers across several requests.&lt;/p&gt;

&lt;p&gt;In XMPP, it turns out that the messages are small and have very low overhead - switching from XML here into a binary format (like EXI, for example) makes surprisingly little difference.&lt;/p&gt;

&lt;h2&gt;
  
  
  High Latency
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqxf8k0vqyjaozhvv68ue.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqxf8k0vqyjaozhvv68ue.gif" alt="Please Wait" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Latency is how long an individual bit takes from leaving one machine to arriving at another. We rarely actually measure this - it's extremely difficult to measure, for one thing - instead we measure how long it takes to go from one machine to another and back again. This is known as the Round Trip Time, or RTT for short.&lt;/p&gt;

&lt;p&gt;Latency has a much higher range than bandwidth in modern networks. Bandwidth above about 40Mb/s doesn't make a huge difference for a single application, whereas latencies still have a visible human effect even when they drop below 30ms.&lt;/p&gt;

&lt;p&gt;In addition, networks vary hugely. A good DSL has comfortably low latency - I can ping a typical service in around 16.5ms from mine. 4G, though, leaps to 100ms. 3G is 300ms - the same as fast Satellite, like Iridium. X-Band has higher bandwidth, but you pay with 800ms of latency.&lt;/p&gt;

&lt;p&gt;If you think that's slow, try STANAG 5066, a military radio system used within NATO. While it typically runs at a delightfully nippy 2400b/s, that's only in one direction at a time. To get anything back in the other direction, you'll need to wait a whopping 30 seconds.&lt;/p&gt;

&lt;p&gt;And that's on an unladen connection - as the data send approaches the bandwidth limit, latencies skyrocket, since the data sits about waiting to be sent.&lt;/p&gt;

&lt;p&gt;You might be familiar with packet loss - this plays a part here too, mostly by manifesting itself as even higher latency. In general this is so low-level that we as application programmers never see actual packet loss.&lt;/p&gt;

&lt;p&gt;I'll go into this in a bit more detail later for HTTP and XMPP, but ultimately any time you make a request, you're going to have to wait around a full RTT before you see the response.&lt;/p&gt;

&lt;p&gt;HTTP and XMPP amortize the latency over several requests when possible, in slightly different ways. HTTP/1.1 uses "pipelining", where a sequence of requests is sent at once, before waiting for the responses, and these are replied to in order. XMPP uses concurrent requests, where each request can be responded to anytime. HTTP/2 also handles full concurrency.&lt;/p&gt;

&lt;p&gt;Of note is that HTTP/1.1 will cancel the pipeline and close the connection on any error (4XX or 5XX) - that means that the effective latency of a request cancelled this way can be huge.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unreliable
&lt;/h2&gt;

&lt;p&gt;Constantly switching networks means that any long-term connection is going to have to be re-established quite a lot.&lt;/p&gt;

&lt;p&gt;HTTP has the advantage here - being stateless, there's just TCP and TLS to reestablish. For XMPP, however, there's a lot more state, and we've had to develop tricks like &lt;a href="https://xmpp.org/extensions/xep-0198.html" rel="noopener noreferrer"&gt;XEP-0198&lt;/a&gt; to counter that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bandwidth Delay Product
&lt;/h2&gt;

&lt;p&gt;As a last little consideration, if a network drops (the WiFi goes away, or the 4G signal fades, or whatever), the data that could be "in flight" is given by Bandwidth x Latency. In high-performance networking, this becomes an issue concerned with TCP Window size tuning, but in my world, it translates into potential data loss every time a device switches bearer.&lt;/p&gt;

&lt;p&gt;XMPP's &lt;a href="https://xmpp.org/extensions/xep-0198.html" rel="noopener noreferrer"&gt;XEP-0198&lt;/a&gt; tackles this problem very well, but an HTTP pipeline filled with non-idempotent requests that goes missing could easily ruin your whole day.&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTP
&lt;/h3&gt;

&lt;p&gt;Looking at the timing diagram for HTTP gives you some idea of how long things can take on a bad connection. Here I'm counting in RTTs (and for simplicity, not that it matters, assuming that latency is symmetric):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhwmb3kumg9h9a1i27lwg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhwmb3kumg9h9a1i27lwg.png" alt="HTTP Startup, 5 RTTs" width="467" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, assuming DNS takes a single RTT (an A record lookup), then it'll take 4 RTTs before we can send the first request. We can pipeline afterward, but even so it's going to be 5 RTTs before we get a response.&lt;/p&gt;

&lt;p&gt;If that first request fails, or we lose the network after it, or we're waiting around too long (the connection will be closed anyway after a few seconds), a second request will need all those RTTS repeated.&lt;/p&gt;

&lt;p&gt;Luckily, this isn't quite true normally - firstly, the DNS lookup can usually be cached, so we can forget about that entirely. Secondly, TLS has some tricks up its sleeve if you're rapidly reconnecting, allowing another RTT to disappear:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyg4gpe0c6mqksm48fdcf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyg4gpe0c6mqksm48fdcf.png" alt="HTTP Resume, 3RTTs" width="354" height="319"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Reducing even further is possible - TLS 1.3 gives us "Zero Round-Trip" (0-RTT) handshakes, at a cost of increasing security risk, and QUIC (and HTTP/3) give us a replacement for TCP that reduces the handshake there, too.&lt;/p&gt;

&lt;h3&gt;
  
  
  XMPP
&lt;/h3&gt;

&lt;p&gt;XMPP is a stateful, connection-oriented protocol. This is very effective when we have a stable network and lots of interaction to do, because the connection will stay live for a long time - hours or even days, compared to less than a minute for typical HTTP - and clients only need a single connection, always to the same server.&lt;/p&gt;

&lt;p&gt;But this comes at a cost in terms of the connection setup. A cold setup is over twice as long as HTTP, and that's before considering getting to the meat of the protocol - presence and messaging:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnbw43agzuk2je8f97igb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnbw43agzuk2je8f97igb.png" alt="XMPP Cold Start, 12 RTTs" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point, we can start receiving some traffic - though a typical client will need to wait at least another RTT before getting presence and messages.&lt;/p&gt;

&lt;p&gt;Some of this is because XMPP uses more DNS than HTTP, of course, but some is because it uses inline negotiation for TLS, has a mandatory authentication step (here using a strong mechanism), and so on.&lt;/p&gt;

&lt;p&gt;As with HTTP, this shrinks down a bit with DNS caching and TLS session resumption. That would bring us from 12 RTTs to a mere 9. But there's clearly more we can do.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3lszpverdtnmwqsovao4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3lszpverdtnmwqsovao4.png" alt="XMPP Warm Start, 9 RTTs" width="685" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Working from left to right, we can get rid of the inline negotiation for TLS, and go to TLS directly, just as HTTP does. That's covered in &lt;a href="https://xmpp.org/extensions/xep-0368.html" rel="noopener noreferrer"&gt;XEP-0368&lt;/a&gt;, which ironically reverts to the same way the protocol used to work when it was called Jabber, before the IETF took it on. Direct, or immediate, TLS gains us another 2 RTTs - so we're now down to 7.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F28agayd5oa5b6sklmdsg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F28agayd5oa5b6sklmdsg.png" alt="XMPP Direct TLS, 7 RTTs" width="576" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Authentication in XMPP is pluggable, because it operates using the SASL framework (which is also used by IMAP, SMTP, and LDAP). There's plenty of 1-RTT authentication mechanisms available, and some don't compromise security too badly - I mean, no worse than anything used in HTTP. This brings us to 6.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frn3v4ruolnj3xia1co34.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frn3v4ruolnj3xia1co34.png" alt="XMPP 1-RTT Auth, 6 RTTs" width="531" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So far, we've only used widely-supported techniques. But if we move into more advanced - but also more experimental - territory we can save even more.&lt;/p&gt;

&lt;p&gt;The way SASL is wired into XMPP could also benefit from some improvement. Because XMPP is designed to be extensible, it allows us to replace even complex parts of the protocol like this - &lt;a href="https://xmpp.org/extensions/xep-0388.html" rel="noopener noreferrer"&gt;XEP-0388&lt;/a&gt;, known as SASL2, gives us a slightly more efficient, and much more extensible, SASL profile than the original standard. Just switching saves us another round-trip, so we're now down to 5.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkdpjtgoo45doaw5phe1h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkdpjtgoo45doaw5phe1h.png" alt="XMPP SASL2, 5 RTTs" width="478" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we're somewhat brave, we can actually reduce this further by caching the server's SASL configuration, pipelining that green bit in the middle. Technically this is frowned upon, but relatively safe, and saves us another RTT, so hey ho.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faigye2atm7tcc7r7nv0c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faigye2atm7tcc7r7nv0c.png" alt="XMPP Cache SASL Config, 4 RTTs" width="430" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is now faster than a cold-start HTTP, which is quite fun. We can reduce it even further, though, by using Instant Stream Resumption. A fairly common extension, &lt;a href="https://xmpp.org/extensions/xep-0198.html" rel="noopener noreferrer"&gt;XEP-0198&lt;/a&gt; allows us to switch connectivity if (for example) WiFi drops by using a token - this saves us all the additional round-trips that a complex IM client will need to do at some point. &lt;a href="https://xmpp.org/extensions/xep-0397.html" rel="noopener noreferrer"&gt;ISR&lt;/a&gt; takes this a step further and builds on SASL2 to give us a mere 3 RTTs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx2at0a1uen52atip965u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx2at0a1uen52atip965u.png" alt="XMPP ISR" width="430" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is now as fast as HTTP/1.1 or HTTP/2 will go without introducing risky compromises like TLS 1.3 0-RTT - which are considerably less risky with a solid SASL mechanism in play.&lt;/p&gt;

&lt;p&gt;Well, if only anyone was using either SASL2 or ISR quite yet, anyway - I did warn you these were experimental.&lt;/p&gt;

&lt;h2&gt;
  
  
  Slow
&lt;/h2&gt;

&lt;p&gt;When dealing with "long thin" networks - especially when these networks are unreliable - some challenges are unavoidable. There's nothing we can do to transfer a JPEG image any faster without compromising on visual quality.&lt;/p&gt;

&lt;p&gt;But latency is both a worse problem and one we can do a lot to help with. The cutting edge of both HTTP and XMPP has made a huge number of advancements in this area, and if you're working in this space (or just need very snappy connections), it's worth looking at the low level to ensure you're getting the value from your stack.&lt;/p&gt;

&lt;p&gt;And, as ever, if you are playing around with XMPP on awful networks, come and join the conversation at &lt;a href="https://xmpp.org/" rel="noopener noreferrer"&gt;the XSF&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>messaging</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Sending a Message</title>
      <dc:creator>Dave Cridland</dc:creator>
      <pubDate>Mon, 18 Nov 2019 16:23:18 +0000</pubDate>
      <link>https://forem.com/dwd/sending-a-message-mmc</link>
      <guid>https://forem.com/dwd/sending-a-message-mmc</guid>
      <description>&lt;h1&gt;
  
  
  How hard can it be?
&lt;/h1&gt;

&lt;p&gt;Messaging is something of a niche - you can find web developers by the truckload, but when you're after someone with messaging experience, there's really very few of us around.&lt;/p&gt;

&lt;p&gt;That's probably because messaging is so simple, right? All we need to do is take a message from one place and put it into another. How hard can that be?&lt;/p&gt;

&lt;h1&gt;
  
  
  A Discussion about what a Message is
&lt;/h1&gt;

&lt;p&gt;Messages can be anything - a heat sensor might emit the current temperature, or we might want to send log messages, or status updates.&lt;/p&gt;

&lt;p&gt;But it's probably easiest to consider the case of text chat, since we're probably all familiar with it.&lt;/p&gt;

&lt;p&gt;In its simplest form, a text message can be simply the text itself, whom it's from, and (probably) some indication of where to send it. We'll start with this, then.&lt;/p&gt;

&lt;p&gt;We could use any format to discuss this, but it's more useful to work with a concrete example, so I'm going to use the XML syntax of XMPP. XMPP is an Open Standard messaging protocol, and it's used more or less everywhere messaging becomes critically important, like the military, governments, and hospitals. Also, it's used quite heavily in games - Fortnite, for example. There are client libraries for every language, and lots of different servers to choose from too.&lt;/p&gt;

&lt;p&gt;XMPP uses addressing based on something that looks very like an email address with an optional "resource identifier" added onto the end (which, fact-finders, I'll leave out of the examples). There are differences between email addresses and those of XMPP, mostly around Unicode support (XMPP has it) and legacy support (XMPP doesn't need to support &lt;a href="https://tools.ietf.org/html/rfc2156" rel="noopener noreferrer"&gt;X.400&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;So here's a very simple text message in XMPP:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;message&lt;/span&gt; &lt;span class="na"&gt;from=&lt;/span&gt;&lt;span class="s"&gt;'me@myserver.net'&lt;/span&gt; &lt;span class="na"&gt;to=&lt;/span&gt;&lt;span class="s"&gt;'you@yourserver.net'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;Hey, this is my first message!&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/message&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an entirely legal XMPP message, with all the required metadata - all the stuff that would, in an email message, be in the headers. It's pretty small, and hopefully the XML won't put you off too much. Normal developers never have to deal with XML at all when using XMPP, any more than web developers have to deal with header parsing - but it's convenient to show, and XMPP's use of XML is relatively clean.&lt;/p&gt;

&lt;h1&gt;
  
  
  Loss, and Tragedy
&lt;/h1&gt;

&lt;p&gt;XMPP works over TCP - a reliable connection - but there's a lot that can go wrong.&lt;/p&gt;

&lt;p&gt;If we lose connectivity - if the WiFi goes down or the 4G signal drops - we can't easily know if the message got through before the network died entirely.&lt;/p&gt;

&lt;p&gt;Sometimes we do - TCP gives very strong guarantees in some cases, so we know that if we send a second message and that one gets through, the first one certainly did. But the guarantees of TCP are fundamentally about ordering and corruption rather than simple loss.&lt;/p&gt;

&lt;p&gt;Rapid network changes - as you get with a smartphone - make what used to be an edge case on desktop a nightmare on mobile. Dealing with other network types can be even worse - XMPP will operate over &lt;a href="https://xmpp.org/extensions/xep-0365.html" rel="noopener noreferrer"&gt;military radios&lt;/a&gt; which can only transmit or receive, not both, and take half a minute to switch modes.&lt;/p&gt;

&lt;p&gt;While someone not getting the message above is mildly irritating, the nature of where XMPP is used means that the outcomes can be fair worse than merely irritating. If we send a message about, for example, new medication for a patient, it's of critical importance we know if it was received.&lt;/p&gt;

&lt;h1&gt;
  
  
  Acknowledgements
&lt;/h1&gt;

&lt;p&gt;The simplest solution is for the receiver to say they got the message. We can handle this in XMPP by adding an extension. There's two we can use, either the older (and more widespread) &lt;a href="https://xmpp.org/extensions/xep-0184.html" rel="noopener noreferrer"&gt;Delivery Receipts&lt;/a&gt;, or the much newer &lt;a href="https://xmpp.org/extensions/xep-0333.html" rel="noopener noreferrer"&gt;Chat Markers&lt;/a&gt;. I'm going to discuss the latter, because it's a little more interesting from a theoretical standpoint.&lt;/p&gt;

&lt;p&gt;First, we're going to add in the additional metadata we need. If we're going to refer to a message by saying we received one, we'll need to have a way to identify which message we're talking about. XMPP handles this by a simple &lt;code&gt;id&lt;/code&gt; attribute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;message&lt;/span&gt; &lt;span class="na"&gt;from=&lt;/span&gt;&lt;span class="s"&gt;'me@myserver.net'&lt;/span&gt; &lt;span class="na"&gt;to=&lt;/span&gt;&lt;span class="s"&gt;'you@yourserver.net'&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;'1'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;Hey, this is my first message!&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/message&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a real implementation, we'd use a UUIDv4 or similar, but for this example we'll just use an integer counter.&lt;/p&gt;

&lt;p&gt;Now we need to indicate to the receiver that we support chat markers. We could do this by discovery - having the receiver ask our client directly - but it's simpler in our case to include this in every message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;message&lt;/span&gt; &lt;span class="na"&gt;from=&lt;/span&gt;&lt;span class="s"&gt;'me@myserver.net'&lt;/span&gt; &lt;span class="na"&gt;to=&lt;/span&gt;&lt;span class="s"&gt;'you@yourserver.net'&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;'1'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;Hey, this is my first message!&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;markable&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;'urn:xmpp:chat-markers:0'&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/message&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;XMPP uses namespaced elements for extensions like this. XML namespaces can get a bit wordy, so we use a URN namespace to keep them as small as possible. The good news is that you can create your own without risk of clashing, using a URL you control.&lt;/p&gt;

&lt;p&gt;When we receive such a message, we can respond by telling the sender where we are. But thanks to the ordering that TCP gives us (and XMPP builds on), we don't have to send a response to every message - we can just respond to the last one. Previous messages are guaranteed to be delivered if a subsequent one is.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;message&lt;/span&gt; &lt;span class="na"&gt;from=&lt;/span&gt;&lt;span class="s"&gt;'you@yourserver.net'&lt;/span&gt; &lt;span class="na"&gt;to=&lt;/span&gt;&lt;span class="s"&gt;'me@myserver.net'&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;'2'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;received&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;'urn:xmpp:chat-markers:0'&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;'1'&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/message&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There we go. When we receive this, we can show the message has been received (or displayed) by putting a couple of ticks next to the message.&lt;/p&gt;

&lt;p&gt;But this acknowledgement is, of course, a message (in the sense of "Messaging") as well.&lt;/p&gt;

&lt;p&gt;And so it, too, can be lost...&lt;/p&gt;

&lt;h1&gt;
  
  
  A Short Interlude about Storming Cities
&lt;/h1&gt;

&lt;p&gt;Imagine, for a moment, there is a fortress city, defended so well that a single army cannot hope to conquer it.&lt;/p&gt;

&lt;p&gt;Imagine, further, that there is not one, but two armies arrayed against it - one on each side of the city.&lt;/p&gt;

&lt;p&gt;Because the city is astride a river, and has the only bridge for miles within its walls, the general of each army can only communicate to the other by sending a messenger to sneak through the city.&lt;/p&gt;

&lt;p&gt;Attacking individually would leave the Generals' armies defeated utterly - in order to conquer it, they must attack at the same time. So all they have to do is send a message to the other General suggesting a time, and know it got through.&lt;/p&gt;

&lt;p&gt;But what if the message was lost? The first General would be defeated, so unless the first one knows the message got through, he will not attack. Since the second General knows this, they must send a message back, saying they got the first.&lt;/p&gt;

&lt;p&gt;But what if this acknowledgement was lost? The second General would attack, but the first might not, thinking the message hadn't got through. The solution is, of course, to acknowledge the acknowledgement, and ... can you see where this is going?&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/Two_Generals%27_Problem" rel="noopener noreferrer"&gt;Two Generals' Problem&lt;/a&gt; is an insoluble problem in messaging. It literally declares that there is no way for both sides to agree on the current state (or, more accurately, there is no way for two parties to simultaneously know the state of the other).&lt;/p&gt;

&lt;p&gt;So it looks as if, rather than messaging being quite simple, it's actually impossible. And impossible is quite hard.&lt;/p&gt;

&lt;h1&gt;
  
  
  If at first you don't succeed...
&lt;/h1&gt;

&lt;p&gt;We can try addressing this by sending messages more than once; but with human messages this becomes tricky quite fast. Humans do not react well to duplicated messages, as we don't typically include the metadata required to spot them at that level.&lt;/p&gt;

&lt;p&gt;XMPP's ordering rules really help, but they don't make things perfect (but if you don't have ordering rules at all, then things can go really very wrong in a critical messaging environment).&lt;/p&gt;

&lt;p&gt;Besides, we can't blindly resend all the time, since we'd never know when to stop with, for example, acknowledgements. And we can't just acknowledge acknowledgements forever, either - it'd never end.&lt;/p&gt;

&lt;p&gt;It would be useful if we could somehow fix TCP, WiFi, and all the rest so that sending a message was more reliable in the first place.&lt;/p&gt;

&lt;h1&gt;
  
  
  A Place Between Success and Failure
&lt;/h1&gt;

&lt;p&gt;When we send a message in XMPP, we don't actually send it to the other party.&lt;/p&gt;

&lt;p&gt;In common with most messaging systems, we send it to our server, and that takes responsibility for it and sends it onto the other party (or their server, in federated cases).&lt;/p&gt;

&lt;p&gt;This means that there's another, hidden party we can use, and it knows literally every detail of our connection. This in turn means we can change our connection a bit to make it considerably more reliable.&lt;/p&gt;

&lt;p&gt;First, we're going to stop considering a message as simply either "sent" or "not-sent". We're going to introduce a fuzzy state of "maybe-sent". Anytime we send a message over the TCP session, we'll place it into the "maybe-sent" state, and keep a copy.&lt;/p&gt;

&lt;p&gt;Now we need a way to find out what state that message ends up in. We can't do this instantly, but we can eventually. So we'll just ask the server periodically how many messages it got. This won't be a message in the XMPP sense - it'd be far too silly. Instead, it'll be a new thing (defined in Stream Management):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;r&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;'urn:xmpp:sm:3'&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is governed by the same ordering in the TCP session as our messages, so it's reliable in retrospect, just like the messages themselves. This means when the server receives this, it knows exactly how many messages have arrived, and can tell us:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;'urn:xmpp:sm:3'&lt;/span&gt; &lt;span class="na"&gt;h=&lt;/span&gt;&lt;span class="s"&gt;'1'&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Perfect - now we know the server has received our first message, and we can remove it (and any previous messages) from the "maybe-sent" and consider it "sent". It might not have got to the other party yet, of course - but it's no longer our responsibility.&lt;/p&gt;

&lt;p&gt;The UX for this is usually a single tick against the message - it was popularised by WhatsApp, which itself uses a private version of this same protocol developed against their weird WEP fork of XMPP.&lt;/p&gt;

&lt;p&gt;Of course, if we walk out of WiFi and switch to 4G, this still leaves messages in the "maybe-sent" state - so they might be lost (or might not be).&lt;/p&gt;

&lt;h1&gt;
  
  
  Where did you come from, where did you go?
&lt;/h1&gt;

&lt;p&gt;We can address this by asking the server when we reconnect. When negotiating the extension, we just say "Here's my previous session id. I got to here, what about you?"&lt;/p&gt;

&lt;p&gt;The server then tells us where it got to and resends any messages that we missed, and we do the same. This essentially resumes the session exactly where it broke, and means we've extended the ordering and reliability rules from TCP across to a new session. Shiny.&lt;/p&gt;

&lt;p&gt;The specification tells us how this works. We'd send something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;resume&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;'urn:xmpp:sm:3'&lt;/span&gt;
        &lt;span class="na"&gt;h=&lt;/span&gt;&lt;span class="s"&gt;'4'&lt;/span&gt;
        &lt;span class="na"&gt;previd=&lt;/span&gt;&lt;span class="s"&gt;'some-long-sm-id'&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the server responds with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;resumed&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;'urn:xmpp:sm:3'&lt;/span&gt; &lt;span class="na"&gt;h=&lt;/span&gt;&lt;span class="s"&gt;'1'&lt;/span&gt; &lt;span class="na"&gt;previd=&lt;/span&gt;&lt;span class="s"&gt;'some-long-sm-id'&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oh, phew! So the server &lt;em&gt;did&lt;/em&gt; get our message, even though we lost WiFi! Fantastic.&lt;/p&gt;

&lt;p&gt;Sometimes this too fails - maybe the server gave up waiting for us to reconnect - in which case our messages can be stuck in the "maybe-sent" state. The best option we have here depends on what the message is - some messages, like Chat Markers, can be resent very safely, whereas for human messages we might choose to flag this condition to the user instead, and let them decide.&lt;/p&gt;

&lt;h1&gt;
  
  
  What we know we know
&lt;/h1&gt;

&lt;p&gt;At this point, these two protocols are working in combination. We can be confident that when we send a message it'll (eventually) get to the recipient, and when we get a chat marker back, we know they'll have received everything up to that point.&lt;/p&gt;

&lt;p&gt;Chat Markers also tell us the messages have been displayed, and - just like text messages - they can be resent automatically if we lose the connection.&lt;/p&gt;

&lt;p&gt;The Two Generals Problem isn't solved in XMPP, of course - that would be impossible - but we have managed to make it a genuine edge case, even in very unstable network environments.&lt;/p&gt;

&lt;p&gt;XMPP achieves all this by building a multi-layered approach to message reliability, combining existing features like TCP's guarantees with both low-level machine acknowledgements and high-level human ones.&lt;/p&gt;

&lt;p&gt;The result is why XMPP is used in hospitals and battlefields - whether real or in a game.&lt;/p&gt;

</description>
      <category>messaging</category>
    </item>
    <item>
      <title>Access Control</title>
      <dc:creator>Dave Cridland</dc:creator>
      <pubDate>Fri, 26 Jul 2019 10:41:05 +0000</pubDate>
      <link>https://forem.com/dwd/access-control-472g</link>
      <guid>https://forem.com/dwd/access-control-472g</guid>
      <description>&lt;p&gt;That chap up there?&lt;/p&gt;

&lt;p&gt;He's called Tim. He's an enchanter, and, more germane to this article, he's also an access control mechanism.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enchanter Based Access Control
&lt;/h2&gt;

&lt;p&gt;Enchanter Based Access Control, or EBAC, isn't available to most developers. It needs a highly trained enchanter, a bridge, and some questions, and is a highly interactive solution.&lt;/p&gt;

&lt;p&gt;In general, we restrict interactivity to authentication, and leave authorization as a non-interactive solution.&lt;/p&gt;

&lt;p&gt;Plus, it's a little weird, in terms of an example. Let's talk about patient records in healthcare as our example instead. Patient records have lots of parts to them - there's data such as names and addresses, and also medical data, both current and historical. Since almost everyone gets some medical attention at some point, we also have lots of them.&lt;/p&gt;

&lt;p&gt;So, how do we protect access to this data?&lt;/p&gt;

&lt;p&gt;Authentication gets us to a proven identity, and sometimes that's good enough.&lt;/p&gt;

&lt;h2&gt;
  
  
  Identity Based Access Control
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpv71671wzvgd8l9a95m0.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpv71671wzvgd8l9a95m0.jpeg" alt="NHS Sample Identity Badge" width="290" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have an identity, that might be all you need. Alice and Bob have access to this data. Nobody else does.&lt;/p&gt;

&lt;p&gt;It's a perfectly good way of specifying Access Control Lists, or ACLs. But what happens when Alice leaves the organisation? We need to go through every ACL she's listed in and remove her. Alice's replacement also needs adding in - and that means tracking, somehow, what New Alice needs access to.&lt;/p&gt;

&lt;p&gt;So if you've a lot of data - and therefore a lot of ACLs - a little indirection might be useful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Role Based Access Control
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn8v0w4oq6s1qhn6lyl70.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn8v0w4oq6s1qhn6lyl70.jpeg" alt="The Doctor Role" width="275" height="183"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we could do is group people into types - from the data access perspective, anyway. Managers, Doctors, Developers, Admins, and so on. These different types are called Roles, and it means that your ACLs now just contain a set of Roles, rather than people.&lt;/p&gt;

&lt;p&gt;When Alice stops being a Doctor, we just remove that Role form her. A replacement gets the Doctor role. It's vastly simpler to manage.&lt;/p&gt;

&lt;p&gt;Often, you can mix and match - an ACL can contain a mix of Roles and Identities, because any Identity is a (specialized) Role.&lt;/p&gt;

&lt;p&gt;The trouble is, that still leaves you with a lot of ACLs, and fine-grained access control - where you might have thousands of records to manage - means thousands of ACLs against every record. Or, even, parts of records. Perhaps a little more indirection might help.&lt;/p&gt;

&lt;p&gt;What if you could describe the sensitivity of data, and then describe the trust and training of people to handle such data?&lt;/p&gt;

&lt;h2&gt;
  
  
  Rule Based Access Control
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F14uv1iy2jlxo0bsxxgsd.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F14uv1iy2jlxo0bsxxgsd.jpeg" alt="Labels in action" width="275" height="183"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's say that all patient records are "Sensitive". The personal information there - names, addresses, in particular - we'll add a tag of "Personal". The medical stuff, well let's just give that a tag of "Medical". So some of our data is now "Sensitive Personal", and some is "Sensitive Medical". We'll call these confidentiality labels, or just "labels" for short.&lt;/p&gt;

&lt;p&gt;Now, we say that Doctors and Nurses, when they're employed by the hospital, get mandatory training in how to handle sensitive information. They're also trained - and trusted - to handle personal and medical information. We call this a "clearance".&lt;/p&gt;

&lt;p&gt;Lab staff might only see the medical data, and other hospital staff might only see the personal - and their clearances reflect that.&lt;/p&gt;

&lt;p&gt;You might recognise these terms from films and TV shows - "Sensitive" would be a "Classification", if that helps.&lt;/p&gt;

&lt;p&gt;There's a number of standardized ways of handling this kind of mechanism, such as SDN.801(c) (A joint NIST/NSA private publication), STANAG 4774 (A NATO standard), and FIP-188 (A NIST public publication). I also wrote an MIT-licensed library to handle these, because I am insane and like insanely difficult problems which attract the attention of national security agencies. (Hi guys!).&lt;/p&gt;

&lt;p&gt;These also define another element - a "Policy", which lists the classifications and tags, and tells you how they can be combined.&lt;/p&gt;

&lt;p&gt;The Policy also adds two key features of a labelling system. Firstly, it tells you how to mark protected data. This means not only that the Doctor viewing some data is clearly informed about the sensitivity of the data, but we can also include some tags which aren't used for access control at all, and just provide information about the sensitivity of the data.&lt;/p&gt;

&lt;p&gt;Finally, the policy tells you how to how to translate a label into someone else's policy (when, for example, you want to send a patient record to a different hospital).&lt;/p&gt;

&lt;p&gt;When sending an email, or an instant message, people can put the right label onto the message both to ensure safety and convey the sensitivity - part of what's called "Originator Controlled Access Control", or ORCON -so messages outside the patient record are still protected.&lt;/p&gt;

&lt;p&gt;It's a powerful system, which probably explains why it's the system used by militaries and intelligence agencies around the world to protect their data, as well as being recommended by HL7 and other healthtech standards groups for medical data.&lt;/p&gt;

&lt;p&gt;But should any Doctor really be able to see any patient record? In the Netherlands, there was a recent scandal where a celebrity's patient records were looked at out of pure curiosity by medical staff (and others) who had no need at all.&lt;/p&gt;

&lt;p&gt;We could combine Rule Based Access Control (RBAC) with Role Based Access Control (RBAC - yes, I know), and give things both a label and an ACL. But that's really quite painful to manage. Patients move between wards and departments as their care progresses and changes, and managing this is again going to be painful.&lt;/p&gt;

&lt;p&gt;What if we could do this automatically, so when a patient gets transferred to a ward the ward staff automatically get the access they need?&lt;/p&gt;

&lt;h2&gt;
  
  
  Attribute Based Access Control
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffcmuycdyv7s7riw1u7vy.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffcmuycdyv7s7riw1u7vy.jpeg" alt="UNIVAC Mainframe" width="290" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ABAC uses the data itself to derive access control decisions on the fly. Its policy is almost a programming language in its own right, digging through the data representing the Resource, Environment, and Subject to decide if the Subject can access the Resource within the Environment.&lt;/p&gt;

&lt;p&gt;Standards such as XACML are frighteningly complex as a result, and just as Role Based Access Control can be used to implement Identity Based Access Control, XACML can be used to implement any access control system we've discussed so far, and several more we've not even thought of. Can Doctors only access Patients Records during working hours on approved equipment? Sure, we can do that.&lt;/p&gt;

&lt;p&gt;Of course, without XACML or some similar system, we can just code the policy in more traditional programming.&lt;/p&gt;

&lt;p&gt;The ABAC system needs to crunch a lot of data for this, of course - it needs to know if a nurse works on a particular ward, as well as if a patient is on that ward. It needs to know if a medical procedure is conducted by the same department as the consultant is working in. It needs to know if a Doctor is on-shift. It needs to know if their personal mobile phone has been approved for use.&lt;/p&gt;

&lt;p&gt;But, if you can get by the complexity, ABAC is astonishingly powerful.&lt;/p&gt;

&lt;p&gt;The downsides beyond complexity are more subtle. Rule Based Access Control gives a federated access control system via its translation capability - ABAC can't actually provide that. It also can't mark data, which is useful to indicate sensitivities beyond simple access control.&lt;/p&gt;

&lt;p&gt;So what's the best access control?&lt;/p&gt;

&lt;h2&gt;
  
  
  Everything Based Access Control
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftq1z7u8lkburcl993lft.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftq1z7u8lkburcl993lft.png" alt="All the things" width="280" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It turns out that all access control systems have advantages and disadvantages - but handily, we can use them in combination, and that mostly removes the disadvantages.&lt;/p&gt;

&lt;p&gt;ACLs remains the simplest solution we have for access control, especially when simplified further with roles. If you're not confident, or not able, to do anything else, just pick this.&lt;/p&gt;

&lt;p&gt;Combine these with labels and clearances, and you've provided immediate assurance that even if the ACL says someone can read data, they still can't if they've got had the right training, or are not trusted. In addition, you have the powerful marking, so useful for human understanding, and the federated controls.&lt;/p&gt;

&lt;p&gt;And both ACLs and Roles can be automatically assigned by code, using techniques from ABAC. Patient record updated to say they're on X Ward? Adjust the ACL automatically, and we're done. Environmental techniques from ABAC can be replicated by injecting device clearances, too.&lt;/p&gt;

&lt;p&gt;Access Control is a complex, and vital, problem in any non-trivial system. But it's also an area that has received considerable academic study as well as numerous pragmatic approaches from industry. As with almost any big architectural decision, the best answer is "mix sensibly", the worst is "mix badly", and the safe option is to pick the easy one. But don't use Tim. He's not actually that good.&lt;/p&gt;

</description>
      <category>security</category>
    </item>
  </channel>
</rss>
