State Machines

The reason for this Q is that I read that some programming communities like to bannish this function from the languages because it is concidered as harmfull

I was just reading something that was new to me in Lua
the use of labels in goto and continue

like:
::labelOne::
do
–someChunk of code
if someVar == 0 then goto labelTwo else goto lableOne end
end

–here goes ::labelTwo:: of course… and soo on

Is this as good as the typical approach of stateMachines ?
I remember the goto stuff from my very early days of programming in Basic C#64 (like 9 years old I think) LOL

You can implement state machines using just if else statements. They’re easy to create, the hard part is determining the  states that make the most sense for your app. Too few and your code gets messy real fast, too many, and well, kind of the same thing… But the idea is along these lines:

[lua]

local StartUp =10

local StartLogin = 20

local StartLoginReply = 30

local StartLoginValid = 40

local StartLoginInvalid = 50

local CurrentState = StartUp

function manageState()

    if( CurrentState == Startup ) then   – Program just launched

           – Show Login screen, which will set state to StartLogin when done…

    elseif( CurrentState == StartLogin) then

          – Check to see if they have filled in login fields, if so, send request

          – and set state to StartLoginReply while we wait

    elseif( CurrentState == StartLoginReply) then

          – Wait for response…

    – More states follow…

     end

end

[/lua]

Goto’s are still considered bad practice in modern languages.

Thanks guys for the answers, my goal in this thread was to put the spotlight on goto and contonue with the use of labels. I red in "Programming in Lua release 3 (the latest one), that it is very nice to use IF you use it right. So I wonder why is it considered as bad practice? And is there any downsides or advantage of using it instead of if else or other closures ( the state machine was just an example). I wonder why Lua and C sharp still has it in the API if its bad practice.

You could ask the same thing about your appendix. Old habits die hard.

Programming theory identifies 4 basic elements of structured languages:

Sequence (executing instructions in order)

Selection (making a decision, and executing one or another pieces of code - if/then type)

Iteration (looping)

Recursion (routines calling themselves)

GOTO’s are in the “Selection” category, and people generally consider code written using them extensively as hard to follow / spaghetti code. You can certainly write spaghetti code without GOTO’s, but in practice, in code many people have “inherited” (legacy code), overuse of GOTO has really messed things up.

In practice, my understanding that GOTO is seen as typically OK in one circumstance: Critical Error Handling. In the case where a catastrophic error has occurred (disk read failure, etc), use of GOTO is ok to jump out of the code to an error handler. Other than that, I haven’t heard of instances these days where average programmers approve of it’s use.

Perhaps it has been retained in the languages for either one of two things:

To robustly support the “Selection” of code.

To ease implementation of Critical Error Handlers.

That was just what I wanted… a real good explenation…And it certainly was. Thanx mpappas !

I can really understand why it got the name spaghetti code LOL

You can implement state machines using just if else statements. They’re easy to create, the hard part is determining the  states that make the most sense for your app. Too few and your code gets messy real fast, too many, and well, kind of the same thing… But the idea is along these lines:

[lua]

local StartUp =10

local StartLogin = 20

local StartLoginReply = 30

local StartLoginValid = 40

local StartLoginInvalid = 50

local CurrentState = StartUp

function manageState()

    if( CurrentState == Startup ) then   – Program just launched

           – Show Login screen, which will set state to StartLogin when done…

    elseif( CurrentState == StartLogin) then

          – Check to see if they have filled in login fields, if so, send request

          – and set state to StartLoginReply while we wait

    elseif( CurrentState == StartLoginReply) then

          – Wait for response…

    – More states follow…

     end

end

[/lua]

Goto’s are still considered bad practice in modern languages.

Thanks guys for the answers, my goal in this thread was to put the spotlight on goto and contonue with the use of labels. I red in "Programming in Lua release 3 (the latest one), that it is very nice to use IF you use it right. So I wonder why is it considered as bad practice? And is there any downsides or advantage of using it instead of if else or other closures ( the state machine was just an example). I wonder why Lua and C sharp still has it in the API if its bad practice.

You could ask the same thing about your appendix. Old habits die hard.

Programming theory identifies 4 basic elements of structured languages:

Sequence (executing instructions in order)

Selection (making a decision, and executing one or another pieces of code - if/then type)

Iteration (looping)

Recursion (routines calling themselves)

GOTO’s are in the “Selection” category, and people generally consider code written using them extensively as hard to follow / spaghetti code. You can certainly write spaghetti code without GOTO’s, but in practice, in code many people have “inherited” (legacy code), overuse of GOTO has really messed things up.

In practice, my understanding that GOTO is seen as typically OK in one circumstance: Critical Error Handling. In the case where a catastrophic error has occurred (disk read failure, etc), use of GOTO is ok to jump out of the code to an error handler. Other than that, I haven’t heard of instances these days where average programmers approve of it’s use.

Perhaps it has been retained in the languages for either one of two things:

To robustly support the “Selection” of code.

To ease implementation of Critical Error Handlers.

That was just what I wanted… a real good explenation…And it certainly was. Thanx mpappas !

I can really understand why it got the name spaghetti code LOL