Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've never seen an assert explain why


The signature for static_assert(3) in C has a second argument that is `char *msg`. With C23 onwards, it becomes optional.


Java has this too.

  assert isSorted(array, start, end) : "The input array for binary search is expected to be sorted";
That said, I think in many cases asserts tend to be fairly self-evident, they typically just list the invariants, e.g.

  assert start <= end;
  assert (end - start) % stepSize == 0;
  // some logic
  assert pos - start >= 0;
  assert pos - end < 0;
  assert (pos-start) % stepSize == 0;
  return pos;


I use it in python all the time:

    assert x.shape == y.shape, f"{x.shape} != {y.shape}"


This should be

  # Confirm that x.shape == y.shape
  assert x.shape == y.shape, f"{x.shape} != {y.shape}"  ## x.shape was not y.shape
/s




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: