Description
The dart language supports exceptions but not checked exceptions.
The lack of check exceptions means that developer must rely on documentation to determine what exceptions a method will throw.
If you look at a significant portions of code published on pub.dev, developers are simply not documenting the exceptions that are thrown.
Documenting exceptions is particularly error prone as it requires the developer to explore each called api to determine the set of exceptions the api throws.
Of course given that most api's are not documenting their exceptions the result is that a developer must examine thousands of lines of code to determine what exceptions are being thrown.
This of course results in the developer not documenting their own api as its simply too much effort and worse they simply are not doing the necessary work to handle exceptions appropriately.
The lack of documentation then cascades up into the next level of code and so on.
The end result is that much of the code published on pub.dev is of questionable quality.
This problem could largely be resolved by providing a lint rule for pedantic that warns if an api throws an exception.
By adding this link to pedantic we would see a very rapid improvement on the quality of code on pub.dev for the following reasons.
- as a user of a pub.dev package I can see what exceptions are being thrown and handle them correctly.
- as a publisher on pub.dev its now easy to document my exceptions as the linter generates a list of thrown exceptions.
- if I make a code change which changes the set of thrown exceptions the linter will notify my to fix the doco.
- ide's can be modified to provide a 'quick fix' for this new lint rule which will make keeping doco up to date simple.
- with exceptions now documented developers will be force to actually consider what exceptions are going to be thrown and take appropriate action rather than just ignoring them as happens now.
- when third party projects change the set of thrown exceptions (as happened recently with firebase) I get a lint rather than having to wait for a runtime exception.
Essentially we end up with a cascade affect and everyone benefits.
Here is an example of how I would see the workflow.
/// Throws NotARealException
void dowork()
{
actuallyDoTheWork();
}
void actuallyDoTheWork()
{
throws PosixException();
}
Linter:
The method dowork throws an non-existant exception NotARealException. Please remove.
The method actuallyDoTheWork throws PosixException. Please document the throws statement.
Once I correct the above lints I should then see:
void dowork()
{
actuallyDoTheWork();
}
/// Throws PosixException when something goes wrong.
void actuallyDoTheWork()
{
throws PosixException();
}
Lint error:
The method dowork throws PosixException. Please document the throws statement.
I see two alternatives for how the linter inspects the code.
- the linter just looks at the body of a method for any direct exceptions and the documentation of all invoked methods (as per the above example)
- the linter looks at the body of the method and the body of any direct api calls.
The 2nd alternative would see a much faster improvement in code on pub.dev as it would:
- allow users of a library to at least see any 1st level exceptions third party packages throw
- would encourage developers to raise issues against the third party project to improve their exception documentation.
The result is that the lint would essentially cascade both 'up' into the developers own code and 'down' into third party projects resulting in an improvement in quality of all dart code.
This lint looks (in my ignorant opinion) fairly easy to implement.
The benefits to the community however will be huge.