Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
103

The use case that exceptions were designed for is "I just encountered a situation that I cannot deal with properly at this point, because I don't have enough context to handle it, but the routine that called me (or something further up the call stack) ought to know how to handle it." The secondary use case is "I just encountered a serious error, and right ...


102

There's a detailed discussion of this on Ward's Wiki. Generally, the use of exceptions for control flow is an anti-pattern, with notable situation- and language-specific cough exceptions cough. As a quick summary for why, generally, it's an anti-pattern: Exceptions are, in essence, sophisticated GOTO statements Programming with exceptions, therefore, leads ...


27

Exceptions are as powerful as Continuations and GOTO. They are a universal control flow construct. In some languages, they are the only universal control flow construct. JavaScript, for example, has neither Continuations nor GOTO, it doesn't even have Proper Tail Calls. So, if you want to implement sophisticated control flow in JavaScript, you have to use ...


25

All a thread does is interleave operations so that parts of the process appear to overlap in time. A single-core machine with multiple threads merely jumps around: it executes small bits of code from one thread, then switches to another thread. A simple scheduler decides which thread is highest priority and is actually executed in the core. On a single-...


18

As others have mentioned numerously, (e.g. in this Stack Overflow question), the principle of least astonishment will forbid that you use exceptions excessively for control flow only purposes. On the other hand, no rule is 100% correct, and there are always those cases where an exception is "just the right tool" - much like goto itself, by the way, which ...


17

What you are experiencing is called 'Code Smell' and is a good indicator that you might be doing something wrong. Luckily though, you saw the problem and already came up with a great solution, so there's not much for me to say here. A state machine works perfectly for what you're doing here. Drawing it out might also help to try and find other states you ...


16

Any single-threaded program running on a machine with a finite amount of storage can be modelled as a finite state machine. A particular state in the finite state machine will represent the specific values of all relevant storage—local variables, global variables, heap storage, data currently swapped out in virtual memory, even the content of relevant ...


13

The Finite State Machine has less computational power than a Turing machine. That is, the Turing Machine can do things that the FSM cannot. This is true because the FSM is limited in memory by the number of (finite) states. The following diagram illustrates the relationship between a Finite State Machine and a Turing Machine. As you can see, the Turing ...


11

Using exceptions for control flow is generally considered an anti-pattern, but there are exceptions (no pun intended). It has been said a thousand times, that exceptions are meant for exceptional conditions. A broken database connection is an exceptional condition. A user entering letters in an input field that should only allow numbers is not. A bug in ...


11

If you want to be really pedantic, every computer program is a Finite State Machine, because even if you convert all matter in the entire universe into a computer, it will still only have finite memory, thus a finite amount of states, and a finite amount of transitions between those states. State Machines are models, just like Lambda Calculus, Turing ...


9

How does one achieve multi-threading functionality in high-level language, such as Java, using only one thread and state machine? For example, what if there are 2 activities to perform (doing calculations and doing I/O) and one activity can block? What you're describing is called cooperative multitasking, where tasks are given the CPU and expected to ...


9

Writing blocking functions is for people who can't create state machines ;) Threads are useful if you can't get around blocking. No fundamental computer activity is truly blocking, it's just that lots of them are implemented that way for ease of use. Instead of returning a character or "read failed", a read function blocks until the whole buffer is read. ...


9

The point of a finite state machine is that it has explicit rules for everything that can happen in a state. That's why it is finite. For instance: if a: print a elif b: print b Is not finite, because we could get input c. This: if a: print a elif b: print b else: print error is finite, because all possible input is accounted for. This ...


9

Direct substate is a one level substate of a state Transitively nested substate is a substate of a substate (any levels down) Consider this diagram: For active state direct substates are filing warming up washing emptying while transitively nested substate are rotating left rotating right inceasing speed (substate of rotating left) full speed rotation (...


8

It's completely possible to handle error conditions without the use of exceptions. Some languages, most notably C, don't even have exceptions, and people still manage to create quite complex applications with it. The reason exceptions are useful is they allow you to succinctly specify two essentially independent control flows in the same code: one if an ...


8

You could implement your washing machine as a state machine, but there's just as much potential for state proliferation as there is for if statements if you don't do things carefully. Working in states requires a different mindset where you think in terms of states (where you are), stimuli (what you get from the outside) and actions (what you do). In that ...


8

Yes, of course it can. See Bohm & Jacopini's 1966 paper (Yes, fifty years ago), "Flow diagrams, turing machines and languages with only two formation rules" for the gory details. They showed that any program, even a mess of GOTO-spaghetti, can be converted into a WHILE-loop around a CASE statement, that together implement a finite-state machine. This ...


8

If you want to hold onto "classical" state machines whose transition functions only depend on the current state and incoming inputs you have to rethink how you want to model the application's state. Formally, you can simulate transitions depending on the previous state (in terms of your current application) by building a new state machine which has pair ...


7

I am sure you already know this but just in case: Make sure every node in the state diagram has an outgoing arc for EVERY legal kind of input (or divide the inputs into classes, with one outbound arc for each class of input). Every example I have seen of a state machine uses only one outbound arc for ANY erroneous input. If there is no definite answer as ...


7

From Wikipedia: State commonly refers to either the present condition of a system or entity... That's pretty much what it means in a computing context: the data that defines the condition of some object or system. The meaning of 'state' isn't specific to programming. There are examples of 'state' literally everywhere you look. The television is off. The ...


7

No, your deterministic finite automaton is incorrect. Your regular expression is. The problem in your DFA is the declaration of 5 and 6 to be terminal nodes. A testcase such as "0." will be accepted by your DFA even though it should not. Optional values I want to point out how you can model a general structure which includes optional values. Let's take ...


7

Based on what you've said in comments, this is how I would handle it: Implement the story as a finite state machine, with a twist. Each State is a page of the story, and each Transition is a link from one page to another. But each Transition also has Conditions. The Conditions could be null, in which case the Transition always shows up as an available ...


7

From a theoretical point of view, it's not different at all. Of course, from a theoretical point of view you can write any program in assembly language and it'll work just as well. While the computer your code is running on may be mathematically equivalent to a very, very complicated state machine, as is any other program that runs on any other computer, ...


7

State machines don't have exceptions, though they may have transitions to an error state. There's no “one size fits all” answer for how you should handle errors. This depends a lot on the problem domain. Possibilities I consider useful: If an exception occurs, this is interpreted as a transition to an error state. This error state may be terminal, i.e. ...


7

There are several variations of the definition of a finite state machine. The one you give is common in CS classes, particularly in regards to formal languages and the theory of parsing. In this context, the FSM is often directly related to a regular expression, and the goal is feed strings into the FSM to see if they match a regular expression. The set of ...


7

Lets focus on the obvious advantage first: in the first example, the controllers are mostly independent from each other. PlayerSelectCharacterController does not need to know about the class CharacterMoveController or vice versa. Each of these classes can be developed separately, tested separately, or if necessary, placed in different compilation units. ...


6

I think you may be getting a little bogged down in the English meaning of state, when compared to the State Pattern (or Finite-State Machine, which is really a diagrammatic representation of a State Pattern). Both are appropriate here, but they shouldn't be confused. The State Pattern is something which should, given various common stimuli, operate on the ...


6

Nondeterministic automata can have far fewer states, but the engine to evaluate them must be capable of being in multiple states at once - all possible states. This can lead to a trade-off between memory requirements and code complexity when executing the automaton. Personally, though, I think the more interesting issues relate to how you manipulate the ...


6

Yes it can, but only in a practically useless sense. The computation engines we usually use today are ultimately equivalent to Turing machines, which do exactly what you describe (in fact, they do less, because they have only linear-access memory to work with rather then random-access memory, but it can be proved that this makes no difference). But the ...


6

In this new answer I will try to explain based on the "Stateless" library mentioned in OP. Draft. More information will be added after I get my hands on Stateless. In any sense, extension to Stateless is required. Fortunately this is highly doable because Stateless is highly extensible using its partial class approach. A simple description is that: One ...


Only top voted, non community-wiki answers of a minimum length are eligible