DESOLATION

Help manual · Coding

Style

One of the most important aspects of coding is readability. Not only is it important if you ever need someone to debug what you’ve written, but there are often cases where people will want to use your work to assist in their learning, and if you are coding areas in part of a domain, or are doing mudlib coding, it is not at all infrequent for other coders to have a need to modify your work, especially if you are, for some reason, unable to do so yourself. Your local mud administrator may need to review the content of your code. You may want to get an opinion on something you have written. Remember, code is usually only written once, but read many times. Thus, it is important that the people who will be reading your code can do so easily.

Layout

One important way to maximize the readability of your code is to use layout techniques to emphasize program structure.

i       = x 
foo     = bar [i];

Consider the following piece of code:

 void foo(){
 int i,j;
 object people;
 do_this();
 do_that();
 i=bar();
 while(i--)do_some_stuff();
 people=users();
 j=count(people);
 if(j<20){
 do_something();
 do_something_else();
 }
 }

The above code is much less readable than the same piece of code when following the above principles:

void foo()
{

  int   i, j;
  object        people;

  do_this();
  do_that();

  i  = bar();

  while( i-- )
         do_some_stuff();

  people   = users();
  j        = sizeof( people );

  if( j < 20 )
  {
     do_something();
     do_something_else();
  }

}

Make sure, however, that you align things systematically. If your variable assignments are indented 2 spaces in one function, then 40 in the next, code suddenly becomes a lot less readable. The muds auto-indenter can sometimes be useful in helping indent well, however, your version of the indenter may have problems, depending on the version of the driver your local mud is running, so it’s best to try not to rely upon it.

Also note that the auto-indenter is invoked on most muds with ‘I’ from the ‘:’ prompt in ed.

Commenting

Some programmers like to use a lot of comments. In many programming classes, people are told that you can never comment enough. However, there are several problems with comments:

The following is real code:

// increment i
i = i+1;

Isn’t that obvious already??

Identifiers

Sometimes, however, if you leave comments out, the intent of the code is not clear whatsoever. LPC is not basic, your variable names are not restricted to 6 letters, or 8 letters. Keeping in mind that code should not need to be commented to be readable:

 spell_target = find_living( "foobird" );
 foobird = clone_object( "/obj/foobird" );

instead of:

 o = clone_object( "/obj/foobird" );

Portable Code

In any code, having constants in the file code may make it difficult to tweak the code or to move the code and still have it work later. In such cases, #define is your friend. #define’s for constants not only make portable, configurable, readable code, they isolate any changes that ARE made to one place.Below are some handy tips:

#define MAX_ELEMENTS_IN_ARRAY   50
  if( sizeof( array ) >= MAX_ELEMENTS_IN_ARRAY )....

as opposed to:

if( sizeof( array ) >= 50 )....

Not only does it allow people to easily modify your program if desired, it makes it much more obvious what your program is doing. This also makes your code portable.

#define CASTLE_ROOMS    "/wiz/rust/rooms/"
  set_exits( ([ "east" : CASTLE_ROOMS "courtyard", ]) );

as opposed to:

  set_exits( ([ "east" : "/wiz/rust/courtyard", ]) );

However, though #defines can be very helpful for good coding, it is important to realize when they are being abused. You CAN have too many #define statements, and you CAN #define the wrong things. Why is this? Basically, macros are difficult to follow when there are a lot of them. This is especially true from people trying to use your code to help learn. Seeing TP as an abbreviation for this_player() is not uncommon, and is almost always seen out of context. For people who don’t quite understand macros, it is VERY confusing to see TP, and trying to figure out what it means. Also, even the informed reader may not find your macro immediately obvious, even if you do, and would likely prefer not to have to search through your files to resolve every definition.

Other Handy Things

Any questions, comments, criticisms, or suggestions for improving this doc should be directed to:

e-mail: jtv2j@virginia.edu

mud mail: rust@tmi-2

4-8-94

Category:Coding