Source Code Formatters

Concept

A source code formatter accepts a program source file, and generates another equivalent source file which is nicely formatted according to the source language syntax, including indentation, normalized case for identifiers, etc. Example Formatted C text

The advantage of using a source formatter is severalfold:

  • Properly nested indentation makes it much easier to identify matching program blocks
  • No more manual re-formatting of programs for readability
  • No more religious wars about the "right format"; automatic formatting kills the argument
  • A standard style used across an organization enables faster work because programmers know what to expect
  • Code from other organizations can be easily standardized
  • Smaller DIFFs when doing code merges
  • Eventually your customer will see some of your company's code, and presentation matters.

Programmers spend 50% of their time just looking at source code. Formatting source code can make them more productive in this task, saving a significant amount of an IT department's time and budget.

A related topic is obfuscators, which are "formatters" designed to make code very difficult to read and comprehend, to discourage reverse engineering.

Technology

Many conventional formatting tools use ad hoc string processing methods to implement the formatting process. This can work pretty well for many sample files. But it often fails when multiple statements per line, nested comments, comments around incomplete blocks of code or keywords, obscure language features such as escapes in quoted strings, etc. are encountered, as they always are in large systems of software. The result of such failure is badly formatted source text, or worse, program source that is no longer is acceptable to the source language compiler.

The most reliable way to build a formatter is to parse and unparse the source language according to the source language lexical and syntax rules. This ensures that the syntax structures found match those of the language. All of SD's formatters work this way, because they are based on DMS's ability to parse and prettyprint source files, and are based on the language definition modules used to drive DMS for large scale software reengineering tasks.

Another useful feature of SD's formatters is the availability for many languages (and the practical possibility of obtaining them for custom languages or dialects). All SD's formatters are designed to operate as command-line style programs, to enable inclusion in scripts. Consistent handling of formatting switches and I/O conventions across formatters aid software engineering staff when handling the multiple languages typically used by an organization.

Files can be formatted one at a time, or, using a project file to specify a list of files, in entire batches. Project files can be built using a text editor, or using a GUI file-selector provided.

Source files can be formatted to US-ASCII (ISO646), Western European ASCII (ISO8859-1), or Unicode representations. This is convenient for applications using non-English text strings or comments.

SD's formatters are presently available on Windows 2003, XP, and later operating systems.

Available Formatters

SD offers a family of formatters based on DMS. Presently available are:

Download an evaluation copy

The following formatters are in Beta. Early adopters, please inquire:

Custom Formatting Options

Semantic Designs can build custom formatters with special features:

  • Unusual languages or dialects
  • HTMLized output with colorized keywords
  • Source Browsers: HTMLized output with clickable cross-references (examples):
  • Automatic insertion of structured comments for data declarations, functions, etc.
  • Anti-formatters or code obfuscators (rename variables, drop comments, remove indentation and line breaks)
For more information: info@semanticdesigns.com    Follow us at Twitter: @SemanticDesigns

Source Code
Formatters