views:

212

answers:

4

Is it possible to convert code into a logical graph?


EDIT:

What I am thinking is that math is absolute. We can judge whether it is right or wrong for sure. But for code, there is something other than the logic. What I want to do is remove that 'something' to keep the logic there only.

A: 

At least the inverse is possible. When programming Siemen's PLCs one of the more common programming forms is "Ladder Logic", where you program graphically.

http://en.wikipedia.org/wiki/Ladder_logic

tekBlues
What about other languages like php,python and so on?
Shore
Shore, nothing about them!
tekBlues
@tekBlues,pity:(
Shore
A: 

Or like reverse engineered UML, with class and sequence diagrams?

Not as valuable as the code itself, IMO. Once you reach a meaningful degree of complexity it's impossible to take in an entire diagram or graph. And the problems that can be graphed effectively aren't worth the effort.

No debuggers or unit tests for graphical representations of any kind.

The value of pictures and design is to reach a point where you understand the problem well enough to code. But once you reach that point, I'd argue that the marginal utility of making them equivalent to engineering drawings isn't worth the effort.

duffymo
What I wish is to make the coding process more logic,though.
Shore
Graphing won't simplify inherent complexity. The only way to do that is with decomposition, both in code and diagrams.
duffymo
+1  A: 

it's possible. flowcharts are logical graphs and could easily be generated by a computer. Every code structure has a corresponding flowchart structure.

quick google brought up: Code Visual to Flowchart


as far as inverse, flowcharts are much too broad to convert to code. There are graphical programming languages though. for instance NI's labView fpga is all graphical programming which can then be converted into VHDL.

Victor
But it seems this kind of flowchart not so helpful to identify the problem,you think?
Shore
And is it possible to do the inverse?
Shore
well, flow charts were meant to be made to plan a task so you know the logic before implementation. but I think looking at directed graphs could help iron out certain problems in the code.although they are meant to be on a much higher level than code so looking at code via graphs could get pretty messy.
Victor
+1  A: 

Not sure this is exactly what you mean, but static analysers like clang parse code, determine all possible branches the code could take, and check for errors. For example, given the code..

1. if(somefunction()){
2.    this_will_break();
3. } else {
4.     if(anotherfunction()){
5.         printf("%i", str(something));
6.     }
7.     print("this is fine");
8. }

You will get output along the lines:

Line 1. Taking true branch.
Line 1. Error: Function this_will_break is not defined
Line 4. Taking true branch.
Line 5. Warning: Function passed incompatible type

You could potentially graph these paths, but I don't see why you would want to (it wouldn't really show anything)

Another possibly similar idea is Gprof2Dot, which takes profiling data, and draws a hierarchal Graphviz chart showing how long each function takes to run, which function called it and so on.

A final thing springs to mind, which is nodal based applications like Shake, or Quartz Composer - ways of "programming visually", representing code like Add(Blur(MyImage), Blur(AlphaChannel)) as:

[MyImage]--->[Blur]----\/
                      [Add]
                       /\
[AlphaChannel]----------

Those aren't logical graphs, but they are ways of visually representing code, which might possibly give you some ideas.. maybe.. unless I'm totally misunderstanding..

dbr
it's close.What I'm thinking is that:Math is absolute,say,we can judge it's right or wrong for quite sure.But for the code,there is something over the logic.What I want to do is remove that 'something' to keep the logic there only.
Shore