4 ms·
Well, indirection can be quite helpful even for simple checks: if is_json(somestring) { // do something } function is_json(string s) { retur
by husainalshehhi 7y ago
Well, indirection can be quite helpful even for simple checks:
if is_json(somestring) {
// do something
}
function is_json(string s) {
return JsonLib.convert(s) != NULL
}
this indirection is easier to read rather than thinking too much about what JsonLib.convert(s) != NULL mean. This does not hinder debugging because you only debug this is_json function when the code does not evaluate is_json as expected. Every other time, you debug what's in the if statement.
The given example x.startswith("foo") is simple enough to include it directly. However, if there is a business reason for why "foo" is checked here, it will be difficult to understand why the check is there. This will be more difficult to read a code like this:
if x.startwith("-")
do something
instead of
if is_yaml_node(x)
do something
def is_yaml_node(x)
x.startwith("-")