nanomorph icon indicating copy to clipboard operation
nanomorph copied to clipboard

Useless statement

Open c-smile opened this issue 4 years ago • 8 comments

This statment: https://github.com/choojs/nanomorph/blob/43d39d01a9c14a8279e398bad93c1ef6edfa03a4/index.js#L160 is useless as the function returns false by default.

c-smile avatar Jun 29 '19 23:06 c-smile

I think if the condition on the next line is true, the function can still return true.

goto-bus-stop avatar Jun 30 '19 09:06 goto-bus-stop

Ordering of the ifs are pretty important. That's why. So, no, it's not useless.

tunnckoCore avatar Jun 30 '19 11:06 tunnckoCore

Consider this:

function same(a,b) is called for a and b of Element type so effectively you get this:

function same (a, b) {
  ...
  if (a.tagName !== b.tagName) return false; // either false
  // if (a.type === TEXT_NODE) return a.nodeValue === b.nodeValue - dead statement if Elements
  return false; // or false again
}

If a and b are Node's then a.tagName is always undefined thus that part looks as this

function same (a, b) {
  ...
  // if (a.tagName !== b.tagName) return false; - dead statment if Nodes 
  if (a.type === TEXT_NODE) return a.nodeValue === b.nodeValue;
  return false;
}

c-smile avatar Jun 30 '19 13:06 c-smile

Even if this makes sense, still. The thing is that we don't know what a and b are at the point where we call the same(). In one case it can be Element, in another, it can be Node. It's not completely useless or just sitting there, never used. I'm pretty sure it is covered when you run the coverage.

There are tons of tests. I also had reimplemented it (in demo-minmorph) a dozen of times just out of curiosity.

tunnckoCore avatar Jun 30 '19 13:06 tunnckoCore

Try this:

<html>
    <head>
        <title>Test</title>
    </head>
    <body>
    <div>1</div>
    <div>2</div>
    </body>
<script type="text/javascript">

function same (a, b) {
  if (a.id) return a.id === b.id;
  if (a.isSameNode) return a.isSameNode(b);
  if (a.tagName !== b.tagName) return false;
  if (a.type === TEXT_NODE) return a.nodeValue === b.nodeValue;
  return false;
}

var divs = document.body.getElementsByTagName("div");
console.log( same(divs[0],divs[1]) );
</script>

</html>

You will see false in console. But I think true is expected there, right?

c-smile avatar Jun 30 '19 14:06 c-smile

But I think true is expected there, right?

Mmm, nope. Why it would be? It goes to the a.nodeValue === b.nodeValue which should return false, because it's 1 === 2.

I'll say that again, same is called recursively anyway, so 1) we don't know what's what in a given point in time and 2) it makes sense to have exactly these checks.

tunnckoCore avatar Jun 30 '19 15:06 tunnckoCore

Mmm, nope. Why it would be?

Then if (a.tagName !== b.tagName) return false; is useless.

QED.

c-smile avatar Jun 30 '19 15:06 c-smile

Omg, in this exact case, yes of course. Remove it and run the tests, and you will see that we need it.

Even if we remove it or put it in separate function... We still need to have this check anyway. That's why it is there in the same.

I'm done here.

tunnckoCore avatar Jun 30 '19 15:06 tunnckoCore