tsolomko / BitByteData

Read and write bits and bytes in Swift.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Replace `precondition` with throwing

Alkenso opened this issue · comments

First of all, thank you for so cool library. Save lots of work for me!

The thing I want to propose it to replace precondition statement in code with just marking methods as throws.

  • rarely in production we want to get crash accidentally.
  • it could be pretty boring all the time check remaining bit size and then perform read

Hi @Alkenso,

Thank you for your kind words and sorry for not responding sooner.

Unfortunately, I have to politely decline your proposal. It was a conscious decision to use preconditions instead of error throwing throughout the project. Changing this would be a fundamental modification of the project with no substantial benefits, in my opinion.

Specifically, I don't really find your arguments convincing:

  • rarely in production we want to get crash accidentally.
  • it could be pretty boring all the time check remaining bit size and then perform read

Philosophically (and practically) preconditions in this project mirror similar out-of-bounds checks performed in the subscripts of the Array and Data types. Those can also crash "accidentally" in production (though, such a crash would indicate a logic error in the code rather than an accident) and it could also be "pretty boring" constantly verifying the validity of your indices. In practice, however, based on the external knowledge available to you about the data that you're reading (and consequently how much of it should be available at any given point in time), you can bundle these checks together to reduce their amount. As an admittedly contrived example you can, for instance, simplify this kind of code,

// var reader = ...
guard reader.bytesLeft >= 1 else { /* ... */ }
reader.byte()
guard reader.bytesLeft >= 2 else { /* ... */ }
reader.uint16()

with the following one

// var reader = ...
guard reader.bytesLeft >= 3 else { /* ... */ }
reader.byte()
reader.uint16()

if you know ahead of time that all valid data should contain at least those 3 bytes.