<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Mikael Lundin Weblog</title>
		<description>Short stories about functional things, non-functional things, and things that doesn&#39;t function.</description>		
		<link>https://blog.mikaellundin.name</link>
		<atom:link href="https://blog.mikaellundin.name/feed.xml" rel="self" type="application/rss+xml" />
		
			<item>
				<title>Configure React with different Environment Settings</title>
				<description>&lt;p&gt;I ran into this problem today, and after solving it was asked to blog about my findings.&lt;/p&gt;

&lt;p&gt;I have a React application that is built to a Docker image and then statically hosted. I needed to configure the application with different OpenID Connect settings for test, staging and production environments. I couldn&amp;#39;t just send environment variables to the Docker container, because the frontend application cannot read them.&lt;/p&gt;

&lt;p&gt;I found a couple of different options.&lt;/p&gt;

&lt;h2&gt;Using .env files&lt;/h2&gt;

&lt;p&gt;Create React App has built-in support for .env files. When you build the application, the configuration options are bundled into the app with the app bundle. The problem is that &lt;a href=&quot;https://www.npmjs.com/package/dotenv&quot;&gt;dotenv&lt;/a&gt; doesn&amp;#39;t work like you expect it to.&lt;/p&gt;

&lt;p&gt;Basically it supports using different configurations for development &lt;code&gt;npm start&lt;/code&gt;, production &lt;code&gt;npm run build&lt;/code&gt; and testing &lt;code&gt;npm test&lt;/code&gt;. It doesn&amp;#39;t support environments like test, staging and production. It is also strongly discouraged to commit .env files to source control.&lt;/p&gt;

&lt;p&gt;Documentation for .env files.&lt;br&gt;
&lt;a href=&quot;https://create-react-app.dev/docs/adding-custom-environment-variables/&quot;&gt;https://create-react-app.dev/docs/adding-custom-environment-variables/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Dynamic Rendering&lt;/h2&gt;

&lt;p&gt;Another technique I’ve used before, is to dynamically render the settings in a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag in &lt;code&gt;index.html&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;lang=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;en&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SERVER_DATA&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&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;nx&quot;&gt;php&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;json_encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;$settings&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;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In your React application you can fetch the settings object from &lt;code&gt;window.SERVER_DATA&lt;/code&gt;. By composing the settings object from environment variables, this lets you configure the React application from Docker container env vars.&lt;/p&gt;

&lt;p&gt;The bad news is that your application is now dependent on external state, and that breaks portability. I don&amp;#39;t like it.&lt;/p&gt;

&lt;p&gt;I couldn&amp;#39;t use the dynamic technique this time as my React files are statically hosted.&lt;/p&gt;

&lt;p&gt;Some official documentation.&lt;br&gt;
&lt;a href=&quot;https://create-react-app.dev/docs/title-and-meta-tags#generating-dynamic-meta-tags-on-the-server&quot;&gt;https://create-react-app.dev/docs/title-and-meta-tags#generating-dynamic-meta-tags-on-the-server&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Fetch Configuration on Startup&lt;/h2&gt;

&lt;p&gt;I also have the option to fetch the configuration when application mounts.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;App&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;kr&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setSettings&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;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({});&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;useEffect&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;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kr&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fetchSettingsAsync&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;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;/api/settings&quot;&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;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;response&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;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;json&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;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&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;nx&quot;&gt;setSettings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&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;nx&quot;&gt;fetchSettingsAsync&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;// rest of the program&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you&amp;#39;re hosting an API for your application already on localhost, this might be a cleaner solution, but I don&amp;#39;t like that my app starts with showing a loading spinner. Not for data that could&amp;#39;ve been bundled with the application.&lt;/p&gt;

&lt;h2&gt;My Solution&lt;/h2&gt;

&lt;p&gt;I like the .env file approach. It is simple and integrates well into local development. However it doesn&amp;#39;t support multiple environments out of the box. We can create a work-around by replacing the .env file in our CI.&lt;/p&gt;

&lt;p&gt;First I create a &lt;code&gt;.env.development&lt;/code&gt; file with reasonable defaults for local development. This can be committed to source control for simplified development setup.&lt;/p&gt;

&lt;p&gt;Next, I create two files that I &lt;strong&gt;don&amp;#39;t&lt;/strong&gt; commit to source control.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;.env-staging&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.env-production&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These files contain configurations for staging and production environment. As I&amp;#39;ve been using Azure DevOps I upload these files to the &lt;em&gt;secure file area&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/posts/2021-11-03-configure-react-with-different-environment-settings/secret-files.png&quot; alt=&quot;Upload to Secure Files in DevOps&quot;&gt;&lt;/p&gt;

&lt;p&gt;Next, I modify the build pipeline. Since Docker images are immutable I&amp;#39;ll have to build one image for each environment.&lt;/p&gt;

&lt;p&gt;In the build pipeline I download the secure file and add it as a secret to the &lt;code&gt;docker build&lt;/code&gt; command. If using Docker Compose, secrets will not work during build step. An option here is to copy the file into the build context instead.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-yaml&quot; data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;DownloadSecureFile@1&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;env&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;secureFile&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;env-$(envName)&quot;&lt;/span&gt;

&lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;bash&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;DOCKER_BUILDKIT=1 docker build --secret id=env,src=$(env.secureFilePath)  -t incaps/vaccinated:1.0.0-$(envName) .&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;displayName&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Docker&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;build&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the Dockerfile, I copy the &lt;code&gt;/run/secrets/env&lt;/code&gt; file into my source repository where the &lt;code&gt;.env&lt;/code&gt; file normally would be. This file has preceedence to any .env variant like &lt;code&gt;.env.development&lt;/code&gt; or &lt;code&gt;.env.production&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-dockerfile&quot; data-lang=&quot;dockerfile&quot;&gt;# syntax = docker/dockerfile:1.1-experimental
# ...

RUN --mount=type=secret,id=env cp /run/secrets/env /src/.env
RUN npm run build
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The app will now bundle with the environment specific configuration.&lt;/p&gt;

&lt;h2&gt;Downsides&lt;/h2&gt;

&lt;p&gt;Since I bundle the configuration into the React bundle, I need to build one docker image for each of my environments. This somewhat contradicts the purpose of Docker, build once - run everywhere, but it is the option that I favoured for my specific case.&lt;/p&gt;
</description>
				<pubDate>Wed, 03 Nov 2021 20:39:02 +0000</pubDate>
				<link>https://blog.mikaellundin.name/2021/11/03/configure-react-with-different-environment-settings.html</link>
				<guid isPermaLink="true">https://blog.mikaellundin.name/2021/11/03/configure-react-with-different-environment-settings.html</guid>
			</item>
		
			<item>
				<title>Thoughts on Github Copilot</title>
				<description>&lt;p&gt;Github Copilot is a new service from Github that is currently in preview, and I’ve had the opportunity to take it for a spin. This service integrates with your IDE, helping you to write code by giving suggestions. It does this with the use of AI, which gives context sensitive, and very accurate results.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://copilot.github.com&quot;&gt;Go here and read about it to learn more.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;My Experience&lt;/h2&gt;

&lt;p&gt;I was writing some elaborate text transformation logic and decided to try out Copilot. The quality of the suggestions were good enough that I didn’t have to write much code at all. I wrote some function signatures and could tab myself through the development. Functionality I expected would take several evenings of coding, was done in 30 minutes. The productivity boost was enormous.&lt;/p&gt;

&lt;p&gt;In short what happened during this time&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;My workflow changed from writing code to directing Copilot to write the correct code for me&lt;/li&gt;
&lt;li&gt;The generated code took advantage of the code I&amp;#39;d already written&lt;/li&gt;
&lt;li&gt;There was very little need for adjustments or fixing the code that was generated&lt;/li&gt;
&lt;li&gt;The generated code was working as expected on the first run&lt;/li&gt;
&lt;li&gt;I didn’t spend time unit testing. Couldn&amp;#39;t really see the point for the generated code&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;The Implications&lt;/h2&gt;

&lt;p&gt;I’m thinking about what will happen if this way of programming becomes the norm.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Productivity will sky rocket. Every developer will produce much more code&lt;/li&gt;
&lt;li&gt;Instead of writing code, workflow will focus around generating the code that you want&lt;/li&gt;
&lt;li&gt;There is no need to understand the code you generate as long as it works&lt;/li&gt;
&lt;li&gt;Systems will become less maintainable, focus will shift to rewrite since it&amp;#39;s cheaper&lt;/li&gt;
&lt;li&gt;Manually writing code, will be considered as writing assembly without a compiler today&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The big paradigm    shift will be that system developers will drive the code generation to do what they want, instead of writing the code as we do today.&lt;/p&gt;

&lt;p&gt;What do you think?&lt;/p&gt;
</description>
				<pubDate>Tue, 02 Nov 2021 07:34:02 +0000</pubDate>
				<link>https://blog.mikaellundin.name/2021/11/02/thoughts-on-github-copilot.html</link>
				<guid isPermaLink="true">https://blog.mikaellundin.name/2021/11/02/thoughts-on-github-copilot.html</guid>
			</item>
		
			<item>
				<title>Cloud PC Development Environment - Windows365 Review</title>
				<description>&lt;p&gt;I&amp;#39;ve been using virtual machines for development since 2012. It started as an experiment, but I kept at it because I could easily see the benefits.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I could isolate each customer in a separate virtual machine&lt;/li&gt;
&lt;li&gt;I could install customer specific software, like VPN clients, without screwing up my host&lt;/li&gt;
&lt;li&gt;I could easily archive a customer development environment and resume it later&lt;/li&gt;
&lt;li&gt;I could experiment with the development environment and easily roll back to a snapshot taken before the experiment&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The downside of working in virtual machines are&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;They are slow, especially the Windows ones&lt;/li&gt;
&lt;li&gt;They take up a lot of harddrive space, and they need to stay on the SSD for speed&lt;/li&gt;
&lt;li&gt;They also make the host OS slow as they consume a lot of resources when they run&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I’ve been looking a while for remote development environments suitable for development, and I did experiment with coding over RDP during 2013-2014. It worked okay, but it wasn&amp;#39;t a game changer.&lt;/p&gt;

&lt;p&gt;Running remote Linux environments has been mainstream for quite some time, but getting it to work well with X has been quirky at best. I still prefer to have my development Linux as a local virtual machine.&lt;/p&gt;

&lt;h2&gt;Windows365&lt;/h2&gt;

&lt;p&gt;In July Microsoft announced their Windows365, Cloud PC access on a subscription model like Office365. Their goal is to make it easy for enterprises to hand out a virtual machine sin the cloud to employees where you have the Office suite and Teams already installed.&lt;/p&gt;

&lt;p&gt;I think it&amp;#39;s a great idea, and it could really revolutionize remote working. But I’m not looking at it to write my e-mails. I want to use it for Visual Studio.&lt;/p&gt;

&lt;p&gt;For this they have the following offering.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;4 vCPU&lt;br&gt;
16 GB RAM&lt;br&gt;
128 GB Storage&lt;br&gt;
&lt;strong&gt;$66.00 user/month&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is enough for running Visual Studio. If you want to, you can increase it with more cores, RAM and Storage, but it’ll soon get quite pricey. This was the machine configuration that I chose.&lt;/p&gt;

&lt;h2&gt;Review&lt;/h2&gt;

&lt;p&gt;I’ve been using this cloud machine as my primary development environment for the entire week and I’m positively surprised.&lt;/p&gt;

&lt;p&gt;It was very easy to setup to get started. It took only a couple of minutes before the machine was created, and I could connect with a RDP profile that I easily downloaded for Microsoft Remote Desktop.&lt;/p&gt;

&lt;p&gt;With Windows365 you can also connect with iPhone, iPad or just a browser. I tried the browser option just to get the feel of it, but I really want to use multiple screens, so Microsoft Remote Desktop is the client for me.&lt;/p&gt;

&lt;p&gt;The Cloud PC is very fast. Installing Visual Studio was a breeze and quicker than ever. I rarely feel that the environment is slow. It is much more performant than my local virtual machines ever been.&lt;/p&gt;

&lt;p&gt;I was expecting input lag from working over the Internet, and I did notice it on Monday when the service was new and everyone was trying it out at the same time. Since then it has become much better and I honestly forget that the computer I’m working on is not here in front of me, but in a data center somewhere else. It&amp;#39;s actually that good.&lt;/p&gt;

&lt;p&gt;I work with docker containers, and I had problems of running docker inside virtual machines before, however this was not a problem at all in my Cloud PC. Running Docker in Ubuntu WSL2 on Windows works like a charm. And the Docker Desktop for Windows also seems to be working as expected.&lt;/p&gt;

&lt;p&gt;What strikes me is that I don’t need to carry around a machine anymore. If I&amp;#39;d go into an office, I could just connect to my development environment from any crappy office computer there. That would never happen since I love my MacBook Pro M1 too much, but the idea of it feels very liberating.&lt;/p&gt;

&lt;h2&gt;Downsides&lt;/h2&gt;

&lt;p&gt;It doesn’t replace all the functionality that I had with virtual machines. I can’t&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Take snapshots and roll back&lt;/li&gt;
&lt;li&gt;Archive an environment in order to return to it later&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I actually have no idea how backups work, as I can’t see anything about it in the Windows365 control panel. What happens if I screw things up and want to roll back?&lt;/p&gt;

&lt;p&gt;I would love the ability to just pause my Cloud PC and it would stop costing me money, but as it is now you need to remove the Windows365 license from your user which means that everything is gone and deleted.&lt;/p&gt;

&lt;p&gt;There is a feature to reset your Cloud PC, but that would mean reinstalling all software and configure it from scratch. With local virtual machines I had the option of creating a template, that I’d use to quickly setup new machines. I could delete a development environment and setup a new one in 10 minutes.&lt;/p&gt;

&lt;p&gt;These features are probably stuff that they will add later on.&lt;/p&gt;

&lt;p&gt;My last complaint is about the price. I think the $66/month is a bit high and it means that I will remove my Cloud PC when I’m not using it. If you turn that coin, there is no large upfront cost like buying a physical PC, and the ability to cancel your subscription means that you can pick a higher end configuration and just cancel it those months you don’t need it, and still get a cheaper deal compared to buying a PC.&lt;/p&gt;

&lt;p&gt;I will keep using my new Cloud PC. It really feels like the future to me. My MacBook Pro thanks me as well, as I haven&amp;#39;t heard its fans for the whole week. :)&lt;/p&gt;
</description>
				<pubDate>Fri, 06 Aug 2021 20:34:02 +0000</pubDate>
				<link>https://blog.mikaellundin.name/2021/08/06/cloud-pc-development-environment-windows365-review.html</link>
				<guid isPermaLink="true">https://blog.mikaellundin.name/2021/08/06/cloud-pc-development-environment-windows365-review.html</guid>
			</item>
		
			<item>
				<title>100 Interview Questions for Software Developers</title>
				<description>&lt;p&gt;This blog post is inspired by Jurgen Appelo &lt;a href=&quot;https://noop.nl/2009/01/100-interview-questions-for-software-developers.html&quot;&gt;100 interview questions for Software Developers&lt;/a&gt;. I prefer to have a conversation instead of a knowledge test, so my questions do not have any correct answers. They should lead to conversation instead of validating knowledge.&lt;/p&gt;

&lt;p&gt;Here we go.&lt;/p&gt;

&lt;h2&gt;Development Methods&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;How do you prefer to version control your code?&lt;/li&gt;
&lt;li&gt;Do you follow any known patterns of version control?&lt;/li&gt;
&lt;li&gt;What does continuous integration mean to you?&lt;/li&gt;
&lt;li&gt;What is your definition of done?&lt;/li&gt;
&lt;li&gt;How do you safely deliver code to production?&lt;/li&gt;
&lt;li&gt;How do you make your production environment reproducible in test and dev?&lt;/li&gt;
&lt;li&gt;How would you start troubleshooting an issue only visible in production?&lt;/li&gt;
&lt;li&gt;Describe the steps involved for you to fix a bug&lt;/li&gt;
&lt;li&gt;What is the importance of git commit messages and how do you format them?&lt;/li&gt;
&lt;li&gt;How do you roll-back a failed deployment to production?&lt;/li&gt;
&lt;li&gt;Production goes down because of a bad deployment. What would make you roll-back and what would make you roll-forward?&lt;/li&gt;
&lt;li&gt;How do you manage assets that should not be committed to source control, like passwords or databases?&lt;/li&gt;
&lt;li&gt;Where do you think code ownership should lie?&lt;/li&gt;
&lt;li&gt;When should refactoring happen?&lt;/li&gt;
&lt;li&gt;What code should be written by solitary programmers, and what code should be pair programmed or mob programmed?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Quality Assurance&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What does quality mean for you?&lt;/li&gt;
&lt;li&gt;How do you ensure quality in your delivery?&lt;/li&gt;
&lt;li&gt;Who is testing your code?&lt;/li&gt;
&lt;li&gt;What does testability mean to you?&lt;/li&gt;
&lt;li&gt;What quality measures do you consider low hanging fruit?&lt;/li&gt;
&lt;li&gt;Where do you draw the line between enough testing and too much testing?&lt;/li&gt;
&lt;li&gt;Mention what statical code analysis tools you use&lt;/li&gt;
&lt;li&gt;Do you write tests first, after or at the same time as the code?&lt;/li&gt;
&lt;li&gt;What is your opinion on integration testing?&lt;/li&gt;
&lt;li&gt;How do you deal with a test suite with brittle tests that require much maintenance?&lt;/li&gt;
&lt;li&gt;How can you improve quality by communication?&lt;/li&gt;
&lt;li&gt;How do you think that management can enable quality?&lt;/li&gt;
&lt;li&gt;How do you measure quality?&lt;/li&gt;
&lt;li&gt;What are your thoughts on technical debt?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Coding&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What is your favorite language and why?&lt;/li&gt;
&lt;li&gt;What do you consider a good framework?&lt;/li&gt;
&lt;li&gt;What is your preferred way to document your code?&lt;/li&gt;
&lt;li&gt;How do you make your code readable to others?&lt;/li&gt;
&lt;li&gt;What do you consider maintainable code?&lt;/li&gt;
&lt;li&gt;What are your most used development tools?&lt;/li&gt;
&lt;li&gt;What is the worst feature of your favourite programming language?&lt;/li&gt;
&lt;li&gt;What is the most common programming error people do?&lt;/li&gt;
&lt;li&gt;How do you typically handle errors in your application?&lt;/li&gt;
&lt;li&gt;Mention something you do to make your application secure.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;DevOps&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What’s your suggestion to improve deployment frequency?&lt;/li&gt;
&lt;li&gt;What’s your suggestion to reduce time from you start working on a feature until its available to users?&lt;/li&gt;
&lt;li&gt;What can we do to prepare for a system to be restored faster, when it goes down?&lt;/li&gt;
&lt;li&gt;How would you propose we reduce number of releases that cause problems in production?&lt;/li&gt;
&lt;li&gt;How do you verify the impact of your feature in production?&lt;/li&gt;
&lt;li&gt;Where do you draw the line between development and operations?&lt;/li&gt;
&lt;li&gt;How do you manage different configuration options for different environments?&lt;/li&gt;
&lt;li&gt;How can you as a coder make your service easier to manage for operations?&lt;/li&gt;
&lt;li&gt;What are the things that you believe should be automated?&lt;/li&gt;
&lt;li&gt;What are the tools you need in order to deliver code to production once per day?&lt;/li&gt;
&lt;li&gt;Do trunk-based development contribute to better software delivery?&lt;/li&gt;
&lt;li&gt;How do we create predictability in our software delivery?&lt;/li&gt;
&lt;li&gt;What’s required for you to feel inspired by your work?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Hosting&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Which is your favourite cloud provider and why?&lt;/li&gt;
&lt;li&gt;What does cloud native mean to you?&lt;/li&gt;
&lt;li&gt;What is your relationship with Kubernetes?&lt;/li&gt;
&lt;li&gt;What is your thoughts on serverless?&lt;/li&gt;
&lt;li&gt;How do you measure uptime of your services?&lt;/li&gt;
&lt;li&gt;How do you collect telemetry from your services?&lt;/li&gt;
&lt;li&gt;Mention a way to improve availability of your services&lt;/li&gt;
&lt;li&gt;How do you monitor the services you have released?&lt;/li&gt;
&lt;li&gt;What would make you choose on-prem hosting over cloud?&lt;/li&gt;
&lt;li&gt;What is a good way of managing SSL certificates?&lt;/li&gt;
&lt;li&gt;What aspects do we need to consider regarding data protection laws?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Agile Software Development&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What is your opinion on Scrum?&lt;/li&gt;
&lt;li&gt;What is your opinion on user stories?&lt;/li&gt;
&lt;li&gt;What is the most common impediment for you to be able to do your job?&lt;/li&gt;
&lt;li&gt;How do you estimate correctly?&lt;/li&gt;
&lt;li&gt;Which meetings do you consider necessary?&lt;/li&gt;
&lt;li&gt;How do we make sure to maximise customer value?&lt;/li&gt;
&lt;li&gt;What roles do you have in your optimal development team?&lt;/li&gt;
&lt;li&gt;What activities do you think a team should do to improve its delivery?&lt;/li&gt;
&lt;li&gt;The team will not be able to deliver in time. What is your proposed solution?&lt;/li&gt;
&lt;li&gt;What do you expect from a project manager?&lt;/li&gt;
&lt;li&gt;What does it mean to have a good team culture?&lt;/li&gt;
&lt;li&gt;How do you follow up on progress in a project?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Software Design&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What is your opinion on Microservices?&lt;/li&gt;
&lt;li&gt;What is your opinion on SOLID?&lt;/li&gt;
&lt;li&gt;What is your view on ORM frameworks?&lt;/li&gt;
&lt;li&gt;What does object oriented programming mean to you?&lt;/li&gt;
&lt;li&gt;In what capacity do you use functional programming?&lt;/li&gt;
&lt;li&gt;Where do you draw the line between refactoring and rewrite?&lt;/li&gt;
&lt;li&gt;Where do you draw the line between buying software or building it yourself?&lt;/li&gt;
&lt;li&gt;What is your perspective on “best practices”?&lt;/li&gt;
&lt;li&gt;How do you document your software design?&lt;/li&gt;
&lt;li&gt;What tools do you use to design your software?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Other&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What technological advancement do you consider a fad, and expect to be gone in 5 years?&lt;/li&gt;
&lt;li&gt;How do you think we’ll be writing software in the future?&lt;/li&gt;
&lt;li&gt;What will be the major challenges for our industry moving forward?&lt;/li&gt;
&lt;li&gt;What do you think will happen with Machine Learning and AI?&lt;/li&gt;
&lt;li&gt;What technology would you like to specialise in?&lt;/li&gt;
&lt;li&gt;What technologies would you like to learn to broaden your expertise?&lt;/li&gt;
&lt;li&gt;If you were not a software developer, what would you be doing instead?&lt;/li&gt;
&lt;li&gt;What is, your opinion, the worst mistake done in history of computing?&lt;/li&gt;
&lt;li&gt;What do you think is the most important traits/qualities for a developer to have?&lt;/li&gt;
&lt;li&gt;How do you contribute to a good team culture?&lt;/li&gt;
&lt;li&gt;What is required for you to feel satisfied with your work?&lt;/li&gt;
&lt;li&gt;What can we do to prioritise diversity and promote an inclusive environment?&lt;/li&gt;
&lt;li&gt;How do you learn new skills?&lt;/li&gt;
&lt;li&gt;What does ethical coding mean to you, and how do you write code responsibly?&lt;/li&gt;
&lt;li&gt;Does a software developer have to know how a computer works?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Please don’t ask your candidate all 100 questions :) and make sure that you ask non development related questions as well. Happy hiring!&lt;/p&gt;
</description>
				<pubDate>Fri, 16 Jul 2021 20:55:02 +0000</pubDate>
				<link>https://blog.mikaellundin.name/2021/07/16/one-hundred-interview-questions-for-software-developers.html</link>
				<guid isPermaLink="true">https://blog.mikaellundin.name/2021/07/16/one-hundred-interview-questions-for-software-developers.html</guid>
			</item>
		
			<item>
				<title>It&#39;s about time</title>
				<description>&lt;p&gt;My last two years at Fitness24Seven I&amp;#39;ve been working in the role of  Product Owner for our digital services. Never before have I worked with software projects where the product owner had software delivery experience. So I would like to share my thoughts on product ownership.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;It&amp;#39;s about time.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;You are the Superhero!&lt;/h2&gt;

&lt;p&gt;You have a magic abilily to create time. Nobody else in the team can do this. A good project manager can save time, without making too many tradeoffs, but the product owner can create time out of thin air.&lt;/p&gt;

&lt;p&gt;For a medium sized project of 3000 hours, there are lots of requirements and expectations. Your team will have to work 4 months to create the final product, but as you are aware lots of things may happen in 4 months. The only way to reduce the risk is to split the project into smaller parts.&lt;/p&gt;

&lt;p&gt;As a product owner you split the project into two projects of each 2000 hours. You have now created 1000 hours without increasing the amount of work. What happens when you split work into smaller pieces is that you discover that you didn&amp;#39;t know all the details before. You have discovered work that wasn&amp;#39;t accounted for but still expected to be done within the 3000 hours.&lt;/p&gt;

&lt;p&gt;A 2000 hour project is still a large project so you split it into two 1250 hour projects, and so it goes until you have found the smallest deliverable projects. You take all of your projects to the business people and ask them, &amp;quot;which one of these will provide most value to the business&amp;quot;, and then you start working on that.&lt;/p&gt;

&lt;p&gt;By reducing the size of the deliverable you have not only reduced the risk of the project, but also made sure that the value will be available to the business earlier, and you have created enough time for the team to make a good product. This is your superpower!&lt;/p&gt;

&lt;h2&gt;The smaller the better&lt;/h2&gt;

&lt;p&gt;Once you&amp;#39;ve found your appetite for dividing projects into smaller deployable things there is no end to it. You start splitting user stories into smaller deployable pieces in order to deliver sooner (not faster). The new functionality generates more value, as you get it to your users earlier.&lt;/p&gt;

&lt;p&gt;When we start splitting user stories we do not split it into frontend and backend parts. We split the work into complete vertical slices of functionality. Instead of building an e-shop we will start by delivering a product catalogue. Next we deliver the ability to buy and pick up at a service point. Next we deliver shipping option with a shipping partner.&lt;/p&gt;

&lt;p&gt;By developing and delivering in small chunks we also learn continuously what the users want and can change the prioritization from what we&amp;#39;ve learned. This improves the value output of your product as you do not have to guess what your customer wants. This would never have been possible with the 3000 hours of fixed scope.&lt;/p&gt;

&lt;p&gt;After a while you&amp;#39;ll find that projects are too big pieces to deliver at once. You start splitting it into even smaller parts to be deployed weekly or even daily. &amp;quot;Projects&amp;quot; turn into a bureaucracy for budget planning, a ceremonial to keep business people content, while your team deliver value in a continuous stream of functionality.&lt;/p&gt;

&lt;h2&gt;Scope it down&lt;/h2&gt;

&lt;p&gt;Your stakeholders will come with wishlists of functionality they want included in your product. The UX designer will create modern digital solutions that your users will love. The backend developers will design a system that will scale to 100 million users.&lt;/p&gt;

&lt;p&gt;It is your job to scope it down.&lt;/p&gt;

&lt;p&gt;Find the core functionality that will generate value. Remove all the extra finesse and tell your developers to make the easiest solution possible. You will deploy it within days and validate if it did generate expected value. You decide to add that extra UX cruft, deploy and find that users enjoy it. The feature starts to behave sluggish, so you ask your backend developers to make it work for 1000 users. They deploy and your users are happy.&lt;/p&gt;

&lt;p&gt;What I have found is when you&amp;#39;ve delivered the core functionality, there&amp;#39;s always something else the business wants you to focus on. Since you work in small chunks this is not a problem and you can change priorities in order to deliver as much value as possible.&lt;/p&gt;

&lt;p&gt;Sometimes the core functionality doesn&amp;#39;t feel like the feature is done. But it is good enough for now. If there is a need for it to improve you will get back to it later.&lt;/p&gt;

&lt;h2&gt;How to do this&lt;/h2&gt;

&lt;p&gt;There are a couple of things you need before you can start doing this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You need to make deployments trivial&lt;/li&gt;
&lt;li&gt;You need to get your development team in the boat with you&lt;/li&gt;
&lt;li&gt;The business needs to trust you&amp;#39;re doing the right thing&lt;/li&gt;
&lt;li&gt;You need to approach product ownership as a full time job&lt;/li&gt;
&lt;li&gt;You need to focus on quality and don&amp;#39;t let the team take shortcuts&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Reading list&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Lean Startup, Eric Ries &lt;/li&gt;
&lt;li&gt;Accelerate, Building and Scaling High Performing Technology Organizations by Nicole Forsgren PhD, Jez Humble and Gene Kim&lt;/li&gt;
&lt;li&gt;Get in the boat, Pat Bodin&lt;/li&gt;
&lt;/ul&gt;
</description>
				<pubDate>Thu, 27 May 2021 14:11:02 +0000</pubDate>
				<link>https://blog.mikaellundin.name/2021/05/27/its-about-time.html</link>
				<guid isPermaLink="true">https://blog.mikaellundin.name/2021/05/27/its-about-time.html</guid>
			</item>
		
			<item>
				<title>Ten Principles of Software Projects</title>
				<description>&lt;p&gt;I&amp;#39;ve been working for Fitness24Seven for 2,5 years developing custom software solutions. We have been running a lot of projects, both short and long ones and come to value a set of principles that help us create great deliveries.&lt;/p&gt;

&lt;p&gt;I was thinking I would share those with you.&lt;/p&gt;

&lt;h2&gt;1. We work in a continuous stream&lt;/h2&gt;

&lt;p&gt;When a task is done we release it. And then we bring in a new task. We still have iterations, but only for hosting demo and retrospective with regular intervals.&lt;/p&gt;

&lt;p&gt;We do not&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Work in what Scrum define as time boxed sprints&lt;/li&gt;
&lt;li&gt;Commit to a backlog that should be done at the end of the sprint&lt;/li&gt;
&lt;li&gt;Stress about imaginary deadlines at the end of the sprint&lt;/li&gt;
&lt;li&gt;Cancel work because it wasn&amp;#39;t done at the end of the sprint&lt;/li&gt;
&lt;li&gt;Create a sprint backlog that is closed for modifications during the sprint&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;2. We optimize for the throughput, not for utilization&lt;/h2&gt;

&lt;p&gt;We try to have as little parallel work as possible. We carefully manage our work in progress to make sure that all current tasks are actively being worked on.&lt;/p&gt;

&lt;p&gt;We do not&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bring in more work before we have completed something on the board&lt;/li&gt;
&lt;li&gt;Cancel a task that is being worked on, instead we finish it&lt;/li&gt;
&lt;li&gt;Bring in more work when a team member has spare time. We try to find ways for that team member to help out with the work on the board.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;3. We do not keep a backlog in the traditional sense&lt;/h2&gt;

&lt;p&gt;The most important task to work on is constantly changing. When we finish one task, we will pick the next task that is most valued by our stakeholders.&lt;/p&gt;

&lt;p&gt;We do not&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hoard user stories in a backlog, if they haven&amp;#39;t been picked up in 6 months we delete them&lt;/li&gt;
&lt;li&gt;Fret on old bugs. If nobody has fixed the bug after 6 months of reporting we delete them&lt;/li&gt;
&lt;li&gt;Spend time grooming our backlog. The Kondo Principle applies - if you do not love it, delete it&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;4. We work without estimates&lt;/h2&gt;

&lt;p&gt;Estimates take time from doing the work, it takes time from the project manager to follow up, and it provides very little value. If the product owner needs to know if Task A is more complex than Task B, he can just ask the team.&lt;/p&gt;

&lt;p&gt;We do not&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spend time in planning meetings&lt;/li&gt;
&lt;li&gt;Follow up on how much planned work is left on a task&lt;/li&gt;
&lt;li&gt;Measure or follow up on pace&lt;/li&gt;
&lt;li&gt;Keep a burn down chart&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;5. Our Project Manager is a servant of the team&lt;/h2&gt;

&lt;p&gt;It is the purpose of the project manager to clear a path so the team can work without impediments. Problems should be solved before they affect the work on progress.&lt;/p&gt;

&lt;p&gt;We do not&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Follow up on who works on what, as long as tasks are being completed&lt;/li&gt;
&lt;li&gt;Follow up on how many hours anyone has worked&lt;/li&gt;
&lt;li&gt;Measure the team by key performance indicators&lt;/li&gt;
&lt;li&gt;Keep people responsible for failures&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;6. We love planning, but do not follow a plan&lt;/h2&gt;

&lt;p&gt;In planning we do all the research we need to start a project, &lt;strong&gt;even estimates to figure out a reasonable budget&lt;/strong&gt;. When the project starts we throw away that plan because it is already outdated. What we bring with us into the project is the learnings from making the plan.&lt;/p&gt;

&lt;p&gt;We do not&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Follow up a project based on the original scope&lt;/li&gt;
&lt;li&gt;Bring estimates from budget planning into the project&lt;/li&gt;
&lt;li&gt;Make promises to the business that we cannot keep&lt;/li&gt;
&lt;li&gt;Let an outdated plan affect our decisions in the project&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;7. We build our team on trust, not control&lt;/h2&gt;

&lt;p&gt;We assume that everyone is here to do their best. That is why we do not ask what you did yesterday, but rather what has happened with this task since yesterday. What the team members did yesterday is irrelevant.&lt;/p&gt;

&lt;p&gt;We do not&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Measure how our team members perform&lt;/li&gt;
&lt;li&gt;Follow up on how much time has been spent on a task&lt;/li&gt;
&lt;li&gt;Require team members to be glued to their keyboard&lt;/li&gt;
&lt;li&gt;Ask anyone to stay late to finish their work&lt;/li&gt;
&lt;li&gt;Crunch, ever&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;8. We help each other out, even if it&amp;#39;s not our job&lt;/h2&gt;

&lt;p&gt;Most important is that tasks are progressing, not that a frontend developer only do frontend code or a tester only do testing. A backend developer can create a solution architecture and a frontend developer can review the backend developer&amp;#39;s code by just saying &amp;quot;explain it to me&amp;quot;. &lt;/p&gt;

&lt;p&gt;We do not&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Claim ownership of our role or our tasks&lt;/li&gt;
&lt;li&gt;Let tasks sit dormant waiting for an individual to be free&lt;/li&gt;
&lt;li&gt;Communicate in silos&lt;/li&gt;
&lt;li&gt;Let prestige stop us from doing what&amp;#39;s best for the project&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;9. We design solutions just in time for implementation&lt;/h2&gt;

&lt;p&gt;There is a short due date to design. That is why we do our requirements gathering, graphical design, solution architecture first when it&amp;#39;s time to start working on the task. &lt;/p&gt;

&lt;p&gt;We do not&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start designing until we know that the task will be worked on&lt;/li&gt;
&lt;li&gt;Do technical design or investigations before the work has started&lt;/li&gt;
&lt;li&gt;Groom the backlog with requirement gathering&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;10. We always stay excellent towards each other&lt;/h2&gt;

&lt;p&gt;Personality traits are #1 priority when we bring in new employees or consultants to the team. If we find people that we like, the work will be much more fun and easy going. The technologies are always something you can learn on the job if you have the right attitude.&lt;/p&gt;

&lt;p&gt;We do not&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Require people to be 100% effective on day one&lt;/li&gt;
&lt;li&gt;Hire people based on their technical prowess&lt;/li&gt;
&lt;li&gt;Care about titles&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most important traits are care, compassion, humbleness and the ability to be excellent towards your team mates.&lt;/p&gt;
</description>
				<pubDate>Thu, 20 May 2021 14:11:02 +0000</pubDate>
				<link>https://blog.mikaellundin.name/2021/05/20/ten-principles-of-software-projects.html</link>
				<guid isPermaLink="true">https://blog.mikaellundin.name/2021/05/20/ten-principles-of-software-projects.html</guid>
			</item>
		
			<item>
				<title>Development Methods</title>
				<description>&lt;p&gt;I&amp;#39;m a TDD practitioner, in the sense that I write my tests first. I am a test first developer, a red-green-refactor cultist. I&amp;#39;m very open about it and it offends people. They want to discuss tests with me, tell me that they&amp;#39;ve tried this unit test thing and it doesn&amp;#39;t work, and they want me to convince them that unit testing is the only way so they can complain later about my dogmatism.&lt;/p&gt;

&lt;p&gt;It&amp;#39;s just like being a vegetarian.&lt;/p&gt;

&lt;p&gt;But this was not intended to be a rant. Instead an observation that people seem to think I practice TDD because of testing, while I see unit tests as a byproduct of the way I work. The same people look horrified when I tell them to delete tests that doesn&amp;#39;t work. The purpose of TDD is not to build a test suite. It is to provide a method of developing software.&lt;/p&gt;

&lt;p&gt;Starting from a blank IDE. How do you start implementing a feature? What is your method to go from nothing to something?&lt;/p&gt;

&lt;p&gt;In TDD we start with a test, or rather, we start with thinking.
- What do I want this code to do?
- Then I implement a test that verify it
- Then I implement code to satisfy the test
- Then I refactor
- And then I start thinking again what I want this code to do.&lt;/p&gt;

&lt;p&gt;It is a development method that provides very well thought programs. And it provides a test suite. But if you tell me that the test suite cost more to maintain than the value we get through regression, I will gladly delete it. But I will add a new test again for the next feature that is being developed.&lt;/p&gt;

&lt;p&gt;This is the development method that I prefer. How do other people do it? How do they go from zero to something? &lt;/p&gt;

&lt;p&gt;Most people I asked this question to doesn&amp;#39;t know how they do it, they can&amp;#39;t articulate it. But if you sit and observe what they do, you start to notice that they do have a method. Here are some methods that I&amp;#39;ve observed.&lt;/p&gt;

&lt;h2&gt;Design Up Front Development&lt;/h2&gt;

&lt;p&gt;I once were in a team that made every piece of code in UML before they started writing it. There was an architect that would draft out sequence diagrams and hand over to the developers. The developers would then take a look at the sequence diagram and base their implementation on that.&lt;/p&gt;

&lt;p&gt;There was no requirement that the sequence diagram had to be implemented exactly as specified in the UML to code. Most often the coder would find things the sequence diagram was missing and fill in the blanks. The sequence diagram was never treated as a source of truth. After the implementation it was pretty much thrown away.&lt;/p&gt;

&lt;p&gt;The UML was just a way to think about the problem before putting it on paper, in code. Big Design Up Front has been so criticised in the industry for a long time, but this way of communicating the problem and the solution worked very well.&lt;/p&gt;

&lt;h1&gt;Top Down Development&lt;/h1&gt;

&lt;p&gt;Developers that are very UI oriented tend to use expressive programming like HTML to implement the user interface first. After they have the inputs, the buttons, the navigation structure, they start working themselves down to the code that is going to provide the data. I call this top-down-development. You start from the UI and the interactions and you write code to satisfy it. Adobe Flash was the pinnacle of Top Down Development.&lt;/p&gt;

&lt;p&gt;Solutions that are implemented top-down are always very user oriented, and they often run into trouble where they do not consider how the backing services expect data to be managed. You&amp;#39;ll have beautifully implemented frontends that are slow, unmaintainable because of the lacking backends.&lt;/p&gt;

&lt;h2&gt;Bottom Up Development&lt;/h2&gt;

&lt;p&gt;You tell a bottom-up developer to integrate the e-commerce solution with a payment provider. He will study the e-commerce interface in detail and he will map that exactly onto the payment provider so it fits in a beautiful way. However he will never along the way ask how it&amp;#39;s supposed to be used.&lt;/p&gt;

&lt;p&gt;This is when you get UIs that looks like a Google Form on top of a Microsoft Access database. All the fields are there and you can almost sense the database table structure from the way that input fields are positioned.&lt;/p&gt;

&lt;p&gt;Combining a top-down developer with a bottom-up developer is of course a very good idea. A lot of interesting things will happen when they try to interface to one another.&lt;/p&gt;

&lt;h2&gt;Code Fix Development&lt;/h2&gt;

&lt;p&gt;One of the most common way of developing software I see when I meet other developers is the code-fix development. They rarely realise they are practicing a development method but they do.&lt;/p&gt;

&lt;p&gt;First they write up a draft of a program that they think will work. Then they run it. The program will most often crash, or not do what it was supposed to do, so they fix the problem and run it again. The program will fail on a different thing and they will fix it again. This will go on until they are satisfied with the result.&lt;/p&gt;

&lt;p&gt;Depending on how much thinking is involved in this process, you will get different results. This is what managers mean when they say &lt;em&gt;good developers&lt;/em&gt; and &lt;em&gt;bad developers&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I think we can agree that this way of developing software by trial and error is not a very good one, but it is incredibly common. There will always be issues left with the code that reaches production. I get the sense that the code is never quite &lt;em&gt;done&lt;/em&gt;. There is always one more fix needed.&lt;/p&gt;

&lt;h2&gt;Debug Driven Development&lt;/h2&gt;

&lt;p&gt;In .NET the debug tools has become so powerful that you can use them all the time, and it is very common for .NET developers to write code with the debugger. It works like this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Write some code&lt;/li&gt;
&lt;li&gt;Debug it and make sure it did what it was supposed to do&lt;/li&gt;
&lt;li&gt;Rinse and repeat&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is a much more thorough development method than code-fix development, because you actually stop and analyse what your code does before you try to run it, but it is also a very slow process, and neither does it provide a step for thinking or refactoring your code.&lt;/p&gt;

&lt;p&gt;The risk that the debug-driven developer run into is that they feel they need to refactor in the middle of debugging, so they start refactor the code before they&amp;#39;ve finished what they were doing. Feature changes and refactoring intermingles in an uncontrolled way, which is often obvious looking at the commit history.&lt;/p&gt;

&lt;p&gt;I know many developers that write code this way and it works well for small solutions, but as soon as the code grows big it will have tons of technical debt because thinking is not a required step and neither is refactoring.&lt;/p&gt;

&lt;h1&gt;Summary&lt;/h1&gt;

&lt;p&gt;These are the development methods that I&amp;#39;ve observed in the wild. What development methods do you practice or observed?&lt;/p&gt;
</description>
				<pubDate>Mon, 07 Jan 2019 19:57:02 +0000</pubDate>
				<link>https://blog.mikaellundin.name/2019/01/07/development-methods.html</link>
				<guid isPermaLink="true">https://blog.mikaellundin.name/2019/01/07/development-methods.html</guid>
			</item>
		
			<item>
				<title>Static Generated Websites are Shit</title>
				<description>&lt;p&gt;Maybe I missed a &amp;#39;the&amp;#39; in the title, maybe I didn&amp;#39;t.&lt;/p&gt;

&lt;p&gt;My blog is old. It dates back to 2007 when I started it on Wordpress. One great thing about a blog platform like Wordpress is that even if the CMS platform will age and become harder and harder to update, the application will keep on working. You will still be able to update with content even if your PHP is 3 major versions behind.&lt;/p&gt;

&lt;p&gt;I moved off Wordpress because the platform didn&amp;#39;t feel secure. It kept nagging me about updates and it was hard to make changes the way I wanted to.&lt;/p&gt;

&lt;p&gt;So I moved the whole thing over to Orchard CMS. That was a fun project because I was learning a new CMS and it was my first adventure on Azure. However it turned out that Azure was way too expensive for my small little blog, and Orchard CMS was a pain to keep updated.&lt;/p&gt;

&lt;p&gt;My last move to Jekyll was great! Finally! No database, only markdown files. However similiar problems started to appear a year after the initial release. I had reinstalled my computer and didn&amp;#39;t have the whole Jekyll setup. I found out the painful way that finding and installing old Jekyll dependencies was a real pain. It would take a whole day to setup an environment to start blogging, every time I would reinstall my system. So for long periods of time had no setup where I was able to update the blog.&lt;/p&gt;

&lt;p&gt;It struck me that I need to script the setup. I don&amp;#39;t want a blog virtual machine. I want to be able to write blog posts on both of my Macs, but I really don&amp;#39;t want to install Ruby on any of them. So I concluded that I need to create a Docker container that have all the dependencies to generate my blog.&lt;/p&gt;

&lt;p&gt;It did take a week, but here is my Dockerfile.&lt;/p&gt;

&lt;p&gt;&lt;noscript&gt;&lt;pre&gt;# Create a container for blog.mikaellundin.name
FROM ubuntu:14.04
MAINTAINER Mikael Lundin &amp;lt;hello@mikaellundin.name&amp;gt;&lt;/p&gt;

&lt;h1&gt;Install prerequisites&lt;/h1&gt;

&lt;p&gt;RUN apt-get update &amp;amp;&amp;amp; apt-get install -y git git-core curl zlib1g-dev libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev libgdbm-dev libncurses5-dev automake libtool bison libffi-dev libgmp-dev openjdk-7-jre&lt;/p&gt;

&lt;h1&gt;Setup prerequisites for nodejs installatino&lt;/h1&gt;

&lt;p&gt;RUN curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
RUN apt-get install -y nodejs build-essential &lt;/p&gt;

&lt;h1&gt;install ruby 2&lt;/h1&gt;

&lt;p&gt;RUN gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
RUN curl -L https://get.rvm.io | bash -s stable
ENV PATH /usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
RUN /bin/bash -l -c &amp;quot;rvm requirements&amp;quot;
RUN /bin/bash -l -c &amp;quot;rvm install 2.2.5&amp;quot;
RUN /bin/bash -l -c &amp;quot;rvm use 2.2.5 --default&amp;quot;&lt;/p&gt;

&lt;h1&gt;Install jekyll and redcarpet&lt;/h1&gt;

&lt;p&gt;RUN /bin/bash -l -c &amp;quot;gem install jekyll jekyll-gist jekyll-sitemap mustache pygments.rb rake redcarpet s3_website --no-ri --no-rdoc&amp;quot;
RUN /bin/bash -l -c &amp;quot;gem install sass -v 3.4.21 --no-ri --no-rdoc&amp;quot;&lt;/p&gt;

&lt;h1&gt;Install bower and gulp globally&lt;/h1&gt;

&lt;p&gt;RUN npm config set strict-ssl false
RUN npm config set global false
RUN npm config set registry=&amp;quot;http://registry.npmjs.org/&amp;quot;
RUN npm install --global bower gulp-cli&lt;/p&gt;

&lt;h1&gt;Use local copy of the project, instead of cloning it from github&lt;/h1&gt;

&lt;p&gt;COPY . /var/www&lt;/p&gt;

&lt;h1&gt;Set current working directory&lt;/h1&gt;

&lt;p&gt;WORKDIR /var/www&lt;/p&gt;

&lt;h1&gt;Download frontend dependencies&lt;/h1&gt;

&lt;p&gt;RUN bower install --config.interactive=false --allow-root&lt;/p&gt;

&lt;h1&gt;Download gulp dependencies&lt;/h1&gt;

&lt;p&gt;RUN /bin/bash -l -c &amp;quot;npm install --only=dev&amp;quot;&lt;/p&gt;

&lt;h1&gt;Build static assets, once&lt;/h1&gt;

&lt;p&gt;RUN /bin/bash -l -c &amp;quot;gulp&amp;quot;&lt;/p&gt;

&lt;h1&gt;Install all jekyll dependencies&lt;/h1&gt;

&lt;p&gt;RUN /bin/bash -l -c &amp;quot;bundle install&amp;quot;&lt;/p&gt;

&lt;h1&gt;Make a volume for the code&lt;/h1&gt;

&lt;p&gt;VOLUME /var/www&lt;/p&gt;

&lt;h1&gt;open port 4000 for http&lt;/h1&gt;

&lt;p&gt;EXPOSE 4000&lt;/pre&gt;&lt;/noscript&gt;&lt;script src=&quot;https://gist.github.com/miklund/3d5511daecfefb1bbccd85430b5c39ba.js?file=Dockerfile&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;All this crap are things that I would need to figure out every time I&amp;#39;d setup a new environment for my blog. Now I can just run the following command &lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;docker build -t miklund/tailcalloptimized:1.0 .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;..and I have a complete environment for developing, writing and deploying. That is pretty neat.&lt;/p&gt;

&lt;h2&gt;Cloudfare&lt;/h2&gt;

&lt;p&gt;Another thing that has happened during my absense is that Google&amp;#39;s now punishing you in the search result for hosting over HTTP.&lt;/p&gt;

&lt;p&gt;I&amp;#39;m all for security, but I have not prioritized hosting my static content over HTTPS because there is no real threat here. The only plausable thing I can see happening is that a router infected with malware could inject links/ads into my content. That might concern me if my blog had thousands of visits per month, or representing a legitimate business, but not really for my rants about javascript.&lt;/p&gt;

&lt;p&gt;Anyway, being punished in the search result for hosting over HTTP does concern me, but at the time of setting up my static blog the only option was jumping through a lot of hoops at Amazon which would require an investment that wasn&amp;#39;t worth it by a long shot.&lt;/p&gt;

&lt;p&gt;Today &lt;a href=&quot;https://www.cloudflare.com/&quot;&gt;Cloudfare&lt;/a&gt; is offering a service for websites where they proxy your content and serve it over HTTPS. That is pretty great, and since I don&amp;#39;t have any high requirements I get away with their free plan (but I would be willing to pay $5 for their service if they start charging).&lt;/p&gt;

&lt;h2&gt;Google Analytics&lt;/h2&gt;

&lt;p&gt;When I created the Jekyll blog I added Google Analytics. Why do you add GA to a site? Well, to get some insight on how many users you have.&lt;/p&gt;

&lt;p&gt;In the passing years Google has become more and more evil. They collect all information they can about every person on the internet in order to target advertisement and sell the information to the highest bidder. It is services like Google Analytics that allow them follow and collect everything you do around the internet. I don&amp;#39;t want to be a part of that problem, so I&amp;#39;m removing Google Analytics from my site.&lt;/p&gt;

&lt;p&gt;I&amp;#39;d rather be blind on my site&amp;#39;s statistics, than intruding you my visitors&amp;#39; privacy.&lt;/p&gt;

&lt;p&gt;To protect yourself on the internet, remember to&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Browse Twitter, Google, Facebook in incognito mode&lt;/li&gt;
&lt;li&gt;Regularly clear browser history/cache/cookies&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mullvad.net&quot;&gt;Use a VPN&lt;/a&gt;, always&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.getfirefox.com&quot;&gt;Don&amp;#39;t use Google Chrome&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Search the internet with an &lt;a href=&quot;https://www.duckduckgo.com&quot;&gt;engine that doesn&amp;#39;t track you&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cheers!&lt;/p&gt;
</description>
				<pubDate>Thu, 06 Sep 2018 05:34:35 +0000</pubDate>
				<link>https://blog.mikaellundin.name/2018/09/06/static-generated-websites-are-shit.html</link>
				<guid isPermaLink="true">https://blog.mikaellundin.name/2018/09/06/static-generated-websites-are-shit.html</guid>
			</item>
		
			<item>
				<title>Recursively Search and Update a JSON/JavaScript Object</title>
				<description>&lt;p&gt;Hello my blog. Sorry I haven&amp;#39;t updated you in a while, but I&amp;#39;ve been busy. However it seems like I have a reason to start writing again, so here we go.&lt;/p&gt;

&lt;p&gt;The other day I came across the following problem.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I have a JSON export of a data schema from inRiver, and I have a CSV file with translations for each property in inRiver. The only thing I need to do is to update the JSON export with the values from the CSV file.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Easy! Or not so easy. I quickly figured out that C# was not the language for this task. I would be better off using a dynamic language like javascript.&lt;/p&gt;

&lt;p&gt;The gist here is not about reading or parsing CSV or JSON, but rather how to search and update a big javascript object. If it were XML, &lt;a href=&quot;/2008/12/15/xml-future-of-the-past.html&quot;&gt;I would just have used XSLT&lt;/a&gt; and be done in a snap.&lt;/p&gt;

&lt;p&gt;Let&amp;#39;s look at the end result.&lt;/p&gt;

&lt;p&gt;&lt;noscript&gt;&lt;pre&gt;// findObjectById: helper function
// comment: will recurse through an object graph, looking for matching Id&amp;#39;s and run the update function
const findObjectById = (obj, id, updateFn) =&amp;gt; {
  // have found the object
  if (obj[&amp;quot;Id&amp;quot;] === id) {
    return updateFn(obj);
  }
  else {
    // iterate over the properties
    for (var propertyName in obj) {
      // any object that is not a simple value
      if (obj[propertyName] !== null &amp;amp;&amp;amp; typeof obj[propertyName] === &amp;#39;object&amp;#39;) {
        // recurse into the object and write back the result to the object graph
        obj[propertyName] = findObjectById(obj[propertyName], id, updateFn);
      }
    }
  }
  return obj;
};&lt;/pre&gt;&lt;/noscript&gt;&lt;script src=&quot;https://gist.github.com/miklund/4def343a5938c13c9af4d7bb353857c0.js?file=update.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;This code will recurse through an object graph, looking for an object with matching ID property. If it finds it, it will run the update function on that object.&lt;/p&gt;

&lt;p&gt;The important thing to notice in this code is that javascript is always copy by value. We cannot just recurse down to the part we want to change and update it, because then we would update a copy of the object. Instead we need to make sure that we write back each recursion to the object graph to get the change in the end.&lt;/p&gt;

&lt;p&gt;This is how we use it!&lt;/p&gt;

&lt;p&gt;&lt;noscript&gt;&lt;pre&gt;var animals = {
    anything {
        ID = &amp;#39;123&amp;#39;,
        Name = {
            &amp;#39;en&amp;#39;: &amp;#39;Phoney Pony&amp;#39;
        }
    }
};&lt;/p&gt;

&lt;p&gt;var updateFn = obj =&amp;gt; {
    obj.Name[&amp;#39;sv-SE&amp;#39;] = &amp;#39;Fånig ponny&amp;#39;
};&lt;/p&gt;

&lt;p&gt;var result = findObjectById(animals, &amp;#39;123&amp;#39;, updateFn);&lt;/p&gt;

&lt;p&gt;// the result will look like this
// var animals = {
//     anything {
//         ID = &amp;#39;123&amp;#39;,
//         Name = {
//             &amp;#39;en&amp;#39;: &amp;#39;Phoney Pony&amp;#39;,
//             &amp;#39;sv-SE&amp;#39;: &amp;#39;Fånig ponny&amp;#39;
//         }
//     }
// };
&lt;/pre&gt;&lt;/noscript&gt;&lt;script src=&quot;https://gist.github.com/miklund/4def343a5938c13c9af4d7bb353857c0.js?file=usage.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;
</description>
				<pubDate>Sun, 02 Sep 2018 12:15:32 +0000</pubDate>
				<link>https://blog.mikaellundin.name/2018/09/02/recursively-search-and-update-a-json-javascript-object.html</link>
				<guid isPermaLink="true">https://blog.mikaellundin.name/2018/09/02/recursively-search-and-update-a-json-javascript-object.html</guid>
			</item>
		
			<item>
				<title>Creating a WebAssembly binary and running it in a browser</title>
				<description>&lt;p&gt;&lt;a href=&quot;https://github.com/WebAssembly/&quot;&gt;WebAssembly&lt;/a&gt;, is the promise of an intermediate language of the web, or &lt;a href=&quot;http://www.hanselman.com/blog/JavaScriptIsAssemblyLanguageForTheWebSematicMarkupIsDeadCleanVsMachinecodedHTML.aspx&quot; title=&quot;Scott Hanselman - JavaScript is Assembly Language for the Web: Sematic Markup is Dead! Clean vs. Machine-coded HTML&quot;&gt;JavaScript as an Assembly langauge&lt;/a&gt; if you want to. It really came from the AsmJS initiative, where a static subset of javascript was found to run much much faster than the full dynamic language. WebAssembly is the next iteration of that where a new assembler language has been designed to run on top of the javascript engine. This comes with a couple of promises&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It will run faster than ordinary JavaScript&lt;/li&gt;
&lt;li&gt;It will load faster than AsmJS because it doesn&amp;#39;t have to be interpreted from text&lt;/li&gt;
&lt;li&gt;It has a binary format that will be smaller than anything text based&lt;/li&gt;
&lt;li&gt;It will, in the future, also be debuggable in the browser&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The most uses we have seen of asmjs so far has been compiling C/C++ into asmjs in order to run these in the browser. We have all seen the Unity 3D demos and been impressed. For me, WebAssembly is the assembler language of the web, and this provides a unique opportunity to create new languages that will target the web and compile to a real IL language, instead of compiling to the less efficient javascript.&lt;/p&gt;

&lt;p&gt;This blog post will take a look at the binary format of WebAssembly, how to read it, write it and run it.&lt;/p&gt;

&lt;h2&gt;WebAssembly text syntax&lt;/h2&gt;

&lt;p&gt;In order to better understand the WebAssembly (from now on called wasm) we&amp;#39;re going to take a look at the sample module for this blog post.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;(module
  (type $0 (func (param i32 i32) (result i32)))
  (export &quot;add&quot; $add)
  (func $add (type $0) (param $var$0 i32) (param $var$1 i32) (result i32)
    (i32.add
      (get_local $var$0)
      (get_local $var$1)
    )
  )
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This looks a little bit like LISP syntax. It is the &lt;a href=&quot;https://github.com/WebAssembly/design/blob/master/TextFormat.md&quot;&gt;textual representation&lt;/a&gt; of the module in an &lt;a href=&quot;https://en.wikipedia.org/wiki/S-expression&quot;&gt;s-expression&lt;/a&gt; syntax. The module exports one function called add, that will add two integer numbers together.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;type&lt;/strong&gt; section contains any function definition that are used in our module. It will be referenced to later.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;export&lt;/strong&gt; section contains any functions that should be exported, and able to be called from outside this module.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;code&lt;/strong&gt; section starts with a function declaration. This is the declaration of our function &amp;quot;add&amp;quot;. It will only accept types of i32 (32 bit integer) and will add them together by the operator i32.add.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;WAsm only has 4 types.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;i32 (32 bit integer)&lt;/li&gt;
&lt;li&gt;i64 (64 bit integer)&lt;/li&gt;
&lt;li&gt;f32 (32 bit float)&lt;/li&gt;
&lt;li&gt;f64 (64 bit float)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Any other type will need to be constructed artificially by reading and writing raw memory, but this is out of scope for this blog post.&lt;/p&gt;

&lt;h2&gt;Reading the Wasm binary format&lt;/h2&gt;

&lt;p&gt;By going through the &lt;a href=&quot;https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md&quot;&gt;specification of the binary format&lt;/a&gt; I have written a wasm binary from scratch. You don&amp;#39;t have to do this, there are excellent libraries like &lt;a href=&quot;https://github.com/WebAssembly/binaryen&quot;&gt;binaryen&lt;/a&gt; that can help you transform a S-expression syntax directly to wasm binary. However, I wanted to do this from javascript and in that the tools are scarse.&lt;/p&gt;

&lt;p&gt;Here&amp;#39;s our program.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;00000000  00 61 73 6d 0b 00 00 00  04 74 79 70 65 87 80 80  |.asm.....type...|
00000010  80 00 01 40 02 01 01 01  01 08 66 75 6e 63 74 69  |...@......functi|
00000020  6f 6e 82 80 80 80 00 01  00 06 6d 65 6d 6f 72 79  |on........memory|
00000030  85 80 80 80 00 80 02 80  02 01 06 65 78 70 6f 72  |...........expor|
00000040  74 86 80 80 80 00 01 00  03 61 64 64 04 63 6f 64  |t........add.cod|
00000050  65 8c 80 80 80 00 01 86  80 80 80 00 00 14 00 14  |e...............|
00000060  01 40 04 6e 61 6d 65 86  80 80 80 00 01 03 61 64  |.@.name.......ad|
00000070  64 00                                             |d.|
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It has a preamble and 6  sections as we can see in the clear text representation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;preamble&lt;/li&gt;
&lt;li&gt;type&lt;/li&gt;
&lt;li&gt;function&lt;/li&gt;
&lt;li&gt;memory&lt;/li&gt;
&lt;li&gt;export&lt;/li&gt;
&lt;li&gt;code&lt;/li&gt;
&lt;li&gt;name&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each section starts with the name of that section and a count on how many bytes long the section is in total.&lt;/p&gt;

&lt;h3&gt;Preamble&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/assets/posts/2016-06-19-creating-a-webassembly-binary-and-running-it-in-a-browser/preamble.png&quot; alt=&quot;wasm preamble&quot;&gt;&lt;/p&gt;

&lt;p&gt;The preamble is the first 8 bytes of the file. The first 4 bytes is a uint32 &amp;#39;magic number&amp;#39; &lt;code&gt;0x6d736100&lt;/code&gt; that spells out &amp;#39;\0asm&amp;#39; in ASCII. The point of this magic number is to quickly determine if this file is a wasm module or not.&lt;/p&gt;

&lt;p&gt;The next four bytes is a uint32 number that represents the version of the wasm specification this file was constructed with. In this case it is version 11, or 0x0000000b in hex.&lt;/p&gt;

&lt;h3&gt;Type section&lt;/h3&gt;

&lt;p&gt;All sections are optional, so when they appear they need to be correctly labeled. We start with the type section.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/posts/2016-06-19-creating-a-webassembly-binary-and-running-it-in-a-browser/type.png&quot; alt=&quot;wasm type section&quot;&gt;&lt;/p&gt;

&lt;p&gt;The first byte 0x04 tells the intepreter that the name of this section is the next 4 bytes. So &lt;code&gt;0x74 0x79 0x70 0x65&lt;/code&gt; are not surprisingly the ASCII codes for the word &lt;em&gt;type&lt;/em&gt;. Following that is a varuint32 telling how many bytes this section is, and by some reason I haven&amp;#39;t figured out, (maybe because I&amp;#39;m reading the specs for version 10 and this file format is version 11) this varuint32 in &lt;a href=&quot;https://en.wikipedia.org/wiki/LEB128&quot;&gt;LEB128&lt;/a&gt; format is padded to all 4 bytes and an extra byte for the sign &lt;code&gt;0x0080808087&lt;/code&gt;. The translation
of this to pure English is that the type section is 7 bytes long.&lt;/p&gt;

&lt;p&gt;The type section is defined as follows&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;0x01&lt;/code&gt; There is one type defined in this section&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x40&lt;/code&gt; This is a function type that is defined&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x02&lt;/code&gt; The function type has two parameters&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x01&lt;/code&gt; First parameter is of type i32&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x01&lt;/code&gt; Second parameter is of type i32&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x01&lt;/code&gt; The function returns a result&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x01&lt;/code&gt; The result is of type i32&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What we&amp;#39;re doing here is registering the function signature of &lt;code&gt;i32 add(arg1 : i32, arg2 : i32)&lt;/code&gt; in the type section of the wasm binary.&lt;/p&gt;

&lt;p&gt;This section is now complete and the next section follows directly there after.&lt;/p&gt;

&lt;h3&gt;Function section&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/assets/posts/2016-06-19-creating-a-webassembly-binary-and-running-it-in-a-browser/function.png&quot; alt=&quot;wasm function section&quot;&gt;&lt;/p&gt;

&lt;p&gt;Again the starting &lt;code&gt;0x08&lt;/code&gt; tells us the identifier of this section is 8 characters and the following bytes &lt;code&gt;0x66 0x75 0x6e 0x63 0x74 0x69 0x6f 0x6e&lt;/code&gt; spells out the word &lt;em&gt;function&lt;/em&gt;. The following varuint32 value &lt;code&gt;0x0080808082&lt;/code&gt; tells us that this section is two bytes long.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;0x01&lt;/code&gt; There is one function we want to aknowledge.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x00&lt;/code&gt; The function of interest has index 0 in the type section.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That&amp;#39;s it. We point out the function signature from the type section.&lt;/p&gt;

&lt;h3&gt;Memory section&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/assets/posts/2016-06-19-creating-a-webassembly-binary-and-running-it-in-a-browser/memory.png&quot; alt=&quot;wasm memory section&quot;&gt;&lt;/p&gt;

&lt;p&gt;The memory section starts with &lt;code&gt;0x06&lt;/code&gt; that specifies that this section&amp;#39;s identifier is the next six characters &lt;code&gt;0x06 0x6d 0x65 0x6d 0x6f 0x72 0x79&lt;/code&gt;. Those spell out the name &lt;em&gt;memory&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The following varuint32 value &lt;code&gt;0x0080808085&lt;/code&gt; tells us that this section is 5 bytes long.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;0x0280&lt;/code&gt; Tells the intepreter that the initial size of memory allocated by this module should be 256 pages of 64KiB memory. That is 16 MB memory.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x0280&lt;/code&gt; Tells the intepreter that the maximun size of memory this module is allowed to allocate is 256 pages of 64KiB memory.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x01&lt;/code&gt; Specifies that this memory is exported and visible outside the module.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I&amp;#39;m not so sure about this section and I need to play around with it more to find out the implications of the different settings here. These settings were just something I came up with for default until I&amp;#39;ve figured it out.&lt;/p&gt;

&lt;h3&gt;Export section&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/assets/posts/2016-06-19-creating-a-webassembly-binary-and-running-it-in-a-browser/export.png&quot; alt=&quot;wasm export section&quot;&gt;&lt;/p&gt;

&lt;p&gt;The export section starts with &lt;code&gt;0x06&lt;/code&gt; that specifies that the following identifier is six characters long &lt;code&gt;0x65 0x78 0x70 0x6f 0x72 0x74&lt;/code&gt; spelling out &lt;em&gt;export&lt;/em&gt;. The following varuint32 value &lt;code&gt;0x0080808086&lt;/code&gt; tells us that this section is 6 bytes long.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;0x01&lt;/code&gt; Specifies how many exports there will be&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x00&lt;/code&gt; Specifies the index of the first export in the function table&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x03&lt;/code&gt; The identifier of the exported function is 3 bytes long&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x61 0x64 0x64&lt;/code&gt; Is the name of the exported function in ASCII, meaning &lt;em&gt;add&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This section makes the add function visible outside this module by exporting it. It is through this exported interface that we will call the add function from javascript.&lt;/p&gt;

&lt;h3&gt;Code section&lt;/h3&gt;

&lt;p&gt;This is the fun section.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/posts/2016-06-19-creating-a-webassembly-binary-and-running-it-in-a-browser/code.png&quot; alt=&quot;wasm code section&quot;&gt;&lt;/p&gt;

&lt;p&gt;The code section starts with &lt;code&gt;0x04&lt;/code&gt; that specifies that the identifier for this section is four characters &lt;code&gt;0x63 0x6f 0x64 0x65&lt;/code&gt; and it spells out &lt;em&gt;code&lt;/em&gt;. The following varuint32 value &lt;code&gt;0x008080808c&lt;/code&gt; tells us that this section is 12 bytes long.&lt;/p&gt;

&lt;p&gt;It might be good to revisit that AST of our program now.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;(module
  (type $0 (func (param i32 i32) (result i32)))
  (export &quot;add&quot; $add)
  (func $add (type $0) (param $var$0 i32) (param $var$1 i32) (result i32)
    (i32.add
      (get_local $var$0)
      (get_local $var$1)
    )
  )
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Everything except the body of the $add function has already been written down. The code section will contain the body part. Each function call has its own &lt;a href=&quot;&quot;&gt;HEX opcode&lt;/a&gt;, that you&amp;#39;ll be aware of. But most importantly, this is binary coded post-order, so that first the left node is written, then right now, and last the parent node.&lt;/p&gt;

&lt;p&gt;Let&amp;#39;s take a look&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;0x01&lt;/code&gt; There is one function body.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x0080808086&lt;/code&gt; This first function body is 6 bytes long&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x00&lt;/code&gt; There are 0 local variables&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x14&lt;/code&gt; The opcode for the left get_local call&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x00&lt;/code&gt; Get the 0 index parameter to this function&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x14&lt;/code&gt; The opcode for the right get_local call&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x01&lt;/code&gt; Get the 1 index parameter to this function&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x40&lt;/code&gt; The opcode for i32.add&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you can see the code is almost written backward. This is the effect of rendering the AST post-order.&lt;/p&gt;

&lt;h3&gt;Name section&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/assets/posts/2016-06-19-creating-a-webassembly-binary-and-running-it-in-a-browser/name.png&quot; alt=&quot;wasm name section&quot;&gt;&lt;/p&gt;

&lt;p&gt;The name section starts with &lt;code&gt;0x04&lt;/code&gt; indicating that the identifier is 4 characters long, namely &lt;code&gt;0x6e 0x61 0x6d 0x65&lt;/code&gt; which is the word &lt;em&gt;name&lt;/em&gt; translated from ASCII.&lt;/p&gt;

&lt;p&gt;The following varuint32 value &lt;code&gt;0x0080808086&lt;/code&gt; specify that the length of this section is 6 bytes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;0x01&lt;/code&gt; There is 1 name entry&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x03&lt;/code&gt; The first name is 3 characters long&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x61 0x64 0x64&lt;/code&gt; The name &lt;em&gt;add&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0x00&lt;/code&gt; There are 0 local names defined&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I&amp;#39;m not really sure what a local name is, but I guess I will find out. And with this we have written all 114 bytes that conclude our simple wasm module.&lt;/p&gt;

&lt;h2&gt;Writing a WebAssembly binary module&lt;/h2&gt;

&lt;p&gt;I did not write this WebAssembly by hand in an HEX editor. I wrote me some javascript to generate the code for me, but before I did that I took a look at what data types are used in this format.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;uint8 - single-byte unsigned integer&lt;/li&gt;
&lt;li&gt;uint32 - four-byte little endian unsigned integer&lt;/li&gt;
&lt;li&gt;varint32 - singed LEB128 variable-length integer, limited to int32 values&lt;/li&gt;
&lt;li&gt;varuint1 - unsigned LEB128 variable-length integer, limited to values 0 or 1&lt;/li&gt;
&lt;li&gt;varuint7 - unsigned LEB128 variable-length integer, limited to values 0 to 127&lt;/li&gt;
&lt;li&gt;varuint32 - unsigned LEB128 variable-length integer limited to uint32 values&lt;/li&gt;
&lt;li&gt;varint64 - signed LEB128 variable length integer, limited to int64 values&lt;/li&gt;
&lt;li&gt;value_type - single-byte unsigned integer encoded as [1, i32; 2, i64; 3, f32; 4, f64]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My first reaction to this was &lt;em&gt;this ain&amp;#39;t too bad&lt;/em&gt; and the second was &lt;em&gt;what is an LEB128?&lt;/em&gt;. It is a special type of encoding that you can use for compressing unused binary space. If you have an int32 and store a value less than 256, you only need one byte of that integer. The three other bytes are excessive. LEB128 let&amp;#39;s you encode that binary so you only have to store the bytes you&amp;#39;re using of the data type.&lt;/p&gt;

&lt;p&gt;&lt;noscript&gt;&lt;pre&gt;function signedLEB128 (value) {
    var v = [],
        // log2 is expensive, could be replace by a lookup table
        size = Math.ceil(Math.log2(Math.abs(value))),
        more = true,
        isNegative = (value &amp;lt; 0),
        b = 0;&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;while (more) {

    // get 7 least significant bits
    b = value &amp;amp;amp; 127;
    // left shift value 7 bits
    value = value &amp;amp;gt;&amp;amp;gt; 7;

    if (isNegative) {
        // extend sign
        value = value | (- (1 &amp;amp;lt;&amp;amp;lt; (size - 7)));
    }

    // sign bit of byte is second high order bit
    if ((value == 0 &amp;amp;amp;&amp;amp;amp; ((b &amp;amp;amp; 0x40) == 0)) || ((value == -1 &amp;amp;amp;&amp;amp;amp; ((b &amp;amp;amp; 0x40) == 0x40)))) {
        // calculation is complete
        more = false;
    }
    else {
        b = b | 128;
    }

    v.push(b);
}

return v;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;function unsignedLEB128 (value, padding) {
    var v = [],
        b = 0;&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;// no padding unless specified
padding = padding || 0;

do {
    b = value &amp;amp;amp; 127;
    value = value &amp;amp;gt;&amp;amp;gt; 7;
    if (value != 0 || padding &amp;amp;gt; 0) {
        b = b | 128;
    }
    v.push(b);
    padding--;
} while (value != 0 || padding &amp;amp;gt; -1);

return v;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;}&lt;/pre&gt;&lt;/noscript&gt;&lt;script src=&quot;https://gist.github.com/miklund/79c1f3eb129ea5689c03c41d17922c14.js?file=leb128.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;With the unsignedLEB128 I had to append a padding, for the values that define length of a section. I hope I can remove this in the future.&lt;/p&gt;

&lt;p&gt;With this in place, it was pretty easy to define the data types.&lt;/p&gt;

&lt;p&gt;&lt;noscript&gt;&lt;pre&gt;//
// Data types
//&lt;/p&gt;

&lt;p&gt;// a single-byte unsigned integer
function uint8(n) {
    if (n &amp;lt; 0 || n &amp;gt; 255) {
        throw new Error(&amp;#39;uint8 is limited to [0, 255]&amp;#39;)
    }&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;return [n];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// A four-byte little endian unsigned integer.
function uint32(n) {
    if (n &amp;lt; 0 || n &amp;gt; 4294967295) {
        throw new Error(&amp;#39;uint32 is limited to [0, 4294967295]&amp;#39;)
    }&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;var v = []
for (var i = 0; i &amp;amp;lt; 4; i++) {
    v[i] = n &amp;amp;amp; (255)
    n = n &amp;amp;gt;&amp;amp;gt; 8
}

return v;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// A Signed LEB128 variable-length integer, limited to int32 values.
function varint32(n) {
    if (n &amp;lt; -2147483648 || n &amp;gt; 2147483647) {
        throw new Error(&amp;#39;varint32 is limited to [-2147483648, 2147483647]&amp;#39;)
    }&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;return signedLEB128(n);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// A LEB128 variable-length integer, limited to the values 0 or 1. varuint1 values may contain leading zeros.
function varuint1(n) {
    if (n &amp;lt; 0 || n &amp;gt; 1) {
        throw new Error(&amp;#39;varuint1 is limited to [0, 1]&amp;#39;)
    }&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;return unsignedLEB128(n);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// A LEB128 variable-length integer, limited to the values [0, 127]. varuint7 values may contain leading zeros.
function varuint7(n) {
    if (n &amp;lt; 0 || n &amp;gt; 127) {
        throw new Error(&amp;#39;varuint7 is limited to [0, 127]&amp;#39;);
    }&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;return unsignedLEB128(n);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// A LEB128 variable-length integer, limited to uint32 values. varuint32 values may contain leading zeros.
function varuint32(n, padding) {
    if (n &amp;lt; 0 || n &amp;gt; 0xFFFFFFFF) {
        throw new Error(&amp;#39;varuint32 is limited to [0, 4294967295]&amp;#39;)
    }&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;return unsignedLEB128(n, padding);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// A Signed LEB128 variable-length integer, limited to int64 values.
function varint64(n) {
    if (n &amp;lt; -9223372036854775808 || n &amp;gt; 9223372036854775807) {
        throw new Error(&amp;#39;varint64 is limited to [-9223372036854775808, 9223372036854775807]&amp;#39;)
    }&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;return signedLEB128(n);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// A single-byte unsigned integer indicating a value type.
function value&lt;em&gt;type(n) {
    if (n &amp;lt; 1 || n &amp;gt; 4) {
        throw new Error(&amp;#39;value&lt;/em&gt;type is limited to [1, 4] =&amp;gt; [i32, i64, f32, f64]&amp;#39;);
    }&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;return [n];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;}&lt;/pre&gt;&lt;/noscript&gt;&lt;script src=&quot;https://gist.github.com/miklund/79c1f3eb129ea5689c03c41d17922c14.js?file=dataTypes.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;And with the data types in place it was straight forward creating the binary code. It is however heavily commented and it repeats itself, but this is because I did it as a learning excercise. I would extract some methods if I were to make it formal and use it as a library.&lt;/p&gt;

&lt;p&gt;&lt;noscript&gt;&lt;pre&gt;// ////////
// Preamble
// ////////&lt;/p&gt;

&lt;p&gt;// first add the magic wasm number: 0x6d736100
var wasm = uint32(0x6d736100)&lt;/p&gt;

&lt;p&gt;// append the version of wasm, 11 in this case
wasm = wasm.concat(uint32(0x0b))&lt;/p&gt;

&lt;p&gt;// ////////////
// Type section
// https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#type-section
// ////////////&lt;/p&gt;

&lt;p&gt;// The type section declares all function signatures that will be used in the module.
var type = [];&lt;/p&gt;

&lt;p&gt;// id&lt;em&gt;len: section identifier string length
wasm = wasm.concat(varuint32(&amp;quot;type&amp;quot;.length));
// id&lt;/em&gt;str: section identifier string of id_len bytes
wasm = wasm.concat(stringToByteArray(&amp;quot;type&amp;quot;));&lt;/p&gt;

&lt;p&gt;// count: number of type entries to follow
type = type.concat(varuint32(1));&lt;/p&gt;

&lt;p&gt;//
// entries: here starts the first type entry
//&lt;/p&gt;

&lt;p&gt;// form: 0x40 indicates a function type
type = type.concat(varuint7(0x40));&lt;/p&gt;

&lt;p&gt;// param&lt;em&gt;count: the number of parameters to the function
type = type.concat(varuint32(0x02));
// param&lt;/em&gt;types: the parameter types of the function
type = type.concat(value&lt;em&gt;type(1)); // i32
type = type.concat(value&lt;/em&gt;type(1)); // i32&lt;/p&gt;

&lt;p&gt;// return_count: the number of results from the function
type = type.concat(varuint1(1));&lt;/p&gt;

&lt;p&gt;// return&lt;em&gt;type: the result type of the function (if return&lt;/em&gt;count is 1)
type = type.concat(value_type(1)); // i32&lt;/p&gt;

&lt;p&gt;// payload&lt;em&gt;len: size of this section in bytes
wasm = wasm.concat(varuint32(type.length, 4));
// payload&lt;/em&gt;str: content of this section, of length payload_len
wasm = wasm.concat(type);&lt;/p&gt;

&lt;p&gt;// //////////////
// Import section
// //////////////&lt;/p&gt;

&lt;p&gt;// todo: implement this when imports are of import&lt;/p&gt;

&lt;p&gt;// ////////////////
// Function section
// https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#function-section
// ////////////////&lt;/p&gt;

&lt;p&gt;// id&lt;em&gt;len: section identifier string length
wasm = wasm.concat(varuint32(&amp;quot;function&amp;quot;.length));
// id&lt;/em&gt;str: section identifier string of id_len bytes
wasm = wasm.concat(stringToByteArray(&amp;quot;function&amp;quot;));&lt;/p&gt;

&lt;p&gt;var functions = [];&lt;/p&gt;

&lt;p&gt;// count: count of signature indices to follow
functions = functions.concat(varuint32(1));&lt;/p&gt;

&lt;p&gt;// types: sequence of indices into the type section
functions = functions.concat(varuint32(0)); // index should be zero based&lt;/p&gt;

&lt;p&gt;// payload&lt;em&gt;len: size of this section in bytes
wasm = wasm.concat(varuint32(functions.length, 4));
// payload&lt;/em&gt;str: content of this section, of length payload_len
wasm = wasm.concat(functions);&lt;/p&gt;

&lt;p&gt;// /////////////
// Table section
// https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#table-section
// /////////////&lt;/p&gt;

&lt;p&gt;// todo: implement when needed&lt;/p&gt;

&lt;p&gt;// //////////////
// Memory section
// https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#memory-section
// //////////////&lt;/p&gt;

&lt;p&gt;// id&lt;em&gt;len: section identifier string length
wasm = wasm.concat(varuint32(&amp;quot;memory&amp;quot;.length));
// id&lt;/em&gt;str: section identifier string of id_len bytes
wasm = wasm.concat(stringToByteArray(&amp;quot;memory&amp;quot;));&lt;/p&gt;

&lt;p&gt;var memory = [];&lt;/p&gt;

&lt;p&gt;// initial: initial memory size in 64KiB pages
memory = memory.concat(varuint32(256, 1));&lt;/p&gt;

&lt;p&gt;// maximum: initial memory size in 64KiB pages
memory = memory.concat(varuint32(256, 1));&lt;/p&gt;

&lt;p&gt;// exported: 1 if the memory is visible outside the module
memory = memory.concat(uint8(1));&lt;/p&gt;

&lt;p&gt;// payload&lt;em&gt;len: size of this section in bytes
wasm = wasm.concat(varuint32(memory.length, 4));
// payload&lt;/em&gt;str: content of this section, of length payload_len
wasm = wasm.concat(memory);&lt;/p&gt;

&lt;p&gt;// //////////////
// Export section
// https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#export-section
// //////////////&lt;/p&gt;

&lt;p&gt;// id&lt;em&gt;len: section identifier string length
wasm = wasm.concat(varuint32(&amp;quot;export&amp;quot;.length));
// id&lt;/em&gt;str: section identifier string of id_len bytes
wasm = wasm.concat(stringToByteArray(&amp;quot;export&amp;quot;));&lt;/p&gt;

&lt;p&gt;var exports = [];&lt;/p&gt;

&lt;p&gt;// count: count of export entries to follow
exports = exports.concat(varuint32(1));&lt;/p&gt;

&lt;p&gt;//
// entries: repeated export entries as described below
//&lt;/p&gt;

&lt;p&gt;// func_index: index into the function table
exports = exports.concat(varuint32(0));&lt;/p&gt;

&lt;p&gt;// function_len: function string length
exports = exports.concat(varuint32(&amp;quot;add&amp;quot;.length));&lt;/p&gt;

&lt;p&gt;// function&lt;em&gt;str: function string of function&lt;/em&gt;len bytes
exports = exports.concat(stringToByteArray(&amp;quot;add&amp;quot;));&lt;/p&gt;

&lt;p&gt;// payload&lt;em&gt;len: size of this section in bytes
wasm = wasm.concat(varuint32(exports.length, 4));
// payload&lt;/em&gt;str: content of this section, of length payload_len
wasm = wasm.concat(exports);&lt;/p&gt;

&lt;p&gt;// /////////////
// Start section
// https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#start-section
// /////////////&lt;/p&gt;

&lt;p&gt;// todo: this module does not have a start function&lt;/p&gt;

&lt;p&gt;// ////////////
// Code section
// https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#code-section
// ////////////&lt;/p&gt;

&lt;p&gt;// id&lt;em&gt;len: section identifier string length
wasm = wasm.concat(varuint32(&amp;quot;code&amp;quot;.length));
// id&lt;/em&gt;str: section identifier string of id_len bytes
wasm = wasm.concat(stringToByteArray(&amp;quot;code&amp;quot;));&lt;/p&gt;

&lt;p&gt;var code = [];&lt;/p&gt;

&lt;p&gt;// count: count of function bodies to follow
code = code.concat(varuint32(1));&lt;/p&gt;

&lt;p&gt;//
// bodies: sequence of function bodies
//&lt;/p&gt;

&lt;p&gt;var body = [];&lt;/p&gt;

&lt;p&gt;// local_count: number of local entries
body = body.concat(varuint32(0));&lt;/p&gt;

&lt;p&gt;//
// ast
//&lt;/p&gt;

&lt;p&gt;// The program this ast is for
//
//(module
//  (func $addTwo (param i32 i32) (result i32)
//    (i32.add
//      (get&lt;em&gt;local 0)
//      (get&lt;/em&gt;local 1)))
//  (export &amp;quot;addTwo&amp;quot; $addTwo))&lt;/p&gt;

&lt;p&gt;// post-order encoding, right, left then op-code&lt;/p&gt;

&lt;p&gt;// get_local: read a local variable or parameter
body = body.concat([0x14].concat(varuint32(0)));&lt;/p&gt;

&lt;p&gt;// get_local: read a local variable or parameter
body = body.concat([0x14].concat(varuint32(1)));&lt;/p&gt;

&lt;p&gt;// i32.add
body = body.concat([0x40]);&lt;/p&gt;

&lt;p&gt;// body_size: size of function body to follow, in bytes
code = code.concat(varuint32(body.length, 4));&lt;/p&gt;

&lt;p&gt;// body
code = code.concat(body);&lt;/p&gt;

&lt;p&gt;// payload&lt;em&gt;len: size of this section in bytes
wasm = wasm.concat(varuint32(code.length, 4));
// payload&lt;/em&gt;str: content of this section, of length payload_len
wasm = wasm.concat(code);&lt;/p&gt;

&lt;p&gt;// ////////////
// Data section
// https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#data-section
// ////////////&lt;/p&gt;

&lt;p&gt;// todo: no initialized data at this time to be loaded into linear memory&lt;/p&gt;

&lt;p&gt;// ////////////
// Name section
// https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#name-section
// ////////////&lt;/p&gt;

&lt;p&gt;// id&lt;em&gt;len: section identifier string length
wasm = wasm.concat(varuint32(&amp;quot;name&amp;quot;.length));
// id&lt;/em&gt;str: section identifier string of id_len bytes
wasm = wasm.concat(stringToByteArray(&amp;quot;name&amp;quot;));&lt;/p&gt;

&lt;p&gt;var name = [];&lt;/p&gt;

&lt;p&gt;// count: count of entries to follow
name = name.concat(varuint32(1));&lt;/p&gt;

&lt;p&gt;//
// entries: sequence of names
//&lt;/p&gt;

&lt;p&gt;// fun&lt;em&gt;name&lt;/em&gt;len: string length, in bytes
name = name.concat(varuint32(&amp;quot;add&amp;quot;.length));&lt;/p&gt;

&lt;p&gt;// fun&lt;em&gt;name&lt;/em&gt;str: valid utf8 encoding
name = name.concat(stringToByteArray(&amp;quot;add&amp;quot;));&lt;/p&gt;

&lt;p&gt;// local_count: count of local names to follow
name = name.concat(varuint32(0));&lt;/p&gt;

&lt;p&gt;// payload&lt;em&gt;len: size of this section in bytes
wasm = wasm.concat(varuint32(name.length, 4));
// payload&lt;/em&gt;str: content of this section, of length payload_len
wasm = wasm.concat(name);&lt;/p&gt;

&lt;p&gt;///////////////////////////////////////////////////////////////////////////////&lt;/pre&gt;&lt;/noscript&gt;&lt;script src=&quot;https://gist.github.com/miklund/79c1f3eb129ea5689c03c41d17922c14.js?file=buildBinary.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;With this, all we need to do is to write the binary array to hdd.&lt;/p&gt;

&lt;p&gt;&lt;noscript&gt;&lt;pre&gt;/ output
console.log(&amp;#39;Complete HEX to output&amp;#39;)
var output = &amp;quot;&amp;quot;;
for (var i = 0; i &amp;lt; wasm.length; i++) {
    output += wasm[i].toString(16) + &amp;quot; &amp;quot;;
}
console.log(output);&lt;/p&gt;

&lt;p&gt;var buffer = Buffer.from(wasm);
var fd = fs.openSync(&amp;#39;out.wasm&amp;#39;, &amp;#39;w&amp;#39;);
fs.write(fd, buffer, 0, buffer.length, 0, function (err) {
    if(err) {
        console.log(err)
    } else {
        console.log(&amp;#39;The byte buffer (%d bytes) was saved to out.wasm&amp;#39;, wasm.length)
    }
});&lt;/pre&gt;&lt;/noscript&gt;&lt;script src=&quot;https://gist.github.com/miklund/79c1f3eb129ea5689c03c41d17922c14.js?file=writeFile.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;h2&gt;Testing the WebAssembly binary in a browser&lt;/h2&gt;

&lt;p&gt;We have created a binary wasm file, but how do we test it? The information on this was pretty scarse until I found &lt;a href=&quot;http://webassembly.github.io/demo/&quot; title=&quot;WebAssembly demo page&quot;&gt;this&lt;/a&gt; demo page. You can currently run wasm files in experimental modes on following browsers&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chrome Canary, open chrome://flags/#enable-webassembly&lt;/li&gt;
&lt;li&gt;Firefox Nightly, open about:config and set javascript.options.wasm to true&lt;/li&gt;
&lt;li&gt;Download preview of Microsoft Edge&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have a wasm enabled browser you can go &lt;a href=&quot;/assets/posts/2016-06-19-creating-a-webassembly-binary-and-running-it-in-a-browser/test.html&quot;&gt;here&lt;/a&gt; and use the following &lt;a href=&quot;/assets/posts/2016-06-19-creating-a-webassembly-binary-and-running-it-in-a-browser/out.wasm&quot;&gt;binary&lt;/a&gt; to try it out.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/posts/2016-06-19-creating-a-webassembly-binary-and-running-it-in-a-browser/test.png&quot; alt=&quot;wasm test page&quot;&gt;&lt;/p&gt;

&lt;p&gt;The source for loading and running the wasm module is pretty straight forward.&lt;/p&gt;

&lt;p&gt;&lt;noscript&gt;&lt;pre&gt;&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;style&amp;gt;
    .hidden {
        display: none;
    }
    &amp;lt;/style&amp;gt;
    &amp;lt;script&amp;gt;
    var myModule;&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;function loadModule() {
    // read binary file
    var wasmFile = document.getElementById(&amp;amp;quot;wasmBinary&amp;amp;quot;).files[0];

    // create a reader object
    var reader = new FileReader();

    // on success
    reader.onload = function (e) {
        // save the result ArrayBuffer
        var wasmBinary = e.target.result;

        // display success in the console
        console.log(&amp;amp;#39;Successfully read file %d bytes&amp;amp;#39;, wasmBinary.byteLength);

        // init the module
        myModule = Wasm.instantiateModule(wasmBinary);

        // display the section for testing the module
        document.getElementById(&amp;amp;quot;add&amp;amp;quot;).className = &amp;amp;quot;&amp;amp;quot;;

        return false;
    };

    // on error
    reader.onerror = function (e) {
        // display error in the console
        console.error(&amp;amp;#39;An error reading the file occured&amp;amp;#39;);
    };

    // read the whole file into an ArrayBuffer
    reader.readAsArrayBuffer(wasmFile);

    // do not refresh the page
    return false;
}

function runModule() {
    // get first argument
    var arg1 = parseInt(document.getElementById(&amp;amp;quot;arg1&amp;amp;quot;).value);
    // get second argument
    var arg2 = parseInt(document.getElementById(&amp;amp;quot;arg2&amp;amp;quot;).value);
    // caluclate the addition
    var result = myModule.exports.add(arg1, arg2);
    // display the result
    document.getElementById(&amp;amp;quot;result&amp;amp;quot;).innerText = result;
}

&amp;amp;lt;/script&amp;amp;gt;
&amp;amp;lt;script src=&amp;amp;quot;polyfill-prototype-1/jslib/load-wasm.js&amp;amp;quot;&amp;amp;gt;&amp;amp;lt;/script&amp;amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Upload a WebAssembly module&amp;lt;/h1&amp;gt;
    &amp;lt;input type=&amp;quot;file&amp;quot; id=&amp;quot;wasmBinary&amp;quot; name=&amp;quot;wasmBinary&amp;quot;  /&amp;gt;
    &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Load Module&amp;quot; onclick=&amp;quot;loadModule()&amp;quot; /&amp;gt;
    &amp;lt;div id=&amp;quot;add&amp;quot; class=&amp;quot;hidden&amp;quot; style=&amp;quot;display:hidden&amp;quot;&amp;gt;
        &amp;lt;h2&amp;gt;Module Loaded&amp;lt;/h2&amp;gt;
        &amp;lt;p&amp;gt;Test the functionality here&amp;lt;/p&amp;gt;
        &amp;lt;div&amp;gt;
            &amp;lt;input id=&amp;quot;arg1&amp;quot; name=&amp;quot;arg1&amp;quot; size=&amp;quot;7&amp;quot; /&amp;gt;
            &amp;lt;span&amp;gt;+&amp;lt;/span&amp;gt;
            &amp;lt;input id=&amp;quot;arg2&amp;quot; name=&amp;quot;arg2&amp;quot; size=&amp;quot;7&amp;quot; /&amp;gt;
            &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;=&amp;quot; onclick=&amp;quot;runModule()&amp;quot; /&amp;gt;
            &amp;lt;span id=&amp;quot;result&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/pre&gt;&lt;/noscript&gt;&lt;script src=&quot;https://gist.github.com/miklund/79c1f3eb129ea5689c03c41d17922c14.js?file=test.html&quot;&gt; &lt;/script&gt;&lt;/p&gt;
</description>
				<pubDate>Sun, 19 Jun 2016 12:47:32 +0000</pubDate>
				<link>https://blog.mikaellundin.name/2016/06/19/creating-a-webassembly-binary-and-running-it-in-a-browser.html</link>
				<guid isPermaLink="true">https://blog.mikaellundin.name/2016/06/19/creating-a-webassembly-binary-and-running-it-in-a-browser.html</guid>
			</item>
		
	</channel>
</rss>
