Unsuccessful programs are not necessarily shorter than defensive style programs: it depends on the implementation and the measures necessary to securely protect your code.
In the case of Erlang, fault tolerant programs are usually shorter because of the declarative style and how the VM necessarily generates errors for you. For example, in a function:
day(1) -> sunday; day(2) -> monday; day(3) -> tuesday; day(4) -> wednesday; day(5) -> thursday; day(6) -> friday; day(7) -> saturday;
Any unexpected value passed to the function will result in an error that can be caught and processed by another process (i.e.: supervisor). Such errors will never endanger the system as a whole and do not require adding code to the function itself - all this is done outside the normal execution path using predefined actions.
In a dynamic language where fail-fast is not the norm, you have to manually check the boundaries and throw an exception yourself. Then you should catch the exception locally (at the top level of try ... catch) if you do not want the whole system to go down. Error handling code should usually be inserted during the normal execution path.
In a static language where fail-fast is not the norm, while how long your code will be highly dependent on your type system. If the language allows you to determine the types in which the final cases end with a check for you by the compiler, you usually do not need to process this inside the code, outside of non-deterministic events (files do not work, unexpected user input, etc.). languages โโwith such type systems, many errors will be detected before runtime, and therefore you will not have so many protective cases.
Error handling cannot be avoided, languages โโthat support fault-tolerant idioms (e.g. Erlang) will allow for sharper code than languages โโthat are not (static or not), mainly because the code for special cases is not mixed with the ideal path code fulfillment.
I GIVE TERRIBLE ADVICE
source share