- Jun 15, 2001
- 33,932
- 1,113
- 126
I'm writing an object constructor which will load a file and set several properties of the object by performing a series of if statements on the file contents to check various equalities. Certain equalities will result in setting a boolean IsValid to false. Once it has been set to false, there is no need to test the other conditions, because clearly something got messed up.
I realize that I could stack this all into some monster if by using a lot of or statements, but that seems like a mess. I was thinking about something like this (and please understand that this is just an pseudocode example, these aren't the actual tests I'd be doing):
I've always thought that was bad form, but since constructors don't return anything, I can't use returns. On a similar note, is it bad form to even be doing these kinds of things in the constructor to begin with?
Thanks
Edit: The CODE tag is being weird, sorry about the strange whitespace.
I realize that I could stack this all into some monster if by using a lot of or statements, but that seems like a mess. I was thinking about something like this (and please understand that this is just an pseudocode example, these aren't the actual tests I'd be doing):
Code:
while (1){
[INDENT]if (a==4){
[INDENT]IsValid=false;
break;[/INDENT]
}[/INDENT]
[INDENT]if (b==7){
[INDENT]IsValid=false;
break;[/INDENT]
}[/INDENT]
...
}
I've always thought that was bad form, but since constructors don't return anything, I can't use returns. On a similar note, is it bad form to even be doing these kinds of things in the constructor to begin with?
Thanks
Edit: The CODE tag is being weird, sorry about the strange whitespace.