I often find myself in this frustrating situation: I can picture in my head exactly the code that I need to write, but I know it's going to take me several minutes to type it. For example, when I add some new variables and need to write some get/set methods for them. The kind of code that requires little or no brainwork, and a lot of typing. The camel book has a section at the end with tips for programmer efficiency, but it seems sort of tongue-in-cheek; the opposite advice for each of these tips is given in the next section, maintainer efficiency.

I've always thought that I could do more with emacs, and last week I decided to put some time into searching for emacs tricks. I found a pretty cool way to create templates for commonly typed code - the DMACRO package. With it I've set up macros for common blocks of code/comments. So now all I have to do to create an if-else block, for example, is type "C-c d ife", which prompts me in the minibuffer for the if condition. Once I enter that, emacs inserts the following code and puts the prompt in the right place for me to start filling in the if block.

if (condition) { (prompt here) } else { }
So far I've set up templates for 'if', 'if/else', 'foreach', and 'for' statements, and for subs.

Anyone else have good tricks or suggestions for speeding up the process of moving code from your head into the computer? Here are some other ideas I've thought of:

Anyway, maybe some of you have suggestions that aren't as outlandish as those two ideas.

Here's my dmacro setup for anyone who's interested:

1. some docs are at: http://www.informatik.uni-hamburg.de/RZ/software/emacs/dmacro/dmacro_toc.html
2.downloaded dmacro at ftp://ftp.sgi.com/other/dmacro and unzipped to C:\emacs\dmacro
3.opened emacs and ran M-x customize-option, typed load-path, and then inserted the C:\emacs\dmacro dir so that emacs can find the dmacro files
4.added this to my .emacs file:

(require 'dmacro) ;;; load my macros: (dmacro-load "C:/emacs/dmacro/joe.dm") ;;; my macros use this function for tabbing: ;;; (indent-region doesn't bring the {'s with it in perl-mode.) (def-dmacro-function perltab perl-indent-command)
5.And here's my macros file, joe.dm:
####### # MODE: nil ###### # ####### fori indent for statement (prompts for variable name) for (~(prompt var "Variable: ") = 0; ~prompt < ~@; ~prompt++) ~perltab { ~perltab ~perltab ~mark } ~perltab # ####### fore indent interactive foreach type statment (prompts for varia +ble names) foreach my ~(prompt loopVar "loop var: ") (~(prompt loopList "list/arr +ay to loop over: ")) ~perltab { ~perltab ~perltab ~@ } ~perltab # # ####### if expand if statement if (~(prompt conditionString "Condition: ")) ~perltab { ~perltab ~perltab ~@ } ~perltab # # ####### ifelse expand if-else statement if (~(prompt conditionString "Condition: ")) ~perltab { ~perltab ~perltab ~@ } ~perltab else ~perltab { ~perltab ~perltab ~mark } ~perltab # # ####### sub expand create a new sub \# Name: ~(prompt subName "sub name: ") \# Synopsis: \# Arguments: \# Returns: \# Notes: \# sub ~prompt ~perltab { ~perltab ~perltab ~@ } ~perltab #

Replies are listed 'Best First'.
Re: Programmer Efficiency / dmacro
by lachoy (Parson) on Jan 03, 2002 at 18:11 UTC

    At YAPC 1999 Jon Orwant gave a talk where he analyzed all the modules in CPAN for character frequency. He extrapolated this into the Perl keyboard, of which someone took a picture.

    More seriously: getting the tools to automate stuff for you is excellent, but augmenting it with really fast typing gives you great perl-fu. Not thinking about the keys you're hitting puts less impedance on the thoughts flowing. Getting a programmable keyboard doesn't hurt either. (clemburg has more experience with remapping the keys on this keyboard)

    Chris
    M-x auto-bs-mode

Re: Programmer Efficiency / dmacro
by Chmrr (Vicar) on Jan 03, 2002 at 11:45 UTC

    Yeah, I know the itch that you're scratching. cperl-mode for Emacs with (setq cperl-hairy t) does much the same thing, auto inserting braces, newlines, and blocks as you type. Rather handy, expecially when you just can't wrap your mind around exactly what conditional you need, but you do know what you want the block to do -- and will forget it if you stop to think about the conditional for too long. The woes of having limited stack space in your wetware. :)

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

      Perhaps you need simpler blocks since wetware has a 7+/-2 chunk buffer (aka. short term memory) ;-).

      --
      perl -pe "s/\b;([st])/'\1/mg"

Re: Programmer Efficiency / dmacro
by YuckFoo (Abbot) on Jan 03, 2002 at 22:45 UTC
    I know nothing warms the cockels of my boss's heart more than to walk/lurk through the area and hear the 'clackety clackety bang bang clackety' of software being written. Sometimes it pays to save the typing workouts for moments like these. vi mixed with considerable typing skills will soon earn you the nickname 'The Hammer'. :o)

    Sad but true, waitresses that make it look too easy earn less tips than those that are 'working hard'.

    Yuckfoo

Re: Programmer Efficiency / dmacro
by FoxtrotUniform (Prior) on Jan 03, 2002 at 22:36 UTC

    I've been having similar ideas regarding DBI calls. One should be able to take a file full of SQL CREATE TABLEs and generate some of the more mundane DBI code (retrieving a row based on primary keys, for instance) programatically. I find it difficult to believe that nobody's tried to do this before, but I can't find any code to do it. Once writing the code generator becomes easier than grinding out the DBI stuff by hand (probably the next major DB project I work on), I'll hack something up.

    --
    :wq
Re: Programmer Efficiency / dmacro
by Aristotle (Chancellor) on Jan 04, 2002 at 00:22 UTC
    Voice recognition systems are not currently in a state to be useful for this. The way they force you to speak, you are faster with conventional typing on an oldfashioned keyboard than by dictation. Maybe in 10 years..
Re: Programmer Efficiency / dmacro
by jplindstrom (Monsignor) on Jan 04, 2002 at 09:57 UTC
    The macros I use in UltraEdit are:

    * Template for a method

    * Template for a property

    "$self->"

    "->"

    "{}" (setting the cursor in between)

    " {
    }" (setting the cursor at the new line)

    As you can see, I try to avoid typing common and weird characters that are difficult to produce on a Swedish keyboard. This has really sped up my Perl typing and reduced typing errors.


    /J