views:

217

answers:

7

It is useful to have skeleton or template files that you can just copy and use as a basis for a new script or app.

For example, I use the following ones (emacs with the auto-insert module automatically opens a copy of the appropriate skeleton file when I create a new file).

Perl:

#!/usr/bin/perl -w 

use strict;
use Getopt::Long;

my $verbose = 1;

GetOptions("verbose!" => \$verbose
) or die("options error");

C++:

#include <iostream>
#include <stdexcept>

int main(int argc, char** argv){
  try{

  }
  catch(std::exception& e){
    std::cerr << e.what() << std::endl;
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}

Arguably, one could include basic code for boost::program_options etc.

What are your favorite skeleton files?

+3  A: 

My Perl templates look like this:

If I am opening a .pm module:

use MooseX::Declare;
class What::Ever {

};

1;

Or, if not on a MooseX::Declare project:

package What::Ever;
use Moose;

1;

If it's a .pl file:

#!/usr/bin/env perl
use strict;
use warnings;
use feature ':5.10';

Since I use autoinsert.el, I also have it ask me if I want to use FindBin; if so:

#!/usr/bin/env perl
use strict;
use warnings;
use feature ':5.10';

use FindBin qw($Bin);
use lib "$Bin/../lib";

The necessary emacs code is in my elisp repository at http://github.com/jrockway/elisp/blob/fd5d73530a117a13ddcde92bc1c22aba1bfde1f0/_local/auto-inserts.el.

Finally, I think you will prefer MooseX::Getopt to plain Getopt. It is a much more maintainable approach to writing "one-off" scripts. (The next few lines go something like:

use My::Script;                    # that's why we did the "use lib" thing
My::Script->new_with_options->run; # this parses the command line, creates a new object, and runs the script

All the important code goes in a class that can be unit tested, glued to a web app, etc.)

jrockway
Cool, I didn't know MooseX. Thanks!
+3  A: 

The only skeleton file I have is for LaTeX.

\documentclass{article}
%\documentclass[11pt]{amsart}
\usepackage[dvips]{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{cancel}
\oddsidemargin0cm
\topmargin-1cm
\textwidth16.5cm
\textheight23.5cm
\parskip1ex
\parindent0ex
\begin{document}
\title{ ... }
\author{ ... }
\date{ ... }
\maketitle

\end{document}

Obviously I use this for writing math papers.

Otherwise, I always start from scratch. There's no programming language I can think of where the requisite infrastructure is more than you can keep around in your brain or take longer than 20 seconds to type out.

ephemient
Multiply the number of files you create in your lifetime by the 4 lines of boilerplate per file.... and I think you'll find it worthwhile to let your editor do the typing.
jrockway
A: 

When I'm writing code that will be OSS I have a simple boiler plate template that I can key in the license and URL to the license text. The boiler plate has author details, and other crap hard coded.

For commercial dev I have a boiler plate with company information, and standard copyright notices in it.

I don't keep any standard skeletons because I've found I just cut out the content and added my own anyway. Most cases are different enough that changing the skeleton to match takes as long as bashing it out by hand.

Adam Hawes
+1  A: 

In visual studio, they're called Project files; my current favorite is Windows Application ;-)

Steven A. Lowe
still no sense of humor on SO - very sad
Steven A. Lowe
+1  A: 

Java

package edu.vt;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Template
{
   private Log log = LogFactory.getLog(getClass());

   /* Constructors
   ***************************************************************************/

   public Template()
   {
   }

   /* Accessors/Mutators
   ***************************************************************************/

   /* Local Methods
   ***************************************************************************/
}

and

package testing.edu.vt;

import edu.vt.Template;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TemplateTestCase extends TestCase
{
   private final Log log = LogFactory.getLog(getClass());

    public TemplateTestCase(final String name)
    {
      super(name);
    }

    protected void setUp()
    {
    }

    protected void tearDown()
    {
    }

    public void testLifeCycle() throws Exception
    {
      assertTrue(true);
    }
}
Joe Liversedge
Do you really need all those *******... comments, especially considering you can jump to any method definition with a few keystrokes?
jrockway
Most code is a candidate for an open source contribution, where it's nice to give context. And I already did everyone a favor and left out the Javadoc comment blocks. Remember, these are skeleton files.
Joe Liversedge
+1  A: 

Python is simple, but it still helps if you import things with shortcut names, for example:

import sys
import numpy as np
import pylab as pyb
import matplotlib.pyplot as plt
import matplotlib as mpl

But just don't do: import skynet.

Alex
+1  A: 

Bourne Shell

#!/bin/sh

usage() {
cat <<EOF
  $0 <cmd>
cmd:
  samplecmd
EOF
}

cmd=${1}
shift

case ${cmd} in
    samplecmd)
        arg1=${arg1:-${1}} # arg list takes precedence over env var
        if [ "x${arg1}" = "x" ] ; then
            usage
        fi
        samplecmd ${arg1}
        ;;
    *)
        usage
        ;;
esac

I like to make little helper scripts like this to document commands I type in the shell.

piyo