{
  "version": "https://jsonfeed.org/version/1.1",
  "title": "chem-bla-ics",
  "description": "Chemblaics (pronounced chem-bla-ics) is the science that uses open science and computers to solve problems in chemistry, biochemistry and related fields.",
  "home_page_url": "https://chem-bla-ics.linkedchemistry.info/",
  "feed_url": "https://chem-bla-ics.linkedchemistry.info/2014/11/16/programming-in-life-sciences-19.json",
  "icon": "https://chem-bla-ics.linkedchemistry.info/assets/images/chem-bla-ics_logo.png",
  "language": "en",
  "authors": [
    {
      "name": "Egon Willighagen",
      "url": "https://orcid.org/0000-0001-7542-0286",
      "_orcid": "0000-0001-7542-0286"
    }
  ],
  "items": [

    {
      "id": "https://doi.org/10.59350/m3gc1-3mv56",
      "url": "https://chem-bla-ics.linkedchemistry.info/2014/11/16/programming-in-life-sciences-19.html",
      "title": "Programming in the Life Sciences #19: debugging",
      "content_html": "<p>Debugging is the process find removing a fault in your code\n(<a href=\"https://en.wikipedia.org/wiki/Software_bug#Etymology\">the etymology</a> goes further back\nthan the moth story, I learned today). Being able to debug is an essential programming skill,\nand being able to program flawlessly is not enough; the bug can be outside your own code.\n(… there is much that can be written up about module interactions, APIs, documentation, etc,\nthat lead to <em>malfunctioning</em> code …)</p>\n\n<p>While there are full debugging tools, achieving the task of finding where the bug is can\noften be reached with simpler means:</p>\n\n<ol>\n  <li>take notice of error messages</li>\n  <li>add debug statements in your code</li>\n</ol>\n\n<h2 id=\"error-messages\">Error messages</h2>\n\n<p>Keeping track of error messages is first starting point. This skill is almost an art:\nit requires having seen enough for them to understand how to interpret them. I guess\nerror messages are the worst developed aspects of programming language, and I do not\nfrequently see programming language tutorial that discuss error messages. The field can\ncertainly improve here.</p>\n\n<p>However, at least error messages in general give an indication where the problem occurs.\nOften by a line number, though this number is not always accurate. Underlying causes of\nthat are the problem that if there is a problem in the code, it is not always clear what\nthe problem is. For example, if there is a closing (or opening) bracket missing somewhere,\nhow can the <a href=\"http://chem-bla-ics.blogspot.nl/2013/10/exercise-what-variable-type-would-you.html\">compiler</a>\ndecide what the author of the code meant? Web browsers like Firefox/Iceweasel and\nChrome (Ctrl-Shift-C) have a console that displays compiler errors and warnings:</p>\n\n<p><img src=\"/assets/images/debug1.png\" alt=\"\" /></p>\n\n<p>Another issue is that error messages can be cryptic and misleading. For example, the\nabove error message <em>“TypeError: searcher.bytag is not a function example1.html:73”</em>\nis confusing for a starting programmer. Surely, the source code calls <code class=\"language-plaintext highlighter-rouge\">searcher.bytag()</code>\nwhich definately is a function. So, why does the compiler say it is not?? The bug here,\nof course, is that the function called in the source code is not found: it should be\n<a href=\"https://github.com/openphacts/ops.js/blob/master/src/ConceptWikiSearch.js#L9\">byTag()</a>.</p>\n\n<p>But this bug at least can be detected during interpretation and executing of the code.\nThat is, it is clear to the compiler that it doesn’t know how to handle the code.\nAnother common problem is the situation where the code looks fine (to the compiler),\nbut the data it handles makes the code break down. For example, an variable doesn’t\nhave the expected value, leading to errors (e.g. null pointer-style). Therefore,\nunderstanding the variable values at a particular point in your code can be of\ngreat use.</p>\n\n<h2 id=\"console-output\">Console output</h2>\n\n<p>A simple way to inspect the content of a variable is to use this console visible in\nthe above screenshot. Many programming languages have their custom call to send output\nthere. Java has the <code class=\"language-plaintext highlighter-rouge\">System.out.println()</code> and JavaScript has <code class=\"language-plaintext highlighter-rouge\">console.log()</code>:</p>\n\n<p><img src=\"/assets/images/debug2.png\" alt=\"\" /></p>\n\n<p>Thus, if you have some complex bit of code with multiple for-loops, if-else statements,\netc, this can be used to see if some part of your code that you expect to be called\nreally is:</p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nx\">console</span><span class=\"p\">.</span><span class=\"nf\">log</span><span class=\"p\">(</span><span class=\"dl\">\"</span><span class=\"s2\">He, I'm here!</span><span class=\"dl\">\"</span><span class=\"p\">);</span>\n</code></pre></div></div>\n\n<p>This can be very useful when using asynchronous web service calls! Similarly, see\nwhat the value of some variable is:</p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">var</span> <span class=\"nx\">label</span> <span class=\"o\">=</span> <span class=\"nx\">jsonResponse</span><span class=\"p\">.</span><span class=\"nx\">items</span><span class=\"p\">[</span><span class=\"nx\">i</span><span class=\"p\">].</span><span class=\"nx\">prefLabel</span><span class=\"p\">;</span>\n<span class=\"nx\">console</span><span class=\"p\">.</span><span class=\"nf\">log</span><span class=\"p\">(</span><span class=\"dl\">\"</span><span class=\"s2\">label: </span><span class=\"dl\">\"</span> <span class=\"o\">+</span> <span class=\"nx\">label</span><span class=\"p\">);</span>\n</code></pre></div></div>\n\n<p>Also, because JavaScript is not a <a href=\"https://en.wikipedia.org/wiki/Strong_and_weak_typing\">strongly typed programming</a>\nI frequently find myself inspecting the <a href=\"http://chem-bla-ics.blogspot.nl/2013/10/exercise-what-variable-type-would-you.html\">data type</a>\nof a variable:</p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">var</span> <span class=\"nx\">label</span> <span class=\"o\">=</span> <span class=\"nx\">jsonResponse</span><span class=\"p\">.</span><span class=\"nx\">items</span><span class=\"p\">[</span><span class=\"nx\">i</span><span class=\"p\">].</span><span class=\"nx\">prefLabel</span><span class=\"p\">;</span>\n\n<span class=\"nx\">console</span><span class=\"p\">.</span><span class=\"nf\">log</span><span class=\"p\">(</span><span class=\"dl\">\"</span><span class=\"s2\">typeof label: </span><span class=\"dl\">\"</span> <span class=\"o\">+</span> <span class=\"k\">typeof</span><span class=\"p\">(</span><span class=\"nx\">label</span><span class=\"p\">));</span>\n</code></pre></div></div>\n\n<h2 id=\"conclusion\">Conclusion</h2>\n\n<p>These tools are very useful to find the location of a bug. And this matters. Yesterday,\nI was trying to use the <a href=\"http://chem-bla-ics.blogspot.nl/2014/11/programming-in-life-sciences-18.html\">histogram code in example6.html</a>\nto visualize a set of values with negative numbers (<a href=\"https://en.wikipedia.org/wiki/Zeta_potential\">zeta potentials</a>\nof nanomaterials, to be precise) and I was debugging the issue, trying to find where\nmy code when wrong. I used the above approaches, and the array of values looked in\norder, but different from the original example. But still the histogram was not\nshowing up. Well, after hours, and having asked someone else to look at the code\ntoo, and having ruled out many alternatives, she pointed out that the problem was\nnot in the JavaScript part of the code, but in the HTML: I was mixing up how\ndefault JavaScript and the d3.js library add SVG content to the HTML data model.\nThat is, I was using <code class=\"language-plaintext highlighter-rouge\">&lt;div id=\"chart\"&gt;</code>, which works with <code class=\"language-plaintext highlighter-rouge\">document.getElementById(\"chart\").innerHTML</code>,\nbut needed to use <code class=\"language-plaintext highlighter-rouge\">&lt;div class=\"chart\"&gt;</code> with the <code class=\"language-plaintext highlighter-rouge\">d3.select(\".chart\").innerHTML</code>\ncode I was using later.</p>\n\n<p>OK, that bug was on my account. However, it still was not working: I did see a\nhistogram, but it didn’t look good. Again debugging, and after again much too long,\nI found out that this was a bug in the d3.js code that makes it impossible to use\ntheir histogram example code for negative values. Again, once I knew where the bug\nwas, I could Google and quickly found\n<a href=\"http://stackoverflow.com/questions/15388481/d3-js-histogram-with-positive-and-negative-values\">the solution for it on StackOverflow</a>.</p>\n\n<p>So, the workflow of debugging at a top level, looks like:</p>\n\n<ol>\n  <li>find where the problem is</li>\n  <li>try to solve the problem</li>\n</ol>\n\n<p>Happy debugging!</p>",
      "summary": "Debugging is the process find removing a fault in your code (the etymology goes further back than the moth story, I learned today). Being able to debug is an essential programming skill, and being able to program flawlessly is not enough; the bug can be outside your own code. (… there is much that can be written up about module interactions, APIs, documentation, etc, that lead to malfunctioning code …)",
      "image": "https://chem-bla-ics.linkedchemistry.info/assets/images/debug2.png",
      "date_published": "2014-11-16T00:00:00+00:00",
      "date_modified": "2025-02-15T00:00:00+00:00",
      "tags": ["pra3006"],
      
      
      
      
      
      
        "authors": [ { "name": "Egon Willighagen", "url": "https://orcid.org/0000-0001-7542-0286" } ]
      
    }

  ]
}
