<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>rafb</title>
    <subtitle>read a fucking book</subtitle>
    <link rel="self" type="application/atom+xml" href="https://rafb.eu/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://rafb.eu"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-10-28T00:00:00+00:00</updated>
    <id>https://rafb.eu/atom.xml</id>
    <entry xml:lang="en">
        <title>Design of C++ classes</title>
        <published>2026-10-28T00:00:00+00:00</published>
        <updated>2026-10-28T00:00:00+00:00</updated>
        
        <author>
          <name>Rafb</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafb.eu/blog/cpp-class-design/"/>
        <id>https://rafb.eu/blog/cpp-class-design/</id>
        
        <content type="html" xml:base="https://rafb.eu/blog/cpp-class-design/">&lt;h2 id=&quot;motivation&quot;&gt;Motivation&lt;/h2&gt;
&lt;p&gt;Inspired by
&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://www.youtube.com/watch?v=2OMRWPOSw9s&quot;&gt;How to write the perfect function (for humans who still write code)&lt;/a&gt;,
I would like to share how I write and design classes in &lt;code&gt;C++&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;A good &lt;code&gt;C++&lt;/code&gt; class&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;shows a clear interface in the header&lt;/li&gt;
&lt;li&gt;is testable&lt;/li&gt;
&lt;li&gt;and &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://xkcd.com/303/&quot;&gt;compiles fast&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&#39;s implement a simple class to do health checks using some protocol using a &lt;code&gt;tcp&lt;/code&gt; connection.
To do so we are going to use &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://think-async.com/Asio/&quot;&gt;&lt;code&gt;asio&lt;/code&gt;&lt;/a&gt;, a cross platform networking library.
Like many powerful &lt;code&gt;C++&lt;/code&gt; libraries it make heavy use of &lt;code&gt;template&lt;/code&gt;s, which resolves in slow compilation.&lt;/p&gt;
&lt;p&gt;Let&#39;s to a straight forward solution and then refactor it.&lt;/p&gt;
&lt;h2 id=&quot;classic-approach&quot;&gt;Classic Approach&lt;/h2&gt;
&lt;p&gt;The header, that shows the interface and capabilities of our class.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#ifndef&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; UUID_793DB3EF_DAD5_46CA_9E1F_0F95F0BE6EF3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; UUID_793DB3EF_DAD5_46CA_9E1F_0F95F0BE6EF3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;lt;asio/ip/tcp.hpp&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;lt;string_view&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;  service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  virtual&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; ~service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // `virtual` for testability&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  virtual bool&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; is_healthy&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;ip&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;tcp&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;socket&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; connect&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; make_request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; send_request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  bool&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; parse_response&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; response&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;io_context&lt;/span&gt;&lt;span&gt; io&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;ip&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;tcp&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;socket&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; client&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#endif&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The Implementation file&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;quot;service_health_checker.hpp&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;ip&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;tcp&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;socket&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make_connected_tcp_socket&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;io_context&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;io&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;string_view address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // unimplemented, for readability&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // here happens:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // - resolve address&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // - create socket&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // - connect&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;string_view address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    : client&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make_connected_tcp_socket&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;io&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)} {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;is_healthy&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  auto&lt;/span&gt;&lt;span&gt; request&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; make_request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  auto&lt;/span&gt;&lt;span&gt; response&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; send_request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; parse_response&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;response&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;ip&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;tcp&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;socket&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;connect&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // unimplemented, for readability&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make_request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // unimplemented, for readability&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;parse_response&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; body&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // unimplemented, for readability&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;three-issues-with-the-classic-approach&quot;&gt;Three issues with the classic approach&lt;/h2&gt;
&lt;p&gt;The first issue I want to focus on is the compilation-time!
Due to the &lt;code&gt;#include &amp;lt;asio.hpp&amp;gt;&lt;/code&gt; our compilation-time raises by several seconds, for any file that includes &lt;code&gt;service_health_checker.hpp&lt;/code&gt;.
Let&#39;s not forget that these includes are transitive. Each time we include &lt;code&gt;service_health_checker.hpp&lt;/code&gt; in another header &lt;code&gt;asio&lt;/code&gt; gets included.
After a while and many more features the project compilations times raise to more than thirty seconds (or worse).&lt;/p&gt;
&lt;p&gt;My second issue with the classic approach is that things are getting worse when we acknowledge that &lt;code&gt;asio&lt;/code&gt; is only used internally. Inside the class!
Its types are not part of the signatures of the public functions.
So let&#39;s assume &lt;code&gt;service_health_checker.hpp&lt;/code&gt; becomes part of a library. In that library that header gets exposed as a feature.
Now each executable using that library has to know and provide the dependency &lt;code&gt;asio&lt;/code&gt;, although it&#39;s an internal dependency. A implementation-detail. Terrible!&lt;/p&gt;
&lt;p&gt;My third issue with the classic approach is that we are exposing internal logic:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;ip&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;tcp&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;socket&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; connect&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; make_request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; send_request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; parse_response&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; response&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;io_context io&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;ip&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;tcp&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;socket&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; client&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can do better!&lt;/p&gt;
&lt;h2 id=&quot;a-better-approach&quot;&gt;A Better Approach&lt;/h2&gt;
&lt;p&gt;A classic approach, &lt;strong&gt;that I don&#39;t like&lt;/strong&gt;, is the &lt;code&gt;Pimpl&lt;/code&gt; (&lt;em&gt;Pointer To Implementation&lt;/em&gt;) solution.&lt;/p&gt;
&lt;p&gt;We hide the actual implementation behind a pointer:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;lt;memory&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;lt;string_view&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;  service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  virtual&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; ~service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  virtual bool&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; is_healthy&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  class&lt;/span&gt;&lt;span&gt; service_health_checker_impl&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;service_health_checker_impl&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; pimpl&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The interface of &lt;code&gt;service_health_checker&lt;/code&gt; is now much smaller an cleaner.
No heavy dependencies to &lt;code&gt;asio&lt;/code&gt; are now exposed in our header.
The internal variables and methods are gone too.
All we have is a single pointer to the actual implementation.&lt;/p&gt;
&lt;p&gt;Let&#39;s look at the implementation file:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;quot;service_health_checker.hpp&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;quot;service_health_checker_impl.hpp&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;string_view address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    : pimpl&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;span&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make_unique&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;service_health_checker_impl&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span&gt;address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)} {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;is_healthy&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // delegate all calls&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  return&lt;/span&gt;&lt;span&gt; pimpl&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;is_healthy&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We include the actual implementation in the &lt;code&gt;service_health_checker.cpp&lt;/code&gt; file, unlike in the classic approach, where we include it in the header file &lt;code&gt;service_health_checker.hpp&lt;/code&gt;.
Then we delegate all calls to the implementation.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;service_health_checker_impl.hpp&lt;/code&gt; now contains the content of the header from the classic approach.
The only difference is, that &lt;code&gt;service_health_checker_impl&lt;/code&gt; it derives from &lt;code&gt;service_health_checker&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;quot;service_health_checker.hpp&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;lt;asio/ip/tcp.hpp&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;lt;string_view&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; service_health_checker_impl&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; :&lt;/span&gt;&lt;span&gt; service_health_checker &lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;  service_health_checker_impl&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;  ~service_health_checker_impl&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  bool&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; is_healthy&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; override&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;ip&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;tcp&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;socket&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; connect&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; make_request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; send_request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  bool&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; parse_response&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; response&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;io_context&lt;/span&gt;&lt;span&gt; io&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;ip&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;tcp&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;socket&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; client&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is another file &lt;code&gt;service_health_checker_impl.cpp&lt;/code&gt; which I&#39;m gonna skip because it&#39;s just the contents of &lt;code&gt;service_health_checker.cpp&lt;/code&gt; from the classic approach
but instead of &lt;code&gt;service_health_checker&lt;/code&gt; it&#39;s implemented for &lt;code&gt;service_health_checker_impl&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;With the &lt;code&gt;Pimpl&lt;/code&gt; approach we solved all three issues we had with the classic approach.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We improved compilation speed. Unless we change the public interface of &lt;code&gt;service_health_checker&lt;/code&gt; we will only recompile &lt;code&gt;service_health_checker.cpp&lt;/code&gt; and &lt;code&gt;service_health_checker_impl.cpp&lt;/code&gt; on changes.&lt;/li&gt;
&lt;li&gt;We don&#39;t expose &lt;code&gt;#include &amp;lt;asio.hpp&amp;gt;&lt;/code&gt; to other &lt;code&gt;cpp&lt;/code&gt; files using &lt;code&gt;service_health_checker.hpp&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We have a clean interface in the &lt;code&gt;service_health_checker.hpp&lt;/code&gt;, not exposing any internal implementation details.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;However&lt;/strong&gt; now we got &lt;em&gt;four&lt;/em&gt; files for a single class!&lt;/p&gt;
&lt;p&gt;We can do even better!&lt;/p&gt;
&lt;h2 id=&quot;the-best-approach&quot;&gt;The best approach&lt;/h2&gt;
&lt;p&gt;We declare a interface in the header and add a factory function.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;lt;memory&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;lt;string_view&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  static&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;  make&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  virtual&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; ~service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  virtual bool&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; is_healthy&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The constructor got replaced by a factory function &lt;code&gt;make&lt;/code&gt;.
There are no member variables and &lt;code&gt;is_healthy&lt;/code&gt; became a &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://en.cppreference.com/cpp/language/abstract_class&quot;&gt;pure virtual function&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Let&#39;s look at the implementation file:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;quot;service_health_checker.hpp&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;lt;asio/ip/tcp.hpp&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;ip&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;tcp&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;socket&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make_connected_tcp_socket&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;io_context&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;io&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;string_view address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // unimplemented, for readability&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // here happens:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // - resolve address&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // - create socket&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // - connect&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; service_health_checker_impl&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; :&lt;/span&gt;&lt;span&gt; service_health_checker &lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;io_context&lt;/span&gt;&lt;span&gt; io&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;ip&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;tcp&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;socket&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; client&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  service_health_checker_impl&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;string_view address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      : client&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make_connected_tcp_socket&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;io&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)} {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  bool&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; is_healthy&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; override&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    auto&lt;/span&gt;&lt;span&gt; request&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; make_request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    auto&lt;/span&gt;&lt;span&gt; response&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; send_request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; parse_response&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;response&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;asio&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;ip&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;tcp&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;socket&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; connect&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // unimplemented, for readability&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; make_request&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // unimplemented, for readability&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  bool&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; parse_response&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; body&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // unimplemented, for readability&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;string_view address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make_unique&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;service_health_checker_impl&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span&gt;address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First I&#39;m deriving the public interface &lt;code&gt;struct service_health_checker_impl : service_health_checker&lt;/code&gt;.
Then I&#39;m declaring the variables. Due to the implementation happening in the &lt;code&gt;cpp&lt;/code&gt; file, I&#39;m not even considering using &lt;code&gt;public/protected/private&lt;/code&gt;.
I can go straight to business. When I need another method I just write it, no jumping between header/source files.&lt;/p&gt;
&lt;p&gt;For an even cleaner implementation one can wrap the &lt;code&gt;service_health_checker_impl&lt;/code&gt; in an &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://learn.microsoft.com/en-us/cpp/cpp/namespaces-cpp#anonymous-or-unnamed-namespaces&quot;&gt;anonymous namespace&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Instancing happens at the bottom in a static function.
Unlike in the classic and the &lt;code&gt;Pimpl&lt;/code&gt; we can&#39;t do a direct allocation.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// classic and `Pimpl` approach&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;auto&lt;/span&gt;&lt;span&gt; checker&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make_unique&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;example.com&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// factory function&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;auto&lt;/span&gt;&lt;span&gt; checker&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;example.com&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using this approach we successfully&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;improve compilation-time. When we change logic of &lt;code&gt;service_health_checker&lt;/code&gt; only &lt;code&gt;service_health_checker.cpp&lt;/code&gt; has to recompile.&lt;/li&gt;
&lt;li&gt;hide dependencies. In our example &lt;code&gt;#include &amp;lt;asio.hpp&amp;gt;&lt;/code&gt; happens in the implementation file, not in the header file.&lt;/li&gt;
&lt;li&gt;provide a clean header file, exposing exclusively methods and function interesting for the consumer.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The main other side-effect of this approach are that I can much easier reason about the code, because everything happens in a single file.
This makes searching and reading much easier.&lt;/p&gt;
&lt;h2 id=&quot;a-word-about-factory-functions&quot;&gt;A word about factory functions&lt;/h2&gt;
&lt;p&gt;Factory functions have some nice side-effects. For example, let&#39;s assume we later want to refactor the &lt;code&gt;service_health_checker&lt;/code&gt;
to be asynchronous.
To do so the class internal uses a &lt;code&gt;shared_ptr&lt;/code&gt; to itself in the callbacks.
This gets accomplished by using &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://en.cppreference.com/cpp/memory/enable_shared_from_this&quot;&gt;&lt;code&gt;std::enable_shared_from_this&lt;/code&gt;&lt;/a&gt;.
Now we can enforce and ensure that the class get&#39;s instanced using the factory-function:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;shared_ptr&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;string_view address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make_shared&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;service_health_checker_impl&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span&gt;address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;C++&lt;/code&gt; type system handles the rest of the refactoring.&lt;/p&gt;
&lt;p&gt;Another use-case could be different instances dependent on parameters of flags. For example:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;service_health_checker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;string_view address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;rfind&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;tcp://&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make_unique&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;service_health_checker_tcp&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span&gt;address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make_unique&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;service_health_checker_generic&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span&gt;address&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;In conclusion I like to design my business logic as a pure virtual class in a header.
Then implement it with all functions and methods in the implementation file.
Instancing happens using factory functions.&lt;/p&gt;
&lt;p&gt;This results in:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a clear interface for users of the class&lt;/li&gt;
&lt;li&gt;fast compilation&lt;/li&gt;
&lt;li&gt;better readability and maintainability of the code&lt;/li&gt;
&lt;/ul&gt;
</content>
        
    </entry>
</feed>
