{
  "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/2010/05/15/cleaner-cdk-code-5-developer-against.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/1bedv-rsy83",
      "url": "https://chem-bla-ics.linkedchemistry.info/2010/05/15/cleaner-cdk-code-5-developer-against.html",
      "title": "Cleaner CDK Code #5: develop against interfaces",
      "content_html": "<p><a href=\"http://blog.rguha.net/\">Rajarshi</a> is at the <a href=\"http://www.ebi.ac.uk/\">EBI</a> (or at least was yesterday), talking about his\n<a href=\"http://cran.r-project.org/web/packages/rcdk/index.html\">rcdk</a> package (<a href=\"http://www.slideshare.net/rguha/cheminformatics-in-r\">his excellent slides</a>).\nOne slide is about how to create a new atom; he mentions not to use new Atom() but the\n<a href=\"http://pele.farmbio.uu.se/nightly-1.2.3/cdk-javadoc-1.2.4/org/openscience/cdk/DefaultChemObjectBuilder.html\">DefaultChemObjectBuilder</a>\ninstead (slide 73). I do not entire agree with the message given.</p>\n\n<h2 id=\"develop-against-interfaces\">Develop against interfaces</h2>\n\n<p>The slide seems to favour the DefaultChemObjectBuilder, but there like are more suitable builders for a particular application.\nCurrently, I am aware of two alternative builders: the <em>DebugChemObjectBuilder</em>, and the <em>NoNotificationChemObjectBuilder</em>.</p>\n\n<p>The reason the CDK has a builder pattern is the following. It is cleaner to write against interfaces than against\nimplementations, because it allows alternative implementations. I just listed all three provided by the CDK library itself,\nbut other implementations may exists too; they might have a completely different data model, e.g. fully CMLDOM-based, or\nfully SQL-based. By programming against interfaces, changing the implementation becomes easy.</p>\n\n<p>Now, by using <code class=\"language-plaintext highlighter-rouge\">new Atom()</code> you choose a particular implementation (in this case, the one around the <code class=\"language-plaintext highlighter-rouge\">DefaultChemObjectBuilder</code>.\nHowever, you like the user to pick the implementation. This is why the CDK library itself uses builders all over the place:\nit assumes a <code class=\"language-plaintext highlighter-rouge\">IChemObjectBuilder</code> is predefined and that is used.</p>\n\n<p>For example:</p>\n\n<div class=\"language-java highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nc\">IChemObjectBuilder</span> <span class=\"n\">builder</span> <span class=\"o\">=</span> <span class=\"k\">new</span> <span class=\"nc\">DefaultChemObjectBuilder</span><span class=\"o\">();</span>\n<span class=\"nc\">IMolecule</span> <span class=\"n\">molecule</span> <span class=\"o\">=</span> <span class=\"n\">builder</span><span class=\"o\">.</span><span class=\"na\">newMolecule</span><span class=\"o\">();</span>\n<span class=\"nc\">IAtom</span> <span class=\"n\">atom</span> <span class=\"o\">=</span> <span class=\"n\">builder</span><span class=\"o\">.</span><span class=\"na\">newAtom</span><span class=\"o\">();</span>\n<span class=\"n\">molecule</span><span class=\"o\">.</span><span class=\"na\">addAtom</span><span class=\"o\">(</span><span class=\"n\">atom</span><span class=\"o\">);</span>\n</code></pre></div></div>\n\n<p>If your method actually has an IChemObject as input, it can retrieve the builder from there:</p>\n\n<div class=\"language-java highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">public</span> <span class=\"nc\">IMolecule</span> <span class=\"nf\">addToMolecule</span><span class=\"o\">(</span><span class=\"nc\">IAtom</span> <span class=\"n\">atom</span><span class=\"o\">)</span> <span class=\"o\">{</span>\n  <span class=\"nc\">IChemObjectBuilder</span> <span class=\"n\">builder</span> <span class=\"o\">=</span> <span class=\"n\">atom</span><span class=\"o\">.</span><span class=\"na\">getBuilder</span><span class=\"o\">();</span>\n  <span class=\"nc\">IMolecule</span> <span class=\"n\">molecule</span> <span class=\"o\">=</span> <span class=\"n\">builder</span><span class=\"o\">.</span><span class=\"na\">newMolecule</span><span class=\"o\">();</span>\n  <span class=\"n\">molecule</span><span class=\"o\">.</span><span class=\"na\">addAtom</span><span class=\"o\">(</span><span class=\"n\">atom</span><span class=\"o\">);</span>\n<span class=\"o\">}</span>\n</code></pre></div></div>\n\n<p>This latter situation is what exists most in the CDK library, actually. In some cases, this is not the case, and sometimes\nyou may need to pass an IChemObjectBuilder to a constructor. This is, for example, the case with the constructor of the\n<code class=\"language-plaintext highlighter-rouge\">SmilesParser</code>.</p>\n\n<p>Now, reconsider the first code example. By defining a builder only once, and reusing that builder in the rest of your code,\nyou only have to change one line to use a different implementation. For example, the DebugChemObjectBuilder that sends\ndebug messages for each set and get call to one of the data classes. I used this in the past, and solved several nasty bugs\nwith this; just by seeing in what order data was set and read. And I only needed to change one single line of code for that.</p>",
      "summary": "Rajarshi is at the EBI (or at least was yesterday), talking about his rcdk package (his excellent slides). One slide is about how to create a new atom; he mentions not to use new Atom() but the DefaultChemObjectBuilder instead (slide 73). I do not entire agree with the message given.",
      
      "date_published": "2010-05-15T00:00:00+00:00",
      "date_modified": "2010-05-15T00:00:00+00:00",
      "tags": ["cdk","java"],
      
      
      
      
      
      
        "authors": [ { "name": "Egon Willighagen", "url": "https://orcid.org/0000-0001-7542-0286" } ]
      
    }

  ]
}
