Ruby exception naming convention

I have already released quite a few libraries. I have not yet decided what to name my exceptions. The standard Ruby library always names exceptions as such (noun Exception or Error ) - for example

  • Runtimeerrror
  • Eoferror
  • Threaderror

Rails, by contrast, use abbreviated exception names such as RecordInvalid , etc.

If I write libraries, and most of them are not Rails related, what naming convention should I use? I have to admit that Rails' short names "appeal to me more, because when an exception occurs, you already see that this exception or some kind of error, because it appears in the logs / debugger / stderr.

+4
source share
2 answers

This is how I do it.

Does the name of the exception indicate what is wrong? If yes, then everything is ready. If not, add the suffix Error .

So using your example:

  • ThreadError : Here Thread already a constant in ruby, as well as a bad exception descriptor. Therefore, to indicate some common error in the stream, a suffix is ​​required.
  • RecordInvalid : In this case, the name of the exception clearly indicates what is wrong. RecordInvlaidError will be redundant.

Another way to think about it can be general and specific.

  • ThreadError : a common error, something bad happened to the thread, I don’t know exactly what, but it was bad. Thus, this describes some kind of “error” in the “stream”.
  • RecordInvalid : A specific error, this record right here has a specific data integrity problem and cannot be saved. So the “record” is “invalid” and all you have to say.
+7
source

Personally, I'm in the Error camp, but only when it is good. Putting Error at the end, it makes it clear what a class is, that it is not an ordinary object, etc. I find Exception bit long for my taste, and the lack of a suffix is ​​too ambiguous.

+1
source

All Articles