<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>dnsh.io</title>
    <description>One mans rant is another mans bant.</description>
    <link>http://dnsh.io/</link>
    <atom:link href="http://dnsh.io/feed.xml" rel="self" type="application/rss+xml" />
    
      <item>
        <title>Why Mass Surveillance Is Counter Productive</title>
        <description>&lt;p&gt;Last Thursday(17th Nov 2016), The &lt;a href=&quot;https://www.theguardian.com/world/2016/nov/19/extreme-surveillance-becomes-uk-law-with-barely-a-whimper&quot;&gt;UK Parliament approved a bill called Investigatory Powers Act&lt;/a&gt; that legalise a plethora of digital spying activities by government agencies. This bill is also known as “The Snoopers Charter” and grants following powers:&lt;/p&gt;

&lt;h2 id=&quot;equipment-interference-by-government-agencies-becomes-legal&quot;&gt;Equipment interference by government agencies becomes legal&lt;/h2&gt;
&lt;p&gt;Upon obtaining a warrant, government agencies may hack into computers, networks, mobile devices, servers or any other connected or offline device. This would allow government agencies to do things like:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;download data from a mobile phone at that is stolen on left unattended&lt;/li&gt;
  &lt;li&gt;install intrusive software that track every key pressed on a laptop.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While this is nothing new, it is the first time the practice has been made legal in a western democracy. Given that a warrant is required before any attacks, most people would be fine with this law. But…&lt;/p&gt;

&lt;h2 id=&quot;bulk-hacking-and-collecting-bulk-data-sets-also-becomes-legal&quot;&gt;Bulk hacking and collecting bulk data sets also becomes legal&lt;/h2&gt;
&lt;p&gt;This is where things start to get too intrusive. “Bulk hacking” grants government agencies to perform proximity attacks on networks and devices in bulk, and almost indiscriminately. For example, it would become legal for government agents to hack your company’s network if they think one of your colleagues is emailing ISIS. Not only that, it’s also legal for them to keep whatever data they might find about YOU even though you had nothing to do with it.&lt;/p&gt;

&lt;h2 id=&quot;ip-bill-and-web-logs&quot;&gt;IP Bill and web logs&lt;/h2&gt;
&lt;p&gt;Internet service providers are now obliged to store internet history data for all their customers for a minimum of 12 months. Internet history data logs will be made up of websites that you may have visited and devices that you have connected with. Government agencies are to be given full access to all of this data without any need for a warrant.&lt;/p&gt;

&lt;h2 id=&quot;ok-so-what-whats-wrong-with-all-this&quot;&gt;OK so what? What’s wrong with all this?&lt;/h2&gt;
&lt;p&gt;Let’s not worry about &lt;em&gt;equipment interference&lt;/em&gt; since this is quite similar to other breaking and entering laws that many think is fair. However, very few would find indiscriminate attacks on private data to be fair.&lt;/p&gt;

&lt;p&gt;But I’m not fussed about the fairness of this bill, actually. What I’m concerned about is how ineffective are these policies at doing what the government claim they do: &lt;strong&gt;counter-terrorism and provide security&lt;/strong&gt;. Before going further, let me introduce some security engineering jargon:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Policy&lt;/strong&gt; (or the objective goal)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mechanism&lt;/strong&gt; (ciphers, access controls, hardware tamper-resistance and other machinery that you assemble in order to implement the policy)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Assurance&lt;/strong&gt; (the amount of reliance you can place on each particular mechanism)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Incentive&lt;/strong&gt; (the motive for people to either guard or try and defeat the policy)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Snoopers Charter is a collection of &lt;strong&gt;&lt;em&gt;ineffective policies&lt;/em&gt;&lt;/strong&gt; backed by &lt;strong&gt;&lt;em&gt;weak mechanisms&lt;/em&gt;&lt;/strong&gt; brought forward to give the &lt;strong&gt;&lt;em&gt;illusion of assurance&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;These policies are ineffective mainly due to weak mechanisms. IP bill can be defeated quite easily &lt;a href=&quot;http://www.pcadvisor.co.uk/how-to/internet/how-use-vpn-set-up-vpn-for-private-browsing-3466190/&quot;&gt;by proxying through a VPN&lt;/a&gt;. Someone who has a vested interest in maintaining anonymity and communicating secretly can still do that quite easily. You don’t need to be a genius to learn how to use Tor browser or &lt;a href=&quot;https://en.wikipedia.org/wiki/Pretty_Good_Privacy&quot;&gt;PGP encryption&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So what’s actually happening is that criminals and terrorists can go about their business without having to change &lt;em&gt;anything&lt;/em&gt;. ISPs will have to spend millions of pounds to collect and store weblogs. Surely, this is a cost they will just pass on to their customers. Digital intrusion is a paid service, you see.&lt;/p&gt;

&lt;p&gt;But what is dangerous about this is that there will no doubt be a demand in the digital blackmarket for all this web-log data. Yahoo were the most recent company to come out clean &lt;a href=&quot;https://www.theguardian.com/technology/2016/sep/23/yahoo-questinos-hack-researchers&quot;&gt;about how successful they were at securing their customer data&lt;/a&gt;. When it comes to privacy, it’s very difficult to put the genie back in the bottle. So the fewer the data that is stored about you, the better your privacy.&lt;/p&gt;

&lt;p&gt;Since the 9/11 attacks the Transport Security Administration in USA has spent $14.7 billion on aggressive passenger screening, which is fairly ineffective since well below half of the weapons taken through screening (accidentally or for testing purposes) were picked up, while $100m spent on reinforcing cockpit doors would remove most of the risk.&lt;/p&gt;

</description>
        <pubDate>Sat, 19 Nov 2016 00:00:00 +0000</pubDate>
        <link>http://dnsh.io/security/2016/11/19/why-mass-surveillance-is-counter-productive/</link>
        <guid isPermaLink="true">http://dnsh.io/security/2016/11/19/why-mass-surveillance-is-counter-productive/</guid>
      </item>
    
      <item>
        <title>String concatenation in Rust</title>
        <description>&lt;p&gt;So I’ve been messing with &lt;a href=&quot;https://www.rust-lang.org&quot;&gt;Rust language&lt;/a&gt; for a couple of weeks now and I am thoroughly enjoying
it. Rust is a systems programming language developed by Mozilla foundation. It’s becoming popular with plenty of rave reviews
about seg-fault safety and the modern syntax. I highly recommend taking a look if you haven’t already.&lt;/p&gt;

&lt;p&gt;Rust is obsessed about memory from ground up. This is understandable given that avoiding seg-faults is one of its unique selling points. When you write Rust, you are contantly thinking about where things are in memory. Sometimes that makes it very difficult to do some of the most trivial things in programming.
Take string concatenation for example.&lt;/p&gt;

&lt;p&gt;This is how you do it in Javascript:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hello &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;xy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;y&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 in PHP:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hello &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;nv&quot;&gt;$xy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$y&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;So you would think that the following would work in Rust:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&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;That doesn’t compile. This is because Rust has 2 types of strings. &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;str&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;String&lt;/code&gt;. Both &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; are of type &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;str&lt;/code&gt;. These are called &lt;code class=&quot;highlighter-rouge&quot;&gt;string literals&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;string slices&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;static strings&lt;/code&gt;. They have a fixed size and cannot be mutated.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A string literal is a string slice that is statically allocated, meaning that it’s saved inside the compiled program,
    and exists for the entire duration it runs. The reference binding is a reference to this statically allocated string.
    Any function expecting a string slice will also accept a string literal.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So what about &lt;code class=&quot;highlighter-rouge&quot;&gt;String&lt;/code&gt;? Strings are allocated on the heap and can be mutated. It’s essentially a vector of unicode chars.
&lt;code class=&quot;highlighter-rouge&quot;&gt;Strings&lt;/code&gt; are generally created from a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;str&lt;/code&gt; by using to_string() function. So lets try again:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello &quot;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.to_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.to_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.push_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&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;That doesn’t work either. It’ll give you &lt;code class=&quot;highlighter-rouge&quot;&gt;expected &amp;amp;str, found struct &lt;/code&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;std::string::String&lt;/code&gt;. Ok. So how about this then:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello &quot;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.to_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.to_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&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;Again, the compiler will throw the same error as before. So in order to get around it, you have to dereference one of the
&lt;code class=&quot;highlighter-rouge&quot;&gt;Strings&lt;/code&gt; to obtain the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;str&lt;/code&gt; version of it. This is done easily by prefixing an ampersand to the second argument, which
essentially dereferences the &lt;code class=&quot;highlighter-rouge&quot;&gt;String&lt;/code&gt; and passes a pointer of the &lt;code class=&quot;highlighter-rouge&quot;&gt;string slice&lt;/code&gt; to the concat function. So the working
version of the previous example would look like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello &quot;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.to_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.to_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&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 you are making a call to a function that’s returning a &lt;code class=&quot;highlighter-rouge&quot;&gt;String&lt;/code&gt; and you want to concatenate it to an existing string
then you just add the ampersand to the beginning of the function call.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_world&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;    &lt;span class=&quot;s&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.to_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&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;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello &quot;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.to_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_world&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;but-wtf-why-do-you-have-to-do-that&quot;&gt;But WTF? why do you have to do that?&lt;/h3&gt;
&lt;p&gt;The simple answer is because how concatenation is done. When you are using the &lt;code class=&quot;highlighter-rouge&quot;&gt;+&lt;/code&gt; operator for &lt;code class=&quot;highlighter-rouge&quot;&gt;Strings&lt;/code&gt; in Rust, you are
essentially calling the &lt;a href=&quot;https://doc.rust-lang.org/std/ops/trait.Add.html&quot;&gt;std::ops::Add Trait&lt;/a&gt; on the &lt;code class=&quot;highlighter-rouge&quot;&gt;String&lt;/code&gt; on the
left hand side. And this Trait is designed so that the left hand side argument should be &lt;code class=&quot;highlighter-rouge&quot;&gt;String&lt;/code&gt; and the right hand side
argument should be an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;str&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So now we understand that this is because of a particular design decision of the standard library. But why? Why not just
allow for two arguments like &lt;code class=&quot;highlighter-rouge&quot;&gt;add(a: String, b: String) -&amp;gt; String&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;This is basically down to how and where things are stored in the memory and the operational cost of mutating them.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;impl&amp;lt;'a&amp;gt; Add&amp;lt;String&amp;gt; for &amp;amp;'a str&lt;/code&gt; requires prepending, which is not as efficient as appending&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;impl Add&amp;lt;String&amp;gt; for String&lt;/code&gt; needlessly consume two arguments when one is sufficient&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;impl&amp;lt;'a, 'b&amp;gt; Add&amp;lt;&amp;amp;'a str&amp;gt;&lt;/code&gt; for &amp;amp;’b str hides an unconditional memory allocation&lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Thu, 06 Oct 2016 00:00:00 +0000</pubDate>
        <link>http://dnsh.io/music/2016/10/06/string-concatenation-in-rust-is-not-tivial/</link>
        <guid isPermaLink="true">http://dnsh.io/music/2016/10/06/string-concatenation-in-rust-is-not-tivial/</guid>
      </item>
    
      <item>
        <title>Label centric music discovery</title>
        <description>&lt;p&gt;Music is big business. In fact it’s a business that &lt;a href=&quot;http://www.ifpi.org/global-statistics.php&quot;&gt;entertained $15bn (USD) from global record sales alone&lt;/a&gt;.
45% of this $15bn was generated through digital sales channels. So it would be easy to make the assumption that digital
sales are on the up because digital consumption of music is so much easier and cheaper for the consumer.&lt;/p&gt;

&lt;p&gt;But as it is always the case with statistics, numbers doesn’t always paint the full picture. Sure it’s easier now than 15 - 20
years ago to go and buy a number off the top 10; but if you venture 1 degree from the mainstream taste, you’ll find yourself
spending hours clicking through music (as opposed to digging in the past - I’ll come to that too) trying to find the perfect
tuna.&lt;/p&gt;

&lt;p&gt;Let me describe the struggle in detail: You hear this incredible piece of music somewhere and you get instantly hooked.
You’ve never heard this stuff before (and you blame yourself for it) and you are sure hearing this sound has changed your life.
Now you HAVE to spend your next two weeks listening to some more of the same. In the most ideal situation, you heard this
music while you were sitting in your friend’s sofa and you interrogate him until he gives up all his knowledge of that piece.
And in less than ideal situations, you hear it while you’re out and about - in a pub or a festival. If you are really lucky,
one of your friends (or Shazam) would have successfully id’ed the track. Great - but you want more of the same. Which finally
brings us to the topic of this article.&lt;/p&gt;

&lt;h2 id=&quot;more-of-the-same-please&quot;&gt;More of the same please!&lt;/h2&gt;
&lt;p&gt;You managed to find out the name of the artist and the name of the song. This is the beginning of your hunt. You do your basic
searches on Google, or apps like Shazam will even attempt to recommend similar. This might yield some decent results depending
on the popularity of the type of music that you are hunting. Underground music tends to get the least amount of exposure
(for obvious reasons). Things like Spotify can be very useful in your road to new music discovery because it allows you
to listen to a incredibly large collection of recorded music. But it falls short on the discovery bit.&lt;/p&gt;

&lt;p&gt;When you know the artist and the song, you can try and listen to more from the same artist or find other artists that they
collaborated with or even try “similar artists” recommended by Spotify in your quest to find more of that flavour. Sometimes
this doesn’t work because the the song that you heard from that artist may have been a special release and the rest of their
releases are a completely different genre altogether (yes, this happens - quite a lot in niche genres).&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Algorithmic music discovery doesn’t quite work for creative content because art is often varied and doesn’t always follow
   a consistent and a predictable pattern.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The thing is, the music industry had been around for a long time and this problem had already been solved. Algorithmic music
discovery doesn’t quite work for creative content because art is often varied and doesn’t always follow a consistent and
a predictable pattern. So &lt;strong&gt;&lt;em&gt;human curation&lt;/em&gt;&lt;/strong&gt; is such a big part of music discovery process. But it’s a lot of work to
manually curate and categorise all this amount of digital content that you would think this is not an economically viable
option. But in fact, this is exactly how underground music is pushed.&lt;/p&gt;

&lt;h3 id=&quot;piggy-back-on-the-work-of-the-labels&quot;&gt;Piggy back on the work of the labels&lt;/h3&gt;
&lt;p&gt;Record labels often times act like human curators. They work with artists during the recording and release process and
the label themselves decide to release music through their brand that they can identify with. So check out what label released
that song that you really liked. If it’s not one the major labels, then there’s a good chance that you will like most of
the content released by that label. Hunting this way will introduce you to new artists that you would otherwise wouldn’t
know to look up.&lt;/p&gt;

&lt;p&gt;While you are doing this, you can use &lt;strong&gt;Spotify’s Advanced Search&lt;/strong&gt; functionality to filter by labels. Just typing in the
label name in search wouldn’t get you any results on Spotify. In fact, labels aren’t even top level entities like Artist,
Albums and Songs. I think this is a mistake from Spotify. So you have to use &lt;a href=&quot;https://support.spotify.com/uk/using_spotify/search_play/advanced-search1/&quot;&gt;Advanced Search&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So to filter by label, type in &lt;code class=&quot;highlighter-rouge&quot;&gt;label:&quot;Name of the Label&quot;&lt;/code&gt; in to Spotify’s search bar. The example below shows how to
find all releases made by the record label 1080p:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;label:&quot;1080p&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Go out and try it and zero in on the music you reeeeaaaalllly want.&lt;/p&gt;
</description>
        <pubDate>Sun, 02 Oct 2016 00:00:00 +0000</pubDate>
        <link>http://dnsh.io/music/2016/10/02/label-focused-music-discovery/</link>
        <guid isPermaLink="true">http://dnsh.io/music/2016/10/02/label-focused-music-discovery/</guid>
      </item>
    
      <item>
        <title>PHP 7.0 short-change</title>
        <description>&lt;p&gt;So, It’s been almost a year now since PHP 7 had been released. I have to say that regardless of the tone of this article’s
title, I am over the moon about this major version. I think it’s really come a long way from the old “Personal Home Page”
laughing-stock. And I’m not even talking about the &lt;a href=&quot;http://benchmarksgame.alioth.debian.org/u64q/php.html&quot;&gt;PHP 7 vs Python 3 benchmark game&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;No, I mean it had actually become a decent language and I am not that sad about having to work with it day in day out. I
sometimes even admit at parties to being a PHP developer.&lt;/p&gt;

&lt;p&gt;PHP 5.6 was already pretty good. We had short syntax arrays, consistent and standard compliant JSON serialization/de-serialisation,
Traits, Reflection and so on. So the big thing apart from all the &lt;em&gt;we’re faster than python&lt;/em&gt; performance improvements has
to be the inclusion of optional typing for scalars (again, PHP 5.6 fell a little short on this one).&lt;/p&gt;

&lt;p&gt;I love it! Now I don’t have to spend time type checking at the beginning of every method like we used to:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&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;nb&quot;&gt;is_numeric&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$x&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;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;is_numeric&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;    &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;\InvalidArgumentException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Both X and Y has to be a number&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Look at that shit! Anyone who says dynamic typing makes it easier to write code probably doesn’t type check their input arguments.
In the above example, 3 lines of code went in to type checking and the actual business logic was a one-liner. So I’m glad
that PHP 7 allows us to type hint scalars. Another bonus is that we can now declare the return type as well. This now cuts
down the amount of code on the calling method. Check what the same example looks like on PHP 7:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$y&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;nx&quot;&gt;int&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Beautiful! And this is great when taking two integers and returning one. But people pay me money for my code so life is never
that simple. I have to do things from time to time that isn’t discussed in the release notes. Like return null sometimes
(or all the time for that matter). Check this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getOneLemon&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;nx&quot;&gt;Lemon&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&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;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lemons&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lemons&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;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Looks pretty reasonable but the code above will throw a fatal because you promised to return a &lt;code class=&quot;highlighter-rouge&quot;&gt;Lemon&lt;/code&gt; but sometimes you can’t.
One way around it was to get rid of the return type declaration and maybe describe the return types in the docblock so your
IDE knows what you are up to. I mean… we just lost all the power that this feature was supposed to give us.&lt;/p&gt;

&lt;p&gt;Another way was to throw an exception when you can’t return the type you promised. This is a little bit better but if you
really think about it, you are relying on a construct designed to signal failures and errors as a part of the logic flow.
This makes me uncomfortable. If I go to a shop and ask for some sugar I don’t want the shop keeper to start shutting down
the shop when he notice that he doesn’t have any sugar to sell. No is a perfectly acceptable answer.&lt;/p&gt;

&lt;p&gt;But there is light at the end of the tunnel. An RFC was accepted and this issue is ironed out in the next minor version upgrade
(7.1) - along with a heap of other improvements. The official released is set to be in Nov this year. So in 7.1 the last
example would look like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getOneLemon&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;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Lemon&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&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;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lemons&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lemons&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;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Boom. The question mark in front of the &lt;code class=&quot;highlighter-rouge&quot;&gt;Lemon&lt;/code&gt; indicates that it’s optional. That means you can return null as well as
&lt;code class=&quot;highlighter-rouge&quot;&gt;Lemon&lt;/code&gt; (but no other type). Pretty solid. Another thing - you can also return a new &lt;code class=&quot;highlighter-rouge&quot;&gt;void&lt;/code&gt; type. Looking forward to that!&lt;/p&gt;
</description>
        <pubDate>Fri, 23 Sep 2016 00:00:00 +0000</pubDate>
        <link>http://dnsh.io/programming/2016/09/23/php-7-0-short-change/</link>
        <guid isPermaLink="true">http://dnsh.io/programming/2016/09/23/php-7-0-short-change/</guid>
      </item>
    
      <item>
        <title>PhpSpec is a bloody nightmare</title>
        <description>&lt;p&gt;Where I work, we use PhpSpec for unit-testing our code. One might argue that these should not be called unit tests since
you write specifications rather than tests. Call it what you will - we can have that argument another day. Today I will
rant about the good and some of the bad things about it.&lt;/p&gt;

&lt;p&gt;In a nutshell, PhpSpec helps bring the TDD buzzword to the PHP world. By allowing you to mock shit straight off the bat,
PhpSpec makes it difficult to come up with excuses like &lt;em&gt;“I wrote the feature but I haven’t written any tests yet”&lt;/em&gt;.
Again, I’m not pissed off about that.&lt;/p&gt;

&lt;p&gt;In fact, I actually like it because I’m lazy and PhpSpec generates shit for me. I
just make grand statements like &lt;em&gt;“Oh, I got a Some\Bullshit. And it takes a CrunkJuiceFactory as a constructor
argument. And I can getJuice() from CrunkJuiceFactory anytime I want and it’ll give me n number of juice hits”&lt;/em&gt;. But I
haven’t written any of that behaviour yet. Better get that scaffolded… So I do:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;bin/phpspec desc Some/Bullshit&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then PhpSpec just makes a specification for you with nothing much in it but you can’t argue with that since you haven’t
done any work yet yourself. But you don’t care. This is TDD so you run the test anyway:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;bin/phpspec run spec/Some/BullshitSpec.php&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Oh look! PhpSpec wants to help you create the actual test subject!&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;Some/Bullshit&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;  10  - it is initializable&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;      class Some&lt;span class=&quot;se&quot;&gt;\B&lt;/span&gt;ullshit does not exist.&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;1 specs&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;1 example &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;1 broken&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;36ms&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;  Do you want me to create Some&lt;span class=&quot;se&quot;&gt;\B&lt;/span&gt;ullshit &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;you? &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;Y/n]&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;You take &lt;code class=&quot;highlighter-rouge&quot;&gt;Y&lt;/code&gt;, and spec gives you your new file and you are ready to write some code. At this point, I know the IDE warriors
are up on my case pointing out that one could achieve the same with 3 clicks and you don’t have to keep chatting to a terminal. You are
correct. But that’s not where Spec saves you. PhpSpec gives you the ability to focus on TDD, writing your subject and mocking
the behaviour of its dependencies right from the start.&lt;/p&gt;

&lt;p&gt;For example, you can just scaffold out &lt;code class=&quot;highlighter-rouge&quot;&gt;CrunkJuiceFactory&lt;/code&gt; object just like your &lt;code class=&quot;highlighter-rouge&quot;&gt;Bullshit&lt;/code&gt; object. And then you can mock
return values of methods in &lt;code class=&quot;highlighter-rouge&quot;&gt;CrunkJuiceFactory&lt;/code&gt; so that you can just focus on writing the actual behaviour of &lt;code class=&quot;highlighter-rouge&quot;&gt;Bullshit&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;spec\Some&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PhpSpec\ObjectBehavior&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Prophecy\Argument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BullshitSpec&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ObjectBehavior&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CrunkJuiceFactory&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$cjFactory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;        &lt;span class=&quot;nv&quot;&gt;$cjFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getJuice&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;na&quot;&gt;willReturn&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;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;        &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;beConstructedWith&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$cjFactory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;You just noticed that you started describing behaviour of &lt;code class=&quot;highlighter-rouge&quot;&gt;CrunkJuiceFactory&lt;/code&gt; and making claims about what it will do before
you even thought about implementing it. This is great for productivity because you don’t have to write both classes in parallel.&lt;/p&gt;

&lt;p&gt;But how does that even work?? This witchcraft makes you productive but unless you understand the underlying mechanics of
the framework, you will run in to problems. Essentially, PhpSpec makes heavy use of another buzzword. &lt;strong&gt;Reflection&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Reflection allows PhpSpec to get a description of the object and the dependencies that you are trying to test. And using
this information, PhpSpec creates stunt doubles of your objects which it pushes through your test subject. And this stunt
double essentially intercepts calls made from your test subject to it and then returns values that you have mocked. This
is what you did when you described &lt;code class=&quot;highlighter-rouge&quot;&gt;$cjFactory-&amp;gt;getJuice()-&amp;gt;willReturn(5);&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;All this stuff sounds great! Why the damn rant? Well, the problem is that this makes it very difficult to test your tests.
Spec gets weird when you are testing some code that you have wrapped around a &lt;code class=&quot;highlighter-rouge&quot;&gt;try .. catch&lt;/code&gt; block.&lt;/p&gt;

&lt;p&gt;If you cause a PhpSpec error, inside a try catch block in your subject (like making a call to a method that you didn’t describe
in the spec) then you are going to spend hours trying to figure out why PhpSpec keep running forever and eventually die
without giving you any bacon. Or anything for that matter.&lt;/p&gt;

&lt;p&gt;Let me explain better:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getBacon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;        &lt;span class=&quot;nv&quot;&gt;$bacon&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;baconFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getSome&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;\Exception&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;        &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;critical&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;OUT OF BACON!!!! :O&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&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 above scenario, if you had forgotten to mock &lt;code class=&quot;highlighter-rouge&quot;&gt;getSome()&lt;/code&gt; method in your &lt;code class=&quot;highlighter-rouge&quot;&gt;BaconFactory&lt;/code&gt; object inside your spec then
PhpSpec will throw an Exception right where that ‘Undefined method’ was called. This means that the exception gets caught
by your subject and not PhpSpec itself. And then PhpSpec hangs. And you have no idea WTF just happened. Nothing in the
terminal. Just hangs. If you ran it with php -d memory_limit=-1 then it hangs even longer. Possibly until the end of the
world.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So how do you deal with this?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you come across this sorry situation in your life, try and narrow down exactly which spec is hanging. And then you will
probably notice that your subject has a try catch block. Then stick a var_dump in the catch block with the exception message
and die. It’ll show you the message on your terminal so you can move forward.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getBacon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;        &lt;span class=&quot;nv&quot;&gt;$bacon&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;baconFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getSome&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;\Exception&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;        &lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getMessage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;die&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;        &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;critical&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;OUT OF BACON!!!! :O&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;br data-jekyll-commonmark-ghpages=&quot;&quot; /&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Oh, and don’t forget to remove the die statement once you are done debugging.&lt;/p&gt;
</description>
        <pubDate>Thu, 22 Sep 2016 00:00:00 +0000</pubDate>
        <link>http://dnsh.io/programming/2016/09/22/phpspec-is-a-bloody-nightmare/</link>
        <guid isPermaLink="true">http://dnsh.io/programming/2016/09/22/phpspec-is-a-bloody-nightmare/</guid>
      </item>
    
  </channel>
</rss>
