How do I streamline my coding and work habits?
April 13, 2007 10:02 AM   Subscribe

What coding habits and working methodologies help you maximize productivity while reducing angst?

So I'm picking up more business and shifting my life/career gradually into full-time freelance web programming work. Right now, I'm working mostly in PHP, implementing and tweaking various CMS's (primarily Drupal and Wordpress), but I'm also picking up some flash/actionscript work, and occasionally call on my previous experience coding in PERL. I like the trajectory I'm on and want to continue expanding both the depth and breadth of my skills and knowledge.

However, I'm beginning to feel that my working/coding habits are more haphazard and cumbersome than they should be -- sometimes I find myself tripping over my own shoelaces. E.g., I find myself saying things like this to myself more often than I'd like to admit: "Where did I make that change? Where did I see that function? Oh, wait, it was in the previous version of the file that I just overwrote. Ok, I know that function xyz is somewhere in the bottom 3/4 of the file -- should I just scroll until I find it or do I need to do a search for the function name?"

This sort of tripping-over-my-shoelaces thing didn't happen as frequently when I had only 1 project at a time; but as I take on more projects, it's happening more frequently. In short, I've realized that if I'm going to continue in this line of work, I need to develop some better habits and strategies.

So far, I've done most of my coding/editing/etc with a fairly simple text editor (Crimson Edit). I generally set up a mirror of the site on my local Windows machine running apache, mysql, etc, then FTP files up to the server. However, I realize that some people swear by IDE's like Eclipse, and others use utilities like grep, awk, etc., to facilitate coding.

So I'm looking for recommendations on streamlining my coding/work habits/experience. I know there's not a one-size-fits-all approach, so any suggestions/advice will be greatly appreciated.
posted by treepour to Work & Money (22 answers total) 19 users marked this as a favorite
I code Flash ActionScript, so it's not *exactly* the same thing (not even 100% sure how many of these will really apply)...but some of the more helpful things for me have been:

- Keep all of your variables in one place. I put mine up at the top of my code document. If I ever come back to it months later, start looking at a function, and wonder what the hell "pFI" is, I know right where to look.

- Make lots of comments...more than you think you'd need.

- Keep everything very flexible. Instead of making one long spaghetti-code function to handle a dozen things, why not make individual functions, and then write a parent function to call them? Modular is good!
posted by kaseijin at 10:17 AM on April 13, 2007

You need to get yourself a good source control system. The standard free software version is CVS, but there's numerous others. This will let you keep a history of every change you make, and allow you to revert back to any state of the code you need.
posted by mikw at 10:18 AM on April 13, 2007

You definitely need source control. I'd recommend Perforce, since it comes with some nice GUI tools & for a single user tends to work better than CVS.

You also should a class/function browser, & generate whatever the PHP equivalent of doxygen docs are now & then to keep track of what you're doing. Keeping a by-hand changelog is another option.
posted by devilsbrigade at 10:22 AM on April 13, 2007

"I find myself saying things like this to myself more often than I'd like to admit: "Where did I make that change? Where did I see that function? Oh, wait, it was in the previous version of the file that I just overwrote."

That pain down there isn't "tripping over your own shoelaces," it's the feeling of shooting yourself in the foot because you're not using version control. Set aside a couple of days to play with some of the tools out there until you find one you like, or maybe screw around with it over the weekend, but DO NOT WRITE ANOTHER LINE OF CODE UNTIL YOU HAVE VERSION CONTROL.

"Ok, I know that function xyz is somewhere in the bottom 3/4 of the file -- should I just scroll until I find it or do I need to do a search for the function name?"

You seem to like simple text editors for your development environment. Consider me a sympathizer. It's a very rare IDE that I find doesn't get in the way of the code. It turns out that a fair number of simple editors can parse function declarations. I'm out of the loop on Windows text editors these days, but back in the olden days one would just pick up the UltraEdit hammer and turn everything into a nail.

"I know there's not a one-size-fits-all approach, so any suggestions/advice will be greatly appreciated."

You're right. What works for one person annoys the next, so building a development environment and workflow is a very personal experience. Some general rules of thumb apply, however:


Feel free to fiddle around with tool choice and the requirements of your language and platform, but don't violate the commandments.
posted by majick at 10:25 AM on April 13, 2007 [2 favorites]

For PHP, requires are really good for keeping everything straight. Group your functions/classes(if you use them) in different files, so that way when you're tracking down a function, you can think of it's broader category first. Plus, then you can just import the functions a particular page needs without importing a bunch of unnecessary ones.

Modularity is definitely your friend. write everything with an eye towards reusability. what you'll wind up doing is building yourself a library of reusable functions that you use for every project. Through use, you'll become very familiar with where everything is ad how it works, and tracking down changes will be much easier.

One PHP specific thing I like to do is keep the rendering functions completely separate from the database functions and whatnot. As a matter of fact, I like to try to make it so that someone who doesn't know any PHP at all could come into whatever I write, and make it look like whatever they want. This also forces you to organize things more, and makes your project more comprehensible for evryone involved.

Lastly, in general, i try to write everything I do with the idea that someone else could be taking it over at any time. I've taken over other people's projects mid-stream before, and I know how confusing it can be. Get an idea of what confuses you about other people's stuff, then don't do that yourself.
posted by fnerg at 10:29 AM on April 13, 2007

n-th the source-control suggestion (CVS or Subversion are both excellent and free).

You may also want to start keeping a code database or at least a notebook for indexing the location of useful code snippets. I use Evernote (also free) for this.
posted by jknecht at 10:32 AM on April 13, 2007

The Pragramtic Programmer is a good guidebook of work practices/conventions. They do address issues like code organization, method/variable naming, project documentation, and source management. Where it's most helpful is as a succinct introduction to a bunch of practices make your life easier.
posted by nakedcodemonkey at 10:32 AM on April 13, 2007

Get a good source control system, like Subversion. Set up your own personal standards for organizing your files and organizing your code. In PHP, for instance, keep similar functions together in the same include file and always prefix your functions with the name of the file they're in. For instance, keep all your functions related to users in '/include/user.php' and name them all 'user_nameOfFunction()'
posted by lsemel at 10:37 AM on April 13, 2007

Many open source projects are switching from CVS to SVN. I haven't done this yet, but if I were going to learn one new, that's where I would start.

I also write code on one machine and run it on others. I use CVS to transfer the code between the machines, which makes it easy to find out whether I am synced or not, and also enforces that I save a new revision.

Make lots of comments...more than you think you'd need.

In my opinion, it is far better to write code that needs as few comments as possible. Rather than this:
wl = [foo, bar] # widget list

### regenerate all the widgets
for w in wl:
Do this:
def regen_widgets(widgets):
    for widget in widgets:

regen_widgets([foo, bar])
Write code Once and Only Once. Keep code you will reuse in several projects in a separate directory rather than copying and pasting into different packages.

Name your methods consistently. Something like verb_nouns. Try to use the same set of verbs where possible (for example, get, load, save).
posted by grouse at 10:39 AM on April 13, 2007 [2 favorites]

I should underscore this: you should use comments to explain complicated details about your code, such as how an algorithm works. Yes, use comments. Please. But for simple details, you should rewrite it so that it is understandable without a comment. For explaining the interface, you should write documentation.
posted by grouse at 10:41 AM on April 13, 2007

Read Code Complete and The Pragmatic Programmer. Do everything in them.
posted by matildaben at 11:11 AM on April 13, 2007

Use a model-view-controller-based web application framework like CakePHP or CodeIgniter for PHP (they're similar to Ruby on Rails). It makes your code organized and easy to understand. Models handle the database work, views are the screens that appear in the application, and controllers tie everything together.
posted by kirkaracha at 11:15 AM on April 13, 2007

You need to get yourself a good source control system. The standard free software version is CVS


Almost any current source control system is better than CVS.

Anyway. You need 1) Version control 2) A decent bug / feature tracker which ideally integrates with your version control (trac is good) 3) A development environment that makes life easier.
posted by pharm at 11:22 AM on April 13, 2007

Great suggestions, all, thanks! The consensus seems to be that I desperately need to use a source control system -- something I've been shying away from for awhile. I have some very preliminary experience using TortoiseSVN, so I'll take a deeper look at it this weekend.

kirkaracha, I watched a couple of the introductory videos for CakePHP and CodeIgniterPHP -- really exciting stuff, and I'll definitely give one of these a try when I'm working on building something from scratch. For right now, though, I don't see a clear way to leverage these frameworks for use with already-existing CMS's. Thanks for pointing them out, though -- I had no idea they even existed.
posted by treepour at 11:49 AM on April 13, 2007

BTW, as a follow-up question . . . does anyone have a recommendation for a windows text editor which features decent automatic code formatting? I've stuck with Crimson primarily because it automatically indents (or removes indents) after curly braces -- something I'd have a hard time living without, having gotten used to it. Notepad++ looks wonderfully feature-rich, but lacks the automatic indentation. Thanks again.
posted by treepour at 11:55 AM on April 13, 2007

Seconding reading the pragmatic programmer. It is a great book that goes over lots of the various processes that make coding much much easier. Everywhere from source control to project automation to text editors. It really does cover everything you need, and it's an easy read.
posted by cschneid at 12:58 PM on April 13, 2007

"Notepad++ looks wonderfully feature-rich, but lacks the automatic indentation."

Look into the possibility of plugging functionality like this in from external programs. Those "grep, awk, etc." people you're talking about are taking advantage of the general unix philosophy that you don't have to pack every feature into a single program. Instead, you call other programs to do specialized things like fixing up your messy typing before you commit the code to the repository.

If you're at all philosophically like me -- and you may or may not be; there certainly are plenty of people who like their Monster IDE approach to development -- you might find yourself happy with a development environment glued together out of "best of breed" tools and tied together with some scripts and a decent programmer's editor (oh, how I miss thee, CygnusEd) that knows how to call them.
posted by majick at 2:37 PM on April 13, 2007

treepour: Emacs would be one option.
posted by pharm at 1:47 AM on April 14, 2007

ExpressionEngine is a CMS by the same people who made CodeIgniter.
posted by kirkaracha at 10:17 AM on April 14, 2007

Seconding kirkaracha.

Also have a look at Symfony.

The documentation is superb and the development model it enforces has been a real revelation to me. It takes care of a lot of the grunt work and frees your mind to work out how you want things to work. It also integrates a database abstraction layer and object model. If you're doing web apps, it can be a godsend.
posted by MasterShake at 1:03 PM on April 14, 2007 [1 favorite]

does anyone have a recommendation for a windows text editor which features decent automatic code formatting?

I really like PSPad:

It has syntax highlighting for a good selection of languages and configurable auto-complete and auto-formatting. It also has a built-in ftp client, which makes it nice for web projects -- I can open a file from the server, and every time I save it automatically uploads the saved version.
posted by scottnic at 3:22 PM on April 14, 2007

Sorry about the non-linked link...
posted by scottnic at 3:23 PM on April 14, 2007

« Older Non-morally objectionable deodorant that works...   |   Finding 1920s Style DJ Sets - Big Band Bush Doofer... Newer »
This thread is closed to new comments.