Okay, I've been in a couple of recent discussions about how to best protect/guard behaviors. This kind of thing can happen across any number of languages, but my example is going to be based on a recent experience.
Basically, the questions is: should you only allow inputs you expect, or should you only ban inputs that cause problems the programmer couldn't have expected?
My most recent example is:
So, in this case, the concern was, "Whoa! You are letting someone run any code they want in that statement." My answer was, "Yeah, and?" Basically, this is a script that you would only run as a test, that's not a common variable name, and it's not asking for any extra privileges. His point was, "You should prune off any possible bad behaviors at the start."
So, the question was, should that be turned into a number, and then compared with something like:
Which is fine, but if you really want to run something unsafe on your machine, you pretty much can without this script in the way, anyway. But it gets to the philosophy I have seen repeated:
This code is a little more contrived, so my apologies. But this is the concept I want to get at. In that "move_item" call, we have a call that will force a physical change of position -- literally moving an item in physical space from one location to another. So, there are certainly needs to be 'safe' to make sure you don't get into bad physical realities. It's also unlikely that someone really wants to move something from a position to the same position.
But, my point is, it doesn't hurt. It's a no-op. There are potentially unimagined workflows that could benefit from having that no-op. And I see no reason to assert against it. The co-worker I was discussing with was of the point, "I see no reason to allow it, because I have no examples of why we would want it."
My issue is that when I'm in the middle of development, over-asserting is one of those things I have to constantly un-do. Which is even more painful if there are a bunch of tests confirming the presence of the asserts. His point is that in the middle of development, being stopped early when you are doing something you don't realize you are doing is a better result.
And thus, I turn to the "Wisdom of the Board". Are you a liberal coder or a conservative coder?
Basically, the questions is: should you only allow inputs you expect, or should you only ban inputs that cause problems the programmer couldn't have expected?
My most recent example is:
Code:
#!/bin/bash
is_test=${is_test:-false}
function do_something() {
if "$is_test"; then
echo "Doing the thing"
else
do_the_thing
fi
}
So, in this case, the concern was, "Whoa! You are letting someone run any code they want in that statement." My answer was, "Yeah, and?" Basically, this is a script that you would only run as a test, that's not a common variable name, and it's not asking for any extra privileges. His point was, "You should prune off any possible bad behaviors at the start."
So, the question was, should that be turned into a number, and then compared with something like:
Code:
is_test=${is_test:-0}
function do_something() {
if [[ is_test -ne 0 ]]; then
echo "Doing the thing"
else
do_the_thing
fi
}
Which is fine, but if you really want to run something unsafe on your machine, you pretty much can without this script in the way, anyway. But it gets to the philosophy I have seen repeated:
Code:
int move_item_from_a_to_b(const PositionType &a, const PositionType &b) {
assert(a != b);
...
}
This code is a little more contrived, so my apologies. But this is the concept I want to get at. In that "move_item" call, we have a call that will force a physical change of position -- literally moving an item in physical space from one location to another. So, there are certainly needs to be 'safe' to make sure you don't get into bad physical realities. It's also unlikely that someone really wants to move something from a position to the same position.
But, my point is, it doesn't hurt. It's a no-op. There are potentially unimagined workflows that could benefit from having that no-op. And I see no reason to assert against it. The co-worker I was discussing with was of the point, "I see no reason to allow it, because I have no examples of why we would want it."
My issue is that when I'm in the middle of development, over-asserting is one of those things I have to constantly un-do. Which is even more painful if there are a bunch of tests confirming the presence of the asserts. His point is that in the middle of development, being stopped early when you are doing something you don't realize you are doing is a better result.
And thus, I turn to the "Wisdom of the Board". Are you a liberal coder or a conservative coder?