I am no newb on OO programming, but I am faced with a puzzling situation. I have been given a program to work on and extend, but the previous developers didn’t seem that comfortable with OO, it seems they either had a C background or an unclear understanding of OO. Now, I don’t suggest I am a better developer, I just think that I can spot some common OO errors. The difficult task is how to amend them.
In my case, I see a lot of this:
if (ret == 1) {
out.print("yadda yadda");
} else if (ret == 2) {
out.print("yadda yadda");
} else if (ret == 3) {
out.print("yadda yadda");
} else if (ret == 0) {
out.print("yadda yadda");
} else if (ret == 5) {
out.print("yadda yadda");
} else if (ret == 6) {
out.print("yadda yadda");
} else if (ret == 7) {
out.print("yadda yadda");
}
ret is a value returned by a function, in which all Exceptions are swallowed, and in the catch blocks, the above values are returned explicitly. Oftentimes, the Exceptions are simply swallowed, with empty catch blocks.
It’s obvious that swalllowing exceptions is wrong OO design. My question concerns the use of return values. I believe that too is wrong, however I think that using Exceptions for control flow is equally wrong, and I can’t think of anything to replace the above in a correct, OO manner.
Your input, please?
But this is Java (not C++). So if you work with `codes’ like that you ought to work with Enums. And using Enums (or integers, incidentally) you can use the switch() statement to improve code like that a lot.
This can be extended to give it some more Continuation Passing Style flavour; by defining a callback method for ErrorCode and calling that method instead of doing the switch() statement.