ERNI Technology Post No. 40 - Using Notepad++ as Editor for Structured Definitions of Algorithms

Introduction

For programmers, “specifying” an algorithm is quite easy: They code it. Source code is clearly one of the best ways to specify an algorithm. There is no manual transformation necessary between specification (source code) and running algorithm (compiled code) as this is done completely automatically. This transformation is correct by definition provided we trust the compiler.

The problem is that often the persons who have the business knowledge of algorithms are not able to express their thoughts in code. When persons who do not have much knowledge about programming languages have to specify algorithms, they do so in natural language. This seems to be a very simple and logical approach. But natural language is full of possible misunderstandings, synonyms, homonyms, unknown acronyms and unclear semantics. So it is very likely that the specification they write on paper is interpreted in different ways and that the transformation, or translation, of a specified algorithm into source code, which is performed by a developer who does not know about the logic of algorithms, leads to errors and problems in implementation.

A similar problem occurs if the programming language that is used to implement an algorithm cannot adequately represent the semantics of the algorithm in the way which was intended by the author. If, for example, you have a language with recursion as its main control structure, and you want to specify some sort of loop, this is possible but not easy to understand because of the manual transformation between programming language and algorithm idea.

Of course, such problems occur all the time when specifying. But when specifying business algorithms, this often poses a very critical problem. Regarding business algorithms, the common understanding of a business expert and a development expert might be minimal.

Here it should be possible to establish a solution that enables business experts to specify algorithms in as much natural language as possible, but using enough formalism to facilitate their straightforward transformation into programming language, by systematically cutting the number of interpretation possibilities.

Requirements for the solution

A general solution for this problem should have the following components:

  • A kind of special language, to reduce interpretation possibilities
  • A process to guide the business specialist how to write down his ideas
  • A tool to support the two points above

The requirements for such a solution are:

a)      It should enable persons to write down algorithms in a clear, unambiguous way.

b)      The manual transformation effort should be minimized, meaning the transformation should closely correspond to the way an algorithm specifier is thinking.

c)       The specifier should be warned if he specifies something that is not clearly defined.

d)      The result should be perfectly easy to read.

A very simple solution with Notepad++

Using the Notepad++ text editor with a user-defined pseudo-code language is an extremely simple solution and implements many of the requirements listed above, as we will see.

Notepad++ has some features that help to develop the solution quite easily:

  • Installation is easy, the tool is free.
  • Working with Notepad++ is easy, but complex scenarios can also be dealt with.
  • Adding a user-defined language is quite easy.
  • Efficient search possibilities help to understand the scenarios.

A simple (pseudo-code) language that might be sufficient for the beginning may be defined as follows. It is a language which is fairly similar to the one used by a customer.

1)      Define some different keyword lists: the first for general keywords like “if”, “else”, “int”, “string” and so on; the second for logic that is solved by existing frameworks, tools and other software like “sort”, “check”, “pivot” and so on.

2)      Define some sort of “boundaries”, meaning regions that can be opened and closed, such as in xml or {} in c#. One boundary for functions ([]), and one boundary for plain text (“”).

3)      Define that normal text is displayed in red, because text is not thought about when just typed; text must either be a keyword or a comment or something belonging to one of the three boundaries.

4)      Define some helpful add-ons like a preceding underscore for known atomic objects that need no more explanation.

That results in a very easy workflow: You enter whatever you want. It appears in red letters unless you define that it is a keyword, a comment, something like a function call, something like an object or something like text that you have clearly intended to be just plain text.

How to define the language in Notepad++

A user-defined language is very easily added to Notepad++. First select “User-Defined Dialogue…” (which means user-defined language) in the View menu:

The user-defined language window is shown. Now define your language. For each group of definitions you find an appropriate tab in the window.

When done, you may save your language by clicking the button “Save As…”.

The process

The process is very easy:

  • Specify your algorithm
  • Try to eliminate whatever is red. This can be done by
    • Defining a sublogic with brackets []
    • Defining something as an atomic thing (usually object) by adding a preceding underscore (_)
    • Defining something as clear by putting it in double quotes (“”)
    • Some further operations might be specified, which goes beyond the scope of a techletter.

An example

We try to specify an algorithm that compares two files. Depending on the business specialist, the workflow could be done by him himself, or worked out by a team comprising business specialist and developer. In the latter case, the developer asks the right questions to eliminate the red passages. The solution then also serves as a “meeting protocol”.

First step: Write your algorithm:

At the beginning, everything is red, which means it is not well-defined. We try to eliminate “choose” and “compare” by defining sublogic, and define atomic objects, our files. This way, we eliminate the word “another”, by giving a clear definition of two files. We define that our algorithm works with exactly two files. A clear sublogic is then defined by putting it into double quotes. It should be seen that our algorithm gets more detailed as we try to eliminate the red passages.

Now we have to eliminate the result and the unknown specification of “compare”:

Of course “compare” is not very well defined, you could go into more detail if you wanted to.

The important thing is that the writer has to think his definition over and that he tries to reduce the amount of red passages. The definition therefore becomes more and more formal.

Conclusion

The requirements a) to d) are fulfilled with such a combination of tool, language and process. Of course the definition of the language has to be chosen carefully, to be understandable, not too complex but expressive enough.

With Notepad++ and a user-defined language optimized to meet a customer’s needs, it is very easy to create an environment that helps a specifier to define algorithms in a semi-formal way. This minimizes the room for misinterpretation when transforming the natural language into code. It also helps to conserve knowledge as it is written down in a standard way.

Although there are certainly better and more sophisticated solutions, it is worth trying this approach since it involves very little cost and time.

References

[1] http://notepad-plus-plus.org

posted on 27.09.2012
Categories: 
by: Peter Maerz