Fail Fast Principle in Software Development - tarunchhabra/parakalo GitHub Wiki
Fail-Fast-
https://medium.com/@marxallski/fail-fast-or-fail-safe-3b9ff8f68c26
a) throw exception early rather than executing many lines of code and then throwing exception. So, put an if condition before doing calculations on the fields. Use- Objects.Null, Guava's Preconditions.checkArguments, Apache's ObjectUtils.isnOtEmpty()
How to fail first in a correct way?
Use annotations and define correct contracts for your methods
Avoid “if something then return” on the top of your methods that check state of parameters is the contract of the method already defines it. If you know that this should not happen throw and exception!
Avoid “try catch” for no reason,“just as precaution” is not a valid sentence. If the method really needs a try catch, use it, and be specific as possible, don’t just use Exception class or throwable.
Print the stack trace!! On Android is even better if you do something like Log.e(TAG, “Some text that leads easily to this part of code”, throwable);
Avoid empty else.
b) Circuit breaker in micro services
Some people recommend making your software robust by working around problems automatically. This results in the software “failing slowly.” The program continues working right after an error but fails in strange ways later on.
A system that fails fast does exactly the opposite: when a problem occurs, it fails immediately and visibly.
Failing fast means that code should reveal its bugs as early as possible. The earlier a problem is observed (the closer to its cause), the easier it is to find and fix.
For example, consider a method that reads a property from a configuration file. What should happen when the property isn’t present? A common approach is to return null or a default value:
public int maxConnections() { string property = getProperty(“maxConnections”); if (property == null) { return 10; } else { return property.toInt(); } } In contrast, a program that fails fast will throw an exception: public int maxConnections() { string property = getProperty(“maxConnections”); if (property == null) { throw new NullReferenceException (“maxConnections property not found in “ + this.configFilePath); } else { return property.toInt(); } }
## Fail-fast fundamentals