ContentsImportant RulesProgrammers Should Not Design Web Pages
Previous: Web Page Designers Should Not Write Programs | Next: Proprietary Solutions Have Hidden Costs

Programmers Should Not Design Web Pages

I hope that last example made you say "web page designers shouldn't have to learn the TCL programming language".  That was the intent.  Getting the programmer to realize that they shouldn't have to learn HTML is a bit tougher.  Bear with me and I'll try to explain why.

First, let's do exactly the same example in Java.  Once again, I'm showing what a programmer can do but shouldn't.

Please remember that I'm showing you what you should NOT do as a programmer!

Suppose you want to provide a WELD command that lets the web page designer see a pallet of sample HTML colors by just placing "[colors-wrong-way]" in their web page where ever they want the table of color samples to appear.  This is the class you might write:

import tcl.lang.*;

public class ColorsWrongWayCmd implements Command {

public void cmdProc(Interp interp, TclObject [] args) throws TclException {
  StringBuffer sb = new StringBuffer();
  sb.append("<table border=3><tr><th width=20%>Color");
  sb.append("<th width=10%>Red<th width=10%>Green<th width=10%>Blue");
  String [] range = {"00", "40", "80", "C0", "FF"};
  for (int red = 0; red < range.length; ++red) {
    for (int green = 0; green < range.length; ++green) {
      for (int blue = 0; blue < range.length; ++blue) {
        String rgb = "#" + range[red] + range[green] + range[blue];
        sb.append("<tr><td bgcolor=" + rgb + " align=center>");
        sb.append(rgb);
        sb.append("<td align=center>");
        sb.append(range[red]);
        sb.append("<td align=center>");
        sb.append(range[green]);
        sb.append("<td align=center>");
        sb.append(range[blue]);
      }
    }
  }
  sb.append("</table>");
  interp.setResult(sb.toString());
}

}

Sadly, this works.  You might even think it's the right way to handle this request.  Why not do it this way? - because the programmer has taken over control of the appearance of the resulting web page:

Putting web page design elements in the Java code may simplify the use of the command for most cases, but making changes would require a Java programmer and would have the undesirable effect of changing the output format everywhere that the command is used!

This command would be a lot better if it processed the command arguments to give the formatting control back to the web page designer, where it belongs.  Usage in the WELD file would then be something like:

	colors varname range rowformat colformat

For example, the WELD file might contain something like this:

	<table border=3><tr><th width=20%>Color<th width=10%>Red<th width=10%>Green<th width=10%>Blue
	[colors x {00 40 80 C0 FF} {<tr><td bgcolor=#$x align=center>#$x} {<td align=center>$x}]
	</table>

Here's the improved version of the "colors" command - a little more Java code and a little more WELD syntax for the web designer to specify, but much more flexible:

import tcl.lang.*;
import java.util.*;

public class Colors implements Command {

public void cmdProc(Interp interp, TclObject [] args) throws TclException {
  if (args.length != 5) {
    throw new TclNumArgsException(interp, 1, args,
      "varname range rowformat colformat");
  }
  String varName = args[1].toString();
  StringTokenizer st = new StringTokenizer(args[2].toString());
  String [] range = new String [st.countTokens()];
  for (int j = 0; j < range.length; ++j) range[j] = st.nextToken();
  String rowCmd = "weld {" + args[3].toString() + "}";
  String colCmd = "weld {" + args[4].toString() + "}";
  StringBuffer sb = new StringBuffer();
  for (int red = 0; red < range.length; ++red) {
    for (int green = 0; green < range.length; ++green) {
      for (int blue = 0; blue < range.length; ++blue) {
        interp.setVar(varName,
          TclString.newInstance(range[red] + range[green] + range[blue]),
          TCL.NAMESPACE_ONLY);
        interp.eval(rowCmd);
        sb.append(interp.getResult());
        interp.setVar(varName, TclString.newInstance(range[red]), TCL.NAMESPACE_ONLY);
        interp.eval(colCmd);
        sb.append(interp.getResult());
        interp.setVar(varName, TclString.newInstance(range[green]), TCL.NAMESPACE_ONLY);
        interp.eval(colCmd);
        sb.append(interp.getResult());
        interp.setVar(varName, TclString.newInstance(range[blue]), TCL.NAMESPACE_ONLY);
        interp.eval(colCmd);
        sb.append(interp.getResult());
      }
    }
  }
  interp.setResult(sb.toString());
}

}

Without making any further changes to the Java implementation of the command, the web page designer now has a tool to do some unexpected things such as putting something like this in the WELD file:

	<ul>[colors curcolr {40 C0} {<li>This text is <font color=#$curcolr>in color</font>} {}]</ul>

Which would result in this output:

Another use might be:

<form action=somthing>
	[colors curcolr {40 C0} {
		<input type=radio name=tcolr value=$curcolr>
		Show text in <font color=#$curcolr>this color</font><br>} {}]
<input type=submit></form>

Which would show up like this in the user's browser:

Show text in this color
Show text in this color
Show text in this color
Show text in this color
Show text in this color
Show text in this color
Show text in this color
Show text in this color

In other words... the programmer should not output any HTML - that should be left to the web page designer's creativity!


ContentsImportant RulesProgrammers Should Not Design Web Pages
Previous: Web Page Designers Should Not Write Programs | Next: Proprietary Solutions Have Hidden Costs

Modified: Thu Apr 13 14:32:00 EDT 2000