Difference between revisions of "ALPHABETIZE"

From ThoughtBridges
Jump to: navigation, search
(Examples)
(Related metapatterns)
 
Line 127: Line 127:
 
   [[READABLE]]
 
   [[READABLE]]
 
   [[REDUCED_ROT]]
 
   [[REDUCED_ROT]]
   [[REGULAR_TABS�]]
+
   [[REGULAR_TABS]]

Latest revision as of 02:58, 6 April 2017

Alphabetize Functions, Constants and Variables

Metapattern Code: ALPHABETIZE

For Graphics Designers

When you encounter a script that has this metapattern applied, you know that the functions and a lot of the (global) variables and constants in the script are listed in alphabetical order.

If you use a ‘smart’ text editor like TextWrangler or Notepad++, the editor will automatically find out the names of all the functions in the script and give you an easy way to ‘jump’ to any of the functions.

With those kind of editors, there is no need for this pattern.

But you might be looking at a printed version of the script, or have it opened in a text editor that is not JavaScript/ExtendScript-aware.

In those cases, having alphabetically ordered functions is a boon when trying to read a script.

Following the flow of execution often forces you to look up many functions, and when they’re listed alphabetically, they’re easier to find.

For Software Developers

As a software developer, having your functions alphabetized is rarely important, but alphabetizing will help readers of your script.

Alphabetizing global variables and constants often helps keep similar or related items grouped together when combined with a judiciously chosen, appropriate naming.

Detailed description of the metapattern

Order the functions in your source code in case-insensitive alphabetical order.

Constants and globals listed at the top should also be alphabetized as much as possible.

By carefully naming related variables or constants using a regular, predictable naming scheme, you can make sure mutually related names end up 'close together' when ordered in alphabetical order.

Examples

Force tag name and attribute constants to group together by prefixing them with a shared prefix - e.g. kXMLAttr..., kXMLTag...

...
const kXMLTag_FlattenPDFResponse = "flattenedpdf";
const kXMLTag_FlattenPDF = "flattenpdf";
const kXMLTag_ImageZone = "imagezone";
const kXMLTag_LayerSelectionZone = "layerselectionzone";
const kXMLTag_Page = "page";
const kXMLTag_Process = "process";
const kXMLTag_Request = "request";
const kXMLTag_Response = "response";
const kXMLTag_SectionDIY = "sectiondiy";
const kXMLTag_SectionTemplateBased = "sectiontemplatebased";
const kXMLTag_TextZone = "textzone";
const kXMLTag_Zone = "zone";

const kXMLAttr_AssetAttributeValueId = "assetAttributeValueId";
const kXMLAttr_AssetId = "assetId";
const kXMLAttr_BlackAndWhite = "blackAndWhite";
const kXMLAttr_ColorTemplatePath = "colorTemplatePath";
const kXMLAttr_CropData = "cropData";
const kXMLAttr_FontSize = "fontsize";
const kXMLAttr_FontTemplatePath = "fontTemplatePath";
const kXMLAttr_Height = "height";
const kXMLAttr_LayerNames = "layernames";
const kXMLAttr_LineCount = "linecount";
...

Examples of alphabetized functions (this is an excerpt from a real project; the actual coding for the functions has been removed.

function AdjustCheckedFile(file) 
{
...
}

// ****************

function AppliedCSSStyles(tag, caches)
{
...
}

// ****************

function BuildBook(serverContext, request) 
{
...
}

// ****************

function BuildDocument(caches, serverContext, request) 
{
...
}

// ****************

function BuildPDFPreview(serverContext, request) 
{
...
}

// ****************

function CreateCheckedFile(filePath) 
{
...
}

// ****************

function CloseAllDocs() 
{
...
}

...

Imagine your’re reading the BuildPDFPreview function and find that it refers to the CreateCheckedFile function, and you want to have a look at that second function.

You’ll know that this function comes after BuildPDFPreview, so you need to browse forward to find it.

Related metapatterns

 INTRO
 LINE_END_CRLF
 MAIN
 READABLE
 REDUCED_ROT
 REGULAR_TABS