Effective Coding on Unix
Home
Updated 2006-10-04
In this article, I will explain how I code on unix.
Use a unix Computer
Store your source code and compile it on a unix computer. It will be your development server. I recommend using a popular version of Linux:
Gentoo,
Debian,
or Ubuntu.
I don't think that Cygwin makes a good development platform.
Avoid sysadmin work. Use a computer that somebody else manages. If you're a university student then get a shell account on the university's system. If you're a
UIC CS student then use oscar.cs.uic.edu.
Use PuTTY
If you already have a Linux workstation or a Mac with OS X then you can run ssh from a terminal to connect to your development server.
On Windows, use the free SSH client,
PuTTY.
Follow these instructions to install it properly on Windows XP™:
-
Go to the PuTTY Download page.
Download the "Windows-style installer". At the time of this writing, it is called putty-0.58-installer.exe.
-
Run the installer. Install with the default options. This puts the PuTTY files into C:\Program Files\PuTTY
-
The pscp.exe program must appear on the path for it to be useful.
Configure the PATH environment variable:
-
Right-click on My Computer and choose Properties.
The System Properties dialog box will appear.
- Click the Advanced tab.
-
Click the Environment Variables button at the bottom.
The Environment Variables dialog box will appear.
-
Under System variables, scroll down and select
Path.
-
Click the Edit button.
The Edit System Variable dialog box will appear.
-
Click in the box labeled Variable value: and
press the End button on your keyboard to go to the end of the text.
-
Type ;C:\Program Files\PuTTY; at the end of the text.
The semicolons are important!
- Click OK to save changes to the Path variable.
- Click OK to close the Environment Variables dialog box.
- Click OK to close the System Properties dialog box.
-
Make a shortcut to your development server, oscar.cs.uic.edu:
-
Open My Computer and navigate to
C:\Program Files\PuTTY.
- Press Alt-F-W-S to open the Create Shortcut wizard.
- Type putty oscar.cs.uic.edu and hit Enter.
-
Type oscar.cs.uic.edu as the name of the
shortcut and hit Enter to finish creating the shortcut.
- Press Alt-F-I to add it to the Start Menu.
PuTTY comes with good default settings.
Commands and settings are available by clicking the icon in the upper-left corner of the window, just left of the title bar. I like to increase the Font size and maximize the window.
You can get full-screen mode with ALT-Enter.
This makes the PuTTY window take up the entire screen.
It looks just like your computer is the development server.
Turn on the ALT-Enter shortcut key like this:
- Click the tiny icon in the upper-left corner of the PuTTY window
- Choose Change Settings
- In the tree on the left, select Window / Behavior
- Check the box next to "Full screen on Alt-Enter"
- Click the Apply button
To make your changes stick in the program,
you need to save over PuTTY's default settings:
- Click the tiny icon in the upper-left corner of the PuTTY window
- Session should already be selected on the left side
- "Default Settings" should already be selected on the right side
- Click the Save button
- Click the Apply button
Use Tab-Completion
All decent unix shells can interpret the tab key to complete a partially typed filename. With tab-completion, one can manage files very effectively. It is much faster than using a using a mouse and graphical file manager.
- Single tab to complete - complete match appears with a space after
- partial match - no space
- after partial match, double tab prints out candidates
- Tcsh
- Bash - on by default
- Name your files so tab completion is easy.
- all lowercase
Use Screen
Do all of your development with one terminal.
Use screen to create virtual terminals and switch betwen them using the keyboard command.
There is a huge advantage to keeping your hands on the keyboard all the time.
- Run screen with
screen -R
- Disconnect the screen session and return to the shell:
^a d
(it means hold down CTRL and press A, then press D)
Your programs will remain in the background, waiting for you to run
screen -R again
.
- If you close PuTTY or get disconnected you can just log in again and reconnect.
- Create a screen:
^a c
Each screen gets a number, starting at 0, up to 9.
- Switch to screen 2:
^a 2
- Switch back to previous screen:
^a ^a
- Close a screen:
^a k
or exit the shell running in it
- Renumber current screen to be 9:
^a : number <space> 9 <enter>
- Always use the same screen numbers for each program that you use. For example:
- Screen 0: intial screen, use for emacs (or vim)
- Screen 1: shell for running your program
- Screen 2: shell for looking up man pages
- Screen 9: irssi IRC client for consulting the gurus on Freenode
- Add an extra window:
^a S
(that is a capital S)
- Switch focus to next window:
^a <tab>
- Remove the focused window:
^a X
- Zoom the focused window:
^a Q
- Accidentally pressing
^a s
(lower case S) sends xoff to the current window.
This tells the program to stop updating the screen. You can fix this by
sending xon: ^a q
(lower case q)
- Help:
^a ?
- Enter the long form of a screen command after typing
^a :
You can also put these commands in your ~/.screenrc
file so they run
each time you start screen. A useful command is escape ^\
which
changes the command prefix from ^a
to ^\
.
There are many commands. Read about them in the man page: man screen
Use Emacs
Read
Effective Emacs (local copy).
Customize your ~/.emacs file. I have incorporated most of the suggestions in Effective Emacs into my .emacs file.
- Cancel any command with Esc Esc Esc
- Quit with ^x ^c
-
Clipboard
- Mark (start of selection)
- Kill (cut)
- Kill line
- Yank (paste)
-
Panes
- Switch Panes ^x o
- One Pane ^x 1
- Two Panes ^x 2
- …
-
Buffers
- Switch to buffer by name ^x b
- Show list of buffers ^x ^b
- Kill buffer ^x k
- Open or create a file ^x ^f (use tab completion on filename!)
- Save current buffer ^x ^s
- Save all buffers ^x s (prompts for each one, ! saves all)
-
Command prompt Alt-x (supports tab-completion of command names!)
- help
- global-font-lock-mode (turns on syntax high lighting)
- put commands in ~/.emacs, with parameters, surrounded by parentheses: (global-font-lock-mode t)
-
Searching
- Incremental search ^s
- Replace Esc %
Write a Good Makefile
Use the makefile to build, clean, and test. For interactive programs, add command line options that bypass the interactive prompts, so you can test your program without interacting with it.
Make many tests: test1, test2, test, etc.
Make tests that should succeed and others that should cause your program to report errors.
Make clean should remove core, *.o, all temporary files created by tests, and the program's executable.
Learn to Read Manpages
They can be tough for an inexperienced person to understand. Read the entire manpage of every library function call that is used by your program. Also take a look at man pages listed in the SEE ALSO section.
The manpage open(2) is for the open in section 2 of the manual. Read it with man 2 open.
If your unix system views manpages with more then change it to less.
Keep Source Code Organized
Be organized. Don't mix source code and other files.
Put test data files in a test/ subdirectory. What is suitable for the source code directory: c, cpp, h, makefile, readme, INSTALL, COPYING, any data files required by your program.
Use tar
tar czvf project.f.tar.gz project/
tar xzvf project.f.tar.gz
Keep backups of your source code directory: a, b, c, …, z.
Use pscp to copy the backups to your workstation
Prevent Programming Errors
- Use assert
- Fix bugs immediately
- Test after every bug fix and edit
-
Write proper function headers before writing the function. Example:
/* concat - joins the proclists together into a new list
@param head the head of the new list, or NULL if empty
@param tail the tail of the new list, or NULL if empty
@return the new list, or NULL if empty
*/
-
Avoid deep indentation by using functions.
Make all code have a maximum of 3 levels of indentation. Example:
int foo (int x)
{
if (100>x)
{
if (0==x)
{
/* let's indent only this far and use functions */
return bar(x);
}
}
else return x;
}
- Put constant first in comparisons: 0==x, 100>=x, -5>x
- Learn to use const
- Learn about variable scope and lifetime
- Learn about pointers
- Learn C-strings really really well
TODO
Things to add to this tutorial:
- Run your development system in a virtual machine such as VirtualPC, Xen, etc.
- Use subversion, cvs, or darcs to keep your source and config under revision control.
- Back up your repository with rsync
- Use ssh keys and Pageant to avoid typing your password
- Make sections for C, C++, Java, Erlang, Python
- Include example Makfile
- Write about removing distractions
- Write about taking breaks to let your subconscious mind solve problems
- Write about reduced productivity late at night (like now)
Home
Copyright © 1999-2012 Michael Leonhard