Home Blog Page 8545

How to document your code

By Jenn Vesperman

Many programmers don’t know what to write in code documentation, and the lack of documentation remains a frequent complaint about Open Source or Free Software programs. Common advice is “write what you’d want to see if you were reading the code,” but that’s vague and not entirely helpful. Programmers need specifics: clear guidelines, in categories and with reasons they can understand.

A mnemonic is also useful. I’m going to use the “how, when, where and why; what, which and who?” list of question starters.

What does it do?

This is the most important question. What does the program do? If I’m trying to fix a bug report that says “it doesn’t foo the bar,” I need to know if the bar is supposed to be fooed.

Write at least a sentence for each function, a paragraph or more for each module or group of functions, and at least a paragraph for the program itself. In these, explain what the code is supposed to do.

Add a comment after every few lines of code stating what those lines are intended to do, put these in every time you move to a new step of the algorithm.

How does it work

This is only slightly less important than “what does it do?” Admittedly, it’s often possible to answer this one by reading the code — but only if the code isn’t buggy.

If you have a loop counting from 1 to 20, I can’t tell if you’re accidentally or deliberately ignoring element zero. The only way I can know for sure is if you commented the code.

Any code where you can expect an error should have a comment stating your intention. I suggest commenting places where an off-by-one error is possible, anywhere that uses pointers; and anywhere that you have complicated logic, regular expressions, or elegant code that another person might not understand.

Why was it written this way?

Why a stack, not a list? Why in C not in Python? Why repeat-until not while? Good coding, like good gaming, is a series of interesting choices.

Record your choices, and record the reasons for them. Later maintainers (or you!) can then make informed decisions about updating and modifying the code. Circumstances change, and people forget why decisions were made the way they were. The only certain way to remember your reasons is to document them.

Also record bugfixes. Later on, someone will (not might!) be tempted to pull out an apparently useless bit of code that you put in to prevent baz problems in quux machines. And it took you a week to get it right.

“Why this way” comments are essential anywhere you choose to break programming guidelines, lest someone try to “fix” the code to fit the guideline. Besides, you might teach someone something.

Which part does what?

Back to our fooed bar. We have determined that fooing is, in fact, a feature of our program, and that the bug reporter is correct and fooing is failing. How do we fix it? Where do we find the foo module?

The most effective way from the maintainer’s point of view is to have some sort of document with the source code which describes the overall structure of the code and explains which code modules support which user-view features.

This doesn’t have to be a long document, and can be a simple list like:

foo
foo.c, interface.c
baz
baz.c, quux.h, interface.c

Use the same terminology in the features list as the users see, and include at least the main module for that feature in the code list.

Where do I find each part?

You’ve been thorough documenting your code, and carefully written an index of features and modules. Your maintainer knows he’s after foo.c. So where is it?

Include your installation guide, your “make install,” or some similar guide in your technical document. Include the location of your makefile — once the maintainer has fixed your code, she will need to build it!

If your feature index lists functions rather than files, you will need to list which file each function is in. You can list the functions as “foo() in foo.c” in the feature index.

Who wrote it?

Always add this. You never know when you’ll get a call from a headhunter offering you a lucrative contract because of a piece of code they saw. Or for more mundane reasons — someone might need to ask you about the code.

When was it written?

Add this, too. It might be useful to the headhunter — and it also gives a guideline for what sort of machine you wrote it for. And it may give a clue as to why you put in that apparently useless delay loop.

Final words

This is only one approach to documentation — most professional technical writers use much more structured and detailed approaches.

If you clearly answer each of these questions, the people who maintain your code will be much, much happier. And technical writers will be able to go through your code for their material, rather than making you explain it all.

Example program


/*
 * greet.cc
 *
 * A 'Hello World' program written to demonstrate code commenting.
 *
 * Written by Jenn Vesperman
 * March 2002
 */

/*
 * 'Hello World' was chosen because almost everyone is familiar with it
 * in some form or other, so the reader can almost ignore the code
 * and concentrate on the commenting.
 *
 * C++ is chosen partly because it has two styles of comment. Where a language
 * offers two comment styles, one can be used for extensive blocks of comment
 * like this, for function and program descriptions, and for important 
 * 'pay attention to this' comments.
 * This lets the other style be used for small, embedded comments that are
 * used simply to clarify code.
 */

/*
 * This program, having only one function, is too small for supplementary
 * documentation. It should just compile and run.
 * Compile with: g++ -ogreet greet.cc
 */

#include <iostream>
#include <cstring>
#include <string>
/*
 * This function finds out who to greet, greets them, and exits if
 * the input is the string "quit".
 */
int main()
{
	std::string name;

	start:		// label to use with goto

	// Finding out who to greet
	std::cout << "Enter a name (one word only), or 'quit' to exit" << std::endl;
	std::cout << "> ";	// prompt

	/*
	 * Warning: While buffer overflow isn't a problem here, there's
	 * not actually any protection against, say, getting a gigabyte of
	 * input with no whitespace in sight. That alone could DoS
	 * a machine.
	 */
	std::cin >> name; 

	/*
	 * strcmp returns 0 (false) if the two strings are the same. 
	 * Therefore we use a comparison with 0, rather than simply 
	 * using 'if !strcmp'.
	 * We're using strcmp() for a reason, here: As an example.
	 * As such, we're making a point of mentioning the choice, for those readers
	 * who are wondering 'why?'
	 */
// is it quit? If not, greet then return to start
	if (!(std::strcmp(name.c_str(),"quit")==0)) {
		std::cout << "Hello " << name << std::endl;

		/* 
		 * We use the 'goto' construct rather than a while or do-while
		 * because we need to demonstrate commenting when we break the 
		 * rules. There's no other reason for it. 
		 */
		goto start;

	// if it is quit, exit with the return value 'true'.
	} else {
		return 1;
	}

	// Should never get here. Return false if we do.
	return 0;
}


Jenn Vesperman is an Open Source coder and coordinator of Linuxchix.

Category:

  • Migration

Democrats vs. New Media

Techcentralstation.com has a column on why Democrats are supporting Big Hollywood by introducing bills like the Consumer Broadband and Digital Television Promotion Act. “Then there’s the loss of moral legitimacy: It’s hard to pose as friends of the little guy against Big Business when you’re taking money from Big Business while taking long-established rights away from the little guy. (Scott Harshbarger of Common Cause calls this move ‘a shocking fire sale.’)”

Steve Gibson interview on PortaZero

Anonymous Reader writes: “portazero has a Steve Gibson interview about security, Internet, why he’s using FreeBSD instad of Linux.”

Category:

  • Linux

Linux in education report #67 for April 1

“The Open Source Education Foundation has announced their Free Curricula License.” More at seul.org.

Microsoft asks the court to reconsider preliminary injunction ruling supporting Lindows.com

“Microsoft Corporation asked
a Seattle Court to reconsider a strongly-worded ruling upholding
Lindows.com’s use of the terms “Lindows.com” and “LindowsOS.”

The court’s favorable ruling, issued on March 15, 2002, cites that
“Microsoft has raised serious questions about the validity of its
[Windows] trademark.”

The suit, filed by Microsoft in December of 2001,
         sought to stop Lindows.com, a 30-person company that offers a
         Linux-based operating system, which runs popular Windows-based
         programs, from branding itself as Lindows.com. The March 15, 2002
         ruling can be read, in its entirety, at www.lindows.com/opposition.

         The Microsoft Corporation asked the court, in a 19-page document, to
         reconsider the ruling stating that the court reached "an incorrect
         result." Microsoft's motion for reconsideration can be read at
         www.lindows.com/opposition.

         Judge John C. Coughenour found in the preliminary injunction ruling
         that Microsoft had not shown that Lindows.com should be prevented from
         using the names Lindows.com and LindowsOS as part of their business,
         stating that, "The Court finds that Lindows.com has met its burden of
         proof in rebutting the validity of the Windows trademark."

         "Lindows.com supporters submitted thousands of references to windows
         spanning the last 20 years," said Michael Robertson, Chief Executive
         Officer of Lindows.com. "They helped build a strong foundation which
         the Judge relied on in his initial ruling. Microsoft's hundreds of
         attorneys and billions of dollars can't rewrite history and the fact
         that Windows is a generic term."

         "Microsoft's actions are attempts to belabor the outcome and drag our
         company through a lengthy and costly legal battle. We're looking
         forward to getting this issue in front of a jury and getting resolution
         in a manner which allows us to put a competitor on the store shelf
         which will cost a third or less of Microsoft's offerings," Robertson
         added.

         The Judge can deny the reconsideration motion or ask Lindows.com to
         file a legal brief in response. Microsoft has until April 15th to file
         an appeal on the Judge's initial ruling. The appeal, if filed, would be
         heard by the 9th Circuit Court of Appeals.

         Lindows.com has released a Sneak Preview of LindowsOS to a select group
         of Insiders (www.lindows.cominsider). The Sneak Preview is not a fully
         completed product, but showcases many of the unique features such as a
         "Friendly-Install" alongside an existing Microsoft Windows operating
         system, a streamlined installation process which requires minimal
         computer knowledge, and the ability to run popular Windows-based
         programs. Version 1.0 will go on sale later this year for one-third of
         the cost of a comparable Microsoft offering. For more information see
         www.lindows.com/products.

         To receive Lindows.com press releases via email signup at
         www.lindows.com/mailing.

         About Lindows.com, Inc.

         Lindows.com is a consumer company that brings choice to computer users.
         Lindows.com, Inc. uses the latest technology to create affordable,
         stable, user-friendly products. Lindows.com, Inc. was started by
         Michael Robertson, founder and former CEO of MP3.com. At the core of
         Lindows.com is a new operating system called LindowsOS, a modern,
         affordable, easy-to-use operating system with the ability to run both
         Microsoft Windows and Linux® software.

A detailed guide on how to install and use Indic scripts

Maninder Bali writes: “The Indic-Fonts-HOWTO has been written to help you setup your Linux box to use UTF-8 encoding for using various Indic scripts. I have tested the IndiX system on Exodus GNU/Linux, RedHat Linux, and Mandrake Linux.

The Linux user and business database

Rainer Manns writes: “Here’s a website to find contacts, products and applications. Visit free-it.org.

Review: Yellow Dog Linux 2.2

DocTomoe writes: “Yellow Dog Linux is currently the only PowerPC distribution that does not exist for other platforms. Although YDL is based on RedHat, it’s a unique distribution and it’s understandable that each release of YDL is followed with great interest by the PowerPC Linux community. iMacLinux.net has reviewed their latest release, version 2.2, which started shipping last week.”

Category:

  • Linux

Three of the Internet’s best places to get started with Linux

by Tina Gasperson
This is for people who want to learn the basics of Linux at home, on the
Internet. You don’t have to be a “learn all alone” kind of person to do it this
way — there’s plenty of human help out there. These three venues offer current, interactive,
real-life Linux help for real people.Newchix

The Newchix
mailing list
is an offshoot of the popular LinuxChix community built around
female Linux users. There is no set curriculum or structure to this beginner’s
forum — just bring your questions and join in. The LinuxChix lists have a
reputation for polite repartee that is rare in tech discussions of any kind, and
the Newchix list is no exception. All questions are welcomed and treated with
care by experienced and not-so-experienced members of the list. The membership
is mostly female, but men are welcome.

List traffic is moderate but growing. Previous topics have included “firewalls,”
“Mandrake install problem,” “cannot load desktop manager,” “baud and bps,” “does
Linux crash?” “about downloading Red Hat,” and “PCMCIA card config.”

MandrakeUser.org

Obviously, this is a Mandrake-specific site. But because it is geared to
beginners, go ahead, pick up a copy of Mandrake and get busy installing. This is the best site out there for current docs and tutorials. I’ve used this site extensively to find out
which files to edit, how to edit them, what programs I need to install and when; and also to learn about .rpm packages and the dependency game.

There’s no “step-by-step” installation guide here, but that’s one of the nice
things about Mandrake (and many of the other distributions) — the process is so
automated you won’t need that kind of help. If you get stuck on X configuration,
you will find help for that here.

A nice feature is the interactive discussion forum, which in itself serves as a
nice, big archive of solutions. You should search the current posts before you
ask your question, because it is likely someone else has had the same problem
you’re having.

Basic Linux Training

Henry White and Anita Lewis run this low-key mailing list and Web site
that is the best free program on the ‘Net, dedicated to the Linux newbie who is
willing to do some studying to learn the basics. They’re so low-key they
didn’t want us to give them too much publicity because “idle-curiosity seekers”
take up too much bandwidth, according to Henry White, and he pays for that
out of his own pocket. So, no visiting unless you’re a serious newbie who’s
ready to learn!

They’re tolerant of all questions that come up on the “BLT” list, and don’t
allow flaming or “RTFM”
comments. But don’t expect to get your hand held too much — after all, this is
Linux, and you have to be ready to read up.

Potential students should be
prepared to wait; the class size is limited and there’s usually a backlog of
registrants, says White. Once you’re in, you’ll get two or three lessons at a
time to work through on your own. The mailing list is for lesson-related
questions or other difficulties.

From the site: “Basic Linux Training is a
brief, introductory level course written specifically for those coming from a
DOS/Windows background, without any knowledge of Unix or programming. (Those
coming from Apple/Mac are welcome and should get a lot out of this course
despite the orientation. Just be aware that Mac users have always been rare on
the mailing list so you’ll almost certainly have to supplement this course with
other Mac users who have Linux installed.) The course is designed to be used
with virtually any introductory Linux textbook, and is vendor and distribution
neutral.”

How to find and report software bugs

Author: Peter Galli

By Telsa Gwynn
Anyone can file a bug on anything: The difficult part is
knowing how to write the report and where to send it. For some reason, people
are wary about entering trivial bugs or typos in things users can see (dialogue
boxes and docs for example) if they know there are worse bugs to
fix. But they are easy to fix: People just have to know they exist.Bug-reporting is a learning curve, and it does take time at first,
but then it gets faster and faster until it’s routine. You get to pick
things up about what might be relevant, and how to search different
bug-trackers effectively, and where developers think an obvious
place to put FAQs and bug report hints are.

I think the biggest thing is to stop every time you notice
something odd and look at it there and then. If I meet a bug
in a new program and think “I’ll just finish this and then
go back to that bug,” I tend not to be able to find it again.

Take notes: whether on paper, on the computer, or into a
tape-recorder. Don’t do it on the computer if it’s an X or kernel
hang or crash: unless you have auto-save on in your editor you’ll
lose your notes. A separate machine is okay though. If there’s
an error message, copy it precisely. If it’s horrendously long
and you have a digital camera, take a photo and stick the pic on
the Web. (I’ve seen kernel oopses treated like this.)

If your screensaver or apm is going to kick in before you can copy half
a screen of X crash errors, read the numbers into a tape recorder
slowly and then play it back and type it in at your leisure. If it’s
something at the console and isn’t a crash, use “script” to
capture exactly what you typed and what spewed out. Script is
brilliant for saving stack traces from gdb if you don’t have
cut and paste handy, too.

Particularly, write down exactly what you did. “How to reproduce” is
often the most important bit, especially if some kind maintainer makes
a patch and you apply it and try to test the fix and think, “Now, how
did I get it to do that again?”

Then, figuring out what exactly broke is the next big one, and that
can be a pig. Because UNIX is so full of lots of little programs
calling different ones to do different bits, what you start is not
always what’s actually breaking: Sometimes it’s a library the
program is using and sometimes it’s even more arcane. A really
good (or bad, depending on your point of view) example was the
time I wrote a quick rot13 script using the “tr” command. The
script broke in peculiar ways. The culprit was not exactly obvious.
Earlier, I had changed my locale to en_GB. Changing your locale that
way changes the sort order (LC_COLLATE). And the way I had used tr
relied on the sort order being “C” rather than “en_GB”. Uurgh. I was
fairly proud that I figured that out before Alan did.

It’s worth checking the FAQ, /usr/(share/)doc/packagename/README, and
the
already-open bugs against the package. I wrote a very long screed
about
“docs wrong” for one app a while back, checked bugzilla before
entering
it, and someone else had already done it for me. So I just added
some extra to that one.

And file the bug. Add something about “what more information do you
need?” because it’s very possible there is more: I have become
used to attaching XF86Configs, the output of lspci -vv, my .gtkrc
and so on. Developers differ here: RH’s bugzilla tends to be full
of “Please run this command and attach the results,” but some other
people give responses of, “Do you have the foo module and was
poo compiled with — plop?” which is not always something I can answer.

The other thing to remember is that developers are human, too.
Slagging off the package and the character of the person who wrote
it with copious ad hominem attacks is not going to get your bug
looked at first. Sadly, there are the occasional folk who treat
bug-trackers as a way to flame people. This isn’t fun and it’s
not fair on the people going through the bugs, who are not necessarily
going to be the person who wrote it in the first place. Saying “I am
not
going to use this any more” isn’t a good idea either: Why fix it if
the reporter is not going to test the fix?

Of course, it works both ways. Developer responses of, “Don’t do
that, then,” “This is not a bug,” or just silence are not at
all encouraging. And blaming users for, “You used the wrong
compiler,” when the user just shoved a CD in is not fair. There
are some apps I won’t file bugs on these days because I’m scared
of the response I’ll get from certain people.

Wow, I bet I put everyone off now. If you’re not sure where
bugs go, I have a partial
list of bug-trackers
I use on my Web site: I’m thinking of turning it into
something more complete. If I didn’t put people off, I suppose the advice is to
find a package you like — or that you want to learn about, because
it’s often people who haven’t subconsciously learned workarounds
who find the real howlers. And then just read the man page, try
it with different versions of options, feed it obvious stuff by
extrapolation of, “Well, if this works, then this should … oops.”