Live data from Hacker News

Designing state machines

drivy.engineering

51–58 of 58 posts

Re: Designing state machines

#51
post #32

Earlier quoted context omitted.

Is "state machines" a global term that includes finite state machines like how vending machines deal with varying coin amounts that go in to trigger the price and dispense proper change? Googled looks like it is. That was a cool homework/project.

Yes, the vending machines examples is one of the most directly relatable ones. In fact, pretty much every piece of code you write has gone through a "Finite State Automata" (during the lexical phase of compilation to be exact) which in very simple terms is just a huge set of rules saying - "if this then that".

finite state automata haha

Re: Designing state machines

#52
post #50

Earlier quoted context omitted.

That's the problem with kids these days. Everything has to be a friggin class. You know we did some pretty amazing things with computers before OO. We even landed people on the Moon. Can anyone program without OO any more? Just saying.

I recognise that your tongue and cheek may be in close proximity here, but care to elaborate?

It's just a silly observation. I come from a generation that had to create products like AutoCAD to run on CP/M systems with 64K of RAM and precious little permanent storage. As an aside, few people know that software like AutoCAD started on such --by today's standards-- crippled systems.

Anyone who came up in software development through that path has a full understanding of the cost of every bit of code written, every abstraction.

Today's programmers, unless they've taken university courses to expose them to the low level stuff, are extremely isolated from the innards of what they write. They reach for classes and objects by default and seem incapable of thinking outside of these boxes. All of this adds overhead and bloat, something you had to be keenly aware of with limited memory and horsepower.

I do understand that a higher level of abstraction in the context of massive amounts of memory and horsepower can be nice. What gets me is that this is often an unnecessary default.

Going back to state machines. One does not to create a bunch of classes and methods to implement them. I see that almost as a violation of the conceptual simplicity of state machines.

I mean, to go in a different direction, I use state machine in FPGA's all the time. The synthesized hardware works fine and represents the bare minimum necessary to implement these very high speed state machines. No classes involved. One can do the same in software development.

Re: Designing state machines

#53
post #50

Earlier quoted context omitted.

I recognise that your tongue and cheek may be in close proximity here, but care to elaborate?

It's just a silly observation. I come from a generation that had to create products like AutoCAD to run on CP/M systems with 64K of RAM and precious little permanent storage. As an aside, few people know that software like AutoCAD started on such --by today's standards-- crippled systems. Anyone who came up in software development through that path has a full understanding of the cost of every bit of code written, ev…

Even earlier: I implemented a Statistical Multiplexor - 8 async serial users muxed over a single 9600 Bd synchronous line - on a Z-80 with 4K RAM and 4K ROM. In 1977. Garunteed correct data delivery over lines as noisy as 1x10^3 BERT. A datacomms protocol that can do that is not a trivial beastie. The only technique that let me fit it in was state machine. It was the start of a very successful product line. I live by FSMs ever since. Software and FPGAs. And I agree about the OO cruft. I just use a function for each state and execute to completion. Perfect for event driven systems, which datacomms are a prime example. And very lightweight. In High level world, this needs a language with functions as first class objects. I have other useful representtions too. My favorite table based form is very handily implemented in Lua, should anyone be interested in seeing it.

Re: Designing state machines

#54
post #18

What about the DB side of things ? How do you model the state graph in the DB ? I have been thinking of using Postgresql CTE to do this. When a cycle occurs, I just create a new step to the dB. Anyone from CRM startups who have interesting learnings here ?

You can use user defined aggregates in postgres to model state machines. I'm using this technique quite successfully and have been meaning to write a blog post about it.

could you talk a bit more about this ?

i was looking at https://sqlsunday.com/2014/05/25/directed-acyclic-graphs-vs-... https://www.qcode.co.uk/post/73 to learn how others have done it

Re: Designing state machines

#55
post #12

I have recently created some dead simple code to explain the concept to colleagues whom, as the article notes, were not using this very simple and effective construct at all to control state transitions in business apps. As the article does not really explain how state machines work let me try to do it here so maybe you will go back to your code and put one in place :) In general, if anywhere in your code you have so…

Brilliant. Superb explanation! Just couple of days I was trying to understand this from my team mates and it took him 30 mins to make it complicated.

Re: Designing state machines

#56
post #50

Earlier quoted context omitted.

I recognise that your tongue and cheek may be in close proximity here, but care to elaborate?

It's just a silly observation. I come from a generation that had to create products like AutoCAD to run on CP/M systems with 64K of RAM and precious little permanent storage. As an aside, few people know that software like AutoCAD started on such --by today's standards-- crippled systems. Anyone who came up in software development through that path has a full understanding of the cost of every bit of code written, ev…

Well.

I'll call it idiomatic form to use OO style in Ruby, which the article (and my examples) were written in. Considering the interpreted and managed-memory model of the language runtime, it is utterly pointless to worry about a few object instances. Heck, this is a language where primitives are objects.

I suspect we work in quite different areas though, which probably gives us very different perspectives of what matters. The kind of applications I build on a daily basis, deal with business processes, and integration with external systems. I/O is going to be 90% of my machines load, if it's ever doing any real work in the first place. Most of the time it'll just sit waiting for a human to do something though.

Re: Designing state machines

#57
post #56

Earlier quoted context omitted.

It's just a silly observation. I come from a generation that had to create products like AutoCAD to run on CP/M systems with 64K of RAM and precious little permanent storage. As an aside, few people know that software like AutoCAD started on such --by today's standards-- crippled systems. Anyone who came up in software development through that path has a full understanding of the cost of every bit of code written, ev…

Well. I'll call it idiomatic form to use OO style in Ruby, which the article (and my examples) were written in. Considering the interpreted and managed-memory model of the language runtime, it is utterly pointless to worry about a few object instances. Heck, this is a language where primitives are objects. I suspect we work in quite different areas though, which probably gives us very different perspectives of what m…

Well, of course, "When in Rome do as the Romans".

Not saying OO has no place. Of course not. That would be crazy.

Re: Designing state machines

#58
The podcast Podcast.__init__('Python')[1] recently interviewed the developer of Automat module [2]. The author explains when you should consider using a state machine in your Python project. I didn't realize there were so many types of state machines, and the author explains where his module fits into this ecosystem.

[1]: https://www.podcastinit.com/automat-state-machines-with-glyp...

[2]: https://pypi.python.org/pypi/Automat/0.5.0

Post reply on HN