I have a big method, part of which checks the state of an object and throws an exception if the state is invalid. Should I extract a method? The new method 'CheckState' will do nothing or throw an exception.
-
1Questions on Stack Overflow are expected to be in English. That isn't English. It's Engrish. – Kendall Frey Jun 04 '12 at 20:26
-
What about `CheckState` returning bool? – L.B Jun 04 '12 at 20:26
-
The OP means that the method will have no observable effect on the caller (semantically speaking) if the check succeeds, since it neither returns a value nor alters any state. And no, it is not uncommon practice. – Douglas Jun 04 '12 at 20:35
-
2Seriously, how does a question like this get 4 upvotes? – Kendall Frey Jun 04 '12 at 20:38
-
5@Kendall Frey: I, for one, disapprove of this impatience and hostility towards new users. The question is not that hard to understand once you put some effort into it. I upvoted to compensate for the unjustified downvotes. – Douglas Jun 04 '12 at 20:39
-
2Does this question shows research effort? Is it useful? Is it *clear*? – Kendall Frey Jun 04 '12 at 20:41
-
1@KendallFrey I did the same as Douglas. It's the internet, if you are downvoting everyone without perfect English then I pity your mouse buttons. – NominSim Jun 04 '12 at 20:41
-
1To avoid a major flame war, lets agree to disagree. :/ *resists urge* – Kendall Frey Jun 04 '12 at 20:44
-
1The scenario that the OP is asking about is identical to what is implemented in [`Dispatcher.VerifyAccess`](http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.verifyaccess.aspx). What’s wrong with asking about a programming practice? – Douglas Jun 04 '12 at 20:44
4 Answers
The convention is for Check—
to return bool
, whilst Verify—
would throw exception if the verification fails.
See, for example, Dispatcher.CheckAccess
and Dispatcher.VerifyAccess
:
The difference between
CheckAccess
andVerifyAccess
isCheckAccess
returns aBoolean
indicating whether the calling thread has access to theDispatcher
andVerifyAccess
throws an exception.

- 53,759
- 13
- 140
- 188
If I understand you correctly, you mean to say that part of your method checks the state of an object and throws an exception if it is invalid.
You are further asking whether you should move this to its own method(one that checks the state of an object and throws an exception of it is invalid).
The answer really is; probably neither. An exception should really only be thrown in "exceptional" circumstances. If you enter your method and expect the object to be in a valid state, then use it as if it were.
If something occurs that is unexpected, then catch that exception and throw your "InvalidStateException". (If programmed properly this shouldn't even be necessary either.)
If you enter your method and are not sure that your object is in a valid state, then it being in an invalid one is not "unexpected" behavior, and it should be handled differently.
This is where your Check
method would come into place. It shouldn't throw an exception but should probably return a boolean, which will determine what you do next. An invalid object in your case is perfectly reasonable, so use your code to handle that case with a check method that returns its valid_state boolean
.
It's even good practice to separate the state-checking from the state-changing code. However, if your class is relying much on state here and there, you should probably take a look at the State Design Pattern.
In this pattern, the difference in behavior is modeled by using a method, implemented differently for each State class.
This may be implemented better than following, but it gives you a taste:
class FooState {
FooState handleFoo();
FooState handleBar();
}
class ValidState {
FooState handleFoo(){...
}
FooState handleBar(){
return InvalidState(stateful);
}
}
class InvalidState {
FooState handleFoo() { throw InvalidState(); }
FooState handleBar() {
return ValidState(stateful);
}
}
class StatefulObject {
public FooState state;
public void foo(){ state=state.handleFoo(); }
public void bar(){ state=state.handleBar(); }
}

- 40,723
- 12
- 105
- 192
If it truly does nothing, then you should be able to safely remove the code completely.
Otherwise, the code MUST do something. In that case then you should be able to safely create a separate method which will most certainly make your code cleaner.

- 242,243
- 40
- 408
- 536