It's been good to get back into coding after several years away from it. Things are starting to click for me again, and it's interesting to see how much things have changed (and how much they've stayed the same!)
One thought that occurred to me last night as I was working on a subtle bug was the idea of presumption of innocence in law, and how it applied to coding. Basically, there are two ways that one can approach a criminal case: a) you can assume that the person being accused is guilty and ask them to prove their innocence, or b) you can assume they are innocent and ask the accuser to prove their guilt. Either position has some drawbacks, but the general consensus is that (b), the presumption of innocence, is the most civilized manner in which to construct law.
So how does this relate to coding? Much like the law, we code to define a certain conduct of behavior and the consequences for not following that conduct. And also much like the law, we can code with the assumption that the user is innocent or guilty. Consider the simple if - then - else clause. Typically, this is written as
if (something_that_is_true) {
then_do_this();
} else {
then_do_that();
}
At minimum, we can remove the else (and in some languages the surrounding braces too, but I'll be sticking with Java syntax), to just:
if (something_is_true) {
then_do_this();
}
What if we consider "true" as equal to innocent, and "false" as equal to guilty?
Presumption of Innocence would suggest that we assume truth and only code for guilt:
if (something_is_false) {
then_do_that();
} else {
then_do_this();
}
Or even better, for languages that support the construct:
unless (something_is_false) {
then_do_this();
}
If you aren't using such a language, but do support ad-hoc returns from functions:
if (something_is_false)
return;
then_do_this();
The idea being that unless guilt (falseness) is proven, you assume truth (innocence) and proceed with code execution. In contrast, presumption of guilt would only execute code if it is proven true (innocent):
if (something_is_true) {
then_do_this();
}
Most of the time I find myself mixing styles. Sometimes coding with presumption of innocence, sometimes with presumption of guilt. I wonder what would happen if I only ever used a single style? Would this be better or worse, or no change?
One thought that occurred to me last night as I was working on a subtle bug was the idea of presumption of innocence in law, and how it applied to coding. Basically, there are two ways that one can approach a criminal case: a) you can assume that the person being accused is guilty and ask them to prove their innocence, or b) you can assume they are innocent and ask the accuser to prove their guilt. Either position has some drawbacks, but the general consensus is that (b), the presumption of innocence, is the most civilized manner in which to construct law.
So how does this relate to coding? Much like the law, we code to define a certain conduct of behavior and the consequences for not following that conduct. And also much like the law, we can code with the assumption that the user is innocent or guilty. Consider the simple if - then - else clause. Typically, this is written as
if (something_that_is_true) {
then_do_this();
} else {
then_do_that();
}
At minimum, we can remove the else (and in some languages the surrounding braces too, but I'll be sticking with Java syntax), to just:
if (something_is_true) {
then_do_this();
}
What if we consider "true" as equal to innocent, and "false" as equal to guilty?
Presumption of Innocence would suggest that we assume truth and only code for guilt:
if (something_is_false) {
then_do_that();
} else {
then_do_this();
}
Or even better, for languages that support the construct:
unless (something_is_false) {
then_do_this();
}
If you aren't using such a language, but do support ad-hoc returns from functions:
if (something_is_false)
return;
then_do_this();
The idea being that unless guilt (falseness) is proven, you assume truth (innocence) and proceed with code execution. In contrast, presumption of guilt would only execute code if it is proven true (innocent):
if (something_is_true) {
then_do_this();
}
Most of the time I find myself mixing styles. Sometimes coding with presumption of innocence, sometimes with presumption of guilt. I wonder what would happen if I only ever used a single style? Would this be better or worse, or no change?
No comments:
Post a Comment