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 #

In reply to Programmer Efficiency / dmacro by blahblahblah

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.