Close
Register
Close Window

DSA Coursenotes

Chapter 3 Week 4

Show Source |    | About   «  3.1. Binary Trees Part 1   ::   Contents   ::   3.3. Binary Search Trees  »

3.2. Clean Code

3.2.1. Example 1 (1)

  • A real bug from some code for an OpenDSA exercise. You don’t need to know much about the language to recognize the bug (once you spot it):

    <li><var><code>O(n^2)</code></var></li>
    <li><var><code>O(n \log n)</code></var></li>
    <li><var><code>O(\log n)</code><var></li>
    <li><var><code>O(1)</code></var></li>
    

3.2.2. Example 1 (2)

  • A real bug from some code for an OpenDSA exercise. You don’t need to know much about the language to recognize the bug (once you spot it):

    <li><var><code>O(n^2)</code></var></li>
    <li><var><code>O(n \log n)</code></var></li>
    <li><var><code>O(\log n)</code><var></li>
    <li><var><code>O(1)</code></var></li>
    
  • When refactored for readability, the bug pops out immediately:

    <li><var><code>O(n^2)     </code></var></li>
    <li><var><code>O(n \log n)</code></var></li>
    <li><var><code>O(\log n)  </code><var></li>
    <li><var><code>O(1)       </code></var></li>
    

3.2.3. Example 2 (1)

  • A real sample of code:

    var nodeOne = graphTwo.addNode("a ", {left: 0, top: 0});
    var nodeTwo = graphTwo.addNode("c", {left: 100, top: 0});
    var nodeThree = graphTwo.addNode("e", {left: 50, top: 50});
    var nodeFour = graphTwo.addNode("b", {left: 0,  top: 100});
    var nodeFive =graphTwo.addNode("d ", {left: 100, top: 100});
    

3.2.4. Example 2 (2)

  • A real sample of code:

    var nodeOne = graphTwo.addNode("a ", {left: 0, top: 0});
    var nodeTwo = graphTwo.addNode("c", {left: 100, top: 0});
    var nodeThree = graphTwo.addNode("e", {left: 50, top: 50});
    var nodeFour = graphTwo.addNode("b", {left: 0,  top: 100});
    var nodeFive =graphTwo.addNode("d ", {left: 100, top: 100});
    
  • Refactored for readability:

    var nodea = graph2.addNode("a", {left:   0, top:   0});
    var nodeb = graph2.addNode("b", {left:   0, top: 100});
    var nodec = graph2.addNode("c", {left: 100, top:   0});
    var noded = graph2.addNode("d", {left: 100, top: 100});
    var nodee = graph2.addNode("e", {left:  50, top:  50});
    

   «  3.1. Binary Trees Part 1   ::   Contents   ::   3.3. Binary Search Trees  »

nsf
Close Window