<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: John Kilmister</title>
    <description>The latest articles on DEV Community by John Kilmister (@johnkilmister).</description>
    <link>https://dev.to/johnkilmister</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F231460%2F763bed52-4f92-4645-b804-4e3145754631.jpg</url>
      <title>DEV Community: John Kilmister</title>
      <link>https://dev.to/johnkilmister</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/johnkilmister"/>
    <language>en</language>
    <item>
      <title>The inevitability of Bugs and the need for Error Codes</title>
      <dc:creator>John Kilmister</dc:creator>
      <pubDate>Sun, 01 Mar 2020 19:54:05 +0000</pubDate>
      <link>https://dev.to/johnkilmister/the-inevitability-of-bugs-and-the-need-for-error-codes-4gg8</link>
      <guid>https://dev.to/johnkilmister/the-inevitability-of-bugs-and-the-need-for-error-codes-4gg8</guid>
      <description>&lt;p&gt;It seems like an inevitability that with modern software comes bugs, errors and unexpected features. Bugs have been there from the beginning with the term bug first being used by Grace Hopper back in 1947 in the pioneering days of computer programming.&lt;/p&gt;

&lt;p&gt;Today there are many ways to handle errors, log them and help users find solutions. An important part of this is the use of error codes.  This is something that when I started out I missed however quickly learnt was essential to diagnosing and correlating user-reported issues. &lt;/p&gt;

&lt;p&gt;When an error occurs the user is ideally presented with a message. For an expected error this can detail out next steps however for an unexpected error the user needs to be presented with something else. It is not good practice to surface the underlying error as at best these contain confusing technical detail that means little to the end-user and at worst can expose sensitive information. Therefore often a generic message is used such as “An unexpected error has occurred, please try again”.  When presented with this the users may turn to the internet, send a support email or share screenshots looking for answers. This is little help when the generic error message is the same no matter what error has happened.&lt;/p&gt;

&lt;p&gt;Adding error codes to your messages can resolve this. Error codes are numeric or alpha-numeric strings that can be added to the error message to help the user find solutions or report issues. These can be used for both expected errors and unexpected exceptions. They allow developers to uniquely identify and correlate issues as if the same error occurs then it should have the same error code.&lt;/p&gt;

&lt;p&gt;Errors can be broken down into expected and unexpected errors. There a number of situations, that although may be unexpected to the end-user can actually be predicted in advance. These can include things like running out of disk space or loss of internet connectivity. In some ways, these are the best type of errors as you can provide potential solutions and static error codes. &lt;/p&gt;

&lt;p&gt;In these cases, an error code can be useful if the message and the proposed solution is not successful. Error codes can allow people to communicate and search for a specific problem without having to use the full message. A knowledge base of error codes can be built up in advance of the software being published and can detail alternative or longer solutions that can be updated independently of the software. Error codes are particularly useful in these cases when the error message and interface is localised as the error code should remain consistent.&lt;/p&gt;

&lt;p&gt;Unexpected errors occur from a mistake in our code or the third-party library. We can try and reduce this through testing, guards statements and predicting scenarios that may happen however in a complex system things may slip through. Even when we are confident it is error-free, external changes can cause new errors to occur. For desktop or mobile apps, these can include OS upgrades or other software running. If you are running a web app, with evergreen browsers, your app is running in an ever-changing environment and plugins that can interfere with your application.&lt;/p&gt;

&lt;p&gt;Unlike expected errors for unexpected errors, we need to generate the codes at run-time inside a global error handler. There are a number of techniques for this however you need to ensure that the codes are short enough to write down or quote, are generated to be unique to the issue but not tied to the user.&lt;/p&gt;

&lt;p&gt;It is useful when logging the exception and stack trace to tag it with the same error code shown to the user. If a user sends a screenshot of the error code you can then find the full stack trace and identify the error quickly.&lt;/p&gt;

&lt;p&gt;Once you have resolved an issue you can report back that the bug with provided error code was fixed in a specific version of the software.&lt;/p&gt;

&lt;p&gt;No matter how many edge cases we guard against a bug is highly likely to make it into our code. Changes to the environment where our code is running that are often outside our control can also cause unexpected behaviours. Through the use of effective error messages and error code generation, we can talk about these errors, correlate them and clearly communicate with our end users to resolve faults.&lt;/p&gt;

</description>
      <category>bugs</category>
      <category>errors</category>
      <category>beginners</category>
      <category>debugging</category>
    </item>
    <item>
      <title>Random Learnings from Entering JS13K Games 2019</title>
      <dc:creator>John Kilmister</dc:creator>
      <pubDate>Sun, 29 Sep 2019 10:24:12 +0000</pubDate>
      <link>https://dev.to/johnkilmister/random-learnings-from-entering-js13k-games-2019-4pcn</link>
      <guid>https://dev.to/johnkilmister/random-learnings-from-entering-js13k-games-2019-4pcn</guid>
      <description>&lt;p&gt;The &lt;a href="https://js13kgames.com/"&gt;JS13K Games&lt;/a&gt; is an annual, month-long coding competition where the aim is to create a web-based game on a theme with a size of only 13 kilobytes. I like to use the competition to find dedicated time to learn new skills and refresh old ones.&lt;/p&gt;

&lt;p&gt;I have entered the JS13K games competition twice before. With &lt;a href="https://js13kgames.com/entries/element-war"&gt;Element War&lt;/a&gt; in 2014, I used it to learn HTML canvas and then again in 2016 with  &lt;a href="https://js13kgames.com/entries/escape-from-maze-13"&gt;Escape from Maze 13&lt;/a&gt; to learn ES6 without frameworks. Both times I learnt many new things that have helped me back in the day job (web developer) along with many that I will probably never use again. &lt;/p&gt;

&lt;p&gt;If you have not played my game &lt;a href="https://js13kgames.com/entries/flight-back-home"&gt;Flight Back Home&lt;/a&gt; why not try it out. As a background, it is a hybrid of HTML5 Canvas and HTML elements to create a repeat after me game. I also encourage you to check out the &lt;a href="https://js13kgames.com/entries"&gt;other 2019 entries&lt;/a&gt; as there are many great games. &lt;/p&gt;

&lt;p&gt;This post is on just a few of the random things I learnt or was reminded of this year.&lt;/p&gt;

&lt;h3&gt;
  
  
  Holding Back, Stepping away and Planning
&lt;/h3&gt;

&lt;p&gt;There are many ways to approach the competition. I had made the mistake many times before of diving in and writing code with the first half baked idea that I had thought of.  &lt;/p&gt;

&lt;p&gt;This year I consciously decided to take the first few days to think, research and explore before going full steam ahead. I had a whole month after all, and a few days considering ideas would save re-work later. This strategy generally worked, and the idea I initially had was scrapped before writing any code.&lt;/p&gt;

&lt;p&gt;Many of the best ideas came when away from the keyboard. It has been &lt;a href="https://lifehacker.com/science-explains-why-our-best-ideas-come-in-the-shower-5987858"&gt;well documented&lt;/a&gt; the many ideas come when away from the task in places like the shower. I was often reminded this throughout the month. &lt;/p&gt;

&lt;h3&gt;
  
  
  SVGs to Make Patterns
&lt;/h3&gt;

&lt;p&gt;Early on I decided to use a hexagon grid and with the limit of 13K I set about looking how to generate it in the smallest and quickest way. I looked at options including HTML5 Canvas, using CSS to make hexagons and SVGs.&lt;/p&gt;

&lt;p&gt;I had often use SVGs and as they are lightweight they were perfect for this. What I did not know is that you can tile them as a background. There is a great post with more details on &lt;a href="https://www.sitepoint.com/6-incredible-svg-pattern-generators/"&gt;sitepoint&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the end, the final version did not use tiled SVGs as I needed to interact with individual grid cells. Though I can see these could be great for other games or splash screens in the future. &lt;/p&gt;

&lt;h3&gt;
  
  
  Optimised SVG
&lt;/h3&gt;

&lt;p&gt;In my previous JS13K entries I had used png images and optimised them using &lt;a href="https://tinypng.com/"&gt;tiny png&lt;/a&gt;. This year I decided to use an SVG image for the plane to try and retain more detail. I had never looked at SVG optimisation before, though as SVG files are XML I imagined them already to be small.&lt;/p&gt;

&lt;p&gt;It turns out after experimenting with different programs each saves different amounts of data in the SVG file. Much of this data is not needed and is either metadata or declarations of default values. Luckily &lt;a href="https://inkscape.org/"&gt;InkScape&lt;/a&gt; has a way of removing this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PlWhdKkl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/a0vf4on7jomrt04r8wvz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PlWhdKkl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/a0vf4on7jomrt04r8wvz.jpg" alt="Save Option"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zvJjMWXr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/t5aycg7vbtrhr2nf20vy.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zvJjMWXr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/t5aycg7vbtrhr2nf20vy.jpg" alt="Save Settings"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Hexagons are not what I thought
&lt;/h2&gt;

&lt;p&gt;You will see hexagon patterns in all sorts of places both man-made and in nature, though I had never really thought about them in detail. I learnt that there are two ways to tile hexagons, they are not the same width to height and many other random hexagon facts.&lt;/p&gt;

&lt;p&gt;The width to height ratio caused collision detection an error early on in the game design. The initial investigation led to trigonometry to calculate the correct values, however, taking a deeper look at the ratio of a regular hexagon is fixed at 1:1.1547005. This discovery saved both trying to remember maths but also all-important bytes in the final filesize. &lt;/p&gt;

&lt;p&gt;If you ever want to know more about hexagons and hexagon grids then there is an &lt;a href="https://www.redblobgames.com/grids/hexagons/"&gt;amazing post at red blob games&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Hexagons Maths is Hard
&lt;/h3&gt;

&lt;p&gt;I chose a hexagon grid to make the game more visually interesting, however, this led to many new challenges. The first was the maths needed to tile hexagons as the offset column is a different width to the non-offset columns. &lt;/p&gt;

&lt;p&gt;As I built the game I then needed to navigate around the grid and find if spaces had been visited before. In a normal grid +1 on X-axis moves you to the right and -1 to the left however in a hexagon grid it is far more complex.&lt;/p&gt;

&lt;p&gt;Once again the &lt;a href="https://www.redblobgames.com/grids/hexagons/"&gt;red blob games page&lt;/a&gt; had great detail on how to achieve the navigation.&lt;/p&gt;
&lt;h3&gt;
  
  
  Native Modal Dilaogs
&lt;/h3&gt;

&lt;p&gt;I have implemented modal dialogs many times in my career and I decided that I would use HTML dialogs rather than create these in the HTML canvas. For me this would make them quicker and easier to style and manage things like aligning text.&lt;/p&gt;

&lt;p&gt;I had heard of native the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog"&gt;HTML dialog element&lt;/a&gt; but had never used it. Using the new element would mean the code was short. It seemed as simple as replacing DIV with Dialog and then call open or close in JavaScript which was ideal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dialog&amp;gt;
    &amp;lt;p&amp;gt;Content of the Dialog&amp;lt;/p&amp;gt;
&amp;lt;/dialog&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const modal = document.querySelector('dialog');
modal.showModal();
modal.close();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In chrome this worked well however it turned out that Firefox does not support native dialogs without a feature flag and the polyfill turned out to be 25.1 KB!!  &lt;/p&gt;

&lt;p&gt;Further research showed that firefox has the CSS in place without the flag. The polyfill was also fully featured supporting many browsers so I could create a cut-down version that worked just in the latest firefox and chrome.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function show(selector) {
  const modal = document.querySelector(selector);
  if (isNative()) {
    if(!modal.hasAttribute("open"))
      modal.showModal();
  } else {
    modal.setAttribute('open','');
    const backdrop = document.querySelector("#backdrop");
    backdrop.style.display = "block";
  }
}

function hide(selector) {
    const modal = document.querySelector(selector);
    if (isNative()) {
        modal.close();
    } else {
        modal.removeAttribute('open');
        const backdrop = document.querySelector("#backdrop");
        backdrop.style.display = "none";
    }
}

function isNative(){
    //Native is not supported by FF yet :(
    return typeof HTMLDialogElement === 'function';
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  There will always be a bug
&lt;/h3&gt;

&lt;p&gt;It was three days before the end of the competition and I had decided to draw a line under what I had done so far just in case there were any issues. There were extra features I could add but I felt now was the time to finish.&lt;/p&gt;

&lt;p&gt;I ran the build process to create the zip file and ran one last playtest before submitting. There was an issue, cells I clicked were off by one grid cell. This was a disaster and the game was unplayable. I opened the dev tools in chrome to take a deeper look. Upon opening the tools it was no longer an issue, closing the tools once more and it was still not an issue. I then refreshed the page and the issue came back, the following refresh and again and it went away!&lt;/p&gt;

&lt;p&gt;After a day of trying many things out, I discovered the answer. It turned out to be dependent on how the browser loaded the HTML elements above the canvas. Sometime the Kontra mouse script would load before the canvas had been pushed down the page by other elements and sometimes after.&lt;/p&gt;

&lt;p&gt;The point is that there will always be an eleventh-hour bug and I was reminded yet again why I always allow extra time for these things.&lt;/p&gt;

&lt;h3&gt;
  
  
  Looking Back
&lt;/h3&gt;

&lt;p&gt;Once again I learnt a lot from developing my game this year and I would recommend any developer to enter even if they have never written a game before. The JS13K Games community has been great giving excellent help and input on social media and their slack channel.&lt;/p&gt;

&lt;p&gt;For my game, there are many things I would have done differently however I try to remember there are not bad choices just decisions that lead to the next challenge from which we can all learn.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>gamedev</category>
      <category>webdev</category>
      <category>jskgames</category>
    </item>
  </channel>
</rss>
