Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

12
  • 8
    @CodeYogi: With... tests. The thing is, it can be impractical to express everything in assertions: if the assertion merely repeats the code, then it does not bring anything new (repetition does not help with quality). This is why here I did not assert in the loop that 0 <= j <= rightIndex or array[j] <= value, it would just repeat the code. On the other hand, is_sorted brings a new guarantee, thus it's valuable. Afterwards, that's what tests are for. If you call insert([0, 1, 2], 2, 3) to your function and the output isn't [0, 1, 2, 3] then you've found a bug. Commented Apr 17, 2016 at 18:11
  • 3
    @MatthieuM. don't discount the value of an assertion simply because it duplicates the code. Infact, those can be very valuable assertions to have if you decide to rewrite the code. Testing has every right to be duplicative. Rather consider if the assertion it is so coupled to a single code implementation that any rewrite would invalidate the assertion. That's when you're wasting your time. Nice answer by the way. Commented Apr 17, 2016 at 20:49
  • 1
    @CandiedOrange: By duplicating the code I mean literally array[j+1] = array[j]; assert(array[j+1] == array[j]);. In this case, the value seems very very low (it's a copy/paste). If you duplicate the meaning but expressed in another fashion, then it becomes more valuable. Commented Apr 18, 2016 at 6:15
  • 10
    Hoare's rules: helping to write correct loops since 1969. "Still, even though these techniques have been known for more than a decade, most porgrammers have never heard of them". Commented Apr 18, 2016 at 6:31
  • 1
    @MatthieuM. I agree that it has very low value. But I don't think of it as being caused by it being a copy/paste. Say I wanted to refactor insert() so that it worked by copying from an old array to new array. That can be done without breaking your other assert's. But not this last one. Just shows how well your other assert's were designed. Commented Apr 18, 2016 at 7:16