// Internet Duct Tape

What Would You Do If You Didn’t Have To Work?

Posted in Workhacks and High Tech Life by engtech on September 11, 2008

Workhacks

Lottery tickets offer us the dream of escaping to a different life. Often I find myself wondering what it would be like if I didn’t have to work full time. I always imagine spending more time on my open source software projects, getting in better shape and doing some freelance consulting to pay the time.

About ten years ago I got to see my parents make the transition from working full-time to retirement. It wasn’t an easy switch for them. There was some sadness, a lack of motivation and a lonliness as they lost the human contact the workplace gave them each day.

For the past two months I’ve gotten to experience what it’s like not to work every day. I was laid off from my previous job with severance. While I money wasn’t tight, there wasn’t enough to jet set off to Europe or go on any big vacations other than the ones I already had planned for the summer. I was stressed out most of the time until I found myself a new job. After I accepted the job offer, I gave myself a big window until I had to start so that I could relax and enjoy my time off.

This was the longest stretch of time I had booked off; it has been 14 years since I’ve had that much time to myself all at once. When I was working, I imagined all the web projects I could do if I didn’t have to go to work. The reality of the experience was quite different. Once you have the freedom to do anything you want with your day, sitting in front of the computer is the last thing you want to do.

For the first few weeks I found myself irritated by 3pm every day. I quickly realized that it was the lack of structure; if I got myself out of the house the feeling went away. So time was spent walking around the city, going to the library and reading in coffee shops. The beautiful weather really helped. Why stay inside on a sunny day if you don’t have to? Part of it was a desire to be around real people, instead of the virtual people I usually associate with if I’m stuck on a computer.

There was a definite priority shift. With the freedom to do anything I wanted with my day, it made so much more sense to focus on those long term, important but not urgent goals. Organizing things around the house. Getting a new family doctor. Renewing my passport. Getting new contacts. Removing clutter from my house and my life. I had to laugh one day when I found an old to-do list from 2006. There was stuff on that list that was important to my life and my well being that I was finally getting around to.

I found it amazing how much clearer it was to process my task list and choose the most important tasks for the day. Because I felt no urgency in any of my tasks, I was able to make much better decisions about what was important vs what wasn’t even worth doing.

I’m about to enter the workforce again, and I hope I can take some of the clairity I currently feel with me.

Building a Better Job Search Site

Posted in Hiring, Job Interviews and Resume Resources, Technology by engtech on July 21, 2008

Workhacks

Job hunting is a massive industry, but unfortunately it’s one that that always leaves job hunters feeling unsatisfied. Monster and Dice are painful to use. The hierarchy trees of job categories are often incorrect and confusing to someone who is looking for a job. There are a few places that are doing something different:

LinkedIn – resume and networking tools to keep in contact with ex-coworkers. The best way to find a job is often through people who know you. You get a job, they often get a referral bonus — win/win.

Peter’s New Jobs – regional tech job searches in Ottawa and Toronto, worth the yearly subscription even if you have a job because it’s a great way to stay current with the job market and how companies are doing.

Standout Jobs – Montreal startup that is focused on humanizing the job search process and giving companies a chance to sell themselves.

Working With Rails – job listings based on people working with a common technology.

Site-based Job Boards – job listings for readers of blogs like 37signals, Joel on Software, and Tech Crunch.

There are a few things I’d like to see in a job search site.

LinkedIn Integration

LinkedIn has taken over as the business contacts networking tool and it has a robust resume feature, yet we’re still forced to manually enter our resume into most job sites.

No Job Categories

Job sites like Dice and Monster all suffer from bad usability with elements like the job category navigation that takes several minutes to fill out. It’s so much simpler to have saved search agents for keywords in resumes and job postings.

Google Maps

I’d really like to see all of the job locations on a Google Map centered around my home address with different colours based on how the fresh the listing is. Job decision is often based on locality and I’ve yet to see a job search site that lets me easily list.

Stock History

For publicly trade companies there is no reason not to integrate a stock ticker widget so that job applicants can quickly see how a company is performing.

Tagged with: , ,

Competition

Posted in Marketing and Promotion, Startups and Business, Technology by engtech on April 29, 2008

Marketing, Branding and Promotion

When we look at technology we use everyday, the great success stories all have one thing in common: competition. They all achieved their success despite healthy competition, or perhaps because of it.

(more…)

Tagged with: , ,

Who Moved My Cheese? The New WordPress Admin Interface

Posted in Programming and Software Development, Technology, WordPress by engtech on April 18, 2008

WordPress Tips and Tricks

Two of my blogging heroes and inspiration Jeff Atwood and Joel Spolsky have joined together on a new venture called StackOverflow: overflowing with awesomeness. They are also doing a weekly podcast, and you can download the first 45 minute podcast here (8 MB). In the discussion, Joel makes a great comment: Windows Vista gives you change without giving you any value. As a Windows XP user there is no compelling reason to upgrade because you’re going to have to relearn where everything is, but you don’t get any new and compelling features or applications to offset that.

(more…)

Searching for the Perfect Inline Code Documentation Tool

Posted in Programming Tools, Ruby, Technology, Writing Better Documentation by engtech on March 07, 2008

Programming Tips

Even amongst programmers I’m weird because I have an intense love for documentation. No, that doesn’t mean I overly comment my code, or that you’ll catch me browsing happily through the product requirements document during my coffee break. I should be more specific.

engtech-documentation-unravelling.png

I have an intense love automatic documentation generation. Nothing makes me more tickled pink than seeing code and documentation living side by side in perfect harmony. I hate seeing documentation put on the company intranet only to diverge from the code it’s supposed to explain as the days go past. I hate hitting my head against a brick wall as I’m pouring through the source code trying to understand an API because at no point does it mention that it’s documented in a Word doc in another directory.

This is my rule of programming: documentation should live beside the code it documents, in the comments, especially if it’s API documentation. If your language of choice doesn’t already have some kind of automatic code generation tool then you’re probably using the wrong language.

(more…)

How to Explain RSS to Normal People – 2008 Edition

Posted in Building a Community, Facebook, Humor, RSS Syndication, Ruby, Technology by engtech on February 28, 2008

Social Software and You

As a geek who enjoys spending too much time on the internet, I like RSS almost as much as delicious toast. As a blogger, RSS is the shiznitz because it lets you consume a lot more information and it makes it easier for other people to read your blog without having to drop by every few days to see if you’ve written something new.

For something so useful, it’s pretty hard to explain why people should use RSS. Lots of people try to do it. This is my take on it. It’s 2008 and explaining RSS should be much simpler because if you’ve used Facebook, then you’ve used RSS.

RSS for Normal People (who use Facebook)

(more…)

Tagged with: , , ,

I Can Has Ruby?

Posted in Asides, Ruby, Technology by engtech on February 26, 2008

I seem to have finagled my way into getting to program in Ruby almost full time at work. One day you’re just reading raganwald, labnotes and gilesbowkett and then the next day you’re doing guerrilla adoption of a new language. Obviously, the internet is a gateway drug. I tried out Ruby mainly as a way of increasing my own job satisfaction after I had heard so many good things about it.

So far it’s working.

Because I’m using Ruby for several hours a day, I’ve decided to start up a tumblr account as a link dump for all the things I’m finding out as I’m learning Ruby.

rubeh.tumblr.com

Related Posts

The Canary in the Coal Mine of Open Source Code Re-use

Posted in Humor, Programming and Software Development, Software, Technology by engtech on February 12, 2008

Programming Tips

Don’t reinvent the wheel. Like all advice it’s much easier to say then it is to do, particularly when it comes to programming. Programmers suffer from a horrible mental disease called Not Invented Here Syndrome (it’s in the DSM — check if you don’t believe me). We will happily rewrite a perfectly good tool because someone else wrote it and it’s easier to rewrite than it is to understand. Sure, we might not handle all the bells and whistles of the original tool (unicode is for sissies) — but at least we got to DIY.

Rewriting from scratch is particularly a bad idea when it comes to open source software. If there’s an open source library or plugin available that does the trick then there’s no reason at all for you not to pick it up and use it. It’s free. If it doesn’t work the way you want it to then you can rewrite that small part or add functionality. There’s no reason to reinvent the open source wheel…

… as long as you can find it. One problem with leveraging open source is finding out if it exists at all. After enough cursing at Google you’ll eventually get a knack for it and know the good code sharing repositories and announcement lists for your languages of choice. The wheel exists! Someone else is having the same problem and came up with a solution, so now you don’t have to! Thank you, lazyweb.

Or that’s what you think, until you try to take the wheel for a test drive. The install instructions are outdated and don’t work quite the way you’d expect. You have slightly different versions of some common components and that causes things to break in mysterious ways. Or you’ve come across a wheel that the wheelmaker gave up on after he got to where he had to go. The wheel looks like it might do what you want it to, but it’s missing the voodoo required to hook it up to anything.

The single biggest problem I have when trying to leverage open source code with my projects is how long does it take me to get it up and running to demo it? If I can see right away that it works and it will solve my needs, then I’m willing to slog through migrating versions of different libraries, sacrificing some chickens and swilling too much coffee to get it integrated with my code on my machine. But the problem is all the installation headaches seem to happen before you reach that nirvana of running a working demo and seeing what it really does (as opposed to what the hastily written release notes ambiguously imply that it does).

I know what you’re thinking fearless reader: this looks like yet another rant about a wasted afternoon trying to be “productive” through code reuse but instead spent glaring at compiling warnings. But there is a light at the end of the tunnel! I have a solution that will keep you from the Poorly Thought Out Install Process Hell that prevents you from making the most out of open source software:

Google Blog Search.

Ok, that was a bit anticlimactic; and it isn’t even Valentine’s Day yet. But stick with me, I have a point lying around here somewhere. If no one other than the author is talking about that tool/plugin/library on blogs then that means no one else is using it. Or, at the very least, that it hasn’t been sufficiently hardened that you should make any assumptions about how smoothly that wheel will run.

At the very least you should be able to find someone writing about what the tool/plugin/library does and any issues they had in getting it running. If no one has bothered taking the time to do that, then that is a huge canary in the mineshaft, dear friend. A huge canary wearing a bright orange tank top with the words “waste of time” written across it.


Photo by tenerife

How to Install the Exception Notifier Plugin with Ruby on Rails

Posted in Ruby on Rails, Technology by engtech on February 06, 2008

Learning Ruby

Exception Notifier is a Rails plugin that will email you when an error occurs in your Rails application with full debugging information. It’s as useful as you can imagine, and running it is the difference between happy users and grumpy users who don’t use your web app because every second click looks like this:

Rails Error Message

Agile Web Development with Rails v2 has the skinny on how to install this plugin starting on pg 629. In my infinite Rails Newbieness, I still had a heck of a time getting it working properly despite excellent guides like this one or the official install notes.

The Newb’s Guide to getting the Exception Notifier plugin to work in Rails

#1: That was easy – Installing the Exception Notifier Plugin

Step #1: On the console in your Rails application root directory type:

 
ruby script/plugin install exception_notification

Step #2: Add the following line to your config/environment.rb file AT THE END OF THE FILE:

 

# Include your application configuration below

ExceptionNotifier.exception_recipients = %w(your@emailaddress.com)

Step #3: Since you’re already changing configuration options, you might as well change these two from the default while you’re at it.

 
ExceptionNotifier.sender_address = %("Application Error" <app.error@myapp.com>)

# defaults to "[ERROR] "

ExceptionNotifier.email_prefix = "[APP] "

Changing the sender_address can go a long way to preventing the emails from being marked as spam.

Step #4: Restart the server! You’ve installed a new plugin which means you have to restart the server in order to use it.

Gotcha #1:

 

active_support/dependencies.rb:266:in `load_missing_constant': uninitialized constant ExceptionNotifier (NameError)

This means that you put the ExceptionNotifier.exception_recipients line in the wrong spot. It goes at the end of the file, not in the class.

#2: The Postman Rings Never – How do I debug the email notification?

Step #1: Open up a console windows and do a tail -f log/development.log and you’ll be able to see the Exception Notifier plugin trying to handle the emails.

It will show information like who the email is being sent to, and delicious tidbits like the email is crashing with an SMTP Authentication Error.

 

endering ./script/../config/../public/500.html (500 Error)

rendering section "request"

rendering section "session"

rendering section "environment"

rendering section "backtrace"

Sent mail:

From: Exception Notifier <exception.notifier@default.com>

To: engtechwp@gmail.com

Subject: [ERROR] mycontroller#error (Net::SMTPAuthenticationError) "334 HASHINFO"

Mime-Version: 1.0

Content-Type: text/plain; charset=utf-8

A Net::SMTPAuthenticationError occurred in mycontroller#error:

#3: But Does It Blend? Generating Exception Notificiations on Development

Step #1: Create a controller action that will always generate an error

Edit one of your controller files and add these lines

 

def error

raise RuntimeError, "Generating an error"

end

You don’t need to create a view for it.

Step #2: Change your development settings to let exceptions generate email notifications. In config/environments/development.rb change these two lines

 

#config.action_controller.consider_all_requests_local = true

config.action_controller.consider_all_requests_local = false # debugging exception_notifier

#config.action_mailer.raise_delivery_errors = false

config.action_mailer.raise_delivery_errors = true # debugging exception_notifier

Step #3: Tell Exception Notifier to ignore it’s local address rules

In app/controllers/application.rb

 

include ExceptionNotifiable

local_addresses.clear # always send email notifications instead of displaying the error

You’ll want to remove these changes once you know the Exception Notification plugin is sending emails.

Step #4: Try it out! Navigate to the http://yourapp/controller/error action you created in step #1 of this section. Instead of seeing the debugging trace you’ll see the standard application error page that your users see. But did you get the email?

#4: The Spice Must Flow – Configuring Action Mailer

If you already have a working ActionMailer configuration then skip this section.

The default settings for Action Mailer will use SMTP on localhost. Give it a try and see if it works. If it doesn’t get sent then it may be because you’ve never configured Action Mailer to know anything about how to send an email! Configuring Action Mailer is  covered on pg 567 of Agile Web Development with Rails v2.

You can see if the email was sent or not by looking at your development log file and seeing if there are any dread SMTP errors like

 
535 5.7.3 Authentication unsuccessful. 

Exchange can be a cruel mistress.

The settings go in config/environment.rb (or one of the files in the environments subdirectory if you have different mail settings for different servers). You’ll have to figure out the correct settings by checking your mail program or by bribing the IT guy.

 
config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {

:address => "domain.of.smtp.host.net",

:port => 25,

:domain => "domain.of.sender.net",

:authentication => :login,

:user_name => "user",

:password => "secret"

}

More information about the ActionMailer configuration options.

I’d like to give a big thank you to all of the commenters on this post, without which I wouldn’t have gotten this working.

Yahoo Pipe: Sub-Reddit Feed Filter

Posted in Delicious, Reddit, Ruby on Rails, Technology, Yahoo Pipes by engtech on January 28, 2008

Hacking RSS with Yahoo Pipes

Popular social bookmarking site Reddit has announced a great new feature: users can create their own sub-reddit. What does this mean in English? Users and communities can create their own social bookmarking sites around specific topics: blogging, wordpress, specific programming languages, etc but still use their regular reddit account for submitting links and voting.

You can see a full list of all the new reddits here, sorted by popularity. Of particular interest to me is the new Reddit created for Ruby/Rails related posts.

Of course, it’d be nice to be able to subscribe to a filtered version of these links. I’ve created a modified version of Dave S‘s “reddit popular on delicious” Yahoo Pipe that works with Sub-reddits.

  1. Click on the link
  2. Enter the name of the sub-reddit you’re interested in
  3. Enter the minimum number of saves on a delicious before a link is included in the feed
  4. Enter keyword inclusion/exclusion filters if you want to limit what you get
    • ie: include only rails-related posts or exclude all rails-related posts
  5. Click Run
  6. Click on the subscribe to RSS button

I’m using the Ruby sub-reddit as an example, but this is a great way to track links based around any topic there is a sub-reddit for. Even lolcats.

I’m looking forward to when this Reddit feature comes out of beta and it’s possible to create a few new sub-reddits like blogging, wordpress and lifehacks.

Related Posts

9 Ways to Know When to Jump Ship at a Startup

Posted in Startups and Business, Technology, Workhacks and High Tech Life by engtech on January 11, 2008

Workhacks

For the last couple of months I’ve been plagued with wondering if I should stay at my current startup. I’ve been approached with a few different job offers that I haven’t followed up on, and maybe it’s time I pursued greener pastures. In the words of the Clash: should I stay or should I go now?

Indecision

Changing jobs is a big, life altering decision and if you have my knack for risk avoidment it can be a horrendous see-saw of uncertainty. It’s this state of uncertainty that is ultimately the cause of the most unhappiness in your life. Leaving your options open is always less satisfactory than making a firm decision.

Compensation

When comparing offers from other companies, you need to compare the full package which is a lot harder than it looks.

  • Health benefits / Health insurance
  • Overtime compensation
  • Pension plans / Pension matching
  • Stock purchase plans / Stock discount
  • Stock options / equity
  • Travel allowance / food allowance
  • Raises

In particular it’s very hard to figure out what stock options are worth, if anything. The best advice I’ve read is that your stock options aren’t worth considering in any compensation comparison unless you are a founder.

This wiki page does a very good job of explaining how any employee can figure out what their pre-IPO equity is worth. What’s most important is to figure out the percentage of total options and how much funding the options are worth. Don’t forget to include capital gains tax (eg: 40%) when figuring out how much those options are worth.

More information on equity dilution

Business Plan

When will the startup be profitable? How much money has been invested in the company? How much more funding is needed until the startup can stand on its own legs? The more you can find out about this, the better off you’ll be, because you can’t accurately evaluate your monetary compensation and the future of the company without it.

At my previous job I was making more money than I am now, plus there was an average of a 5-8% raise per year. Startups often have no salary increases until they are profitable, or at least have revenue on the books. When you look at the roadmap to profitability you need to factor this in so you can evaluate if the potential payoff if the startup does well comes close to matching the potential revenue lost working at another company.

Bankruptcy

Most startups fail. The most likely outcome of working at a startup is showing up to work one day and finding the doors locked. There may be no compensation package for the newly unemployed workers until they land another job. Waiting for a golden handshake from downsizing is a worse idea than acting on an opportunity that has presented itself at a different company.

Technical Debt

Startups cut corners. You may not have the best tools available to get the job done. You are always squeezed for time and money, which means quality suffers. Poor quality can throw a monkey wrench into schedules, forcing crunch time in order to meet the delivery dates. This technical debt is just like any other debt in that it requires interest payments and you’ll have to pay it off eventually — although project managers often ignore it completely. Steve McConnell covers technical debt in more detail.

Signs of Success

Success should happen early. If things are always running smoothly then the work environment will be enriching and enjoyable. If things never work properly the first time then it can create a big cloud of doom that hangs over the head of everyone in the company and curses the new work being done.

Positive Reinforcement

How are employees reinforced for good work? In a startup, it usually won’t be monetary but that’s ok because one of the best rewards is the time to work on pet projects. Interesting work is its own reward.

Work Experience

Monetary compensation might pay off the bills, but it won’t make you feel as satisfied as a job well done. What makes me happiest is learning/improving new skills and knowing that I’ve done a good job. Having to constantly return to the same project that never works properly is one of the most soul-sucking experiences I’ve ever had. It’s like a bad relationship that drags on and on. You’re trying to make things work, but there’s always something new that comes up and drags you back into old issues that you thought were worked out a long time ago.

“Will I enjoy the work?” is the one of the most important criteria for evaluating a job change, because passion can’t be faked and it’s the only way a job will enrich the rest of your life.

People

Jeff hit the nail so squarely on the head when he said that the most accurate predictor of job satisfaction and success is if you like the people you work with. No matter what the problem is, it’s a people problem and if you don’t enjoy working with your coworkers then you’ll never enjoy your job.

Did you like this post? If so then please vote for it on digg. Thanks!

Related Posts

Why Open Source Software Sucks – Software Simplicity Isn’t Simple

Posted in Programming and Software Development, Technology by engtech on December 06, 2007

Programming Tips

Aside: Hosted software would be something like Gmail, while installable software would be something like Outlook. WordPress.com is hosted software by Automattic, but it is also available at WordPress.org where you can download it and install it yourself where ever you want.

There are a few “internet rockstars” in programming circles, and most programmers who read blogs will have heard of Joel Spolsky (one of the few people who writes entertaining tech books) and 37signals (the guys who made Ruby on Rails and Basecamp). The guys at 37signals recently wrote a post about how they prefer creating web-based software that they host vs software that a user would have to download and install themselves because it is so much easier for the software developer. When you don’t have to release your software into the wild you have so many less things to worry about: different operating systems, memory performance, installation dependencies, hardware dependencies.

“You have to deal with endless operating environment variations that are out of your control. When something goes wrong it’s a lot harder to figure out why if you aren’t in control of the OS or the third party software or hardware that may be interfering with the install, upgrade, or general performance of your product. This is even more complicated with remote server installs when there may be different versions of Ruby, Rails, MYSQL, etc. at play.”

Joel looks at his stats and points out that if he didn’t provide installable software then he’d be out of business, because it accounts for 80% of his revenue compared to hosted software.  He also makes a great point that software that people are willing to buy is software that solves a gnarly problem, IE: it deals with complicated stuff. Any other kind of problem can be solved by free software because its uncomplicated enough that one guy in his mom’s basement can churn it out over a weekend.

“The one thing that so many of today’s cute startups have in common is that all they have is a simple little Ruby-on-Rails Ajax site that has no barriers to entry and doesn’t solve any gnarly problems. So many of these companies feel insubstantial and fluffy, because, out of necessity (the whole company is three kids and an iguana), they haven’t solved anything difficult yet. Until they do, they won’t be solving problems for people. People pay for solutions to their problems.”

But he then follows through with a great point that the gnarly problem that 37signals’ applications solve is the problem of design. 37signals might be building fluffy Ruby-on-Rails Ajax sites, but that’s beside the point of the problem they’re really solving: how to design a great looking user experience that makes people happy.

I think this draws a great parallel to what’s wrong with free software: it’s created to scratch a certain itch, and that’s usually all it does. Compelling user interface? Joy to use? Nope, it solves the original programmer’s problem and that’s about it. And before you get all uppity that I’m attacking open source software, let me clarify that I’m talking about the open source software I create.

The problem is two-fold: I have a natural tendency to over-complicate things and I have trouble sharing the customer’s pain (stepping away from the code, and seeing how a stranger would view the end result). Jeff “Metal” Atwood asks “When was the last time you even met a customer, much less tried to talk to them about a problem they’re having with your website or software?”

This hit me last week when I sat down with another engineer to show him an internal tool I was building for him. He started poking a usage case that confused him. It wasn’t in the spec, and it didn’t follow the way he thought of the flow. It was an artifact of the internal data structures I was using that I was exposing to the user. This happens too often. It’s the opposite of opinionated software [1]: pushing the decision making on to the user. [2]

Of course, writing open source software has its benefits because quite often there’s no barrier between you and the people who are using your software other than computer screens. You are your own quality assurance, and you are your own customer service. You have to explain to the users why they should install your software, you have to deal with the installation headaches your platform choice created, you have to explain any complexities with how to use it, and you have to help them when problems occur.

My open source software might suck, but its helping me explore the solution to a gnarly problem: how to solve problems in a way that is easy for other people to use.

Related Posts

Footnotes

1 – There’s an interested essay to be written comparing opinionated software to considerate software.

2 – This programming talk might bore you, but the problem of simplicity in design is cross-discipline and applies to any blogger.

Getting Started with Ruby on Rails – Week 3 – Testing

Posted in Ruby on Rails, Technology by engtech on December 05, 2007

Learning Ruby

I’ve fallen for the hype and started using Ruby on Rails for building database driven web applications. You can follow along with my weekly experience discovering gotchas with Ruby on Rails.

Previously: Getting Started With Ruby on Rails – Week 2

(I swear, back to your regularly scheduled non-rails content soon enough)

Gotcha #1 – after_initialized is after_instantiated

Yes, after_initialized is called more often than just when you call Model.new. Use if new_record? inside of it.

Gotcha #2 – button_to has it’s own class

You can’t pass :class parameters to the button_to helper because it creates it’s own :class=>”button_to”. Use :id instead.

Use console

script/console will give you an interactive console for playing with objects. Use it! It makes debugging tiny little gotchas with ruby syntax you might not be familiar with so much easier. Type reload! in the console to reload your models after any changes you’ve made. Type object.methods to see a list of everything an object responds to.

You can use many familiar console navigation keys like Up, Down to move between previous commands and Ctrl-A for start of line and Ctrl-E for end of line.

The Migration Shuffle

When you’re building a new migration on your development database always do the following:

rake db:migrate
rake db:migrate VERSION={current version - 1}
rake db:migrate

It’ll let you know that you’ve made an error in your down method right away, instead of weeks later when you’re trying to rebuild the database.

Little Bobby Tables

At some point your going to write a bad migration and screw up your development database, so rebuild it.

> mysql -u root -p
drop database proj_development; drop database proj_test;
create database proj_development; create database proj_test;
quit
> rake db:test:prepare
> rake db:migrate

bobby tables

http://xkcd.com/327/

Data Migrations

If you’re building data migrations, always uses .save! so that it will fail on a validation error and you may want to litter your migration with puts statements to jump to which object is failing validation. There’s probably a better way of doing this using fixtures, or using –trace to find which migration failed.

Or hell, don’t use a data migration for bulky legacy data.

There’s Something About Tests

I really like how simple it is to write fairly complicated tests. One thing I didn’t like was how many tests it is possible to write. The examples from Agile Web Development with Rails showed them creating a lot of tests for the validates_* helpers. Unfortunately, you don’t need to create tests that duplicate those helpers because they are bulletproof. You do however need tests to prove that you used them correctly.

Cut-and-paste errors do happen, and double checking my validations did reveal at least one case where I thought I was validating a field but I wasn’t. Not to mention that if you’re using a regular expression filter to validate the format of a field you might forget to put start and end delimiters on it. Even testing something simple like all values are in the list is useful because you might have another validation that invalidates one of the values from your list.

Ruby is Dynamic

One thing I can’t stress enough is how much you NEED unit tests. Ruby is a dynamic language, and as such there isn’t a great and easy way to find out if the code will blow up without running it. If you run it by hand you won’t find all the interesting scenarios for the simple reason that you won’t be rechecking features you implemented last week that exploded because of a change you made this morning. You need a regressable test suite.

And there’s nothing like writing a test to make you realize how much more complicated you’ve made things than they need to be.

How to Run Tests

Run an individual test

ruby test/unit/testname.rb

Run multiple tests:

rake test # run all test
rake test:unit # run unit tests
etc

They can all be done inside of emacs by using the Tests drop down menu in rails-mode. This is the preferred method because you can click on errors and go directly to that file.

rails-mode also lets you use C-c C-c . to run the current test file. This allows you to rapidly iterate through test development.

Running Tests – Verbose Assertions

Here’s another tip that I didn’t realize at first: you can supply a message argument to your assertions that will display when it fails. This is essential if your using loops in your tests, IE: looping over an array of invalid field values, because the line number isn’t enough information to find out why the test failed.

Debugging a Test – Breakpoint

You can use the breakpoint keyword where a test is failing. This will open up a console at the breakpoint spot. Unfortunately it doesn’t work well inside of emacs because the ROutput buffer is read only (in fact, you’ll have to kill the process). So run the test from the command line when you want to play with breakpoints. I can’t seem to find a way to access the local variables in a method… so on to ruby-debug.

Debugging a Test – rdebug (or redbug according to Microsoft Word)

sudo gem install rdebug -y

in config/environments/test.rb

require 'ruby-debug'

Then use the debugger keyword instead of the breakpoint keyword where you want to stop. Don’t use it when you’re running tests from emacs because things will look weird.

Running Tests – Fixtures – Validating Fixtures

Here’s the fun bit: sometimes you break your fixtures. Not on purpose, to test bad data, but because your erb goes a little wrong, or because they’ve gotten out of date with your schema. Here’s a rake task that will let you do rake db:fixtures:validate

If you are using erb to generate your fixtures, you can also see how your fixture will roll out using:

erb test/fixtures/fixturename.yml

And while you’re at it, you probably want to validate your existing database against your models. Here’s a rake task that will let you do rake db:validate_models

Running Tests – Advanced – autotest (part of ZenTest)

There’s a plugin called autotest that will automatically run tests on any files that have changed. This is great because you can keep the console open in the background and it will immediately catch if you’ve saved a file with a typo! No need to go to the web browser, navigate to the changed page and hit refresh. In fact, using the web browser should be an afterthought… you should be able to create tests for any features.

sudo gem install ZenTest
rehash
autotest -rails

One gotcha: disable autotest if you’re manually running tests as well! You’ll end up creating duplicate records in the test database. The solution is: don’t manually run tests with rake at the same time as autotest.

Walkthrough of autotest: http://maintainable.com/articles/dry_up_testing_with_autotest

There’s a way to integrate autotest into emacs.

Running Tests – Advanced – RedGreen (for autotest)

Not the horrible Canadian TV show, but a notifier for autotest status reports.

https://wiki.csuchico.edu/confluence/display/webd/Testing+Rails+Apps

Running Tests – Advanced – ZenTest

ZenTest is useful for parsing your rail files and creating stubs of tests.

Running Tests – Advanced – Test::Rails (part of ZenTest)

Test::Rails provides a mechanism for splitting functional tests into controller tests and view tests. This decoupled lets you check your business logic as is, and your view routing as is.

If you want to add a generator for creating view/integration/controller tests:

./script/plugin install http://topfunky.net/svn/plugins/vic_tests

Some collected thoughts about Test::Rails

Running Tests – Advanced – Rcov

Rcov is another tool that will help your testing by calculating the code coverage of your tests. This is an essential tool to find holes in your testing strategy. All the usual caveats of code coverage apply.

http://eigenclass.org/hiki.rb?rcov

sudo gem install rcov
rehash
rcov test/path_to_specific_test.rb - or - ./script/plugin install http://svn.codahale.com/rails_rcov

Now you can do

rake test:units:rcov

http://agilewebdevelopment.com/plugins/rails_rcov

Running Tests – Advanced – Heckle

Heckle mutates your code to see if the tests actually check anything. Unfortunately highly coupled code is heckle-proof because changing anything breaks everything else.

sudo gem install heckle

Running Tests – Measuring – Flog

Flog measures reports a score based on how complex it thinks your code is. The higher the score, the higher the chance that there is a bug hiding there.

sudo gem install flog

A Handful of Blogs About Rails Testing

These guys have written a lot (all?) of the plugins I’ve mentioned and are worth checking out if this stuff interests you:

Related Posts

Getting Started With Ruby on Rails – Week 2

Posted in Ruby on Rails, Technology by engtech on November 28, 2007

Learning Ruby

I’ve fallen for the hype and started using Ruby on Rails for building database driven web applications. You can follow along with my weekly experience discovering gotchas with Ruby on Rails.

Previously: Getting Started With Ruby on Rails – Week 1

Emacs Rails-mode

Last week I complained about wasting time setting up rails-mode in emacs. I’m starting to find some real time saver though. The navigation short-cuts are absolutely necessary for navigating the file structure of a Rails application and I really like how the syntax highlighting can capture lines that don’t make any sense to Ruby. This is a great feature if you’re learning Ruby at the same time as you’re learning Rails. It has auto completion for “”, [], {} and ending function blocks and even picks up things like when you have one too many ends in your file.

Which files should be checked in?

I couldn’t find a list of what files are allowed to be checked in anywhere in Agile Web Development with Rails. The answer seems to be anything but:

db/schema.rb # easier to let your db:migrate control it
config/database.yml # because it contains database passwords
coverage/* # generated by rcov
logs/* # generated by server
tmp/* # temporary sessions files

Don’t overwrite the flash

Bad, no validation errors will be shown:

@model.save
flash.now[:notice] = "I saved it"

Better:

if (@model.save) then
flash.now[:notice] = "I saved it"
end

Will trap and display ruby errors as well as validation errors:

begin
if (@model.save) then
flash.now[:notice] = "I saved it"
else
  raise "Error saving"
end
# Do stuff
rescue Exception => e
  flash[:notice] = e.message
end

Keep controllers streamlined

I found myself creating one controller that had add/show/delete/list actions for multiple models. It’s much cleaner to have multiple controllers for the individual models.

Conditional Linking

link_to_if will put an unlinked version of the text if the conditional is false. This is much more useful than removing the link text completely for a lot of situations, because you don’t have to worry that the rest of the text around it will look weird. Don’t try to use html_options as a hash! I lost quite a bit of time to this because it won’t use the method parameter, but it doesn’t give you an error.

link_to_if (check_if_user_can_delete),
	"Delete Image",
	{ :action => "delete", :id => @image.id },
	:confirm => "Are you sure?",
	:class => "dangerous",
	:method => :delete

Generate validates_* off of database

I would have liked it if the generate script automatically generated validates_* helpers off of the database table. validates_length_of could be generated for :limit and validates_numericality_of could be generated for :integer.

Using the same partial to display create/edit/show

This is a neat little trick I found. You can use the same partial for your create/edit/show actions by using html_options and setting

{ :disabled => (controller.action_name == "show" ? true : false) }

for all the fields. It might not be useful for many public applications, but for an internal app it’s a great way to use the same ajaxy displays that you use for create/edit in show.

Polymorphic Associations

Are weird if you want to validate uniqueness. They might work better with has_many relationships than with has_one relationships. Or, I made it more complicated that it had to be.

Deploying sucks

It’s true. Played around a bit with capitrano and vlad the deployer but the both seem to assume you’re using subversion.

Free Tidbit: How crypt works in passwd files

I don’t know why this was so hard to find in Google: passwd files that use the crappy crypt mechanism use the first two characters of the expected password as the salt!

given_password = "hello_world"
encrypted_password = "ahga3sgj"
return encrypted_password == given_password.crypt(encrypted_password.slice(0,2))

and don’t worry, I’ll talk about something other than Rails later this week :)

Book Review: Ruby on Rails for Dummies

Posted in Book Reviews, Ruby on Rails, Technology by engtech on November 26, 2007

Ruby on Rails for DummiesI don’t have anything against the for Dummies series (one of my friends is an author), but they’re only good when you want a very general understanding of a concept. I wouldn’t recommend the series for technical books. But my local library happened to have a copy of Ruby on Rails for Dummies, so I gave it a try.Here’s the good news: if you’ve ever used a programming language or used any HTML then you can skip the first 150 pages.

The 26 pages of how to install the software can be skipped by using InstantRails and then downloading RadRails. You’ll want to pay attention to pages 104 to 112 where the author delves into some of the ways Ruby is different than other programming languages (blocks, yielding, symbols, 0 is true).

The book uses RadRails for all of its examples; which is fine except that it takes so much longer to explain how to do something with a GUI than it does to type rails myproject or script/generate controllers ShoppingCart show. I really hate that they don’t show the one line console command as well as the four pages of GUI operations and screenshots. They don’t specifically mention which version of Rails they’re using, but the installation screenshot shows rails-1.1.2, which is a little on the old side (although the only errata I’ve seen is that require_gem doesn’t work anymore).

Thankfully once you’ve skipped ahead to chapter 8 and they start dealing with Rails in all its glory the book gets a lot better.

What The Book Covers

  • Stuff to skip
  • Chapter 8: view, controller, partials, helpers
  • Chapter 9: model, migration
  • Chapter 10: linking with image_tag, link_to, h, how ERb rolls out to HTML
  • Chapter 11: uploading a file, storing binary data in database
  • Chapter 12: validating input, belongs_to/has_many/many-to-many
  • Chapter 13: AJAX, sending email, XML, SOAP web service
  • Chapter 14: web sites (most are still alive)
  • Chapter 15: lots of ruby-specific tricks with no details
  • Chapter 16: Rails concepts aka “I have a job interview in 10 minutes!”
  • Chapter 17: using Rails on legacy databases

What could be discussed more

  • debug helper
  • CSS
  • RJS
  • Layouts
  • Testing
  • Fixtures
  • REST
  • Rdoc
  • routing

One thing that’s pretty dang neat is that the author provides his email address and his phone number. That’s an impressive level of service. He tries a little too hard to be funny in the book, but there were some parts that made me chuckle (like when he talks about sending email reminders to his wife, but using instant messaging when he needs her urgent attention).

Unfortunately I’d recommend picking up a copy of Agile Web Development with Rails (AWDWR) instead of Ruby on Rails for Dummy (RORFD). RORFD is split into many small unrelated examples while AWDWR has more extensive example code that you could use as a skeleton for a professional site. AWDWR is much clearer to read than RORFD, which always interrupts the flow with a new figure and screenshot. One page of text may cross-reference up to ten other figures/screenshots/chapters. It feels like RORFD has ADD and it doesn’t make for an easily digestible read.

You can find a more favorable review here.

Getting Started With Ruby on Rails – Week 1

Posted in Ruby on Rails, Technology by engtech on November 21, 2007

Learning Ruby

I’ve fallen for the hype and started using Ruby on Rails for building a database driven web application. If you’ve never heard of Rails it is a web framework using the Ruby programming language. Ruby is an object-oriented interpreted language, that’s often compared favourably with Smalltalk. [1] What’s a framework? A framework provides a structure and a set of tools usually for solving a particular type of problem. A programming language solves general problems while a framework extends a programming language to better solve a specific problem.

Rails is a framework for building web applications: stuff like blog software, instant messaging, to-do lists, web magazines, and your favorite web comic. Word on the street is that ROR is a resource hog but the resource consumption is balanced out by how much more productive it is to develop with. It’s easier to buy more computers to host a web application than it is to hire more developers. Computers get more powerful over time; developers not so much.

I’ve been developing websites as a hobby off and on since 1994, but I only learned CSS in the past six months. I’ve done some minor hacking of other people’s web apps that were written in ASP or Perl and they were always horrible messes of spaghetti code. I’m really looking forward to trying out a web app from scratch.

Choose Your Path

I run a Windows machine with a VMWare Linux box inside of it, so I can choose to do my Rails development under Windows or under Linux. If I use Windows then I can use InstantRails, which is a one-click installer that gives you everything to need to start coding ASAP. But I much prefer developing under Linux because you can’t beat the power of having a strong command line. The Windows command line console is a joke, and requires a ton of 3rd party utilities for stuff that’s already there under Linux. [2]

The downside is that there is no one-click install for Linux. Well, except for this one, which I didn’t notice until now :)

Installing ruby, gem and rails is simple and I was able to do it under my user account using the standard –prefix=/home/engtech install options.

Gotcha #1 – MySQL

I already had MySQL installed on my Linux box but it was an extremely old version that blew up the second I tried to use Rails to talk to the database. You need at least MySQL 4 to use Rails because it uses ENGINE=InnoDB for its calls. Older versions of MySQL don’t have InnoDB turned on by default, and once you do turn it on they only understand TYPE=InnoDB.

Mysql::Error: You have an error in your SQL syntax near 'ENGINE=InnoDB'

Tip: Get the latest and greatest version of MySQL instead of whatever came with your Linux install. I needed the Server, Client, and Developer RPMs. MySQL was the part of the install process that required root access.

Tip: If you use a password for your MySQL root account, make sure you change config/database.yml to use it.

Gotcha #2 – Integrated Development Environment (IDE)

Rails doesn’t come with a standard IDE, but instead gives you a wide option of choices. Aptana RadRails, based on Eclipse is a good choice. But I’ve already sold my soul to one editor for all my coding needs: emacs. Emacs is the “kitchen sink” IDE because it supports everything: you can find extensions for any programming language or task. The downside is that it has a learning curve like you wouldn’t believe.

There’s a tutorial on how to add rails support to emacs. It’s long and complicated. Using rails mode in emacs requires upgrading to emacs version 22 that broke a lot of my existing DotEmacs hacks. I eventually got it working, but in retrospect I might have been better off going with RadRails because I lost hours to this. I’m still finding emacs keystrokes that don’t do what I expected them to.

I’m unimpressed that there isn’t a quick reference print sheet for rails-mode, this is the best that I could find. So far I’ve only been using the syntax highlighting and C-c C-c g K and C-c Up / C-c Down to navigate between files.

Gotcha #3 – Development Server vs Production

When I was running into MySQL installation problems, I toyed with using SQLite3 instead for a while. Needless to say, make sure your development database is using the same versions of everything as your development and test servers. It’ll save you lots of headaches.

Initial Opinion

People weren’t lying about how productive programming with Ruby on Rails is. In the same amount of time it took me to write this blog post I was able to get a simple web application with user authentication up and running with a web interface that is probably “good enough” for final release. Which is ridiculous, compared to my previous experience hacking apps together using ASP or Perl.

  • Directory structure - Clean, clear, and everything has it’s place.
  • Naming conventions - One of the best things a framework can give is enforcing a standard way of naming things. It takes a while to learn it, but it becomes second nature that if a class is called X, the database table is called Y and the tests are called Z. If you leave it to themselves most developers create small inconsistencies in naming conventions that waste time — especially if more than one person is working on the code.
  • Don’t Repeat Yourself - I really like the way Model/View/Controller separates the code and keeps it becoming a mess. Inheritance and helpers/partials are great for keeping you from duplicating code.
  • Succinct - Wow, you really do get a lot done with very little code writing. They weren’t kidding when they said you could write blogging software in under 15 minutes.
  • HTML / CSS / XML – I really love that it doesn’t try to hide the HTML, CSS and XML under a lot of programming calls. There are helpers for doing common things, but you’re free to write your own web code.
  • Development / Test / Production - In my limited experience with web apps, I’ve never worked on anything that had more than 20 users. Testing was all done manually, and the production server was the development server. It was a mess. Clean separation makes it much easier to work on code independently and only push it out to users once it has been rigorously tested.
  • Migrations - We use to build our database tables using a PHPMyAdmin web interfaces. Needless to say, doing it through scripting where you can tear down, reassemble, and rebuild the database tables is much cleaner because everything is reproducible from scratch.
  • Rake, rdoc, and test - One of the things I like most about Ruby is that it has all the fixings I expect from modern languages: the ability to automatically generate documentation off of the code and a built-in unit testing and build framework. I’m always amazed when I see a language that doesn’t natively support these facilities.
  • Religion - The big downside to Ruby on Rails is that it feels a little bit like a religion sometimes.

Conclusion

I should have tried Ruby on Rails a long time ago. I spent entirely too much time setting up my development environment compared to when I could have been developing a web application. I could have been up and running in less than an hour if I had:

  1. Used InstantRails
  2. Used Aptana RadRails

Footnotes

1 – Did you know that Smalltalk inspired the Macintosh GUI? Smallpark was yet another example of the magic that was going on at XEROX PARC in the 70s. These are the guys who invented the mouse, colour graphic, windows/icons for a GUI, WYSIWYG text editors, Ethernet (how you talk to other computers on a network), and laser printers. Programmers at Work featured interviews with some of the people from PARC.

2 – I’m always amazed that people can program without easy access to diff, find, grep, perl, etc. All of these things are available for Windows for free, but they never work quite the way I expect them to.

People Are Computers Too – How Improving Applications Can Improve Your Life

Lifehacks and Productivity

This week I’ve been talking about code profiling and how if you want to analyze the performance of your application you need to work with large sets of data. Application efficiency isn’t free, it requires measurement, analysis and change. Unsurprisingly, performance analysis for a software application and performance analysis for aspects of your life have a lot in common.

Memory Management

When people ask me how to make their computers faster, my answer is always “more RAM”. The biggest reason why software applications run slowly is because they don’t have enough memory to operate efficiently. People work the same way. You can improve your memory by getting enough sleep at night and by mind-dumping.

Mind-dumping means write things down instead of trying to keep it in your head. Human memory works like computer RAM where it needs constant refreshes of remembering something to keep it from becoming forgotten. By writing down lists of what you have to do in the future you free up your mind and your attention to focus on the present.

Multitasking

Another reason why computer programs run slowly is because there are too many other things going on at the same time. When I’m trying to figure out why a Windows is so slow the first thing I do is look at the task manager or system tray to see what is running at the same time. Context switching between multiple programs adds additional overhead of saving and restoring state information, and if too many things are happening at the same time then nothing gets done.

Like how a computer program will run faster if it’s the only program running, you’ll be able to complete tasks faster if you are focused on only one at a time. Maintaining a list of what you want to accomplish means that when you’re done one task you’ll easily be able to check your list to find something else to work on. Having a list of everything you want to do also makes it easier to prioritize and focus on what’s most important to you.

Profiling and Measurement

It’s easy to read advice on what to improve, but unless you look at your own life and where your time goes you don’t have a clear idea of how your time is wasted. There are several ways to track how your time is used. David Seah’s Printable CEO has print templates that let you easily measure where you are spending your time. I like to use GTDWannabe’s version.

There are a few tools out there that will automatically monitor your computer usage and generate reports about how you spend your time on the computer: TimeSnapper and RescueTime. I’ve been a RescueTime beta tester for a month now and I have to say that I really like the service.

Rescue Time time management screenshot

You can see from my chart that I wasted about 2.5 hrs on personal tasks at work that week. There may be a correlation between working too much overtime and goofing off. Because I know where I’ve spent my time, I know what I can stop doing to free up more time.

Heavy Load

“If you want something done, give it to a busy person.” Nothing makes you more aware of how you are spending your time like when you have no free time at all. Testing your application in a worst case scenario where it has to deal with large sets of data will help you find performance problems. It works the same way with other aspects of your life.

Coming back from a vacation where you haven’t checked mail, email or RSS feeds for two weeks will help you identify the chaff. It’s a great opportunity to unsubscribe from newsletters and set up filters for the major time offenders. You can turn off email notifications when you get messages from Facebook. That relative who always emails you jokes can get their own special folder that you’ll never check. When you have to deal with handling a lot of data it forces you to get rid of the unimportant information.

Do Less

As you measure how your software application performs, you’ll find features that seemed like a good idea but drastically consume resources.  Quite often it is easier to cut those features out rather than spend time improving them. As you measure where you spend time in your life, you’ll find that at their are time consuming activities that seemed like a good idea at the time, but don’t contribute to any of your goals. Don’t spend time on activities with no benefit.

Idle Time

Efficiency for efficiencies’ sake is one of the worst trends of the 20th century. But efficiency in the tasks you have to do frees up time for the tasks you want to do. Every thing you spend time on has an opportunity cost for other things you could be doing with that time. Having idle time in your life frees up room for the unplanned and unexpected.

Programming Best Practices: Profiling

Posted in Firefox and Greasemonkey, Programming and Software Development, Technology by engtech on November 14, 2007

Programming Tips

My first task coming back from my work stress blogging hiatus is to finally fix problems with Akismet Auntie Spam that Lorelle reported over a month ago — if your Akismet spambox has over 10,000 spam comments then Auntie Spam is going to crash hard. Viewing that many comments at once will make Firefox use eight times more memory than normal web browsing, even without using Auntie Spam [1].
This means it’s time to do some code profiling [2]. In programming, profiling means to measure your code and find out which parts are using the most time and the most memory. Profiling gives you performance analysis measurements so that you can optimize your program for speed and/or memory.

“Don’t prematurely optimize” is a programming Best Practice, and it can be summed up in the words of my grandfather: “measure twice and cut once”. You can guess at what parts need fixing, but it is much more effective to measure how your program performs so that you can focus on the worst parts. They have the most room for improvement. Without profiling you could easily spend several hours optimizing a loop that executes in negligible time and ignore the three lines that copy huge chunks of memory for No Apparent Reason. Get it working, and then use your profiler to get it working fast.

Profiling is a Skill

I’ve been creating Greasemonkey scripts using javascript for a year now, and this is my first time firing up any kind of javascript profiler. It really struck me that I waited too long to do this. Don’t prematurely optimize, but also don’t waste any time learning how to run a profiler on your code and interpret the results. If you’ve never gone through the process of optimizing code in a language you regularly use, then you’ve been relying on all kinds of bad habits [3]. Learn how to integrate a profiler with your program as soon as possible so that performance analysis doesn’t become one of those “I’ll get around to it” tasks that never happens.

Another good rule is to always test with large data sets. Ideally you want a fast case for rapid prototyping of new features, and a worst case for stressful testing of that new feature. To often we use small sets of data for development and testing. We never realize how badly our code performs in real world conditions. Speed and responsiveness play a greater factor in whether or not someone becomes a regular user of your program than you might realize.

Footnotes

[1] One thing WordPress does wrong is it includes all of your comment spam in their WordPress export files. One friend saw his export file decrease from 83 MB to 8 MB once he deleted the comment spam.

[2] The best way to profile Javascript is with FireBug, but it doesn’t recognize Greasemonkey scripts unless you embed them in the page so FireBug can find them. Wikipedia has a list of profilers for popular languages.

[3] Some of the bad habits that were lurking in Auntie Spam:

  • I was using a custom getElementsByClassName instead of an XPATH call. XPATH can be so much faster that walking the DOM.
  • I had too many innerHTML assignments instead of leaving HTML as a string and then giving it to the web page to process as a final step
  • Inefficient regular expressions
  • Too many copies of the comments in memory

How to Profile Greasemonkey Scripts with Firebug

Posted in Firefox and Greasemonkey, Programming Tools, Technology by engtech on November 13, 2007

Programming Tips

Running performance analysis on Greasemonkey scripts can be a pain in the butt. They aren’t part of a webpage so standard tools for analyzing web sites don’t work… or do they?

The Goal

Profiling Greasemonkey scripts with Firebug

What You’ll Need

  1. Firefox
  2. Greasemonkey
  3. Firebug extension

The Trick

#1: You need to remove all of the Greasemonkey GM_* functions from the script you want to profile. This is easier than it sounds because all of the functions can be performed by plain ‘ole javascript (except for the open in new tab function and register menu command).

#2: You need to embed your Greasemonkey script inside of the running page so you can analyze it with Firebug’s profile tool. I have a function below that can embed a function inside the current web page.

#3: You’ll need to call the function either using unsafeWindow or by embedding a call to the function in the page.

#4: Litter your code with calls to Firebug’s console.profile() and console.time() functions.

Sample Code Template


(function() {
  function embedFunction(s) {
document.body.appendChild(document.createElement('script')).innerHTML =
s.toString().replace(/([\s\S]*?return;){2}([\s\S]*)}/,'$2');
 }

  function myKickassGreasemonkeyScript() {
    console.profile();
    // Put everything you need for your Greasemonkey script in here
    // Don't use any of the GM_* functions!

function kickass() {
      console.time("Block1");
      // Block of code that might take a lot of time
      console.time("Block2");
      // another block of code
      console.timeEnd("Block2");

      console.timeEnd("Block1");

    }

// more cowbell

console.profileEnd();
  }

  embedFunction(myKickassGreasemonkeyScript);
  // Method 1: embed the function call into the current page
  document.body.appendChild(document.createElement('script')).innerHTML = "myKickassGreasemonkeyScript();";
  // Method 2: directly call the function using unsafeWindow
//     window.addEventListener("load", function(e) {
//                   unsafeWindow.myKickassGreasemonkeyScript();
//                   this.removeEventListener('load',arguments.callee,false);
//                 }, false);

 })();

Firebug Tutorial

Michael Sync has a tutorial on using Firebug that describes the console.time() and console.profile() functions. The official website has a nice list of Firebug keyboard shortcuts and a brief description of all the console.* functions.

Related Posts

Follow

Get every new post delivered to your Inbox.

Join 287 other followers