jump to navigation

JavaScript is Not Industrial Strength, Unless… (part 1) May 6, 2006

Posted by Tom Snyder in : Ajax Technology , trackback

JavaScript, the ‘J’ in Ajax, does not include a preprocessor. Since the 1970s programmers have been using the C preprocessor as a fundamental part of professional software development. With the preprocessor, they can:

industrial.jpg

Without a preprocessor or macro facility, all your JavaScript code is always included in every release on every platform. Either you don’t use the crucial asserts and diagnostic routines used during development, or you include them in the public release of the program. Leaving them in the public release makes it larger, slows it down, and confuses your users with jargon laden pop-ups. You can turn some diagnostics and pop-ups off using global boolean variables, but this still sends part of your development tool set to each and every user. We consider this unacceptable for any but the simplest projects with small user communities.

Now, if you are a Unix (←click it, it’s cool) born programmer, you are confidently chuckling to yourself: “There are plenty of macro packages to choose from.”  Very true. You can incorporate M4, Sed, Awk, or any number of other file processing macro facilities into your JavaScript environment. I’m writing this post to reinforce the importance of doing so, and to let you know you have one additional choice.

It turns out that JavaScript syntax is close enough to C that you can actually use the C preprocessor on your JavaScript code. We chose the C preprocessor because virtually every programmer knows C, compilers are readily available on all platforms, and the preprocessor is incredibly straightforward. Once you know #define and #ifdef, you’re ready to rock.

Here’s how:

  1. Place a subdirectory under your JavaScript source directory called ‘jsout’.
  2. Create a makefile with dependencies to build the ‘jsout’ files from the source files. EGAD!  A makefile for JavaScript! A sample is given below.
  3. In the makefile, set your compile command to: cl -EP srcfile.js > jsout/srcfile.js
  4. Set your Apache config to point your js directory now to jsout.

My next Ajax Technology post will discuss whether to create separate builds for separate browsers. You purists are going to shudder.

SAMPLE MAKEFILE:

PREPROCESS_CORE=gcc -E -P -C -w -x c
PREPROCESS=$(PREPROCESS_CORE) $(DEFINES)
IEDIR=js

# gcc -E=Preprocessor only,
#     -P=don’t include line number decls, they’ll confuse JavaScript.
#     -C=don’t remove comments. It helps us debug to leave them in.
#     -w=don’t warn
#     -x= use the C language as the input file language
#        (this allows us to preprocess it at all)
#     -o= output file.

# DEFAULT TARGET: executing make with no arguments will build the debug
all: list-of-files

# — inetword.js interface object is not customized for Moz:
jsout/afile.js: afile.js
    cp $< $@

 

Comments»

no comments yet - be the first?