<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<title type="text">Components Programming</title>
<link rel="self" type="application/atom+xml" href="https://componentsprogramming.com/feed.xml" />
<link rel="alternate" type="text/html" href="https://componentsprogramming.com/" />
<subtitle type="text">Efficient programming using Components.</subtitle>
<generator uri="https://github.com/mojombo/jekyll">Jekyll</generator>
<icon>https://componentsprogramming.com/favicon.ico</icon>
<logo>https://componentsprogramming.com/icon.png</logo>
<updated>2026-04-05T09:46:55-03:00</updated>
<id>https://componentsprogramming.com/</id>
<author>
  <name>Fernando Pelliccioni</name>
  <uri>https://componentsprogramming.com//</uri>
  <email>fpelliccioni@gmail.com</email>
</author>



<entry>
  <title type="html"><![CDATA[C++26 Wraps Up: Notes from the Croydon WG21 Meeting]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//cpp26-croydon-wg21-meeting/" />
  <id>https://componentsprogramming.com//cpp26-croydon-wg21-meeting</id>
  <published>2026-04-05T09:00:00-03:00</published>
  <updated>2026-04-05T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;figure&gt;
&lt;img src=&quot;https://componentsprogramming.com/images/wg21-2026-croydon.png&quot; alt=&quot;WG21 group photo at the Croydon meeting, March 2026&quot; /&gt;
&lt;figcaption&gt;Photo credit: Herb Sutter&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The ISO C++ committee wrapped up C++26 on March 28 in Croydon, UK. Around 210 people participated (130 in person, 80 remote) representing 24 nations, resolving the last of 411 national body comments. From here the document goes to its final approval ballot. No more features, no more additions. C++26 is done.&lt;/p&gt;

&lt;p&gt;This was my first in-person WG21 meeting. I’ve been a member since 2022 and attended remotely before, but I’ve followed the language’s evolution since the C++0x era, when “0x” was becoming a running joke and nobody was sure it would ship. Being there for the closing of C++26 felt like the right moment to finally show up.&lt;/p&gt;

&lt;h2 id=&quot;contracts-approved-debated-divisive&quot;&gt;Contracts: Approved, Debated, Divisive&lt;/h2&gt;

&lt;p&gt;Contracts made it into C++26. The final vote was 114 in favor, 12 opposed, 3 abstaining. That level of opposition is unusual for a feature at this stage, and the debate in the room was tangible.&lt;/p&gt;

&lt;p&gt;My perspective on contracts comes from Eiffel and its Design by Contract philosophy. In that model, contracts are part of program semantics. A precondition violation means the caller has a bug. A postcondition violation means the callee has a bug. Responsibility is unambiguous, and violations are always bugs, never runtime situations to be handled flexibly.&lt;/p&gt;

&lt;p&gt;C++ took a more configurable path. That’s not necessarily wrong; C++ has always been a language of options. But the added flexibility comes at a visible cost in conceptual simplicity. The gap between what C++ calls “contracts” and the original Design by Contract philosophy is wider than I expected.&lt;/p&gt;

&lt;h2 id=&quot;reflection-the-big-one&quot;&gt;Reflection: The Big One&lt;/h2&gt;

&lt;p&gt;If contracts were the most debated feature, reflection was the most transformative.&lt;/p&gt;

&lt;p&gt;C++26 reflection enables compile-time introspection of types, members, and program structure, things that previously required external tooling or heavy template metaprogramming. GCC already has an implementation merged in trunk.&lt;/p&gt;

&lt;p&gt;This changes what’s possible inside the language. Not incrementally, but categorically. The way we write libraries, generate code, and express abstractions is going to look different. I think we’ll be discovering new patterns around reflection for a long time.&lt;/p&gt;

&lt;h2 id=&quot;the-evening-session-that-changed-my-direction&quot;&gt;The Evening Session That Changed My Direction&lt;/h2&gt;

&lt;p&gt;One evening session was about a structural problem that’s been building for years: there are far more proposals than there is implementation capacity. Compiler teams at Clang, GCC, and MSVC are stretched thin. The bottleneck is no longer design. It’s implementation. Features get approved on paper and then take years to become usable.&lt;/p&gt;

&lt;p&gt;Several ideas came up: that paper authors should bring at least partial implementations, that proposals should be tested against reality before advancing, and even that AI-assisted tooling could help lower the barrier. That last one triggered a visible reaction in the room: murmurs, people speaking out of turn, frustration. It was a brief moment, but telling.&lt;/p&gt;

&lt;p&gt;What stayed with me was the core message from the implementers. The next day, I started contributing to both Clang and GCC, at the library and compiler level. Not out of any grand plan, but because the message landed: if you care about the language moving forward, help build it.&lt;/p&gt;

&lt;h2 id=&quot;being-there&quot;&gt;Being There&lt;/h2&gt;

&lt;p&gt;There’s something about being in the room that remote attendance can’t replicate. The hallway conversations, the body language during debates, the energy when a contentious vote lands.&lt;/p&gt;

&lt;p&gt;C++ keeps getting more powerful, and the cost of keeping up is real. But the community behind it is deeply invested in getting it right, and that was evident in Croydon.&lt;/p&gt;

&lt;p&gt;I’m glad I was there for this one.&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//cpp26-croydon-wg21-meeting/&quot;&gt;C++26 Wraps Up: Notes from the Croydon WG21 Meeting&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on April 05, 2026.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Thanks Alex, once again!]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//thanks-alex-once-again/" />
  <id>https://componentsprogramming.com//thanks-alex-once-again</id>
  <published>2020-02-20T09:00:00-03:00</published>
  <updated>2020-02-20T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;&lt;img src=&quot;https://componentsprogramming.com/images/FormularioMathematico.jpeg&quot; alt=&quot;FormularioMathematico&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Once again, I would like to thank my friends Alex Stepanov and Paul McJones for this beautiful gift.&lt;/p&gt;

&lt;p&gt;I have been looking for this book for years. It is a very difficult book to get.&lt;/p&gt;

&lt;p&gt;I have no words to thank you.&lt;/p&gt;

&lt;p&gt;Thank you!&lt;/p&gt;


  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//thanks-alex-once-again/&quot;&gt;Thanks Alex, once again!&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on February 20, 2020.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Algorithms and specifications]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//algorithms-specifications/" />
  <id>https://componentsprogramming.com//algorithms-specifications</id>
  <published>2020-02-19T09:00:00-03:00</published>
  <updated>2020-02-19T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;In this article I want to talk about 2 topics in which I think that most programmers tend to fail: Algorithms and Specifications.&lt;/p&gt;

&lt;p&gt;I’m going to use a real experience, which happened some time ago:&lt;/p&gt;

&lt;p&gt;For 4 years now I have maintained a multi-currency node (Bitcoin, Bitcoin Cash and Litecoin) called &lt;a href=&quot;https://github.com/k-nuth/kth&quot;&gt;Knuth&lt;/a&gt; (a.k.a. Bitprim).
In November 2017 &lt;a href=&quot;http://bitcoincashnode.org/&quot;&gt;Bitcoin Cash&lt;/a&gt; made its first protocol change after its birth in August of the same year. My job at that time was to update the code of our node to support the protocol changes. From that moment I want to write this article, but … for one or several reasons I did not do it at that moment, I am doing it now.&lt;/p&gt;

&lt;p&gt;The most important change was in the &lt;em&gt;Difficulty Adjustment Algorithm&lt;/em&gt;, from now &lt;em&gt;DAA&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/nov-13-hardfork-spec.md#difficulty-adjustment-algorithm-description&quot;&gt;Here the description of the algorithm&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I do not want to go into detail about the concept of difficulty or the DAA. For this you can refer to: &lt;a href=&quot;https://en.bitcoin.it/wiki/Difficulty&quot;&gt;Difficulty&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What interests me are points 2 and 3 of the description of the DAA:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;mf&quot;&gt;2.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B_last&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chosen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;B_n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B_n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B_n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;
&lt;span class=&quot;mf&quot;&gt;3.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B_first&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chosen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;B_n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;146&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B_n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;145&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B_n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;144&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Both point to the footnote &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[2]&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;mf&quot;&gt;2.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chosen&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;via&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;following&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mechanism&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Given&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;B_n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B_n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B_n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;If&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;greater&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;than&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;If&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;greater&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;than&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;If&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;greater&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;than&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;See&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetSuitableBlock&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The specification of the algorithm points to its implementation, in a function called &lt;a href=&quot;https://github.com/Bitcoin-ABC/bitcoin-abc/commit/be51cf295c239ff6395a0aa67a3e13906aca9cb2#diff-ba91592f703a9d0badf94e67144bc0aaR208&quot;&gt;GetSuitableBlock&lt;/a&gt;. Here the code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/**
 * To reduce the impact of timestamp manipulation, we select the block we are
 * basing our computation on via a median of 3.
 */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CBlockIndex&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetSuitableBlock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CBlockIndex&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pindex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pindex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nHeight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;cm&quot;&gt;/**
    * In order to avoid a block is a very skewed timestamp to have too much
    * influence, we select the median of the 3 top most blocks as a starting
    * point.
    */&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CBlockIndex&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pindex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pindex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pprev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pprev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Sorting network.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

     &lt;span class=&quot;c1&quot;&gt;// We should have our candidate in the middle now.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;What the algorithm does is basically create a sequence of 3 elements (array), order it from least to greatest and return the second element.&lt;/p&gt;

&lt;p&gt;The complexity in time of this algorithm is:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Best case: 0 swaps, 3 comparisons&lt;/li&gt;
  &lt;li&gt;Worst case: 2 swaps, 3 comparisons&lt;/li&gt;
  &lt;li&gt;Average case: 7/6 swaps, 3 comparisons; assuming a uniform distribution of the input data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, look again at the algorithm. An array is being created (using the input data), then sort it up and return the middle element. This is a known algorithm and is called &lt;a href=&quot;https://en.wikipedia.org/wiki/Median&quot;&gt;median&lt;/a&gt;, in particular, &lt;em&gt;median of 3 elements&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The median is a &lt;em&gt;selection&lt;/em&gt; algorithm. Unlike the sorting (inplace) algorithms, the selection algorithms should not mutate the input data, but return one of the elements.&lt;/p&gt;

&lt;p&gt;Here is a sketch of the &lt;em&gt;median of 3&lt;/em&gt; algorithm, in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C++&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotallyOrdered&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotallyOrdered&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;median_3_ab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// precondition: a &amp;lt;= b&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;// a, b, c are sorted&lt;/span&gt;
                       &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// b is not the median&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotallyOrdered&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;median_3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;median_3_ab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                 &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;median_3_ab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Or if you prefer the &lt;em&gt;inline&lt;/em&gt; version of the algorithm:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotallyOrdered&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;median_3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// b, a, c are sorted&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;// a is not the median&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// a &amp;lt;= b&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// a, b, c are sorted&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;// b is not the median&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;I leave the analysis of the code for the reader, for the lazy: what the algorithm does is simply select the middle element between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c&lt;/code&gt;, pretending that the 3 were sorted in ascending order. It does this without mutating or reordering the input data.&lt;/p&gt;

&lt;p&gt;The time complexity of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;median_3&lt;/code&gt; is:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Best case: 0 swaps, 2 comparisons&lt;/li&gt;
  &lt;li&gt;Worst case: 0 swaps, 3 comparisons&lt;/li&gt;
  &lt;li&gt;Average case: 0 swaps, 8/3 comparisons; assuming a uniform distribution of the input data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, we could use our new algorithm in the original &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetSuitableBlock&lt;/code&gt; function:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;CBlockIndex&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetSuitableBlockNewVersion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CBlockIndex&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pindex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pindex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nHeight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;median_3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pindex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pprev&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pprev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pindex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pprev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pindex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Much shorter and understandable, right?.&lt;/p&gt;

&lt;p&gt;Before continuing, we have to fix something: we do not know if the &lt;em&gt;Natural Ordering&lt;/em&gt; specified in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CBlockIndex&lt;/code&gt; class is given by the block’s timestamp (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nTime&lt;/code&gt; attribute).
We need a version of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;median_3&lt;/code&gt; that accepts a form of comparison specified by the user: we need you to accept a &lt;em&gt;strict weak ordering relation&lt;/em&gt; (&lt;a href=&quot;https://componentsprogramming.com/writing-min-function-part3/&quot;&gt;for more information see here&lt;/a&gt;).&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Regular&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StrictWeakOrdering&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Regular&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StrictWeakOrdering&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;median_3_ab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// precondition: a &amp;lt;= b&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;           &lt;span class=&quot;c1&quot;&gt;// a, b, c are sorted&lt;/span&gt;
                       &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// b is not the median&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Regular&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StrictWeakOrdering&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;median_3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;median_3_ab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                   &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;median_3_ab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now, we can correctly implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetSuitableBlockNewVersion&lt;/code&gt;, comparing by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nTime&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;CBlockIndex&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetSuitableBlockNewVersion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CBlockIndex&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pindex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pindex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nHeight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;median_3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pindex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pprev&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pprev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pindex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pprev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pindex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[](&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We have one last problem to solve. Let’s make a small test of the original algorithm and the new one:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CBlockIndex&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nHeight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;CBlockIndex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pprev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;CBlockIndex&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ba&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1558731500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;nullptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;CBlockIndex&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bb&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1558731500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ba&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;//same nTime as previous&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;CBlockIndex&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bc&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1558730000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetSuitableBlockNewVersion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;GetSuitableBlockNewVersion: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nHeight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetSuitableBlock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;GetSuitableBlock:           &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nHeight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The code above prints:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;nl&quot;&gt;GetSuitableBlockNewVersion:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GetSuitableBlock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;           &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;What we are trying to prove with the previous code is the stability of both algorithms. Our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;median_3&lt;/code&gt; algorithm is &lt;em&gt;stable&lt;/em&gt; which means that the relative order of the equivalent elements is preserved (&lt;a href=&quot;https://componentsprogramming.com/writing-min-function-part5/&quot;&gt;for more information see here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;To prove it with data, we will use the previous example, in which we have the following input data for our algorithms:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1558731500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1558731500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1558730000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Where the first element of each pair is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nHeight&lt;/code&gt;, the identifier of the block, and the second element is the timestamp called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nTime&lt;/code&gt;.
Note that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nTime&lt;/code&gt; of the first 2 elements is the same.&lt;/p&gt;

&lt;p&gt;If we sort the previous sequence by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nTime&lt;/code&gt; using a stable ordering algorithm, such as &lt;a href=&quot;https://en.wikipedia.org/wiki/Merge_sort&quot;&gt;Merge sort&lt;/a&gt; we would have something like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1558730000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1558731500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1558731500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Note that the middle element is the one with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nHeight = 1&lt;/code&gt;. This indicates that our algorithm behaved in a stable manner but not the original algorithm used in the Bitcoin Cash DAA.&lt;/p&gt;

&lt;p&gt;In my first implementation of DAA in the Bitprim node I used a code similar to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;median_3&lt;/code&gt; which was also stable, since I had not verified the code of the specification, I had mistakenly assumed that it was also stable.
Then this caused runtime errors of our node on a difficulty change. It did not always happen, but there was a particular case in which we could detect it. After several hours of debugging I could detect that the problem was that the algorithm used by me was not compatible with the “specified” in DAA.&lt;/p&gt;

&lt;p&gt;Therefore, I had to “correct” my algorithm to make it non-stable in the same way as that of the specification.&lt;/p&gt;

&lt;p&gt;Actually, if I remember correctly, the first version of the DAA specification did not mention the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetSuitableBlock&lt;/code&gt; code, but said that the median of 3 elements was calculated. Since the implementation of the median was “incorrect” they had to adapt the specification to be consistent with the code.
Keep in mind, that once the code of a Bitcoin node (or any cryptocurrency) is in operation, a modification in its behavior introduces incompatibilities with previous versions and produces the so-called forks. So once the code is running, it’s about not changing it. For this reason it is why the specification had to be adapted instead of correcting the code.&lt;/p&gt;

&lt;p&gt;Before finishing, let’s make a comparison of both algorithms, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetSuitableBlock&lt;/code&gt; vs. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;median_3&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;median_3&lt;/code&gt; does not make any swap, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetSuitableBlock&lt;/code&gt; can make between 0, 7/6 or 2 swaps, unnecessarily. (Efficiency)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetSuitableBlock&lt;/code&gt; creates an array, unnecessarily. (Efficiency)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;median_3&lt;/code&gt; performs 2, 8/3 or 3 comparisons, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetSuitableBlock&lt;/code&gt; always performs 3 comparisons. (Efficiency)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;median_3&lt;/code&gt; is stable, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetSuitableBlock&lt;/code&gt; is not. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;median_3&lt;/code&gt; is what anyone expects from an algorithm that calculates the median of 3 elements. (Correctness)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And now, to conclude, some conclusions:&lt;/p&gt;

&lt;p&gt;The author of the DAA specification could have chosen a known and “standard” algorithm, but he did not.
And perhaps the worst of all is that the specification refers to the code. &lt;strong&gt;The code must never be specification. The code must be created from a specification.&lt;/strong&gt; So if a specification refers to code, there is no such specification.&lt;/p&gt;

&lt;p&gt;Bye!&lt;/p&gt;

&lt;hr /&gt;

  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//algorithms-specifications/&quot;&gt;Algorithms and specifications&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on February 19, 2020.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Elements of Programming Authors' Edition (free ebook)]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//elements-of-programming-authors-edition/" />
  <id>https://componentsprogramming.com//elements-of-programming-authors-edition</id>
  <published>2019-06-28T09:00:00-03:00</published>
  <updated>2019-06-28T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;These are fresh news.&lt;/p&gt;

&lt;p&gt;This year Elements of Programming celebrates 10 years since its publication, but this time the gift is for us:&lt;/p&gt;

&lt;p&gt;Alex Stepanov and Paul McJones have just released Elements of Programming &lt;em&gt;Authors’ Edition&lt;/em&gt; in two versions:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;A free PDF, &lt;a href=&quot;http://elementsofprogramming.com/&quot;&gt;click here to download it&lt;/a&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;A trade paperback from Semigroup Press, just for USD 9.94, &lt;a href=&quot;http://www.lulu.com/shop/alexander-stepanov-and-paul-mcjones/elements-of-programming/paperback/product-24142479.html&quot;&gt;click here to buy it&lt;/a&gt;.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Amazon listing coming soon.&lt;/p&gt;

&lt;p&gt;Thanks again, Alex and Paul!&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//elements-of-programming-authors-edition/&quot;&gt;Elements of Programming Authors' Edition (free ebook)&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on June 28, 2019.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[My 'The Gift of the Arabs' talk (Spanish)]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//talk-gift-arabs/" />
  <id>https://componentsprogramming.com//talk-gift-arabs</id>
  <published>2018-11-26T09:00:00-03:00</published>
  <updated>2018-11-26T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;Hello!&lt;/p&gt;

&lt;p&gt;I share with you the links to the video and slides of my talk entitled &lt;em&gt;The Gift of the Arabs&lt;/em&gt;, which was presented at the &lt;em&gt;Algorithms Study Group&lt;/em&gt; meetup in May 2017 in Buenos Aires, Argentina.&lt;/p&gt;

&lt;p&gt;The content of my talk is based on &lt;a href=&quot;http://stepanovpapers.com/Journeys/Journeys-0.3.pdf&quot;&gt;Three Algorithmic Journeys&lt;/a&gt; and &lt;a href=&quot;https://www.amazon.es/Mathematics-Generic-Programming-Alexander-Stepanov/dp/0321942043&quot;&gt;From Mathematics to Generic Programming&lt;/a&gt; by &lt;a href=&quot;https://en.wikipedia.org/wiki/Alexander_Stepanov&quot;&gt;Alex Stepanov&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It’s my interpretation of what would have been his fourth episode, which was never presented, and would have had the same name.&lt;/p&gt;

&lt;p&gt;Hope you like it!&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=ZgC6MDh7zJc&quot;&gt;Link to the Video&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com/images/fpelliccioni-ElRegaloDeLosArabes.pdf&quot;&gt;Link to the Slides &lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Regards!&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//talk-gift-arabs/&quot;&gt;My 'The Gift of the Arabs' talk (Spanish)&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on November 26, 2018.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Honorable Mention 'Senator Domingo Faustino Sarmiento']]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//mention-sarmiento/" />
  <id>https://componentsprogramming.com//mention-sarmiento</id>
  <published>2016-11-30T09:00:00-03:00</published>
  <updated>2016-11-30T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;On November 29, 2016 I had the honor and pleasure to receive, as a member of the scientific team of the &lt;a href=&quot;http://www.plataformaargentina.gov.ar/en/(copla_i)&quot;&gt;National Commission of the Outer Limit of the Continental Shelf (COPLA)&lt;/a&gt;, the Honorable Mention “Senator Domingo Faustino Sarmiento”, for our work, which made possible the determination of the new &lt;em&gt;Outer Limit of the Continental Shelf of the Argentine Republic&lt;/em&gt;. &lt;a href=&quot;#Ref1&quot;&gt;[1]&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The award was presented by the Vice President of the Nation, &lt;a href=&quot;https://twitter.com/gabimichetti&quot;&gt;Gabriela Michetti&lt;/a&gt; along with the Senator &lt;a href=&quot;https://twitter.com/omarperotti&quot;&gt;Omar Perotti&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://componentsprogramming.com/images/copla/sarmiento1.jpg&quot; alt=&quot;Sarmiento1&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://componentsprogramming.com/images/copla/sarmiento2.png&quot; alt=&quot;Sarmiento2&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://componentsprogramming.com/images/copla/sarmiento3.jpg&quot; alt=&quot;Sarmiento3&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;blockquote&gt;
  &lt;p&gt;The Honorable Mention “Senator Domingo Faustino Sarmiento” is the highest award given by the Honorable Senate of the Nation to natural or legal persons of the cultural, sports, professional and academic work, for his &lt;strong&gt;entrepreneurial work aimed at improving the quality of life of another individuals&lt;/strong&gt;, institutions and their communities. 
The ceremony was made in the “Salón Azul” (Blue Room) of the Honorable Senate of the Nation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;p&gt;The event was also attended by the vice minister and president of &lt;a href=&quot;http://www.plataformaargentina.gov.ar/&quot;&gt;COPLA&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/cmforadori&quot;&gt;Carlos Foradori&lt;/a&gt;, former minister &lt;a href=&quot;https://twitter.com/JorgeTaiana&quot;&gt;Jorge Taiana&lt;/a&gt;, former vice minister Fernando Petrella, congress representatives Mario Pais, Dalmacio Mera, Norma Durango, Beatriz Mirkin, and Daniela Castro. In addition they participated prominent figures of the academic and scientific scope.&lt;/p&gt;

&lt;p&gt;Minister &lt;a href=&quot;https://twitter.com/SusanaMalcorra&quot;&gt;Susana Malcorra&lt;/a&gt; could not be present since she has traveled to Cuba for the funerals of Fidel Castro in representation of Argentina.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;references&quot;&gt;References&lt;/h3&gt;

&lt;p&gt;&lt;a name=&quot;Ref1&quot;&gt;[1]&lt;/a&gt; The Limit was approved on 11 March 2016 by the &lt;a href=&quot;http://www.un.org/&quot;&gt;United Nations&lt;/a&gt;, by consensus, ie without a single vote against. See: &lt;a href=&quot;/limit-approved/&quot;&gt;The Outer Limit of the Argentine Continental Shelf&lt;/a&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;links-to-news&quot;&gt;Links to News&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://twitter.com/SenadoArgentina/status/803687588653572096&quot;&gt;https://twitter.com/SenadoArgentina/status/803687588653572096&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://twitter.com/CancilleriaARG/status/803711684993024006&quot;&gt;https://twitter.com/CancilleriaARG/status/803711684993024006&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://twitter.com/gabimichetti/status/803697167063912448&quot;&gt;https://twitter.com/gabimichetti/status/803697167063912448&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://twitter.com/omarperotti/status/803723193844371456&quot;&gt;https://twitter.com/omarperotti/status/803723193844371456&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://omarperotti.com.ar/el-senado-distinguio-a-los-cientificos-que-trabajaron-en-el-nuevo-limite-de-la-plataforma-continental/&quot;&gt;http://omarperotti.com.ar/el-senado-distinguio-a-los-cientificos-que-trabajaron-en-el-nuevo-limite-de-la-plataforma-continental/&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.senado.gov.ar/prensa/14718/noticias&quot;&gt;http://www.senado.gov.ar/prensa/14718/noticias&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.mrecic.gov.ar/copla-recibio-la-maxima-distincion-del-senado&quot;&gt;https://www.mrecic.gov.ar/copla-recibio-la-maxima-distincion-del-senado&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//mention-sarmiento/&quot;&gt;Honorable Mention 'Senator Domingo Faustino Sarmiento'&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on November 30, 2016.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[My '¿Fastware?' talk (Spanish)]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//talk-fastware/" />
  <id>https://componentsprogramming.com//talk-fastware</id>
  <published>2016-11-16T09:00:00-03:00</published>
  <updated>2016-11-16T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;Hello!&lt;/p&gt;

&lt;p&gt;Here is the the link to the slides of my talk entitled &lt;a href=&quot;http://es.slideshare.net/FernandoPelliccioni/fastware-64059921&quot;&gt;“¿Fastware?”&lt;/a&gt; (Spanish resource), which was exhibited at the &lt;a href=&quot;https://www.meetup.com/cpp-ba/events/232269015/&quot;&gt;“Second C++ Buenos Aires Meeting”&lt;/a&gt; on July 14, 2016.&lt;/p&gt;

&lt;p&gt;At that meeting we had the pleasure to view excellent talks provided by our colleagues &lt;a href=&quot;http://talesofcpp.fusionfenix.com/&quot;&gt;Agustín (K-Ballo) Bergé&lt;/a&gt;, Francisco Tufró y Hernan Saez. &lt;a href=&quot;https://www.meetup.com/cpp-ba/pages/20784343/Presentaciones/&quot;&gt;See the other talks here.&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hope you like our presentations and you feel like to join &lt;a href=&quot;www.meetup.com/cpp-ba/&quot;&gt;our community&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Kind regards!&lt;/p&gt;


  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//talk-fastware/&quot;&gt;My '¿Fastware?' talk (Spanish)&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on November 16, 2016.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Palindromes and more]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//palindromes/" />
  <id>https://componentsprogramming.com//palindromes</id>
  <published>2016-06-06T09:00:00-03:00</published>
  <updated>2016-06-06T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;!-- &lt;script type=&quot;text/x-mathjax-config&quot;&gt;
 MathJax.Hub.Config({
  &quot;HTML-CSS&quot;: {
    // scale: 200
    scale: (MathJax.Hub.Browser.isChrome &amp;&amp; MathJax.Hub.Browser.isPC ? 100 : 100)
 }});
&lt;/script&gt; --&gt;

&lt;p&gt;I decided to write this article from a question I saw in &lt;a href=&quot;http://stackoverflow.com&quot;&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://stackoverflow.com/questions/4138827/check-string-for-palindrome&quot;&gt;Here&lt;/a&gt; the link to the question.&lt;/p&gt;

&lt;p&gt;The questioner tries to write an algorithm to identify whether a “word” is a &lt;a href=&quot;https://en.wikipedia.org/wiki/Palindrome&quot;&gt;palindrome&lt;/a&gt; or it is not. The algorithm is written using the &lt;a href=&quot;https://en.wikipedia.org/wiki/Java_(programming_language)&quot;&gt;Java programming language&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I do not want to analyze the algorithm proposed by the questioner, but I want to analyze the most voted answer algorithm. The latter has 73 votes versus 65 votes that have the accepted answer (to today’s date, June 6, 2016).&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://stackoverflow.com/a/4139065/1006264&quot;&gt;Here&lt;/a&gt; the link to the algorithm that I want to analyze.&lt;/p&gt;

&lt;p&gt;Here the code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;isPalindrome&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
       &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;StringBuilder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
       &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;reverse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
       &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The algorithm works correctly and the logic is very intuitive. Basically compares for &lt;em&gt;equality&lt;/em&gt; the input word against its reverse.&lt;/p&gt;

&lt;p&gt;The problem with this algorithm is that it is very inefficient compared to the optimal algorithm.&lt;/p&gt;

&lt;p&gt;I made ​​a comment to the author of the algorithm on Stackoverflow:  “Compare the complexity of your algorithm with respect to others.”&lt;br /&gt;
The user &lt;a href=&quot;http://stackoverflow.com/users/276052/aioobe&quot;&gt;@aioobe&lt;/a&gt; replied: “I think it’s the same complexity as the other solutions, no?”&lt;/p&gt;

&lt;p&gt;He is right.&lt;br /&gt;
I was not very specific in my comment. @aioobe surely assumed that I was referring to the &lt;a href=&quot;https://en.wikipedia.org/wiki/Asymptotic_computational_complexity&quot;&gt;asymptotic computational complexity&lt;/a&gt; and he is ok, because usually, when we say “complexity” without specifying anything else, it is assumed that we are referring to the &lt;em&gt;asymptotic computational complexity&lt;/em&gt;.&lt;/p&gt;

&lt;h3 id=&quot;asymptotic-computational-complexity&quot;&gt;Asymptotic Computational Complexity&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Asymptotic asymptotic computational&lt;/em&gt; means how the algorithms respond in time and space as the input grows.&lt;br /&gt;
It is usually associated with the &lt;a href=&quot;https://en.wikipedia.org/wiki/Big_O_notation&quot;&gt;O-notation&lt;/a&gt; introduced by &lt;a href=&quot;https://en.wikipedia.org/wiki/Paul_Gustav_Heinrich_Bachmann&quot;&gt;Paul Bachmann&lt;/a&gt; in his book &lt;a href=&quot;https://archive.org/details/dieanalytischeza00bachuoft&quot;&gt;Die Analytische Zahlentheorie&lt;/a&gt; in 1894. &lt;a href=&quot;#Ref1&quot;&gt;[1]&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way we can measure the scalability of algorithms without relying on machine architecture, CPU speed, the programming language in which is implemented the algorithm, etc…&lt;/p&gt;

&lt;p&gt;While it is very useful in many circumstances, this method of measurement it is not accurate, but it is &lt;strong&gt;approximated&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For more information on O-notation and asymptotic complexity, see &lt;a href=&quot;#Ref2&quot;&gt;[2]&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;concrete-computational-complexity&quot;&gt;Concrete Computational Complexity&lt;/h3&gt;

&lt;p&gt;Another way to measure algorithms is no use approximations but concrete quantity of operations, depending on the input to the algorithm.&lt;/p&gt;

&lt;p&gt;For example, imagine the algorithm to find the minimum (or maximum) of \( n \) elements. We can say that the algorithm has &lt;em&gt;linear complexity&lt;/em&gt; (in time), or the algorithm is \( O(n) \). But specifically, the algorithm needs \(( n - 1 )\) comparisons to find the minimum element.&lt;/p&gt;

&lt;h3 id=&quot;back-to-palindromes&quot;&gt;Back to Palindromes&lt;/h3&gt;

&lt;p&gt;Again, the code of the algorithm.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;isPalindrome&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
       &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;StringBuilder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
       &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;reverse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
       &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;I called &lt;em&gt;Algorithm I&lt;/em&gt; to the previous code. (“I” as inefficient).&lt;/p&gt;

&lt;p&gt;We could say that &lt;em&gt;Algorithm I&lt;/em&gt; is \( O(n) \), but, how can we guarantee it without knowing the complexity of the components in which the algorithm is based?&lt;/p&gt;

&lt;p&gt;To do this, we must review the Java documentation. For example, consider the &lt;a href=&quot;http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#equals-java.lang.Object-&quot;&gt;String.equals()&lt;/a&gt; function. &lt;a href=&quot;#Ref3&quot;&gt;[3]&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you may have noticed on the previous page, the Java documentation does not include the time and space complexity of algorithms and data structures.&lt;br /&gt;
I consider this a failure, because it hinders us specifying the complexity of our algorithms, at least of the algorithms that are based on classes provided by Java.&lt;/p&gt;

&lt;p&gt;To continue trying to specify the complexity of the &lt;em&gt;Algorithm I&lt;/em&gt;, we have no choice, we have to review the source code of Java classes.&lt;br /&gt;
Consider the &lt;a href=&quot;http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/String.java&quot;&gt;source code of String.equals()&lt;/a&gt; (click on the link and find the equals function.)&lt;/p&gt;

&lt;p&gt;As you can verify the code String.equals() has linear complexity in time, \( O(n) \).&lt;br /&gt;
Specifically, String.equals() does \( n \) comparisons (&lt;em&gt;inequality&lt;/em&gt;). (Beyond the noise imposed by Java, such as casts, instanceof, etc …).&lt;/p&gt;

&lt;p&gt;The space complexity of String.equals() is constant, that is, \( O(1) \).&lt;br /&gt;
This means that it uses a constant memory beyond the input of the algorithm.&lt;/p&gt;

&lt;h3 id=&quot;determining-the-complexity&quot;&gt;Determining the complexity&lt;/h3&gt;

&lt;p&gt;We will determine the complexity of the &lt;em&gt;Algorithm I&lt;/em&gt; analyzing each component.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#equals-java.lang.Object-&quot;&gt;String.equals()&lt;/a&gt;. Linear time,  \( n \) &lt;em&gt;inequality comparisons&lt;/em&gt;. Constant space.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/AbstractStringBuilder.java&quot;&gt;StringBuilder.reverse()&lt;/a&gt;. Linear time,  \( 2 \left\lfloor\dfrac{n}{2}\right\rfloor \) assignments. Constant space.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/StringBuilder.java&quot;&gt;StringBuilder.toString()&lt;/a&gt;. Linear time, \( n \) assignments. Linear space, \( n \) elements.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/StringBuilder.java&quot;&gt;StringBuilder constructor&lt;/a&gt;. Linear time,  \( n + 16 \)  assignments. Linear space,  \( n + 16 \) elements.&lt;/p&gt;

&lt;p&gt;So the overall complexity of &lt;em&gt;Algorithm I&lt;/em&gt; is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Time&lt;/strong&gt;: In the worst case, when the word is a &lt;em&gt;palindrome&lt;/em&gt;, this algorithm takes \( n \) &lt;em&gt;inequality&lt;/em&gt; comparisons and \( 2n + 2 \left\lfloor\dfrac{n}{2}\right\rfloor + 16 \) assignments.&lt;br /&gt;
&lt;strong&gt;Space&lt;/strong&gt;: \( 2n + 16 \) elements.&lt;/p&gt;

&lt;p&gt;As you can see, this algorithm is very inefficient, it uses a lot of memory (unnecessarily) and as discussed below, takes over \( 8x \) operations &lt;a href=&quot;#RefAlgoritmoOptimo&quot;&gt;optimal algorithm&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;improving-the-algorithm-naïve-version&quot;&gt;Improving the algorithm (naïve version)&lt;/h3&gt;

&lt;p&gt;Call the following code &lt;em&gt;Algorithm N&lt;/em&gt; (N as naïve). &lt;em&gt;Algorithm N&lt;/em&gt; presents a substantial improvement over the &lt;em&gt;Algorithm I&lt;/em&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;isPalindrome&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;strong&gt;Time&lt;/strong&gt;: In the worst case, when the word is a &lt;em&gt;palindrome&lt;/em&gt;, this algorithm takes \( n \) &lt;em&gt;inequality&lt;/em&gt; comparisons.&lt;br /&gt;
&lt;strong&gt;Space&lt;/strong&gt;: Constant&lt;/p&gt;

&lt;p&gt;No extra memory usage and runs approximately \( \dfrac{1}{4} \) of operations that the &lt;em&gt;Algorithm I&lt;/em&gt;.
While the code is a bit more complex now, it is an easily understandable code, the increased code complexity is negligible compared to the improvement in efficiency.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;RefAlgoritmoOptimo&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;optimal-algorithm&quot;&gt;Optimal Algorithm&lt;/h3&gt;

&lt;p&gt;As we can see in the picture below, there is no need to do \( n \) comparisons to determine whether a word is a &lt;em&gt;palindrome&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://componentsprogramming.com/images/OptimalAlgorithm.svg&quot; alt=&quot;Optimal Algorithm&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It is enough to do just (about) half the comparisons:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If n is even, it performs \( \dfrac{n}{2} \) comparisons&lt;/li&gt;
  &lt;li&gt;If n is odd, it performs ​​\( \dfrac{n - 1}{2} \) comparisons.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The following code will be called &lt;em&gt;Algorithm O&lt;/em&gt; (O as optimal).&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;isPalindrome&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;    
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;strong&gt;Time&lt;/strong&gt;: In the worst case, when the word is a &lt;em&gt;palindrome&lt;/em&gt;, this algorithm performs \( \left\lfloor\dfrac{n}{2}\right\rfloor \) &lt;em&gt;inequality&lt;/em&gt; comparisons.&lt;br /&gt;
&lt;strong&gt;Space&lt;/strong&gt;: Constant&lt;/p&gt;

&lt;h3 id=&quot;algorithm-i-detailed-analysis&quot;&gt;&lt;em&gt;Algorithm I&lt;/em&gt;, detailed analysis&lt;/h3&gt;

&lt;p&gt;As explained earlier, the &lt;em&gt;Algorithm I&lt;/em&gt; is much more inefficient than &lt;em&gt;Algorithms N&lt;/em&gt; and &lt;em&gt;Algorithms O&lt;/em&gt;.&lt;br /&gt;
But besides the complexity analysis, we have to consider other issues that affect the efficiency of the &lt;em&gt;Algorithm I&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Each component used in the &lt;em&gt;Algorithm I&lt;/em&gt; come with certain performance penalties that go unnoticed.&lt;br /&gt;
Let us analyze in detail.&lt;/p&gt;

&lt;h4 id=&quot;construction-of-stringbuilder&quot;&gt;Construction of &lt;a href=&quot;http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html&quot;&gt;StringBuilder&lt;/a&gt;&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;Dynamic memory allocation of the StringBuilder object (&lt;a href=&quot;https://en.wikipedia.org/wiki/Memory_management#HEAP&quot;&gt;heap, free store&lt;/a&gt;, or whatever you call it).&lt;/li&gt;
  &lt;li&gt;Zero-Initialization of members of StringBuilder. &lt;a href=&quot;#Ref4&quot;&gt;[4]&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Dynamic memory allocation of the StringBuilder’s internal array.&lt;br /&gt;
According to the documentation, the size of the internal array is equal to 16 characters plus the size of the original String. &lt;a href=&quot;#Ref5&quot;&gt;[5]&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Zero-Initialization of Array’s members. Length and the array itself. &lt;a href=&quot;#Ref4&quot;&gt;[4]&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Copy of the bytes of the original String to the StringBuilder’s internal array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;reverse-stringbuilder&quot;&gt;reverse() (StringBuilder)&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;Mentioned above. This function does not use additional memory and it is efficient at runtime.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;tostring-stringbuilder&quot;&gt;toString() (StringBuilder)&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;Dynamic memory allocation of the &lt;a href=&quot;http://docs.oracle.com/javase/8/docs/api/java/lang/String.html&quot;&gt;String&lt;/a&gt; object (&lt;a href=&quot;https://en.wikipedia.org/wiki/Memory_management#HEAP&quot;&gt;heap, free store&lt;/a&gt;, or whatever you call it).&lt;/li&gt;
  &lt;li&gt;Zero-Initialization of members of String. &lt;a href=&quot;#Ref4&quot;&gt;[4]&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Dynamic memory allocation of the String’s internal array.&lt;/li&gt;
  &lt;li&gt;Zero-Initialization of Array’s members. Length and the array itself. &lt;a href=&quot;#Ref4&quot;&gt;[4]&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Copy of the bytes from the StringBuilder’s internal array to the String’s internal array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;equals-string&quot;&gt;equals() (String)&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;Mentioned above. This function does not use additional memory and it is efficient at runtime.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;garbage-collection&quot;&gt;Garbage Collection&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;The GC must release any additional memory (unnecessary) that was used and obviously this operation is not “free.”&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;data-cache-misses&quot;&gt;Data Cache Misses&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;Another drawback associated with unnecessary memory consumption is the probability that our objects are too large to fit into the cache, causing &lt;a href=&quot;https://en.wikipedia.org/wiki/CPU_cache#Cache_miss&quot;&gt;cache misses&lt;/a&gt; impacting on runtime performance.&lt;/li&gt;
  &lt;li&gt;Another factor that increases the probability of &lt;em&gt;cache misses&lt;/em&gt; are the indirections (references, pointers) to distant memory locations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;memory-footprint&quot;&gt;Memory footprint&lt;/h3&gt;

&lt;p&gt;To analyze the memory consumption of &lt;em&gt;Algorithm I&lt;/em&gt;, we will use a concrete example. We will use a 9-char &lt;em&gt;palindrome&lt;/em&gt;, the word is “evitative”.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;isPalindrome&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;evitative&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;While memory consumption of &lt;em&gt;Algorithm I&lt;/em&gt; depends on the &lt;a href=&quot;https://en.wikipedia.org/wiki/Java_virtual_machine&quot;&gt;Virtual Machine&lt;/a&gt; and the &lt;a href=&quot;https://en.wikipedia.org/wiki/Java_virtual_machine#Java_Runtime_Environment_from_Oracle&quot;&gt;Runtime Environment&lt;/a&gt; that we are using, in this case we will use a specific platform that is &lt;a href=&quot;#RefPlataforma&quot;&gt;detailed here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Basically in our example two objects are created, one StringBuilder and one String.&lt;/p&gt;

&lt;h4 id=&quot;stringbuilder&quot;&gt;StringBuilder:&lt;/h4&gt;

&lt;p&gt;The &lt;a href=&quot;http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html&quot;&gt;StringBuilder&lt;/a&gt; objects have the following memory representation.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://componentsprogramming.com/images/JavaStringBuilderMemoryRepresentation64CompressedOopsEnabled.svg&quot; alt=&quot;Java StringBuilder memory representation&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A StringBuilder object consists of two parts (not necessarily contiguous in memory):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;First part: usage size of the array (length() of StringBuilder) and a reference to the array where the data resides.&lt;/li&gt;
  &lt;li&gt;Second part: array size (capacity() of StringBuilder) and the array.&lt;br /&gt;
The array size is 16 characters plus the number of characters of the original String (“evitative”) &lt;a href=&quot;#Ref5&quot;&gt;[5]&lt;/a&gt;. In the picture, those 16 extra characters are shown in red, to emphasize that it is wasted memory. In Java, the characters have a size of 2 bytes. &lt;a href=&quot;#Ref6&quot;&gt;[6]&lt;/a&gt;&lt;br /&gt;
So in our example, the array will have a size of \( 2 \cdot (9 + 16) = 50 \) bytes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Java all objects have a &lt;a href=&quot;http://hg.openjdk.java.net/jdk8/jdk8/hotspot/file/87ee5ee27509/src/share/vm/oops/oop.hpp&quot;&gt;&lt;em&gt;header&lt;/em&gt;&lt;/a&gt; (if known any implementation that does not have it, &lt;a href=&quot;https://twitter.com/ferpelliccioni&quot;&gt;let me know&lt;/a&gt;) in our platform the header is 12 bytes. In other popular platforms can be 8 or 16 bytes, &lt;a href=&quot;#Ref7&quot;&gt;see here [7]&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another thing to consider is the &lt;a href=&quot;https://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding&quot;&gt;padding&lt;/a&gt;, which is basically a memory space that is added to meet the &lt;a href=&quot;https://en.wikipedia.org/wiki/Data_structure_alignment&quot;&gt;alignment&lt;/a&gt; requirement. In our case, the objects must be placed in 8-multiples memory addresses.&lt;/p&gt;

&lt;p&gt;In summary, our StringBuilder object has the following memory size (in bytes):&lt;/p&gt;

&lt;p&gt;First part: \( 24 \)&lt;br /&gt;
Second part: \( 16 + 2n + 32 + padding \) &lt;a href=&quot;#Ref8&quot;&gt;[8]&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Total: \( 8(\left\lceil\dfrac{n}{4}\right\rceil + 9) \) bytes&lt;/p&gt;

&lt;h4 id=&quot;string&quot;&gt;String:&lt;/h4&gt;

&lt;p&gt;The &lt;a href=&quot;http://docs.oracle.com/javase/8/docs/api/java/lang/String.html&quot;&gt;String&lt;/a&gt; objects have the following memory representation.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://componentsprogramming.com/images/JavaStringMemoryRepresentation64CompressedOopsEnabledJdk18.svg&quot; alt=&quot;Java String memory representation&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A String object consists of two parts (not necessarily contiguous in memory):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;First part: reference to array (where data resides) and a &lt;a href=&quot;http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#hashCode--&quot;&gt;hash&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Second part: the size of the array (length() of String) and the array.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The String object here described belongs to the &lt;a href=&quot;http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/String.java&quot;&gt;Java 8&lt;/a&gt;.
In &lt;a href=&quot;http://hg.openjdk.java.net/jdk6/jdk6/jdk/file/814bf0775b52/src/share/classes/java/lang/String.java&quot;&gt;older versions&lt;/a&gt; of Java, the String class had more fields, therefore memory size was bigger. &lt;a href=&quot;#Ref7&quot;&gt;[7]&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In summary, our String object has the following memory size (in bytes):&lt;/p&gt;

&lt;p&gt;First part: \( 24 \)&lt;br /&gt;
First part: \( 16 + 2n + padding \) &lt;a href=&quot;#Ref8&quot;&gt;[8]&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Total: \( 8(\left\lceil\dfrac{n}{4}\right\rceil + 5) \) bytes&lt;/p&gt;

&lt;h4 id=&quot;total-memory-footprint&quot;&gt;Total memory footprint:&lt;/h4&gt;

&lt;p&gt;The total memory used by our StringBuilder and String objects is \( 16(\left\lceil\dfrac{n}{4}\right\rceil + 7) \) bytes.&lt;/p&gt;

&lt;p&gt;In our example \( n = 9 \), so the memory used is \( 16(\left\lceil\dfrac{9}{4}\right\rceil + 7) \) bytes, which represent 160 bytes of extra memory, only to determine whether “evitative” is a palindrome or not.&lt;br /&gt;
Remember, these 160 bytes is a totally unnecessary memory consumption.&lt;/p&gt;

&lt;h2 id=&quot;benchmarks&quot;&gt;Benchmarks&lt;/h2&gt;

&lt;p&gt;I was doing some benchmarks where it can be seen that the &lt;em&gt;Algorithm I&lt;/em&gt; is about &lt;strong&gt;8.5x&lt;/strong&gt; slower than Algorithms &lt;em&gt;O&lt;/em&gt; and &lt;em&gt;N&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I’m leaving out some other benchmarks that show a \( \approx 500x \) difference against &lt;em&gt;Algorithm I&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You can see the source code of the benchmarks in &lt;a href=&quot;https://github.com/fpelliccioni/componentsprogramming/tree/master/palindrome/part1/java&quot;&gt;my GitHub account&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The explanation of the benchmarks and the code will be pending for a future article.&lt;/p&gt;

&lt;h3 id=&quot;algorithm-n-versus-algorithm-o&quot;&gt;&lt;em&gt;Algorithm N&lt;/em&gt; versus &lt;em&gt;Algorithm O&lt;/em&gt;&lt;/h3&gt;

&lt;p&gt;While previously we saw that the &lt;em&gt;Algorithm O&lt;/em&gt; performs half of operations than &lt;em&gt;Algorithm N&lt;/em&gt;, in the worst case; the runtime of both algorithms is affected by several factors, including: the length of the word, if the word is palindrome or not, and other factors relevant to the platform.&lt;/p&gt;

&lt;p&gt;In many cases &lt;em&gt;Algorithm N&lt;/em&gt; is faster than &lt;em&gt;Algorithm O&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;We will discuss this in a future article.&lt;/p&gt;

&lt;h2 id=&quot;ultimate-solution&quot;&gt;Ultimate solution?&lt;/h2&gt;

&lt;p&gt;I believe that none of the three algorithms presented in this article represent an ultimate solution, and none of them is a &lt;a href=&quot;/components-programming/&quot;&gt;Component&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;component&lt;/em&gt; should be something that can be reused and, in many cases, the algorithms described in this article are not suitable for re-use.&lt;/p&gt;

&lt;p&gt;In addition, the three algorithms only accept a String as input. A palindrome is not just a sequence of characters that reads the same forwards and backwards. A palindrome can be found in music, in numbers and also in nature.&lt;/p&gt;

&lt;p&gt;I am no expert in genetics, but I know that &lt;a href=&quot;https://en.wikipedia.org/wiki/Palindromic_sequence&quot;&gt;palindromes can be found in DNA strands&lt;/a&gt;.&lt;br /&gt;
Palindromes in DNA are so important that some experts consider them to be responsible for preventing the extinction of the human race (and other species too). Without palindromes in DNA, incorrigible and irreversible genetic mutations would occur, causing the extinction of the species, with the passing of generations.
In a future article we’ll talk about this topic.&lt;/p&gt;

&lt;h2 id=&quot;pending-issues&quot;&gt;Pending issues&lt;/h2&gt;

&lt;p&gt;Replicate the &lt;em&gt;Algorithm I&lt;/em&gt; in &lt;a href=&quot;https://en.wikipedia.org/wiki/C_Sharp_(programming_language)&quot;&gt;C#&lt;/a&gt; and analyze its efficiency in execution time and memory consumption.&lt;/p&gt;

&lt;h2 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;Algorithm I&lt;/em&gt; is an excellent example of brevity and readability, it makes good use of abstractions available to achieve this goal. The problem with &lt;em&gt;Algorithm I&lt;/em&gt; is that it is a terrible example of efficiency.&lt;/p&gt;

&lt;p&gt;Abstractions facilitate our work, let us concentrate on the problem to be solved without having to think about the context, in this case the computer.&lt;/p&gt;

&lt;p&gt;While abstractions are good, they have a big disadvantage. They make us forget how the machine works.&lt;br /&gt;
Modern programmers often abuse of abstractions and they do not have knowledge about very important things that affect the behavior of our programs, such as memory, cache, load/store buffers, branch prediction, pipelines, memory models, vector instructions (SIMD), etc …&lt;/p&gt;

&lt;p&gt;As programmers, we must know in detail the computer, the programming language and the complexity of the components we use. Unfortunately modern programmers are just focused on things like testing, agile, metaprogramming and frameworks/libraries whose lifetime is longer than two years.&lt;/p&gt;

&lt;p&gt;I do not want to forget to mention that the best of all abstractions was discovered by &lt;a href=&quot;https://en.wikipedia.org/wiki/Gottfried_Wilhelm_Leibniz&quot;&gt;Leibniz&lt;/a&gt; in 1679. That abstraction is what allows us to model the real world in a computer. That abstraction is the &lt;a href=&quot;https://en.wikipedia.org/wiki/Bit&quot;&gt;&lt;strong&gt;Bit&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finally, it is noteworthy that &lt;em&gt;Algorithm I&lt;/em&gt; could be useful as a postcondition:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;isPalindrome&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//postcondition: Result := reverse(str) = str&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;    
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h2&gt;

&lt;p&gt;I want to thank Mario dal Lago and Javier Velilla for reviewing the article and suggest corrections.&lt;br /&gt;
And finally, since we can say that &lt;em&gt;we owe our lives to palindromes&lt;/em&gt;, so, a special thank to them. :)&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;notes&quot;&gt;Notes&lt;/h2&gt;

&lt;p&gt;Byte = 8-bits.&lt;br /&gt;
There are architectures where 1 byte is not necessarily equivalent to 8 bits. These architectures are unusual today.
There is no standard that specifies the size of a byte, but can say that the &lt;em&gt;de facto&lt;/em&gt; standard is that 1 byte = 8 bits, is most common in modern computer architectures.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;RefPlataforma&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;platform-used-for-the-analysis-in-this-article&quot;&gt;Platform used for the analysis in this article:&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;CPU
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;http://ark.intel.com/products/75117&quot;&gt;Intel Core i7-4700MQ&lt;/a&gt; CPU @ 2.40GHz, &lt;a href=&quot;https://en.wikipedia.org/wiki/Haswell_(microarchitecture)&quot;&gt;Haswell&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;4 Cores, 8 Threads&lt;/li&gt;
      &lt;li&gt;L1 Data Cache Size  4 x 32 KBytes&lt;/li&gt;
      &lt;li&gt;L1 Instructions Cache Size  4 x 32 KBytes&lt;/li&gt;
      &lt;li&gt;L2 Unified Cache Size 4 x 256 KBytes&lt;/li&gt;
      &lt;li&gt;L3 Unified Cache Size 6144 KBytes&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;RAM: 8192 MBytes, DDR3&lt;/li&gt;
  &lt;li&gt;Operating System: Windows 10 Home 64-bit&lt;/li&gt;
  &lt;li&gt;Java
    &lt;ul&gt;
      &lt;li&gt;Version 1.8.0_60&lt;/li&gt;
      &lt;li&gt;Java(TM) SE Runtime Environment (build 1.8.0_60-b27)&lt;/li&gt;
      &lt;li&gt;64-bit &lt;a href=&quot;https://en.wikipedia.org/wiki/HotSpot&quot;&gt;HotSpot VM&lt;/a&gt;.&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html#compressedOop&quot;&gt;Compressed Oops&lt;/a&gt; enabled.&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Data_structure_alignment&quot;&gt;Objects alignment&lt;/a&gt;: 8 bytes.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For analysis on other platforms, please &lt;a href=&quot;#Ref7&quot;&gt;refer to [7]&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;p&gt;&lt;a name=&quot;Ref1&quot;&gt;[1]&lt;/a&gt; Zahlen means “Numbers” in German. Hence the set of integers is identified with the letter \( \mathbb{Z} \).&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref2&quot;&gt;[2]&lt;/a&gt; The Art of Computer Programming Volume 1, by Donald E. Knuth [3rd Edition, page 107].&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref3&quot;&gt;[3]&lt;/a&gt; Why I call String.equals() “function” and no “method”?. &lt;a href=&quot;/usando-la-terminologia-adecuada-metodo/&quot;&gt;Here is the answer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref4&quot;&gt;[4]&lt;/a&gt; In Java the integral data types and arrays of integral data types are initialized to 0, guaranteed by the language specification. &lt;a href=&quot;https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.5&quot;&gt;4.12.5 Initial Values of Variables&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref5&quot;&gt;[5]&lt;/a&gt; &lt;a href=&quot;http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html#StringBuilder-java.lang.String-&quot;&gt;Java String class&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref6&quot;&gt;[6]&lt;/a&gt; &lt;a href=&quot;https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&quot;&gt;Java Primitive Data Types&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref7&quot;&gt;[7]&lt;/a&gt; &lt;a href=&quot;/palindromos-otras-plataformas/&quot;&gt;Analysis of memory consumption on other platforms&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref8&quot;&gt;[8]&lt;/a&gt; The formulas for calculating the &lt;a href=&quot;https://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding&quot;&gt;padding&lt;/a&gt; of the internal arrays of StringBuilder and String (in bytes):&lt;br /&gt;
      StringBuilder internal array padding = \( 8\left\lceil\dfrac{2n + 48}{8}\right\rceil - (2n + 48) \)&lt;br /&gt;
      String internal array padding  = \( 8\left\lceil\dfrac{2n + 16}{8}\right\rceil - (2n + 16) \)&lt;/p&gt;

&lt;p&gt;(These formulas are specific to the platform described in the article)&lt;/p&gt;

&lt;p&gt;The general formula for the padding of objects is:&lt;/p&gt;

\[alignment\left\lceil\dfrac{size}{alignment}\right\rceil - size\]

  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//palindromes/&quot;&gt;Palindromes and more&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on June 06, 2016.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[My 'Intro to Generic Programming' talk (Spanish)]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//talk-intro-generic-programming/" />
  <id>https://componentsprogramming.com//talk-intro-generic-programming</id>
  <published>2016-06-02T09:00:00-03:00</published>
  <updated>2016-06-02T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;Hello!&lt;/p&gt;

&lt;p&gt;Here is the the link to the slides of my talk entitled &lt;a href=&quot;http://prezi.com/fxhtgwvubb10/?utm_campaign=share&amp;amp;utm_medium=copy&amp;amp;rc=ex0share&quot;&gt;“Introduction to Generic Programming”&lt;/a&gt; (Spanish resource), which was exhibited at the &lt;a href=&quot;http://www.meetup.com/cpp-ba/events/228427233/&quot;&gt;“First C++ Buenos Aires Meeting”&lt;/a&gt; on March 31, 2016.&lt;/p&gt;

&lt;p&gt;At that meeting we had the pleasure to view the excellent talk provided by our colleague &lt;a href=&quot;http://talesofcpp.fusionfenix.com/&quot;&gt;Agustín (K-Ballo) Bergé&lt;/a&gt;, entitled &lt;a href=&quot;https://drive.google.com/open?id=0B1jygKCWf7aSQWt2NGlhTGtUdFM0VUVBQnU2SlQyNDQ1Z213&quot;&gt;“The C++ Way”&lt;/a&gt;, where he tells us about the new features incorporated in C++11 and C++14 .&lt;/p&gt;

&lt;p&gt;I hope you like our presentations and you feel like to join &lt;a href=&quot;www.meetup.com/cpp-ba/&quot;&gt;our community&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Kind regards!&lt;/p&gt;


  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//talk-intro-generic-programming/&quot;&gt;My 'Intro to Generic Programming' talk (Spanish)&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on June 02, 2016.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Vin d'honneur]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//wine-honour/" />
  <id>https://componentsprogramming.com//wine-honour</id>
  <published>2016-05-04T09:00:00-03:00</published>
  <updated>2016-05-04T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;On May 4, 2016, I was invited by the Argentine Minister of Foreign Affairs, &lt;a href=&quot;https://twitter.com/SusanaMalcorra&quot;&gt;Susana Malcorra&lt;/a&gt;, to the &lt;em&gt;Vin d’honneur&lt;/em&gt; (Wine of Honor) that took place in the Golden Hall of the San Martín Palace, on the occasion of the Salute to the members of the &lt;a href=&quot;http://www.plataformaargentina.gov.ar/en/(copla_i)&quot;&gt;National Commission on the Outer Limit of the Continental Shelf (COPLA)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://componentsprogramming.com/images/copla/vinohonor1.jpg&quot; alt=&quot;Malcorra&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://componentsprogramming.com/images/copla/vinohonor2.jpg&quot; alt=&quot;COPLA Team&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Many thanks to the Minister and congratulations to the whole team!&lt;/strong&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;blockquote&gt;
  &lt;p&gt;The most extensive limit of Argentina - Our border with humanity&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;references&quot;&gt;References&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/SusanaMalcorra/status/727976624239554560&quot;&gt;Official Twitter of Susana Malcorra&lt;/a&gt;&lt;/p&gt;

&lt;hr /&gt;

  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//wine-honour/&quot;&gt;Vin d'honneur&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on May 04, 2016.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[The Outer Limit of the Argentine Continental Shelf]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//limit-approved/" />
  <id>https://componentsprogramming.com//limit-approved</id>
  <published>2016-03-11T09:00:00-03:00</published>
  <updated>2016-03-11T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;On 11 March 2016, the plenary of the 40th Session of the &lt;a href=&quot;http://www.un.org/depts/los/clcs_new/clcs_home.htm&quot;&gt;United Nations Commission on the Limits of the Continental Shelf (CLCS)&lt;/a&gt; approved the Recommendations on the Argentine presentation of the Outer Limit of the Continental Shelf. The Argentine Republic had presented the outer limit of the continental shelf of the whole Argentine territory - continental, insular and Antarctic Argentina - to the CLPC on April 21, 2009.&lt;/p&gt;

&lt;p&gt;It has been an honor and pride for me to have been a member of this team and this project, which I hope will serve to improve the quality of life for all Argentines.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://componentsprogramming.com/images/copla/copla1.jpg&quot; alt=&quot;Equipo COPLA&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Congratulations to the team!&lt;/strong&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;blockquote&gt;
  &lt;p&gt;The most extensive limit of Argentina - Our border with humanity&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;references&quot;&gt;References&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://www.plataformaargentina.gov.ar/userfiles/userfiles/FOLLETO-COPLA-2016_1.pdf&quot;&gt;COPLA-2016_1&lt;/a&gt;&lt;/p&gt;

&lt;hr /&gt;

  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//limit-approved/&quot;&gt;The Outer Limit of the Argentine Continental Shelf&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on March 11, 2016.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Alex Retirement]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//alex-retirement/" />
  <id>https://componentsprogramming.com//alex-retirement</id>
  <published>2016-01-14T09:00:00-03:00</published>
  <updated>2016-01-14T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;Alex has retired.&lt;/p&gt;

&lt;p&gt;This is a very nice Commemorative Stamp, by &lt;a href=&quot;https://twitter.com/joycecroft&quot;&gt;Joyce Croft&lt;/a&gt; and Alexander Zeyliger of &lt;a href=&quot;http://a9.com/&quot;&gt;A9&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://componentsprogramming.com/images/Stepanov_poster_FINAL.png&quot; alt=&quot;Alex Stepanov Commemorative Stamp&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I have to confess that I feel sad because I really wished that Alex continue contributing ideas for much longer time.&lt;br /&gt;
But he has retired and I’m happy for him.&lt;/p&gt;

&lt;p&gt;I wish you an excellent and well-deserved rest with your family and friends.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;who&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;successor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Thanks!&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//alex-retirement/&quot;&gt;Alex Retirement&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on January 14, 2016.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Thanks Alex!]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//thanks-alex/" />
  <id>https://componentsprogramming.com//thanks-alex</id>
  <published>2015-11-25T09:00:00-03:00</published>
  <updated>2015-11-25T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;I am very pleased to have received these two gifts.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://componentsprogramming.com/images/FM2GP.jpg&quot; alt=&quot;Alex autograph FM2GP&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://componentsprogramming.com/images/EoP.jpg&quot; alt=&quot;Alex autograph EoP&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://componentsprogramming.com/images/EoPAndFM2GP.jpg&quot; alt=&quot;Elements of Programming and From Mathematics to Generic Programming&quot; /&gt;&lt;/p&gt;

&lt;p&gt;These are two great books.&lt;/p&gt;

&lt;p&gt;It is an honor for me that are autographed by one of the greatest of our discipline.&lt;/p&gt;

&lt;p&gt;Do not deprive the pleasure of reading them.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/Elements-Programming-Alexander-A-Stepanov/dp/032163537X/&quot;&gt;Elements of Programming on Amazon&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/Mathematics-Generic-Programming-Alexander-Stepanov/dp/0321942043/&quot;&gt;From Mathematics to Generic Programming on Amazon&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks again, Alex!&lt;/p&gt;


  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//thanks-alex/&quot;&gt;Thanks Alex!&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on November 25, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[What's your favorite flavor of Object?]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//whats-your-favorite-flavor-of-object/" />
  <id>https://componentsprogramming.com//whats-your-favorite-flavor-of-object</id>
  <published>2015-09-26T09:00:00-03:00</published>
  <updated>2015-09-26T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;In this article I want to continue the discussion about the terminology that we, programmers, use daily; following the line of &lt;a href=&quot;/using-the-right-terms-method/&quot;&gt;“Using the proper terminology: Method?”&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;Programmers, especially since the popularization of &lt;em&gt;Object-Oriented Programming&lt;/em&gt;, use the word &lt;em&gt;Object&lt;/em&gt; without really thinking about its meaning.&lt;/p&gt;

&lt;p&gt;Always struck me all the terminology related to the &lt;em&gt;O-O paradigm&lt;/em&gt;, because I believe that many of the definitions of this paradigm are ambiguous or to vary between different authors.&lt;/p&gt;

&lt;h2 id=&quot;what-object-means&quot;&gt;What “Object” means?&lt;/h2&gt;

&lt;p&gt;In computer science, and especially in programming, there is no formal or a single definition of “Object”.&lt;br /&gt;
Different authors of programming languages ​​and textbooks was given different meanings to it.&lt;/p&gt;

&lt;p&gt;I will try, in this article, to present and analyze the different definitions and reach a conclusion about what definition is more in line to the programming style adopted in this blog.&lt;/p&gt;

&lt;h3 id=&quot;simula&quot;&gt;Simula&lt;/h3&gt;

&lt;p&gt;Here is the definition provided by the authors of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Simula&quot;&gt;Simula programming language&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;An object is a self-contained program (block instance) having its own &lt;strong&gt;local data&lt;/strong&gt; and &lt;strong&gt;actions&lt;/strong&gt; defined by a “class declaration”. The class declaration defines a program (data and action) pattern, and objects conforming to that pattern are said to “belong to the same class”. &lt;a href=&quot;#Ref1&quot;&gt;[1]&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The above definition is important because Simula is considered the first &lt;em&gt;object-oriented&lt;/em&gt; programming language.&lt;/p&gt;

&lt;p&gt;Three things stand out:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;An object has own data.&lt;/li&gt;
  &lt;li&gt;An object includes actions.&lt;/li&gt;
  &lt;li&gt;Membership of an object to a class&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;smalltalk&quot;&gt;Smalltalk&lt;/h3&gt;

&lt;p&gt;Although Simula is the first object-oriented programming language of, the &lt;em&gt;strong marketing&lt;/em&gt; of objects paradigm arose with &lt;a href=&quot;https://en.wikipedia.org/wiki/Smalltalk&quot;&gt;Smalltalk&lt;/a&gt;.&lt;br /&gt;
One can say that Smalltalk is the most influential language in regard to &lt;em&gt;Objects&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;According to the Smalltalk-72 instruction manual &lt;a href=&quot;#Ref2&quot;&gt;[2]&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Every entity in Smalltalk’s world is called an object. Objects can &lt;strong&gt;remember things&lt;/strong&gt; and &lt;strong&gt;communicate&lt;/strong&gt; with each other by sending and receiving messages. Every object belongs to a class…&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The same three things of the Simula definition stand out, but with variations:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;An object has its own data: “remember things”.&lt;/li&gt;
  &lt;li&gt;An object includes actions: communication by messages is a particular form of action.&lt;/li&gt;
  &lt;li&gt;Membership of an object to a class.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And, according to &lt;a href=&quot;#Ref3&quot;&gt;Smalltalk standard [3]&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;An object is a computational entity that is capable of &lt;strong&gt;responding&lt;/strong&gt; to a well defined set of messages. An object may also encapsulate some (possibly mutable) &lt;strong&gt;state&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It stands:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;An object has its own data: “state”.&lt;/li&gt;
  &lt;li&gt;An object includes actions: communication by messages is a particular form of action.&lt;/li&gt;
  &lt;li&gt;New concept: &lt;em&gt;mutability&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;eiffel---object-oriented-software-construction&quot;&gt;Eiffel - Object-Oriented Software Construction&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/Object-Oriented-Software-Construction-CD-ROM-Edition/dp/0136291554&quot;&gt;Object-Oriented Software Construction&lt;/a&gt; is one of the most important references in terms of object-orientation. It was written by &lt;a href=&quot;https://en.wikipedia.org/wiki/Bertrand_Meyer&quot;&gt;Bertrand Meyer&lt;/a&gt;, the designer of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Eiffel_(programming_language)&quot;&gt;Eiffel programming language&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Eiffel is one of the called &lt;em&gt;purely object-oriented languages&lt;/em&gt;, because even the built-in types, as integer or real, are created by classes.&lt;/p&gt;

&lt;p&gt;Here the definition of &lt;em&gt;object&lt;/em&gt; given by Meyer in OOSC &lt;a href=&quot;#Ref4&quot;&gt;[4]&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Object: A run-time data structure made of zero or more &lt;strong&gt;values, called fields&lt;/strong&gt;, and serving as the computer representation of an abstract object. Every object is an &lt;strong&gt;instance of some class&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The above definition states:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;An object has its own data: “made of zero or more values”.&lt;/li&gt;
  &lt;li&gt;Membership of an object to a class.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;c&quot;&gt;C++&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/C%2B%2B&quot;&gt;C++&lt;/a&gt; is a language that supports &lt;em&gt;objects technology&lt;/em&gt;.&lt;br /&gt;
C++ has a definition of the object very different to which most programmers are accustomed:&lt;/p&gt;

&lt;p&gt;According to the &lt;a href=&quot;#Ref5&quot;&gt;C++ standard [5]&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;… An object is a &lt;strong&gt;region of storage&lt;/strong&gt;. [Note: A function is not an object, regardless of whether or not it occupies storage in the way that objects do. —end note] …&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;c-programming-language&quot;&gt;C programming language&lt;/h3&gt;

&lt;p&gt;You may wonder why I’m including the &lt;a href=&quot;https://en.wikipedia.org/wiki/C_(programming_language)&quot;&gt;C programming language&lt;/a&gt; in this list if it does not include features of object-oriented languages.&lt;/p&gt;

&lt;p&gt;While the C language is not an object-oriented language, it includes in its &lt;a href=&quot;#Ref6&quot;&gt;standard [6]&lt;/a&gt; a definition of &lt;em&gt;object&lt;/em&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;object: &lt;strong&gt;region of data storage&lt;/strong&gt; in the execution environment, the contents of which can represent values…&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;elements-of-programming&quot;&gt;Elements of Programming&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://www.elementsofprogramming.com/&quot;&gt;Elements of Programming&lt;/a&gt; is the &lt;em&gt;magnum opus&lt;/em&gt; of &lt;a href=&quot;http://www.stepanovpapers.com&quot;&gt;Alexander Stepanov&lt;/a&gt; and &lt;a href=&quot;http://www.softwarepreservation.org/author/pmcjones&quot;&gt;Paul McJones&lt;/a&gt;.&lt;br /&gt;
According my consideration, it is on the way to become a classic text of programming, so I recommend to all those lovers of this beautiful discipline that do not deprive yourself to read it.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;An object is a representation of a &lt;a href=&quot;#RefConcreteEntity&quot;&gt;concrete entity&lt;/a&gt; as a &lt;strong&gt;value in memory&lt;/strong&gt;. An object has a &lt;strong&gt;state&lt;/strong&gt; that is a value of some value type. The state of an object is &lt;strong&gt;changeable&lt;/strong&gt;. Given an object corresponding to a concrete entity, its state corresponds to a snapshot of that entity…&lt;br /&gt;
…&lt;br /&gt;
An object type is a pattern for &lt;strong&gt;storing and modifying&lt;/strong&gt; values in memory. Corresponding to every object type is a value type describing states of objects of that type. Every object &lt;strong&gt;belongs to an object type&lt;/strong&gt;. An example of an object type is integers represented in 32-bit two’s complement little-endian format aligned to a 4-byte address boundary. &lt;a href=&quot;#Ref7&quot;&gt;[7]&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the above definition, Stepanov and McJones try to take away from any kind of programming paradigm and establish a more general definition of what an &lt;em&gt;object&lt;/em&gt; means. While clear differences with the other definitions are noted, you can see some similarities with them:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;An object has its own data: “value in memory”, “state”.&lt;/li&gt;
  &lt;li&gt;An object includes actions: “storing and modifying values in memory” are basically actions or behavior.&lt;/li&gt;
  &lt;li&gt;Membership of an object to an &lt;em&gt;object type&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;An object is mutable/changeable.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h2&gt;

&lt;p&gt;As you have noticed, there are a variety of definitions of &lt;em&gt;object&lt;/em&gt;. Some match in certain features, some not.&lt;/p&gt;

&lt;p&gt;The blog author prefers the definition given by Stepanov and McJones in &lt;a href=&quot;http://www.elementsofprogramming.com/&quot;&gt;Elements of Programming&lt;/a&gt;. I consider it the most complete, practical, broad and based on a solid mathematical foundation definition.&lt;/p&gt;

&lt;p&gt;It is the most complete definition, because it begins to develop a taxonomy of ideas as &lt;em&gt;Entity&lt;/em&gt;, &lt;em&gt;Species&lt;/em&gt;, &lt;em&gt;Genus&lt;/em&gt;, which serves as the basis for &lt;em&gt;object&lt;/em&gt; definition and other definitions. These definitions are not included in this article to learn more about it please refer to &lt;a href=&quot;http://www.elementsofprogramming.com/&quot;&gt;the book&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It is the most practical definition, since it is aware that all programs run on computers with memory and these constitutes the only available realization of a universal computational device.&lt;/p&gt;

&lt;p&gt;It is the broadest definition, since it does not exclude ​​non-object-oriented languages.&lt;/p&gt;

&lt;p&gt;Again, I made ​​my choice, but there is not need to say that you can choose any definition you like, just make sure that your thinking is free of influences disseminated by the &lt;em&gt;Objects&lt;/em&gt; propaganda machinery :)&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;terminology-series&quot;&gt;“Terminology” series:&lt;/h2&gt;

&lt;p&gt;   &lt;a href=&quot;/using-the-right-terms-method/&quot;&gt;Using the proper terminology: Method?&lt;/a&gt;&lt;br /&gt;
   &lt;a href=&quot;/whats-your-favorite-flavor-of-object/&quot;&gt;What’s your favorite flavor of Object?&lt;/a&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;notes--references&quot;&gt;Notes / References&lt;/h2&gt;

&lt;p&gt;Clarification: The emphasis/bold in each of the definitions is on my own.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref1&quot;&gt;[1]&lt;/a&gt; SIMULA 67: COMMON BASE LANGUAGE, by Ole-Johan Dahl, Bjørn Myhrhaug and Kristen Nygaard. Publication No. S-22. Classes 1. 3. 3 [page 6]&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref2&quot;&gt;[2]&lt;/a&gt; Smalltalk-72 Instruction Manual by Adele Goldberg and Alan Kay [page 44]&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref3&quot;&gt;[3]&lt;/a&gt; &lt;a href=&quot;http://smalltalk.org/versions/ANSISmalltalk/ANSISmalltalkStandard_v1.9_199712_NCITS_X3J20_draft.pdf&quot;&gt;ANSI Smalltalk Standard v1.9 199712 NCITS X3J20 draft&lt;/a&gt;, Section 3.1 [page 9]&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref4&quot;&gt;[4]&lt;/a&gt; Object-Oriented Software Construction (2nd Ed) by Bertrand Meyer [1997, page 1198]&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref5&quot;&gt;[5]&lt;/a&gt; &lt;a href=&quot;http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4527.pdf&quot;&gt;ISO International Standard ISO/IEC 14882:2014(E) – Programming Language C++, current working draft (at Aug, 2015)&lt;/a&gt;, section: 1.8 [intro.object] paragraph 1.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref6&quot;&gt;[6]&lt;/a&gt; &lt;a href=&quot;http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1539.pdf&quot;&gt;ISO/IEC 9899:201x Committee Draft – Programming languages — C (last draft available at Aug, 2015)&lt;/a&gt;, section: 13.15.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref7&quot;&gt;[7]&lt;/a&gt; &lt;a href=&quot;http://www.elementsofprogramming.com/&quot;&gt;Elements of Programming&lt;/a&gt; by Stepanov and McJones [2009, page 4]&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;RefConcreteEntity&quot;&gt;EoP’s concrete entity definition: &lt;/a&gt; An abstract entity is an individual thing that is eternal and unchangeable, while a &lt;strong&gt;concrete entity&lt;/strong&gt; is an individual thing that comes into and out of existence in space and time… Blue and 13 are examples of abstract entities. Socrates and the United States of America are examples of concrete entities…&lt;/p&gt;


  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//whats-your-favorite-flavor-of-object/&quot;&gt;What's your favorite flavor of Object?&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on September 26, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Using the proper terminology: Method?]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//using-the-right-terms-method/" />
  <id>https://componentsprogramming.com//using-the-right-terms-method</id>
  <published>2015-08-12T09:00:00-03:00</published>
  <updated>2015-08-12T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;In this series of articles I intend to review why we, as programmers, use certain terminology, certain words, some of which I consider inappropriate.&lt;br /&gt;
In this case want to talk about the word &lt;em&gt;method&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;First of all …&lt;/p&gt;

&lt;h3 id=&quot;what-is-a-method&quot;&gt;What is a &lt;em&gt;method&lt;/em&gt;?&lt;/h3&gt;

&lt;p&gt;Any programmer knows that an &lt;a href=&quot;#Ref1&quot;&gt;&lt;em&gt;object&lt;/em&gt; [1]&lt;/a&gt; has state and behavior.&lt;br /&gt;
The state is stored in &lt;em&gt;fields&lt;/em&gt; and behavior is achieved through &lt;em&gt;methods&lt;/em&gt;.
A &lt;em&gt;method&lt;/em&gt; (in programming) is a sequence of instructions, is part of an object or class. &lt;a href=&quot;#Ref2&quot;&gt;[2]&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But …&lt;/p&gt;

&lt;h3 id=&quot;why-method&quot;&gt;Why “method”?&lt;/h3&gt;

&lt;p&gt;What is the difference between a &lt;em&gt;method&lt;/em&gt; and a &lt;em&gt;procedure&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;procedure&lt;/em&gt; is a sequence of instructions. &lt;a href=&quot;#Ref2&quot;&gt;[2]&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The only difference is that a method must belong to a class while in the definition of procedure it is unclear whether it belongs to a class or not. The term “procedure” is used long before the existence of the term “class”.&lt;/p&gt;

&lt;p&gt;In summary, a method is a procedure that is a member of a class.&lt;br /&gt;
So, we have &lt;em&gt;member&lt;/em&gt; procedures and &lt;em&gt;free&lt;/em&gt; procedures (outside a class).&lt;/p&gt;

&lt;p&gt;I keep wondering …&lt;/p&gt;

&lt;h3 id=&quot;why-we-use-the-term-method-insted-of-procedure&quot;&gt;Why we use the term “&lt;em&gt;method&lt;/em&gt;” insted of “&lt;em&gt;procedure&lt;/em&gt;”?&lt;/h3&gt;

&lt;p&gt;My guess is that with the establishment of &lt;a href=&quot;https://en.wikipedia.org/wiki/Java_(programming_language)&quot;&gt;Java&lt;/a&gt; as one of the most popular programming languages, also saw the popularization of its terminology. From that moment, other languages ​​and programmers adopt the terminology popularized by Java and it becomes more frequent use than traditional terminology.&lt;/p&gt;

&lt;p&gt;Now, I want to make a search through the history of programming languages ​​to understand why we began calling “method” to the “procedures”. I will go from modern to ancient times …&lt;/p&gt;

&lt;h3 id=&quot;java&quot;&gt;Java&lt;/h3&gt;

&lt;p&gt;In &lt;a href=&quot;https://en.wikipedia.org/wiki/Java_(programming_language)&quot;&gt;Java&lt;/a&gt;, there are no &lt;em&gt;free procedures&lt;/em&gt;, all procedures have to be a member of a class, these member procedures actually are called methods.&lt;/p&gt;

&lt;p&gt;According to &lt;a href=&quot;#Ref3&quot;&gt;Java 8 specification [3]&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The body of a class declares members (fields and methods and nested classes and interfaces), instance and static initializers, and constructors.&lt;/li&gt;
  &lt;li&gt;Method declarations describe code that may be invoked by method invocation expressions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Java specification describes the relationship between a method and a class, but it does not specify what the word “method” means.&lt;/p&gt;

&lt;p&gt;Beyond the definition, in the specification we will not find why language designers decided to call “method” to procedures, so we must go even further back.&lt;/p&gt;

&lt;p&gt;Java inherits most of its syntax (not its semantics) and terminology from &lt;a href=&quot;https://en.wikipedia.org/wiki/C%2B%2B&quot;&gt;C++&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;c&quot;&gt;C++&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/C%2B%2B&quot;&gt;C++&lt;/a&gt; born in the late 1970s, as a superset of the &lt;a href=&quot;https://en.wikipedia.org/wiki/C_(programming_language)&quot;&gt;C programming language&lt;/a&gt; with the addition object technology.&lt;/p&gt;

&lt;p&gt;Therefore, in C++ we have member procedures and free procedures, called &lt;em&gt;member&lt;/em&gt; and &lt;em&gt;non-member functions&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;According to the &lt;a href=&quot;#Ref4&quot;&gt;C++ standard [4]&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Member functions: Functions declared in the definition of a class, excluding those declared with a friend specifier, are called member functions of that class.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other words, the word “method” is not used by C++ programmers. Maybe some C++ programmers use it because the term is very popular today; but neither the C++ standard nor the core C++ programmers use “method”.&lt;/p&gt;

&lt;p&gt;So … What is the correct term for the code that models the behavior of objects? Is it “method”? Is it “procedure”? Which language uses the most appropriate terminology?&lt;/p&gt;

&lt;p&gt;I must go back in time.&lt;/p&gt;

&lt;p&gt;C++ inherits its objects “side” from &lt;a href=&quot;https://en.wikipedia.org/wiki/Simula&quot;&gt;the Simula programming language&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;simula&quot;&gt;Simula&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Simula&quot;&gt;Simula&lt;/a&gt; is considered the first &lt;em&gt;object-oriented&lt;/em&gt; programming language.&lt;br /&gt;
It is a statically typed language based on &lt;a href=&quot;https://en.wikipedia.org/wiki/ALGOL_60&quot;&gt;ALGOL 60&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Simula allows procedures to be declared within a class and also outside it. The former are called the &lt;em&gt;local procedures&lt;/em&gt; and the last &lt;em&gt;non-local procedures&lt;/em&gt;. &lt;a href=&quot;#Ref5&quot;&gt;[5]&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, if C++ and Simula not make mention of something called “method” Where does Java take that word? Is it an invention of language designers? Is it copied from some other language?&lt;/p&gt;

&lt;p&gt;The answer is …&lt;/p&gt;

&lt;h3 id=&quot;smalltalk&quot;&gt;Smalltalk&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Smalltalk&quot;&gt;Smalltalk&lt;/a&gt; is an object-oriented programming language, it is inspired by Simula (in part), it was developed during the 1970s and was born in the early 80’s.&lt;br /&gt;
Unlike Simula, Smalltalk is a dynamically typed language.&lt;/p&gt;

&lt;p&gt;Smalltalk incorporates the concepts of &lt;em&gt;messages&lt;/em&gt; and &lt;em&gt;methods&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;According to the &lt;a href=&quot;#Ref6&quot;&gt;Smalltalk standard [6]&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A message is a request to perform a designated computation. An object is a computational entity
that is capable of responding to a well defined set of messages. An object may also encapsulate
some (possibly mutable) state.&lt;/li&gt;
  &lt;li&gt;An object responds to a message by executing a method. Each method is identified by an
associated method selector. A behavior is the set of methods used by an object to respond to
messages.&lt;/li&gt;
  &lt;li&gt;A method consists of a sequence of expressions. Program execution proceeds by sequentially
evaluating the expressions in one of more methods. There are three types of expressions:
assignments, message sends, and returns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As I could not find references of the word “method” on languages older than Smalltalk, I consulted &lt;a href=&quot;http://www.softwarepreservation.org/author/pmcjones&quot;&gt;Paul McJones&lt;/a&gt;, member of the &lt;a href=&quot;http://www.softwarepreservation.org/&quot;&gt;Software Preservation Group&lt;/a&gt; and coauthor of &lt;a href=&quot;http://www.elementsofprogramming.com/&quot;&gt;Elements of Programming&lt;/a&gt;. 
I consider Paul an authority when it comes to computing and programming language history.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Fernando: … Do you know any programming language, before Smalltalk, that use the word “method”? …&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Paul: You ask a very interesting question. I think you are correct that the word “method” was first used in that sense by the Smalltalk community. I looked through some early documents, such as the Smalltalk-72 Instruction Manual, and I don’t see “method” being used yet…
Dan Ingalls’s 1978 POPL paper on Smalltalk-76 is the earliest paper I’ve found so far that uses method in the sense you are interested in…&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Paul believes, as I do, that the word “method” was first used by the Smalltalk community. He also said that he was reviewing old manuals; the word “method” seems to emerge between 1972 and 1976, acording to the Smalltalk-72 Instruction Manual &lt;a href=&quot;#Ref7&quot;&gt;[7]&lt;/a&gt; and a paper of 1978 which talks about Smalltalk-76. &lt;a href=&quot;#Ref8&quot;&gt;[8]&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In other words, I’m pretty sure the Smalltalk community coined the term, but they didn’t do from the beginning but as the language evolved.&lt;/p&gt;

&lt;h3 id=&quot;other-languages&quot;&gt;Other languages&lt;/h3&gt;

&lt;p&gt;Here I want to discuss other programming languages that also support object technology to verify how “procedures” are called.&lt;/p&gt;

&lt;h3 id=&quot;eiffel&quot;&gt;Eiffel&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Eiffel_(programming_language)&quot;&gt;Eiffel&lt;/a&gt; is is a language born in the mid 1980s and, like C++, is a direct heir of Simula.&lt;/p&gt;

&lt;p&gt;In Eiffel every member of a class is called &lt;em&gt;feature&lt;/em&gt;. The state is called &lt;em&gt;attributes&lt;/em&gt; and the behavior &lt;em&gt;routines&lt;/em&gt;. In turn, the &lt;em&gt;routines&lt;/em&gt; are divided into &lt;em&gt;functions&lt;/em&gt; and &lt;em&gt;procedures&lt;/em&gt; depending on whether or not return a result. &lt;a href=&quot;#Ref9&quot;&gt;[9]&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In Eiffel all &lt;em&gt;routines&lt;/em&gt; are members of a class, the existence of free procedures is not allowed.&lt;/p&gt;

&lt;h3 id=&quot;python&quot;&gt;Python&lt;/h3&gt;

&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Python_(programming_language)&quot;&gt;Python programming language&lt;/a&gt; supports free and member procedures. They are called &lt;em&gt;functions&lt;/em&gt; and &lt;em&gt;methods&lt;/em&gt; respectively. &lt;a href=&quot;#Ref10&quot;&gt;[10]&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Python caught my attention because it’s created a few years before Java; the first implementation of Python is about 1989 &lt;a href=&quot;#Ref11&quot;&gt;[11]&lt;/a&gt; while Java first implementation is from 1995 &lt;a href=&quot;#Ref12&quot;&gt;[12]&lt;/a&gt;.&lt;br /&gt;
It seems that this breaks my guess that Java was the &lt;em&gt;promoter&lt;/em&gt; of the word “method”.&lt;/p&gt;

&lt;p&gt;While the first implementation of Java comes out in 1995, in just a few years, in 1998, Java was one of the most popular languages. Instead, Python had a longer way to popularity, it began to be popular in 2003/2004. &lt;a href=&quot;#Ref13&quot;&gt;[13]&lt;/a&gt;&lt;br /&gt;
So my guess is still valid, the &lt;em&gt;promoter&lt;/em&gt; of “method” was Java and not Python.&lt;/p&gt;

&lt;p&gt;Returning to Python, the word “method” is mentioned in recent versions (3.4.3 and 2.7.10) of the language documentation.&lt;br /&gt;
Then I hesitated about whether Python uses “method” from its origins or its terminology was mutating over time, especially with the popularity of Java.&lt;br /&gt;
Thanks to excellent job done by the &lt;a href=&quot;http://python.org&quot;&gt;python.org&lt;/a&gt; guys keeping the historical versions of documentation I could verify that the word “method” is used since version 0.9.1 &lt;a href=&quot;#Ref14&quot;&gt;[14]&lt;/a&gt; (unless the documentation has been altered over time, but I hope that is not the case).&lt;/p&gt;

&lt;p&gt;In other words, Python is the first language, after Smalltalk, to use the word “method”. At least in the languages ​​I could verify.&lt;/p&gt;

&lt;p&gt;Now I wonder, why Python used method instead of a more common term?.
No one better than the creator of language, &lt;a href=&quot;https://en.wikipedia.org/wiki/Guido_van_Rossum&quot;&gt;Guido van Rossum&lt;/a&gt; to enlighten. I wrote an email to him, Guido was very kind to answer and let me quote him:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Fernando: Why did you use “method” instead of any other word? Have you taken from Smalltalk?  Or,… from any other programming language?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Guido: Good question. I don’t recall, but it’s likely that I was somehow influenced by Smalltalk, or people around me who were influenced by Smalltalk. I definitely opened a big book about Smalltalk to learn about byte code, and I also recall we had a Smalltalk implementation that we sometimes played with (some things that were wrong with that influenced some of my philosophy for Python).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Guido does not remember well, but admits to being influenced by Smalltalk at that time.&lt;/p&gt;

&lt;h3 id=&quot;c-1&quot;&gt;C#&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/C_Sharp_(programming_language)&quot;&gt;C#&lt;/a&gt; is a programming language that is born being very similar to Java (almost identical) with some reminiscences of C++.&lt;br /&gt;
Then, over the years the language formed his own identity.&lt;/p&gt;

&lt;p&gt;C# copy many terms of Java. One of the words that copy is “method”.&lt;/p&gt;

&lt;p&gt;According to the &lt;a href=&quot;#Ref15&quot;&gt;C# standard [15]&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A method is a member that implements a computation or action that can be performed by an object or class.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike Java spec, the C# standard defines the meaning of the word “method”.&lt;/p&gt;

&lt;p&gt;As C# is a widely used language, especially by followers of the Microsoft “philosophy”, it is a major contributor on the popularization of the term.&lt;/p&gt;

&lt;h3 id=&quot;objective-c&quot;&gt;Objective-C&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Objective-C&quot;&gt;Objective-C&lt;/a&gt; is a language developed in the early 1980’s. Like C ++, born as a super-set of C adding object technology.&lt;br /&gt;
Unlike C++, the object model of Objective-C is based on Smalltalk and not in Simula.&lt;/p&gt;

&lt;p&gt;Objective-C, like Smalltalk, has the concept of messages and methods. &lt;a href=&quot;#Ref16&quot;&gt;[16]&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;languages-before-smalltalk&quot;&gt;Languages ​​before Smalltalk&lt;/h3&gt;

&lt;p&gt;I looked in manuals, standards and documentation of programming languages older than Smalltalk, unrelated to object-oriented programming, but very recognized for their influence on others languages.&lt;br /&gt;
Specifically, the languages ​​are &lt;a href=&quot;https://en.wikipedia.org/wiki/Fortran&quot;&gt;Fortran&lt;/a&gt;, &lt;a href=&quot;https://en.wikipedia.org/wiki/ALGOL&quot;&gt;Algol&lt;/a&gt; and &lt;a href=&quot;https://en.wikipedia.org/wiki/C_(programming_language)&quot;&gt;C&lt;/a&gt;. The word “method” is not mentioned in the documentation of any of them. Instead they use terms like routine, subroutine, procedure or function &lt;a href=&quot;#Ref17&quot;&gt;[17]&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;other-sources&quot;&gt;Other sources&lt;/h3&gt;

&lt;p&gt;To enhance my investigation I’ve done a search in some books (about programming in general) I have at my disposal:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.elementsofprogramming.com/&quot;&gt;Elements of Programming&lt;/a&gt; by Alexander A. Stepanov and Paul McJones&lt;/li&gt;
  &lt;li&gt;The Art of Computer Programming Volumes 1, 2, 3 and 4A by Donald E. Knuth&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I found no reference to the word “method” in any of the 3,456 pages of these 5 books. Obviously the word “method” appears on several occasions, but not in the sense that I’m looking for.&lt;/p&gt;

&lt;h3 id=&quot;pending-issues&quot;&gt;Pending issues&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Read documentation/manuals about the following programming languages:
    &lt;ul&gt;
      &lt;li&gt;Lisp&lt;/li&gt;
      &lt;li&gt;Scheme&lt;/li&gt;
      &lt;li&gt;Ada&lt;/li&gt;
      &lt;li&gt;Modula-2&lt;/li&gt;
      &lt;li&gt;Common Lisp&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Look at some classic texts:
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Discipline-Programming-Edsger-W-Dijkstra/dp/013215871X/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1440444447&amp;amp;sr=1-1&quot;&gt;A Discipline of Programming&lt;/a&gt; by Edsger W. Dijkstra&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Algorithms-Data-Structures-Niklaus-Wirth/dp/0130220051/ref=pd_sim_14_5?ie=UTF8&amp;amp;refRID=1WFYYC7DTMCR58G3TVVA&quot;&gt;Algorithms and Data Structures&lt;/a&gt; by Niklaus Wirth&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Structured-Programming-A-P-I-C-studies-processing/dp/0122005503/ref=pd_sim_14_3?ie=UTF8&amp;amp;refRID=1WFYYC7DTMCR58G3TVVA&quot;&gt;Structured Programming, First Edition Edition&lt;/a&gt; by Edsger W. Dijkstra, C. A. R. Hoare and Ole-Johan Dahl&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Systematic-Programming-Introduction-Prentice-Hall-Computation/dp/0138803692/ref=pd_sim_14_4?ie=UTF8&amp;amp;refRID=0E9BAZYMCCHZFEB38592&quot;&gt;Systematic Programming: An Introduction&lt;/a&gt; by Niklaus Wirth&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Send the question to the following programming languages designers:
    &lt;ul&gt;
      &lt;li&gt;Alan Kay&lt;/li&gt;
      &lt;li&gt;Dan Ingalls&lt;/li&gt;
      &lt;li&gt;Dick Gabriel&lt;/li&gt;
      &lt;li&gt;James Gosling&lt;/li&gt;
      &lt;li&gt;Anders Hejlsberg&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I have tried to contact them, unsuccessfully at the moment.&lt;/p&gt;

&lt;h2 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h2&gt;

&lt;p&gt;The word “method” was introduced to programming by the Smalltalk community. Smalltalk was not practically used in the industry in the late 80’s and early 90’s, so I think the word “method” was not as common as it is now.&lt;/p&gt;

&lt;p&gt;Maybe it was a mistake of the Smalltalk designers had not used existing nomenclature, or maybe not, as Smalltalk introduced a object model different from Simula and that change perhaps merited a change of names. It is very difficult to determine if it was a wrong decision or not.&lt;/p&gt;

&lt;p&gt;Java and Python adopt Smalltalk terminology in their vocabulary. I do not find a strong relation between Smalltalk and Java / Python justifying the use of the term, this makes me think that the use of “method” is an error in these languages. Although, as I said, is very difficult to judge whether the decision to use certain nomenclature is wrong or not.&lt;/p&gt;

&lt;p&gt;C++, Objective-C and Eiffel used similar terminology than its predecessors, perhaps with some variations, which may be minimal or necessary to adapt to changes introduced by the languages.&lt;/p&gt;

&lt;p&gt;Some languages, such as C#, copied Java features, but also copied the “error” to incorporate “method” to its terminology.&lt;/p&gt;

&lt;p&gt;The word “method” is infiltrated into the vocabulary of programmers.&lt;br /&gt;
There is no turning back ;)&lt;/p&gt;

&lt;p&gt;You can use the term that you like, I’ll keep using procedure or function (as long as I remember that I do not like the word “method”).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h2&gt;

&lt;p&gt;Special thanks to Paul McJones and Guido van Rossum for kindly respond to my questions and for giving very useful information.&lt;br /&gt;
Also thanks to Mario dal Lago and Javier Velilla for reviewing the article and suggest corrections.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;notes--references&quot;&gt;Notes / References&lt;/h2&gt;

&lt;p&gt;&lt;a name=&quot;Ref1&quot;&gt;[1]&lt;/a&gt; Here we refer to the definition of “object” in the context of &lt;em&gt;Object-Oriented Programming&lt;/em&gt;:&lt;br /&gt;
      Object-Oriented Software Construction (2nd Ed) by Bertrand Meyer [1997, page 1198]&lt;br /&gt;
which differs from the definition of “object” given in:&lt;br /&gt;
      &lt;a href=&quot;http://www.elementsofprogramming.com/&quot;&gt;Elements of Programming&lt;/a&gt; by Stepanov and McJones [2009, page 4]&lt;br /&gt;
This last definition is preferred by the author of the blog. If it is not specified, the reader must assume that I am talking about the definition given by Stepanov.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref2&quot;&gt;[2]&lt;/a&gt; This is a simplified definition, for a more detailed definition refer to:
      Object-Oriented Software Construction (2nd Ed) by Bertrand Meyer [1997, page 174]&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref3&quot;&gt;[3]&lt;/a&gt; &lt;a href=&quot;https://docs.oracle.com/javase/specs/jls/se8/jls8.pdf&quot;&gt;The Java Language Specification (Java SE 8 Edition)&lt;/a&gt;, Chapter 8 [pages 191, 192]&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref4&quot;&gt;[4]&lt;/a&gt; &lt;a href=&quot;http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4527.pdf&quot;&gt;ISO International Standard ISO/IEC 14882:2014(E) – Programming Language C++, current working draft (at Aug, 2015)&lt;/a&gt;, sections: 9.3 [class.mfct] and 8.3.5 [dcl.fct]&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref5&quot;&gt;[5]&lt;/a&gt; There is almost no bibliography available about Simula, but &lt;a href=&quot;http://www.edelweb.fr/Simula/&quot;&gt;here&lt;/a&gt; you can find an excellent collection of old manuals.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref6&quot;&gt;[6]&lt;/a&gt; &lt;a href=&quot;http://smalltalk.org/versions/ANSISmalltalk/ANSISmalltalkStandard_v1.9_199712_NCITS_X3J20_draft.pdf&quot;&gt;ANSI Smalltalk Standard v1.9 199712 NCITS X3J20 draft&lt;/a&gt;, Section 3.1 [page 9]&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref7&quot;&gt;[7]&lt;/a&gt; Smalltalk-72 Instruction Manual by Adele Goldberg and Alan Kay [page 44]&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref8&quot;&gt;[8]&lt;/a&gt; &lt;a href=&quot;https://classes.soe.ucsc.edu/cmps112/Spring03/readings/Ingalls78.html&quot;&gt;The Smalltalk-76 Programming System. Design and Implementation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref9&quot;&gt;[9]&lt;/a&gt; Specified in:&lt;br /&gt;
      Object-Oriented Software Construction (2nd Ed) by Bertrand Meyer [1997, pages 174, 175]&lt;br /&gt;
and:&lt;br /&gt;
      &lt;a href=&quot;http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-367.pdf&quot;&gt;Standard ECMA-367. Eiffel: Analysis, Design and Programming Language. 2nd edition (June 2006)&lt;/a&gt;, Section 8.5.10 [page 38]&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref10&quot;&gt;[10]&lt;/a&gt; &lt;a href=&quot;https://docs.python.org/2/download.html&quot;&gt;Python 2.7.10 Documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref11&quot;&gt;[11]&lt;/a&gt; &lt;a href=&quot;http://python-history.blogspot.com.ar/2009/01/brief-timeline-of-python.html&quot;&gt;A Brief Timeline of Python&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref12&quot;&gt;[12]&lt;/a&gt; &lt;a href=&quot;http://www.oracle.com/technetwork/java/javase/overview/javahistory-index-198355.html&quot;&gt;The History of Java Technology&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref13&quot;&gt;[13]&lt;/a&gt; On the popularization of languages, one of the sources on which I rely is &lt;a href=&quot;http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html&quot;&gt;Tiobe Index&lt;/a&gt;. Although I have some doubts about the measuring method, Tiobe it is the best we have today.&lt;br /&gt;
The other source is my memory. I do not remember Python as a popular language in the 90’s.&lt;br /&gt;
I think Python starts to become popular with the boom of dynamic languages ​​in the early 2000’s and it had an even greater impetus to the creation of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Django_(web_framework)&quot;&gt;Django web framework&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref14&quot;&gt;[14]&lt;/a&gt; &lt;a href=&quot;https://www.python.org/ftp/python/src/&quot;&gt;Repository of older versions of Python&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref15&quot;&gt;[15]&lt;/a&gt; &lt;a href=&quot;http://www.ecma-international.org/publications/standards/Ecma-334.htm&quot;&gt;Standard ECMA-334. C# Language Specification. 4th edition (June 2006)&lt;/a&gt;, Section 8.7.3 [page 34]&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref16&quot;&gt;[16]&lt;/a&gt; Objective-C has neither a standard nor a specification. The only “formal” documentation I could find was a website:&lt;br /&gt;
      &lt;a href=&quot;https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html&quot;&gt;Programming With ObjectiveC&lt;/a&gt;&lt;br /&gt;
Kimberley Hagan wrote me to share a resource that she thinks may be valuable for Objective-C programmers, so here I leave the link in case you want to hit a glance:&lt;br /&gt;
      &lt;a href=&quot;http://www.whoishostingthis.com/resources/objective-c/&quot;&gt;Objective-C: Guide and Resources&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref17&quot;&gt;[17]&lt;/a&gt; For documentation on &lt;a href=&quot;http://www.softwarepreservation.org/projects/FORTRAN/&quot;&gt;FORTRAN&lt;/a&gt;, &lt;a href=&quot;http://www.softwarepreservation.org/projects/ALGOL/&quot;&gt;ALGOL&lt;/a&gt; and C: &lt;a href=&quot;http://www.open-std.org/JTC1/SC22/WG14/&quot;&gt;ISO/IEC JTC1/SC22/WG14 - The international standardization working group for the programming language C&lt;/a&gt;.&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//using-the-right-terms-method/&quot;&gt;Using the proper terminology: Method?&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on August 12, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Stepanov's new book: From Mathematics to Generic Programming]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//from-mathematics-to-generic-programming/" />
  <id>https://componentsprogramming.com//from-mathematics-to-generic-programming</id>
  <published>2014-11-06T09:00:00-03:00</published>
  <updated>2014-11-06T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;The new book by Alexander Stepanov, and his colleague Daniel Rose, is about to go on sale.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/Mathematics-Generic-Programming-Alexander-Stepanov/dp/0321942043/&quot;&gt;
	&lt;!-- &lt;img src=&quot;/images/fmgp.jpg&quot; alt=&quot;Drawing&quot; style=&quot;width: 50%; height:80%;&quot; /&gt; --&gt;
	&lt;img src=&quot;/images/fm2gp_portada.jpg&quot; alt=&quot;Drawing&quot; style=&quot;width: 50%; height:80%;&quot; /&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The book is based on the &lt;a href=&quot;http://www.stepanovpapers.com/&quot;&gt;“Algorithmic Journeys”&lt;/a&gt; course taught by Alex at A9.&lt;/p&gt;

&lt;p&gt;I had the honor of reviewing the draft version, and I can say it’s a wonderful book!&lt;br /&gt;
It combines mathematics, programming and history in a gorgeous and unique way. (It amazes me the knowledge that this guy has.)&lt;br /&gt;
It is a pleasure to read.&lt;/p&gt;

&lt;p&gt;I enjoyed reading it the same way I enjoyed watching the videos of the course.&lt;br /&gt;
I strongly recommend it for all programmers who love their profession.&lt;/p&gt;

&lt;p&gt;You can pre order it on &lt;a href=&quot;http://www.amazon.com/Mathematics-Generic-Programming-Alexander-Stepanov/dp/0321942043/&quot;&gt;Amazon here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Eager to have the printed version in my hands.&lt;br /&gt;
(If my friends from Argentina Customs let me)&lt;/p&gt;

&lt;p&gt;Thanks Alex!&lt;/p&gt;


  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//from-mathematics-to-generic-programming/&quot;&gt;Stepanov's new book: From Mathematics to Generic Programming&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on November 06, 2014.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Components Programming]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//components-programming/" />
  <id>https://componentsprogramming.com//components-programming</id>
  <published>2014-10-28T09:00:00-03:00</published>
  <updated>2014-10-28T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;As you may have noticed, &lt;em&gt;Components Programming&lt;/em&gt; is the name of &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;this blog&lt;/a&gt;.
And perhaps you’re wondering, what &lt;em&gt;Components Programming&lt;/em&gt; means?&lt;/p&gt;

&lt;p&gt;It is convenient to clarify it now, since, in upcoming articles I want to test several programming languages to determine which ones are good for &lt;em&gt;Components Programming&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So, what &lt;em&gt;Components Programming&lt;/em&gt; means?
What is a &lt;strong&gt;Component&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;No one better than &lt;a href=&quot;http://www.stepanovpapers.com/&quot;&gt;Alex&lt;/a&gt; to explain it:&lt;/p&gt;

&lt;p&gt;“&lt;em&gt;One view of a Component is: you take a piece of code, ripped out something, and this is a Component.
Let me tell you: No, it is not a Component.
It’s like, if you rip a piece of meat out of my leg. It’s not a functioning component, it’s a pound of flesh.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A &lt;strong&gt;Component&lt;/strong&gt; is something which solves a problem in a general way. It is something which is not specific, and then could be used by all the applications which needs this particular problem being solved.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A programming language is suitable for &lt;strong&gt;Component Programming&lt;/strong&gt; if it satisfies the following conditions:&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;em&gt;It could describe a general purpose components&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Without losing efficiency&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;The second condition is very, very important, because, obviously in any language you could construct anything. As long as your language is Turing-complete you could describe just about anything… but might be very, very slow.
There are many languages which claim to possess powerful abstraction facilities. But if you start using these facilities everywhere, for example:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;From now on in my language I no longer use int32 I just going to use Integer&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Guess what?
If you start doing it your performance is going to collapse.
It’s not just going to be slow, you are going to be slow compared with stuff written this language without using abstractions.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;That is, efficiency is a two-fold efficiency:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;A component is &lt;strong&gt;relatively efficient&lt;/strong&gt; is when instantiated it’s as fast as a code (non-generic / hand-written) written in the same programming language.&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;A component is &lt;strong&gt;absolutely efficient&lt;/strong&gt; is when instantiated it’s as efficient as anything that could be done on a given machine, basically, as fast as assembly language.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;So I want a language that allows me &lt;strong&gt;Generality&lt;/strong&gt; and &lt;strong&gt;Absolute Efficiency&lt;/strong&gt;.
That is, I can program as general as a like, that is I could talk about things like Monoids and Semigroups. And on the other hand, when the code compiles I could look at assembly code and say ‘this is good!’&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Now, we get to an interesting question.
How do we know that a language is powerful enough to do that?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Long time ago I came up with a test whether your language is good enough.
And you could come up with the following thing ‘If you could implement a major operating system then the language is good enough’.
It is a hard test, you obviously don’t know whether you implemented in a general way or not.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;So I came with a very, very simple test, and I claim this test which allows me (I still use it) to determine whether a language is suitable for what I want to do or not.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;There are three programs which I need to implement in a general way to know that a language is suitable. These three programs are:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;&lt;strong&gt;swap&lt;/strong&gt;: the program which takes two things, and swaps.&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;&lt;strong&gt;min&lt;/strong&gt;: the program which takes two things, and figures out which one is smaller.&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;&lt;strong&gt;linear search&lt;/strong&gt;: the program that goes through bunch of stuff and finds whatever.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;You could say ‘Well Alex, are these too simple?’
Well, if we cannot do simple things, it is very unlikely that we will be able to do hard things.
I want to see solutions to simple problems.
People always thing that exiting thing has to be very complicated.
I claim exiting things tend to be very simple and basic.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;So, every time you want to come to me and say ‘Alex why don’t we use a new language’.
My answer is: ‘Three programs. Go, try implementing them in your favorite language. When you do them in a &lt;strong&gt;general&lt;/strong&gt; way, and at least &lt;strong&gt;relatively efficient&lt;/strong&gt;, then, let us talk’.&lt;/em&gt;”&lt;/p&gt;

&lt;p&gt;Well, now we know what a &lt;strong&gt;component&lt;/strong&gt; is and how to test if a programming language is suitable for &lt;strong&gt;Components Programming&lt;/strong&gt;.
We will begin the test in the upcoming articles.&lt;/p&gt;

&lt;p&gt;Stay tuned.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://youtu.be/4pSqzrbjq4Q&quot;&gt;[1] Efficient Programming with Components: Lecture 4, Part 1&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//components-programming/&quot;&gt;Components Programming&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on October 28, 2014.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Writing min function, part 5: Stabilizing the algorithm]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//writing-min-function-part5/" />
  <id>https://componentsprogramming.com//writing-min-function-part5</id>
  <published>2014-10-05T09:00:00-03:00</published>
  <updated>2014-10-05T09:00:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;This is the fifth article of the series called &lt;em&gt;“Writing min function”&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Finally I will address the last pending issue.&lt;br /&gt;
To see the importance of the point I want to show to you, we have to write another function of our API, the &lt;em&gt;max function&lt;/em&gt;.&lt;br /&gt;
This issue is not related to C++, you could be see it in the implementation of the &lt;em&gt;min&lt;/em&gt;/&lt;em&gt;max&lt;/em&gt; functions of any programming language. Because of that, I want to go back to pseudo-code.&lt;br /&gt;
So, let’s take &lt;em&gt;min&lt;/em&gt; written in pseudo-code and adapt it to write &lt;em&gt;max&lt;/em&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// Requires:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//       a and b are of the same type, here called T,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//  and, T models TotallyOrdered&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Requires:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//       a and b are of the same type, here called T,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//  and, cmp models StrictWeakOrdering on T&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now, it easy to write the &lt;em&gt;max&lt;/em&gt; function:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// Requires:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//       a and b are of the same type, here called T,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//  and, T models TotallyOrdered&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Requires:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//       a and b are of the same type, here called T,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//  and, cmp models StrictWeakOrdering on T&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;em&gt;Min&lt;/em&gt; and &lt;em&gt;max&lt;/em&gt; are almost the same, the only difference resides at the two return clauses.&lt;/p&gt;

&lt;p&gt;Now, to see the problem, let’s write some usage code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;n&quot;&gt;usage_with_employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5000.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Peter&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;6000.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;e3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;George&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4500.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;e4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Frank&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5000.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;min_e1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;salary_comparator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;max_e1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;salary_comparator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;min_e2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;salary_comparator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;max_e2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;salary_comparator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//#1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//#2&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_e2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//#3&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_e2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//#4&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;According to the code above, what will be printed by the lines marked as #1, #2, #3 and #4?&lt;/p&gt;

&lt;p&gt;Let’s analyze it:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;min_e1 is the employee with lower salary between e1 and e2. Because 5000 (John salary) is less than 6000 (Peter salary) then e1 is the &lt;em&gt;min&lt;/em&gt; employee, so in #1 “John” will be printed. Trivial case.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;max_e1 is the employee with higher salary between e1 and e2. Doing a similar analysis, in #2 “Peter” will be printed. Another trivial case.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;min_e2 is the employee with lower salary between e1 and e4. But, in this case both salaries are equal, so, what will be printed?
Well, according to the implementation of our &lt;em&gt;min&lt;/em&gt; function, in case of equality (or equivalence) the second return clause will executed, so, in this case “Frank” will be printed for #3.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;max_e2 is the employee with higher salary between e1 and e4. Doing a similar analysis, in #2 “John” will be printed.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s focus on the last two cases, where we have the equal or equivalent objects.
Are our &lt;em&gt;min&lt;/em&gt; and &lt;em&gt;max&lt;/em&gt; functions correct?&lt;br /&gt;
What should they return in that case?&lt;/p&gt;

&lt;p&gt;You might wonder:&lt;br /&gt;
“Why worry about returning one or another object if both are equal?”&lt;br /&gt;
In mathematics, this question makes sense, because for example, in the set of Natural numbers, 14 is equal to 14, indeed they are &lt;strong&gt;identical&lt;/strong&gt;, so in:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;returning the first 14 or the second 14 does not matter, because they are the same number.&lt;/p&gt;

&lt;p&gt;But, in computer science we deal with things that reside in memory (objects). In a computer I can represent, for example, the chair where I’m sitting now. My chair maybe &lt;strong&gt;is equal to&lt;/strong&gt; the chair next to me, but for sure they are not the same chair, they are not &lt;strong&gt;identical&lt;/strong&gt;.&lt;br /&gt;
So, in cases like this, it is important to distinguish exactly what to return.&lt;/p&gt;

&lt;p&gt;Let’s look at what options we have for returning in case of equality of the objects:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;em&gt;min&lt;/em&gt; returns a and &lt;em&gt;max&lt;/em&gt; returns a&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;min&lt;/em&gt; returns a and &lt;em&gt;max&lt;/em&gt; returns b&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;min&lt;/em&gt; returns b and &lt;em&gt;max&lt;/em&gt; returns a&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;min&lt;/em&gt; returns b and &lt;em&gt;max&lt;/em&gt; returns b&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Our &lt;em&gt;min/max&lt;/em&gt; functions fall in the case number 3. But… why? Have we consciously designed in that way?&lt;br /&gt;
Which of these options is more correct?&lt;/p&gt;

&lt;p&gt;The 1st case looks good, both functions returns the same object in case of equality, the left-hand object. Sounds consistent.&lt;br /&gt;
The 4th function too, both functions returns the same object, but… they are returning the right-hand object… hmm, at least to me, it seems a bit odd.&lt;br /&gt;
The 3rd case sounds more illogical still.&lt;/p&gt;

&lt;p&gt;But, what about the case 2?&lt;/p&gt;

&lt;p&gt;The answer here lies in the concept of &lt;strong&gt;stability&lt;/strong&gt;.&lt;br /&gt;
Surely you know the property of &lt;em&gt;stability&lt;/em&gt; from algorithms such as &lt;em&gt;sort&lt;/em&gt;.&lt;br /&gt;
An &lt;em&gt;stable algorithm&lt;/em&gt; preserves the relative order of equivalent objects.&lt;/p&gt;

&lt;p&gt;But, what have to do &lt;em&gt;stability&lt;/em&gt; and sorting with &lt;em&gt;min&lt;/em&gt; and &lt;em&gt;max&lt;/em&gt;?&lt;br /&gt;
&lt;em&gt;“To see it, let us implement another function that could be done with two objects and a strict weak ordering on them, namely, a sorting function”&lt;/em&gt; &lt;a href=&quot;#Ref1&quot;&gt;[1]&lt;/a&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// Requires:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//       a and b are of the same type, here called T,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//  and, T models TotallyOrdered&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sort_2_wrong&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//Note: ref if used for passing by reference&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//Note: the swap function exchanges the values a and b.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;em&gt;sort_2_wrong&lt;/em&gt; has two problems:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;it does more work than necessary, because equivalent objects are swapped&lt;/li&gt;
  &lt;li&gt;it is not &lt;em&gt;stable&lt;/em&gt;, because the relative order of equivalent objects is not preserved.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;“Stability is an important property, and we should not abandon it without necessity. As a matter of fact, it is trivial to fix the problem by performing the swap only when the second object is strictly less than the first”&lt;/em&gt; &lt;a href=&quot;#Ref1&quot;&gt;[1]&lt;/a&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// Requires:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//       a and b are of the same type, here called T,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//  and, T models TotallyOrdered&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sort_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now, we can see that there should be a relationship between &lt;em&gt;min&lt;/em&gt;, &lt;em&gt;max&lt;/em&gt; and &lt;em&gt;sort_2&lt;/em&gt;: after we sort two elements, the first one should be the &lt;em&gt;minimum&lt;/em&gt; and the second one should be the &lt;em&gt;maximum&lt;/em&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// Type Requirement:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//            a and b are of the same type, here called T,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//       and, T models TotallyOrdered&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Postcondition: &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//       a = min(a, b) and b = max(a, b)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sort_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In the code above, note the specification of the postcondition written as a comment:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;But this postcondition never holds. Our &lt;em&gt;min&lt;/em&gt; function returns the second object and our &lt;em&gt;max&lt;/em&gt; function returns the first one when both objects are equivalent.&lt;br /&gt;
We need to make &lt;em&gt;min&lt;/em&gt; and &lt;em&gt;max&lt;/em&gt; &lt;em&gt;stable&lt;/em&gt; &lt;a href=&quot;#Ref2&quot;&gt;[2]&lt;/a&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// Requires:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//       a and b are of the same type, here called T,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//  and, T models TotallyOrdered&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Requires:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//       a and b are of the same type, here called T,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//  and, cmp models StrictWeakOrdering on T&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Requires:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//       a and b are of the same type, here called T,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//  and, T models TotallyOrdered&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Requires:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//       a and b are of the same type, here called T,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//  and, cmp models StrictWeakOrdering on T&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;You could argue, “&lt;em&gt;Stability&lt;/em&gt; is not a property of the &lt;em&gt;min&lt;/em&gt; (or &lt;em&gt;max&lt;/em&gt;) function, according to its definition. So, if we add &lt;em&gt;stability&lt;/em&gt; to &lt;em&gt;min&lt;/em&gt;, it will become something else, other than &lt;em&gt;min&lt;/em&gt;”&lt;/p&gt;

&lt;p&gt;Thoughts like this are wrong.&lt;br /&gt;
The first thing you have to think is: What is the definition of &lt;em&gt;min&lt;/em&gt;?&lt;br /&gt;
Remember, this is Computer Science, we deal non-identical things.&lt;/p&gt;

&lt;p&gt;Think!. What are our options?&lt;br /&gt;
There are three ways a library can implement &lt;em&gt;min&lt;/em&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Random&lt;/li&gt;
  &lt;li&gt;Anti-Stable&lt;/li&gt;
  &lt;li&gt;Stable&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;em&gt;random&lt;/em&gt; way is obviously crazy and expensive.&lt;br /&gt;
The &lt;em&gt;stable&lt;/em&gt; way is more useful and as efficient as the &lt;em&gt;anti-stable&lt;/em&gt; one.&lt;/p&gt;

&lt;p&gt;So, what would you choose?&lt;/p&gt;

&lt;p&gt;Whatever your decision, you must &lt;strong&gt;specify&lt;/strong&gt; (document) in your library/API the behavior of &lt;em&gt;min&lt;/em&gt; (and &lt;em&gt;max&lt;/em&gt;) in case of equality (or equivalence) of its arguments.&lt;br /&gt;
Do not make me guess or have to look at the implementation details of your API to discover the behavior!&lt;br /&gt;
This would be wrong, for sure.&lt;/p&gt;

&lt;p&gt;We will see in the following articles some languages/libraries whose specification ​​is definitely wrong.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;: Make &lt;em&gt;min&lt;/em&gt; and &lt;em&gt;max&lt;/em&gt; functions &lt;strong&gt;stable&lt;/strong&gt;, and so &lt;strong&gt;consistent&lt;/strong&gt; with the other functions of your API.&lt;br /&gt;
&lt;strong&gt;Specify&lt;/strong&gt; their behavior in the library documentation.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;the-series&quot;&gt;The Series&lt;/h2&gt;

&lt;p&gt;   &lt;a href=&quot;/writing-min-function-part1/&quot;&gt;Part 1: The rise of Concepts&lt;/a&gt;&lt;br /&gt;
   &lt;a href=&quot;/writing-min-function-part2/&quot;&gt;Part 2: Understanding Concepts&lt;/a&gt;&lt;br /&gt;
   &lt;a href=&quot;/writing-min-function-part3/&quot;&gt;Part 3: Weakening the ordering&lt;/a&gt;&lt;br /&gt;
   &lt;a href=&quot;/writing-min-function-part4/&quot;&gt;Part 4: Const-Correctness&lt;/a&gt;&lt;br /&gt;
   &lt;a href=&quot;/writing-min-function-part5/&quot;&gt;Part 5: Stabilizing the algorithm&lt;/a&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;p&gt;&lt;a name=&quot;Ref1&quot;&gt;[1]&lt;/a&gt; Notes on Programming by Alexander Stepanov [2007, Pages 61-62]&lt;br /&gt;
&lt;a name=&quot;Ref2&quot;&gt;[2]&lt;/a&gt; Note the use of the &lt;a href=&quot;http://en.wikipedia.org/wiki/%3F:&quot;&gt;Conditional operator&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//writing-min-function-part5/&quot;&gt;Writing min function, part 5: Stabilizing the algorithm&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on October 05, 2014.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Writing min function, part 4: Const-Correctness]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//writing-min-function-part4/" />
  <id>https://componentsprogramming.com//writing-min-function-part4</id>
  <published>2014-09-24T09:10:00-03:00</published>
  <updated>2014-09-24T09:10:00-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;This is the fourth article of the series called &lt;em&gt;“Writing min function”&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I still have to solve two mistakes made in the code of the previous posts. One of them is C++ specific, and the other it is a mistake that could be made in any programming language.&lt;/p&gt;

&lt;p&gt;Originally, I wanted to address the two mistakes in the same article, but when I started writing about the first one, the article became longer than I wanted, so I will address each mistake in separate posts.&lt;/p&gt;

&lt;p&gt;Therefore, this article will deal with an issue that only concerns to C++ programmers, so if C++ is not of your interest, feel free to jump directly to the fifth article.&lt;/p&gt;

&lt;p&gt;In general, I want that the algorithms written in my articles are not tied to a specific programming language, but in order to write the algorithms in a practical way, I have to do it in a real language, and C++ is my choice.
Once the mistakes have been corrected, with the help of other people, I’ll start writing the min function in other programming languages.&lt;/p&gt;

&lt;p&gt;So, to start, I’ll update the usage code example to reflect things I would like to do using the min function:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;raise_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;salary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The &lt;em&gt;raise_salary&lt;/em&gt; function is very simple, takes a reference (non-const) to an employee and increases her salary by &lt;em&gt;increment&lt;/em&gt;. I would like to use it in the following way:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;usage_with_mutable_objects&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5&apos;000.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;George&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4&apos;500.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;raise_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;salary_comparator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{}),&lt;/span&gt;
        &lt;span class=&quot;mf&quot;&gt;500.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//#1 Compile-time error&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;(&lt;em&gt;employee&lt;/em&gt; and &lt;em&gt;salary_comparator&lt;/em&gt; were defined in &lt;a href=&quot;/writing-min-function-part3/&quot;&gt;Part 3&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;In #1 we get a compile-time error, but, why?
Remember our last version of the min function:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotallyOrdered&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StrictWeakOrdering&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;requires&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SameType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ArgumentType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cmp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cmp&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;(&lt;strong&gt;Note&lt;/strong&gt;: When I refer to the min function, actually I am not referring to a single function, but to a family of functions)&lt;/p&gt;

&lt;p&gt;It takes two constant-references to two objects (and a comparator); and returns a constant reference to one of the objects. That means that the returned reference cannot be modified, because it is returned as const.
The &lt;em&gt;raise_salary&lt;/em&gt; function tries to modify the object returned by the min function, causing the compilation-time error, because you can’t modify something declared as const.&lt;/p&gt;

&lt;p&gt;If I manually expand (or “inliniarize”) the code of the functions min and &lt;em&gt;raise_salary&lt;/em&gt; I would get something like:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// manually-inliniarized code&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5&apos;000.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;George&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4&apos;500.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;salary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;500.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//Compile-time error&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In the last code, it is more clear that we are trying to modify something that is constant.
The variable m is a constant-reference pointing (or rather, referencing) to e1 or to e2.
Both e1 and e2 were not declared as constant, so they are mutable objects.
Then,  why the min function returns a constant reference and not just an ordinary (non-const) reference?
Consider the following variation of a wrong min function:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;//Obs: It is not valid C++ code (ill-formed code)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StrictWeakOrdering&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;requires&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SameType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ArgumentType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cmp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrong_min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cmp&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;It is not valid C++ code (or using Standard C++ terminology, it is ill-formed code), it doesn’t compiles, because we are trying to return something that is constant as non-constant. And what’s up with that?
Well, suppose that the &lt;em&gt;wrong_min&lt;/em&gt; function compiles, in a fictitious programming language, then consider the following usage code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;usage_wrong_min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5&apos;000.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;George&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4&apos;500.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The two employees are declared as const objects, meaning that they are immutable objects, so, they cannot be modified. Then consider the folliwing usage of the &lt;em&gt;wrong_min&lt;/em&gt; function.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;//...&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrong_min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;salary_comparator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{});&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Because, &lt;em&gt;wrong_min&lt;/em&gt; function returns a non-const reference, we can assign the result of the function to a non-const reference, and then we could modify the object referenced by m:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;//...&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;salary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;500.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//#2&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Remember that the &lt;em&gt;wrong_min&lt;/em&gt; function is not valid, but, if it were valid, we would be in trouble, since we would be modifying an object that can not be modified, because it is constant or immutable.&lt;/p&gt;

&lt;p&gt;You might wonder: “But… they are objects, so they reside in memory, and memory can be modified,… what is the problem with that? ”&lt;/p&gt;

&lt;p&gt;According to the &lt;a href=&quot;#Ref1&quot;&gt;&lt;strong&gt;C++ Standard&lt;/strong&gt; [1]&lt;/a&gt;, &lt;em&gt;“…any attempt to modify a const object during its lifetime (3.8) results in undefined behavior.”&lt;/em&gt;
In practice, a const object could be placed by the compiler (and the operating system) in a read-only segment of memory. Any attempt to write to read-only memory causes a segmentation fault, probably causing an abnormal termination of the process (program crash).&lt;/p&gt;

&lt;p&gt;That is the reason why C++ protects us from writing things like &lt;em&gt;wrong_min&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Returning to the min function, then, why not remove const from everywhere?
Let’s see:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotallyOrdered&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;If we wrote the min function as above, then we would have problems with the following code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Compile-time error&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The code above doesn’t compile. Why?
In C++, the values 5 and 7 are called integer-literal, more specifically they are called decimal integer literal (base ten). Literals are something that can’t be modified, they are constants. So to accept the code above we have to return to our original min function, using const everywhere.
But, remember, we want our min function also allows us to work with mutable objects.&lt;/p&gt;

&lt;p&gt;So, we want a function that:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Returns a constant-reference if any of its parameters is a constant object, or,&lt;/li&gt;
  &lt;li&gt;Returns a non-constant-reference if both parameters are non-constant objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The simplest way to do it in C++ is a little verbose, but it works:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotallyOrdered&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotallyOrdered&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We have to repeat the code, the first function covers the case 1 and the second one covers the case 2. Maybe in a future version of C++ this can be done without the need for repetition of code.&lt;/p&gt;

&lt;p&gt;So I’m going to write the full family of min functions we have so far:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StrictWeakOrdering&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;requires&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SameType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ArgumentType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cmp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cmp&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StrictWeakOrdering&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;requires&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SameType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ArgumentType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cmp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cmp&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotallyOrdered&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotallyOrdered&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Remember that in the following article I will address the last outstanding issue. So do not take the above code as totally definitive.&lt;/p&gt;

&lt;p&gt;And, with the code above, we can write things like the following:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5&apos;000.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Peter&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;6&apos;000.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e3&lt;/span&gt;       &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;George&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4&apos;500.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e4&lt;/span&gt;       &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Frank&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5&apos;000.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;salary_comparator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{});&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;salary_comparator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{});&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;salary_comparator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{});&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;salary_comparator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{}).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;salary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;500.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//or&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;raise_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;salary_comparator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{}),&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;500.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Finally, I wrote another version of the min function that looks simpler because it prevents writing the same code twice, but it uses some template-metaprogramming (TMP) hacks.&lt;/p&gt;

&lt;p&gt;The code looks like the following:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotallyOrdered&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TotallyOrdered&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;requires&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SameType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;HERE_SOME_MAGIC&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HERE_SOME_MAGIC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HERE_SOME_MAGIC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then, I presented the code to Alex, because I wanted to know what he thought, and he answered me with the following advice:&lt;/p&gt;

&lt;p&gt;- Alex Stepanov: &lt;em&gt;“My standard advice: concentrate on algorithms and data structures. Do not spend time on meta-programming. If it is not in Knuth (or something like Knuth), do not do it.”&lt;/em&gt; &lt;a href=&quot;#Ref2&quot;&gt;[2]&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At first I did not understand the reason for his answer, but then, after spending hours focusing on details of the template-metaprogramming code, I understood that it is better to focus on real problems, which are the algorithms.&lt;/p&gt;

&lt;p&gt;Even so, if you want to take a look at the code, write me by private and I will gladly share it (See &lt;a href=&quot;https://componentsprogramming.com/about/&quot;&gt;About page&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;I didn’t want to make an article explaining Const-Correctness so long, because there are countless &lt;a href=&quot;#Ref3&quot;&gt;books and articles on the Internet [3]&lt;/a&gt; that explain the topic better than me. But as I tried to explain some things, I thought it was necessary to extend the explanation, which ended in this article.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;the-series&quot;&gt;The Series&lt;/h2&gt;

&lt;p&gt;   &lt;a href=&quot;/writing-min-function-part1/&quot;&gt;Part 1: The rise of Concepts&lt;/a&gt;
   &lt;a href=&quot;/writing-min-function-part2/&quot;&gt;Part 2: Understanding Concepts&lt;/a&gt;
   &lt;a href=&quot;/writing-min-function-part3/&quot;&gt;Part 3: Weakening the ordering&lt;/a&gt;
   &lt;a href=&quot;/writing-min-function-part4/&quot;&gt;Part 4: Const-Correctness&lt;/a&gt;
   &lt;a href=&quot;/writing-min-function-part5/&quot;&gt;Part 5: Stabilizing the algorithm&lt;/a&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;p&gt;&lt;a name=&quot;Ref1&quot;&gt;[1]&lt;/a&gt; Latest publicly available C++ Draft Standard (n3797) (at September 2014) [dcl.type.cv] paragraph 4:
&lt;a href=&quot;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf&quot;&gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref2&quot;&gt;[2]&lt;/a&gt; Here Alex refers to &lt;a href=&quot;http://en.wikipedia.org/wiki/Donald_Knuth&quot;&gt;Donald Knuth&lt;/a&gt;, but more specifically to his series of books, called &lt;a href=&quot;http://en.wikipedia.org/wiki/The_Art_of_Computer_Programming&quot;&gt;“The Art of Computer Programming”&lt;/a&gt; (&lt;a href=&quot;http://www.amazon.com/Computer-Programming-Volumes-1-4A-Boxed/dp/0321751043&quot;&gt;Amazon&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;Ref3&quot;&gt;[3]&lt;/a&gt; For better references about Const-Correctness and ROM-ability, see:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Standard C++ FAQ about Const Correctness:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href=&quot;https://isocpp.org/wiki/faq/const-correctness&quot;&gt;https://isocpp.org/wiki/faq/const-correctness&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The “Guru of the Week” (GotW) articles about Const-Correctness (the original and the new one), from &lt;a href=&quot;http://en.wikipedia.org/wiki/Herb_Sutter&quot;&gt;Herb Sutter&lt;/a&gt;:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href=&quot;http://www.gotw.ca/gotw/006.htm&quot;&gt;http://www.gotw.ca/gotw/006.htm&lt;/a&gt;
&lt;a href=&quot;http://herbsutter.com/2013/05/24/gotw-6a-const-correctness-part-1-3/&quot;&gt;http://herbsutter.com/2013/05/24/gotw-6a-const-correctness-part-1-3/&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Technical Report on C++ Performance, Chapter 7.1, “ROMability”:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href=&quot;http://www.open-std.org/jtc1/sc22/wg21/docs/TR18015.pdf&quot;&gt;http://www.open-std.org/jtc1/sc22/wg21/docs/TR18015.pdf&lt;/a&gt;&lt;/p&gt;


  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//writing-min-function-part4/&quot;&gt;Writing min function, part 4: Const-Correctness&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on September 24, 2014.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Writing min function, part 3: Weakening the ordering]]></title>
  <link rel="alternate" type="text/html" href="https://componentsprogramming.com//writing-min-function-part3/" />
  <id>https://componentsprogramming.com//writing-min-function-part3</id>
  <published>2014-07-15T05:39:29-03:00</published>
  <updated>2014-07-15T05:39:29-03:00</updated>
  <author>
    <name>Fernando Pelliccioni</name>
    <uri>https://componentsprogramming.com/</uri>
    <email>fpelliccioni@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;This is the third article of the series called &lt;em&gt;“Writing min function”&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now we understand what &lt;strong&gt;Concepts&lt;/strong&gt; are (do we?), I will try to complete the min function.&lt;/p&gt;

&lt;p&gt;What do I mean with “complete”?&lt;/p&gt;

&lt;p&gt;Well, first we need to see how to use our min function, for that purpose I want to use a real programming language.&lt;br /&gt;
So in this article I will write code in &lt;a href=&quot;http://www.open-std.org/jtc1/sc22/wg21/&quot;&gt;C++&lt;/a&gt;. Why C++?, it is a topic for another article.&lt;br /&gt;
But, don’t worry if you’re not a C++ programmer, the code will be easy to understand, and, the ideas that I want to talk here are very important for programming algorithms, beyond the programming language.&lt;br /&gt;
Later on, I will try to write the same code (and analyze it) using another programming languages.&lt;/p&gt;

&lt;p&gt;This is the C++ equivalent of the min function code from &lt;a href=&quot;/writing-min-function-part2/&quot;&gt;Part 2&lt;/a&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;//Note 1: this code is Concept-enabled C++.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//Note 2: Still has errors!&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotallyOrdered&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;(We will see how to &lt;strong&gt;define&lt;/strong&gt; the &lt;em&gt;TotallyOrdered&lt;/em&gt; concept in C++ later. For now, think in the mathematical definition presented &lt;a href=&quot;/writing-min-function-part1/&quot;&gt;before&lt;/a&gt;  )&lt;/p&gt;

&lt;p&gt;And we use it in this way:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;usage_with_builtin_types_simple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;34&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;//using variables&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//print the result on standard output&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;//using integer-literals (base10)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;//using variables and literals&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;//assigning the result (copying the result)&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;These are very simple examples using the int builtin type, but our min function is supposed to be &lt;strong&gt;Generic&lt;/strong&gt;, so it has to operate with all types that satisfy the &lt;em&gt;TotallyOrdered&lt;/em&gt; concept. So let’s see a little more complicated example.&lt;/p&gt;

&lt;p&gt;Suppose we maintain the employee database of a company and we want to take two employees and know which is the minimum of the two:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;//A simplified Employee class written in C++&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And now we use the min function with Employees:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;usage_with_employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5&apos;000.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Peter&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;6&apos;000.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e3&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;George&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4&apos;500.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Frank&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5&apos;000.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// #1&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;What happend at #1?&lt;/p&gt;

&lt;p&gt;Well, we should get a compile-time error saying that the employee type doesn’t satisfy the &lt;em&gt;TotallyOrdered&lt;/em&gt; concept.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;First, in C++, there is no way of comparing two Employees using the &lt;em&gt;less-than-operator&lt;/em&gt; required by the &lt;em&gt;TotallyOrdered&lt;/em&gt; concept.&lt;br /&gt;
If we use old-C++, without &lt;em&gt;Concepts&lt;/em&gt; (or duck-typing templates) we will get a compile-time error pointing to the min function, saying that a &amp;lt; b could not be done.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c--&quot; data-lang=&quot;c++&quot;&gt;&lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cpp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;In&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instantiation&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cpp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;required&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;here&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cpp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;no&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;operand&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;types&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;are&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
         &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The error message is not very instructive, right? The error message points to the min function, when the real problem is in the use of it, specifically in line marked as #1 in the &lt;em&gt;usage_with_employees()&lt;/em&gt; function.&lt;/p&gt;

&lt;p&gt;If we use a dynamic duck-typed programming language (like Python or Javascript) we will get a similar error but at runtime.&lt;/p&gt;

&lt;p&gt;The compiler (or interpreter) doesn’t know how to do a &amp;lt; b for Employees, so this is the reason why we get the error.&lt;br /&gt;
So, how to make Employee to satisfy the &lt;em&gt;TotallyOrdered&lt;/em&gt; concept?
Let’s start satisfying the requirements imposed by the concept:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Employee must satisfy the &lt;em&gt;Regular&lt;/em&gt; concept.&lt;/li&gt;
  &lt;li&gt;We have to provide an operator&amp;lt; with the signature:&lt;br /&gt;
Employee x Employee -&amp;gt; bool&lt;/li&gt;
  &lt;li&gt;The operator&amp;lt; must be a &lt;em&gt;total ordering&lt;/em&gt; relation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For now I want to skip the points 1 and 3, we will see them later. Let’s focus on point 2 (remember, it is a syntactic requirement), we have to give the compiler a way to compare two Employees.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;???????&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This is the canonical C++ way for implementing a &lt;em&gt;less-than-operator&lt;/em&gt;, but … What should I put on line 2?&lt;/p&gt;

&lt;p&gt;This should be answered by the designer of the Employee class. Well…, that’s me (?).&lt;/p&gt;

&lt;p&gt;Remember that &lt;em&gt;total ordering&lt;/em&gt; is, roughly speaking, some kind of &lt;em&gt;Natural Ordering&lt;/em&gt;. So we need to know what is the &lt;em&gt;natural ordering&lt;/em&gt; of Employees.
Maybe the &lt;em&gt;natural ordering&lt;/em&gt; of employees is by name, maybe by salary, … I don’t know. This depends on the domain of the application. In a company, I think, employees may have a unique identification number, which seems to be a good candidate for implement &lt;em&gt;total ordering&lt;/em&gt;. So, let’s modify our Employee class:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now, let’s finalize our &lt;em&gt;less-than-operator&lt;/em&gt; using &lt;em&gt;id&lt;/em&gt; as the &lt;em&gt;natural ordering&lt;/em&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now, we have an Employee class with a &lt;em&gt;natural ordering&lt;/em&gt; (by id) that satisfies the &lt;em&gt;TotallyOrdered&lt;/em&gt; concept (remember, we are ignoring the points 1 and 3).&lt;/p&gt;

&lt;p&gt;But, what if we want to know who is the lowest paid employee, and then raise his salary. Should we modify the &lt;em&gt;less-than-operator&lt;/em&gt; to compare by salary?&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;salary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Is it OK?
No, we are imposing an &lt;strong&gt;unnatural&lt;/strong&gt; ordering to employees by default, it is not the right way to do it.&lt;/p&gt;

&lt;p&gt;Changing the Employee’s &lt;em&gt;natural ordering&lt;/em&gt; is not an option, so, we need another way of selecting the minimum employee using an &lt;em&gt;unnatural ordering&lt;/em&gt; relation.&lt;br /&gt;
What we need is another min function, one that takes a relation as parameter.&lt;br /&gt;
Let’s do it using the old-C++ way (without Concepts):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;//Note: It compiles, but is incorrect, still, be patient!&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now we can write code like the following:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;salary_comparator&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                  &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;salary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;usage_with_employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5&apos;000.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Peter&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;6&apos;000.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e3&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;George&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4&apos;500.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Frank&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5&apos;000.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// using natural employee ordering (by id)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// using another (unnatural) ordering&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;salary_comparator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{});&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// using another (unnatural) ordering, with lambdas   &lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                               &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;But so far, I have not mentioned anything that an experienced programmer does not know, the use of &lt;em&gt;predicates&lt;/em&gt; (the cmp comparator) is a common thing in practically all programming languages.
What most programmers (and most of the APIs provided by programming languages) forget is to specify the semantics requirements of the &lt;em&gt;predicate&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So, what are the semantic requirements?
We need to answer: What our &lt;em&gt;comparator&lt;/em&gt; (cmp) is?&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;comparator&lt;/em&gt; is a &lt;em&gt;Relation&lt;/em&gt;, that is, a &lt;em&gt;binary Predicate&lt;/em&gt;. What kind of &lt;em&gt;relation&lt;/em&gt;?
It is an &lt;em&gt;Ordering&lt;/em&gt;. What kind of &lt;em&gt;ordering&lt;/em&gt;?
Is it a &lt;em&gt;Total Ordering relation&lt;/em&gt;? Well, let’s check it:&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Remember, a &lt;em&gt;Relation&lt;/em&gt; r is a &lt;em&gt;Strict Total Ordering&lt;/em&gt; if:&lt;br /&gt;
For all a, b and c in the domain of r, the following must hold:&lt;br /&gt;
      &lt;em&gt;Transitivity&lt;/em&gt;: if r(a, b) and r(b, c) then r(a, c)&lt;br /&gt;
      &lt;em&gt;Trichotomy&lt;/em&gt;: only one of the following holds, r(a, b), r(b, a) or a = b&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;It’s easy to prove that the &lt;em&gt;transitivity&lt;/em&gt; axiom holds, let’s leave it as an exercise for the reader.&lt;br /&gt;
What about &lt;em&gt;trichotomy&lt;/em&gt;? Let’s prove it with an example.&lt;br /&gt;
Given our previous defined employees:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5&apos;000.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Peter&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;6&apos;000.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e3&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;George&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4&apos;500.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Frank&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5&apos;000.0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And our salary_comparator, here called r in order to abbreviate the text:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Take e1 and e2: According to &lt;em&gt;trichotomy&lt;/em&gt;: only one of the following holds, r(e1, e2), r(e2, e1) or e1 = e2&lt;br /&gt;
What is the result of r(e1, e2)? r(e1, e2) means e1.salary &amp;lt; e2.salary, which means: 5000 &amp;lt; 6000, so it holds. The other two propositions are false (intentionally omitted), so &lt;em&gt;trichotomy&lt;/em&gt; holds in that case.&lt;/li&gt;
  &lt;li&gt;Take e1 and e3: Following the same analysis,  r(e1, e3) means e1.salary &amp;lt; e3.salary, which means: 5000 &amp;lt; 4500, so it doesn’t hold. But r(e3, e1) means … 4500 &amp;lt; 5000 which is true, and the last proposition is false (again, intentionally omitted), so &lt;em&gt;trichotomy&lt;/em&gt; holds.&lt;/li&gt;
  &lt;li&gt;Now, take e1 and e4: Following the same analysis, r(e1, e4) and r(e4, e1) are both false, so, the proposition e1 = e4 have to be true if we want &lt;em&gt;trichotomy&lt;/em&gt; holds.&lt;br /&gt;
Is e1 = e4 true?&lt;br /&gt;
No! because &lt;strong&gt;e1 is not equal to e4&lt;/strong&gt;, they are differents employees, they are not the same.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then, the &lt;em&gt;trichotomy&lt;/em&gt; axiom does not hold, that is, the salary_comparator relation is not a &lt;em&gt;Total Ordering&lt;/em&gt; on the Employee Set. This means that &lt;em&gt;Total Ordering&lt;/em&gt; is &lt;strong&gt;too restrictive&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, what kind of &lt;em&gt;ordering relation&lt;/em&gt; should be our &lt;em&gt;Comparator&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Partial Ordering&lt;/em&gt;?&lt;br /&gt;
No, we saw in &lt;a href=&quot;/writing-min-function-part1/&quot;&gt;Part 1&lt;/a&gt; that &lt;em&gt;Partial Ordering&lt;/em&gt; is &lt;strong&gt;too weak&lt;/strong&gt; to define min.
We need something between &lt;em&gt;Partial&lt;/em&gt; and &lt;em&gt;Total Ordering&lt;/em&gt;: what we need is called &lt;a href=&quot;#Ref1&quot;&gt;&lt;strong&gt;Weak Ordering&lt;/strong&gt;[1]&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Roughly speaking, &lt;em&gt;weak ordering&lt;/em&gt; says that if r(a, b) and r(b, a) are false, then, a and b are &lt;strong&gt;equivalents&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, let’s modify the min function to introduce &lt;em&gt;weak ordering&lt;/em&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot; data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;//Note: yes! you guess it, it is incorrect, still!&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StrictWeakOrdering&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;requires&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SameType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ArgumentType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cmp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cmp&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The code above means that we have a function called min, that takes two formal parameters, a and b, both of the same type, called T.&lt;br /&gt;
The funcion has a third formal parameter, cmp, that models the concept called &lt;strong&gt;StrictWeakOrdering&lt;/strong&gt;. The “requires” clause means that T (the type of a and b) and the argument type of the &lt;em&gt;Comparator&lt;/em&gt; (Cmp) must be the same.&lt;/p&gt;

&lt;p&gt;Well, in this article I explained what &lt;em&gt;Weak Ordering&lt;/em&gt; means and why it is important, I want to end it with a quote from Alex:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“Mathematicians are happy with Total and Partial ordering. But most of them don’t know what is Weak Ordering. It is not a common term in mathematics but it is essential in computer science, because when we want to order things, we want to order by something. For example by social security number, by name, by age”&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In the next article, finally, I will tell you what are the mistakes that remain to be addressed.&lt;/p&gt;

&lt;p&gt;You can get the complete source code on my &lt;a href=&quot;https://github.com/fpelliccioni/componentsprogramming&quot;&gt;Github repository&lt;/a&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;the-series&quot;&gt;The Series&lt;/h2&gt;

&lt;p&gt;   &lt;a href=&quot;/writing-min-function-part1/&quot;&gt;Part 1: The rise of Concepts&lt;/a&gt;&lt;br /&gt;
   &lt;a href=&quot;/writing-min-function-part2/&quot;&gt;Part 2: Understanding Concepts&lt;/a&gt;&lt;br /&gt;
   &lt;a href=&quot;/writing-min-function-part3/&quot;&gt;Part 3: Weakening the ordering&lt;/a&gt;&lt;br /&gt;
   &lt;a href=&quot;/writing-min-function-part4/&quot;&gt;Part 4: Const-Correctness&lt;/a&gt;&lt;br /&gt;
   &lt;a href=&quot;/writing-min-function-part5/&quot;&gt;Part 5: Stabilizing the algorithm&lt;/a&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;p&gt;&lt;a name=&quot;Ref1&quot;&gt;[1]&lt;/a&gt;  For a formal definition of &lt;em&gt;Weak Ordering&lt;/em&gt; see:&lt;br /&gt;
&lt;a href=&quot;http://www.elementsofprogramming.com/eop-concepts.pdf&quot;&gt;http://www.elementsofprogramming.com/eop-concepts.pdf&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;https://componentsprogramming.com//writing-min-function-part3/&quot;&gt;Writing min function, part 3: Weakening the ordering&lt;/a&gt; was originally published by Fernando Pelliccioni at &lt;a href=&quot;https://componentsprogramming.com/&quot;&gt;Components Programming&lt;/a&gt; on July 15, 2014.&lt;/p&gt;</content>
</entry>

</feed>