REGULAR TABS

From ThoughtBridges
Revision as of 03:26, 6 April 2017 by Kris (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Predictable tabs

Metapattern Code: REGULAR_TABS (with an underscore between REGULAR and TABS)

For Graphics Designers

Source code material like scripts is normally viewed in a text editor using a non-proportional font, like Courier or Monaco.

Each character is supposed to have the same width, and tabs are expanded into an equivalent of a number of spaces.

You can view scripts using a proportional font, but that will often ruin a carefully crafted layout.

It is best is to leave the text editor set to display scripts in a non-proportional font.

Just like in page layout and word processing, tab characters are often used to add some visual structure to a script.

One or more leading tabs are used to ‘indent’ certain segments of the source code.

A major difference between tabs in a script file or text file, versus tabs in a printable document, is that the tab width settings are not stored with the text file.

Instead, the tab settings are a preference of the text editor program.

Depending on the text editor used, and the way it has been configured, tabs in a text file will be expanded differently in different environments.

When a text file is transported from one computer to another, the look-and-feel of the file will change when it relies on tab characters for visual formatting.

If you want to make sure the script file looks as similar as possible to the original as seen by the script developer, you will need to configure your text editor to have the exact same tab width settings.

A problem occurs when the script developer has inadvertently used a mix of tabbed indents together with spaced indents: some indented lines are indented using one or more tab characters, other lines are indented using multiple space characters.

Such a script will look fine on the developer’s machine.

But when the script is opened in a text editor with a different tab setting, the layout will be ruined.

If the software developer has followed the REGULAR_TABS metapattern, the script should look fine independent of whatever tab setting you have in your text editor, because care has been taken to either only use tabs (and no spaces) or only use spaces (and no tabs) for indenting.

In TextWrangler you can configure the tab settings here: TODO

In Notepad++ you can configure the tab settings here: TODO

For Software Developers

To guarantee proper formatting in text editors with different tab settings, it is important to be consistent in the use of tabs for indenting.

You should either use tabs for indenting or spaces for indenting, but not both.

My approach to this is to not worry about it during the development phase, and clean it up before release.

Set the editor to your favorite setting (e.g. use auto-expanding tabs if you prefer spaced indents), but don’t worry about the occasional misplaced tab or space.

When the time comes to release a version of the script into ‘the wild’ should you clean this up.

I use the ‘entab/detab’ functions in TextWrangler or Notepad++ to quickly clean this up.

First of all, convert all tabs to spaces, and glance over the layout. Clean up anything that might have inadvertently shifted.

Then leave the file ‘as-is’ if you prefer spaced indents.

If you prefer tabbed indents, use the text editor’s entab function which will automatically convert leading spaces into tabs.

The end-result is a consistently formatted file which will display properly independent of the tab settings in the text editor.

Make sure to only use 'leading tabs'.

Tabs in amongst some text will cause your carefully crafted layout to break down on computers where the text editor is set to a different tab indent setting.

Detailed description of the metapattern

Make sure the script file either uses tabs or spaces for indents, but not both.

Make sure the script does not use tabs in the middle of any line.

These tabs will cause the layout to break down when the file is viewed with a different tab width setting.

In either case, mention the width of the indent used in the script intro (INTRO) (i.e. the number of spaces equivalent to a one-level indent).

Example

Consider the following InDesign ExtendScript, which we’ll use as a sample script for a number of metapatterns.

The code has a fair amount of issues, but we won’t discuss them here. See the Appendix ‘MakeTemplate.jsx’ for a properly ‘pokeable’ version of this script.

First the script viewed in TextWrangler with a tab with of 4 spaces. Everything is lining up properly: line 7 is indented by one level, line 21 is indented by two levels...


TWTABS1.png

Viewing the same script with a tab width of 2 spaces is not so pretty.

The indenting around like 18-21 is inconsistent; the text on line 11 does not line up with the text on line 10.

TWTABS2.png

Advanced text editors like TextWrangler can be configured to show tabs as special glyphs, which clearly demonstrates the problem: the lines are inconsistent: line 18 starts with a tab; line 19 starts with four spaces.


TWTABS3.png

To fix this, a few things can be done:

The software developer could add a comment to the start of the script explaining that this particular script needs to be viewed with a tab width of 4 spaces.

The better solution is for the software developer to clean up the script and apply the REGULAR_TABS meta-pattern.

Applying REGULAR_TABS would mean that they either

  • need to get rid of the tabs all together
  • or apply them consistently for the line indents, and replace the ‘middle of the line’ tabs on lines 10 and 11 with equivalent spaces.

Related metapatterns

 LINE_END_CRLF
 INTRO