Live data from Hacker News

Flowcharts of programming language constructs

progsbase.com

31–40 of 88 posts

Re: Flowcharts of programming language constructs

#31
post #2

A brilliant explanation of why no-one uses flowcharts.

...because if they did, they might realize that some of their favorite structures are a hot mess. Seeing things in a new way often leads to learning, which a good developer would welcome. Personally I can't remember the last time I used an actual flowchart, but I do use state machines. Sometimes I'll go through an exercise of re-casting complex logic as a state machine. Often, that leads to a realization that making…

> ...because if they did, they might realize that some of their favorite structures are a hot mess.

They may only be a hot mess when expressed as a flowchart. But so what? I'm not programming in flowcharts. If exceptions can do what I want cleanly, why do I care if it's a mess considered as a flowchart? That's not my problem.

This argument is like saying that the library function I call is a mess of a flowchart. Why should I care? Can I express what I need to express cleanly by calling that function, or not?

Of all the arguments against exceptions, "it's got a mess of a flowchart" is the absolutely least relevant one.

Re: Flowcharts of programming language constructs

#32

Pretty neat. Three things that occurred to me while reading it. (1) Makes it pretty clear why exceptions are even more of a control-flow nightmare than goto. What a messy graph. (2) The 'defer' diagram is a big WTF. Couldn't even figure out which box is supposed to represent which statement in the example. (3) I'd love to see a graph of the control flow when you throw in a few spurious lambdas like "advanced" C++ pro…

My takeaway was the exact opposite of yours. For example the goto graph seems very intuitive and simple. But it is much more complex to reason about compared to exceptions. My takeaway was that simple graphs like these can't really explain the pros and cons of high level programming constructs.

Re: Flowcharts of programming language constructs

#33
post #2

A brilliant explanation of why no-one uses flowcharts.

...because if they did, they might realize that some of their favorite structures are a hot mess. Seeing things in a new way often leads to learning, which a good developer would welcome. Personally I can't remember the last time I used an actual flowchart, but I do use state machines. Sometimes I'll go through an exercise of re-casting complex logic as a state machine. Often, that leads to a realization that making…

I strongly agree that multiple ways to look at things is good.

Programming languages have different "things" built in. Like, if you have re-entrant function calls, then your language has a built-in data structure called "the stack". If you have some algorithm that needs a stack, you have the choice of using the built-in stack, or using an explicit stack.

If you have a programming language with some sort of type system, then you have tagged data built-in. You can model your domain in the type system, or you can model your domain using a tuple like (tag, datablob).

If you have object-oriented programming with dynamic (single) dispatch on the first argument, then your language somewhere has a built-in data structure called a table/dictionary/associative-array. If you want to implement some sort of switching behavior you can do a switch on the `tag`, or encode the tag in the type system, and use dynamic dispatch.

Some programming languages have support for asynchronous calls. That programming language has a built-in scheduler of sorts. You can use it, or build your own, (or use the OS!).

A state machine, I think, is a great example of the same idea. Your CPU has a bunch of state (e.g. the program counter), and your programming languages has a bunch more state (like the current stack frame, the line number). You can encode your state machine in that, or you can model it explicitly. How well you can do it "implicitly" depends on other language features, like whether you have coroutines.

I think it's challenging, without expertise that might come only from having solved an identical problem already, to know ahead of time when to use these built-in things or roll your own. When you mention testing, I think that's a great example of expertise you've acquired. If you would like to test certain invariants in your state machine, e.g. "a transition from A to B never happens", then you're going to have a hard time doing so if your state machine's state is encoded in the line number of your language interpreter or the program counter, or whatever.

Re: Flowcharts of programming language constructs

#34
post #5
post #2

A brilliant explanation of why no-one uses flowcharts.

They can be useful though, like once in a blue moon on a white boarding session with non-technical users, explaining a bug or something like that

They're great for compressing lyrics!

Rick Flow:

https://pbs.twimg.com/media/DHnyCwiXsAAFyzq.jpg

Circumstances In Which Whipping It May Be Appropriate:

https://gypsywitchcampfire.files.wordpress.com/2015/08/whip-...

Total Eclipse of the Heart:

https://i.pinimg.com/originals/86/17/e8/8617e878f9c33ab3a3ba...

We Will Rock You:

https://www.edrawsoft.com/template/lyric-flowchart.png

Let It Be:

https://i.pinimg.com/originals/a7/74/b5/a774b576f45a6eb71a42...

All You Need Is Love:

https://www.researchgate.net/profile/Alex_Ruthmann/publicati...

Modern Love:

https://i.pinimg.com/originals/8f/fc/9a/8ffc9a199caa1da49ec1...

Re: Flowcharts of programming language constructs

#35

Earlier quoted context omitted.

> Exceptions are rather easy to reason in the code In some code, for some people. > their flow chart looks like a hot mess In code where the "exceptions are rather easy to reason" about the flowchart would also be easy to read. In code where the exceptions hide a flowchart that's a hot mess the exception syntactic sugar hides the mess.

One could argue hiding the mess is the whole point of the exception. It allows you think about the problem with a much smaller cognitive load. I'm not suggesting every code with exceptions is good. Just saying that not every exception is bad, and many are indeed great. I can't imagine writing Python without try blocks...

> I can't imagine writing Python without try blocks...

In other words, Python requires that model, but making something necessary isn't the same as it being good. If you go around breaking people's arms then splints and casts become necessary. Does that make arm-breaking OK?

> It allows you think about the problem with a much smaller cognitive load.

There's a very fine line between "smaller cognitive load" and "sweeping stuff under the rug". Since you mentioned Python, I'll point out that a lot of Python code only looks simple because it's incomplete and will fail catastrophically for what should be innocuous errors. Add the proper error handling and it's just as complex as code using an error-return or optional-type model. Often that means even more cognitive load because the happy path and the handlers are in multiple methods/classes/files (especially likely as multiple people hack on code over time).

Re: Flowcharts of programming language constructs

#36

Pretty neat. Three things that occurred to me while reading it. (1) Makes it pretty clear why exceptions are even more of a control-flow nightmare than goto. What a messy graph. (2) The 'defer' diagram is a big WTF. Couldn't even figure out which box is supposed to represent which statement in the example. (3) I'd love to see a graph of the control flow when you throw in a few spurious lambdas like "advanced" C++ pro…

> when you throw in a few spurious lambdas

What do you mean?

Re: Flowcharts of programming language constructs

#37
post #6

Pretty neat. Three things that occurred to me while reading it. (1) Makes it pretty clear why exceptions are even more of a control-flow nightmare than goto. What a messy graph. (2) The 'defer' diagram is a big WTF. Couldn't even figure out which box is supposed to represent which statement in the example. (3) I'd love to see a graph of the control flow when you throw in a few spurious lambdas like "advanced" C++ pro…

Exception is a nightmare only if you use it as a general purpose control flow mechanism.

That's the exception that proves the rule.

Re: Flowcharts of programming language constructs

#38

Earlier quoted context omitted.

...because if they did, they might realize that some of their favorite structures are a hot mess. Seeing things in a new way often leads to learning, which a good developer would welcome. Personally I can't remember the last time I used an actual flowchart, but I do use state machines. Sometimes I'll go through an exercise of re-casting complex logic as a state machine. Often, that leads to a realization that making…

> ...because if they did, they might realize that some of their favorite structures are a hot mess. They may only be a hot mess when expressed as a flowchart . But so what? I'm not programming in flowcharts. If exceptions can do what I want cleanly, why do I care if it's a mess considered as a flowchart? That's not my problem. This argument is like saying that the library function I call is a mess of a flowchart. Why…

The flow charts in the link represent control flow. Compiler optimizations are, in part, constrained by their ability to map and statically determine control flow. So I think it is a grave mistake to say that something having a ‘mess of a flowchart’ is the least relevant concern about some PL construct. The implication that a flowchart representation offers nothing to a programmer, implies that the performance characteristics of your code does matter to the programmer. So sure, in some application domains (although I don’t think there are any) performance may take a backseat to performance and efficiency, this information is useful and can very well illustrate the pitfalls and trade offs of using certain constructs in code.

Re: Flowcharts of programming language constructs

#39
post #2

A brilliant explanation of why no-one uses flowcharts.

These graphs are how the compiler I work on represents your programs while it's compiling them!

This is essentially the point I made in a comment above, these representations are valid considerations for a programmer to weigh when selecting abstractions and constructs to implement some functionality. But it seems like very few people agree.

Re: Flowcharts of programming language constructs

#40

As someone who's self-taught everything, this is... awesome. It's exactly what I needed to be able to up my game in describing code to management using the flowcharts they've asked for.

If you find these flowcharts instructive, and I think that you are well founded in doing so, good on you. A majority of other comments seem to be of the opinion that pointing out the complexity of control flow generated by the constructs in a language is a negative. It certainly seems like the prevailing attitude is ‘if I can’t see it, it doesn’t exist.’ But you said you were working on game code, so I will encourage you to keep thinking about the complexity of the choices you make in which constructs to use and don’t buy into the thought that says performance implications are not a really concern.
Post reply on HN