Raw Syntax

The stuff programs are made of

Solving the General Case

This XKCD comic struck a chord with me. I feel that it presents a false dichotomy. Either miss deadlines to design it right or go wild west cowboy coding and meet deadlines. I think there’s a middle ground between those options. Design it correctly, but deliver less functionality, and add the rest of the functionality at a later date. While that’s a simple solution, this comic touches on other related issues.

Analysis Paralysis

This affliction is pretty common largely as a result of all the choices we have nowadays when designing software. Which language? Which Framework? How about a Micro-Framework? Which Text Editor? Nevermind questions about how to actually solve the problem at hand. If a simple flexible design is not readily coming to mind, I find it helpful to write some code that implements the very basics of the functionality needed, and then re-evaluate to figure out a design. Its a more iterative approach. Write a little code. Refactor. Hopefully a good design will emerge. If not try something else.

Deadline is not a Bad Word

Growing up my dad would always tell me: “There is no time”, but I didn’t understand what he meant until I got a job at 15 years old. Deadlines are part of life, and furthermore they are not bad. A deadline is just a constraint. Without deadlines we developers could work on software design forever and never actually get to implementation! Deadlines can actually help cure analysis paralysis by reducing the number of options available to us. Rewriting the whole project in python suddenly isn’t an option. Deadlines force value judgements to be made.

Why the General Case Takes so Long

As you begin implementing your perfect design you may find holes. This is one of the reasons for the shift from waterfall development to agile (or even just shorter iterative processes). Once your design is implemented to 90% you are constrained with all these implementation details.

To paraphrase Albert Einstein:

“At design-time, design and implementation are the same. At implementation-time, they are not.”

The following quote is about ruby implementation, but it can be applied to any hard general case problem.

Listen, people: Ruby is hard to implement. Oh, it may look easy at a glance, and you can probably get 70, 80, or even 90% of the way pretty quickly. But there’s some crazy stuff in that last 10% or 5% that totally blindsides you if you’re not looking for it. An early Ruby implementation has not run that last mile of Ruby implementation, and it takes almost as much work to get there as it does to run the first 90%.

When he talks about getting blindsided by that last 10%, that is potentially a missed deadline.

Solving the Problem of Solving the General Case

Ask yourself if you can live with a 90% solution. Do you really need to solve the general case? If 90% is not enough, break the general case down into many smaller pieces of functionality and many smaller deadlines. You may still run into missed deadlines and problems, but they will have smaller and more predictable impact. This doesn’t really solve the overall problem of software that takes too long to develop, but it makes the problems easier to manage.

The Problem With µ-Frameworks

There are plenty of micro-frameworks out there for web development. Some ruby based ones are Sinatra, Padrino, and Camping. There’s also Flask and web.py for python. These frameworks promise quick ramp up time and simplicity. They are small and do not provide much functionality, which is sold as “not getting in your way”.

Great! What’s not to like?

In theory this sounds super, and if you have a project that has a very limited scope it works very well. I’m not going to talk much about that case. I find the more common case is that the project grows beyond its original specification. Then suddenly you may need some of those features that were stripped out in order for the framework not to get in your way. You’re left with two options: write the feature yourself or find a plugin that implements it.

Missing Features for Managing Complexity

It can be argued that software development is all about managing complexity. Often micro-frameworks strip out useful features in the name of simplicity. For example, Sinatra does not come with an acceptable partials implementation. Its implementation allows you to render partials so long as there are no instance variables to be passed in. It is only useful for static html partials. This omission just makes programming with sinatra harder. Partial Rendering is a feature that helps reduce complexity. It serves to break up large complex views into smaller manageable pieces and also DRY up the code.

Fat App on a Thin Framework

Pretty soon you find yourself managing a project on a micro-framework with 10 external plugins for the various functionality that was not included out of the box. So now you’re using a micro-framework that is no longer so simple. Its intent to stay out of your way has actually made things harder now since your upgrade path is more complex. When you upgrade the micro-framework how many of your plugins will still work? How does the complexity of a micro-framework and 10 plugins compare to a regular framework (rails in the case of sinatra)? You also have to be sure that the combination of plugins you are using do not conflict with eachother and cause unintended side-effects.

Thin App on Fat Framework

For the project that grows beyond its initial scope, I prefer to start with a thin application on top of a fat framework. There’s just less for me to maintain and to test. In the early days of the project its a really small app, and then when it grows larger it doesn’t start working against the framework.

Micro-Frameworks are Only Good for Micro-Projects

This is not meant to disparage all the micro-frameworks out there. I believe they solve a specific problem. I use micro-frameworks when the project is a good fit for them. However, knowing a micro-framework really well is not a good excuse for not knowing a more powerful framework. Sometimes the more powerful framework is what is needed.

Learn Emacs: Useful Links

I have a running series of blog posts on learning emacs. It is a life-long endeavor. In this post I’m going to share some of the emacs resources that I have come across over the years.

EmacsWiki

This is the definitive source for emacs information (aside from the emacs manual). There’s a ton of information there, some of it is outdated, but it’s still worth your time.

Steve Yegge’s Emacs Posts – old blog and new blog

All I can say is that Steve Yegge has written a lot about emacs, and has made some awesome elisp contributions. I’m a huge fan of js2-mode. Just do a search for emacs on his old and new blogs. Effective Emacs is a good post to start with.

Avdi Grimm’s Emacs Reboot

Avdi Grimm is writing about re-doing his emacs config from scratch. It’s very informative and super easy to follow. I’ve used some ideas from his posts. His emacs config is on github.

emacsrookie

This site is relatively new, and there are only a few posts on it right now, but it is quality emacs content.

emacsrocks

Emacs screencasts. It’s another new site, but I can’t argue with free content.

EmacsForMacOSX

This site is very convenient for getting the latest os x builds of emacs. Or you can run the build-script yourself (source on github).

Github

Search emacs on github. You will find many other emacs users’ configs hosted there. Read their configs, and you will learn emacs. I keep my emacs config there.

Did I miss any useful emacs resources?

UPDATE: The users at emacs reddit let me know I forgot a big one. Check out emacs-fu

OS X Lion New Tab in Terminal Changes Directory

In 10.6 when you opened a new tab in Terminal.app it opened to your home directory. In 10.7 a new tab in Terminal.app opens to your current working directory (from any previous tabs). I found this feature to be annoying, as I already have a command that will do it when I want it, and I prefer the old 10.6 behavior.

Luckily, this is a preference in Terminal.app and can be easily changed.

Learn Emacs: Keyboard Macros

An emacs keyboard macro is just a recording of user input into emacs, which means that most anything you can do in emacs can be recorded as a macro. Read that again. Pretty powerful.

Here’s how it works. To start recording, type

C-x (

and input the commands in your macro. Then type

C-x )

to stop recording. Then type

C-x e

to apply the macro once, or

C-u 0 C-x e

to apply the macro until the bell rings or end of buffer is reached

Keep in mind that you must not ring the bell when defining a keyboard macro (by accident, or with C-g). If you do, you’ll have to start all over defining your keyboard macro

Typically I use a keyboard macro when I have a file full of data that needs slight altering, for example a non-standard CSV-like file. I’ll define a macro in terms of what needs changing on each line and then apply the macro for each line (until the end of the buffer).

Further Reading

Use LiveReload With Rails for Faster Development

LiveReload applies CSS/JS changes to Safari or Chrome without reloading the page, and automatically reloads when the html changes. Let that soak in for a minute. In this post I’ll go over how to get it installed and working on your Rails projects.

The installation process has two steps. First we install a browser extension that listens for livereload events. Then we use guard to listen for system events and send information to the browser.

First install the livereload addon for chrome or safari.

In your rails project gemfile add

gem 'rb-fsevent'
gem 'guard-livereload'

Run

bundle install

Then run

guard init livereload && guard

To setup guard for livereload and start monitoring for file system events. The last step is to activate the browser extension. You will need to click the LiveReload button:

Now when you change a file in your view, Safari / Chrome will load the new content automatically.

Notes on RubyCocoa

The notes on the livereload repo are a little confusing. Livereload can also work via RubyCocoa (instead of guard- livereload). If you’re running ruby 1.9.2 you cannot install RubyCocoa on it. RubyCocoa is not 1.9.x compatible. So I opt to use the browser extension and guard.

More info here

Learn Bash: Remove Commands From Your History

Occasionally I type a password or other sensitive information into a shell prompt. Using bash history, the command can be removed.

Host Your Side Project for Free

In this post I explain how to host your side project on Heroku for free. There are some limits of course. I’m assuming your side project is written in Ruby (RACK compatible), Node.js, or Clojure. I’m assuming you’re using git for version control (heroku uses git push for deploys). I’ll also discuss storage limitations (database, web assets, etc).

Looking for inspiration for a side project? Read my post about side projects

Sign up for Heroku

Create a Project on Heroku

First install the heroku gem.

Then create a project with the cedar stack on heroku.

Log in to Heroku and check out your app.

You should see something similar to the above. At this point you’ve created your project on heroku. Awesome, but there’s still more to do.

Deploy your project to heroku

Note the git repository listed on your project’s heroku page. Add it as a remote on your project with:

But most likely you will get a slew of errors. What about database setup and all the other steps that go with production application setup?

Set your project up for Heroku

Here’s where Heroku’s Addon system comes in. Basically, you’re going to replace your database server, frontend load balancing / reverse proxy, and daemon processes / cron jobs with Heroku Addons. This is a subject for another post, but I’ll go over what my standard stack is.

Furthermore, Heroku has good documentation about their platform and Addons here

My standard stack

Lately I’ve been using Rails 3, Mongoid for models, Haml and Sass for views, and sendgrid for sending emails.

On Heroku I’ll install the following free Addons:

  • Sendgrid for sending and receiving emails (200 a day free)
  • MongoLab for a free mongodb instance (240mb)
  • Advanced Logging
  • Custom Domain (for my own .com instead of a heroku subdomain)

Note that your app slug (application files deployed to heroku) is limited to 100mb, but I have yet to run anywhere close to that. My side projects generally run around 10mb. Heroku recommends you use an asset host for any js/css/images.

Heroku, with these addons is enough to get any side project off the ground. And there’s always the option to scale up with Heroku, or move it to your paid host once it starts to grow.

Rails Authentication Plugins

Considering which authentication plugin to use for rails? I have some recommendations.

Devise

Devise is a full featured authentication plugin. Featuring, remember me, forgot password, last seen timestamps, email confirmation, lockouts, and a slew of other features. They have pretty much anything you could ask for.

Devise supports activerecord and mongoid. I used it recently on a mongoid based rails 3 project. It worked wonderfully. I’ll post a code example of how I did it, in the next few weeks.

Check out their very helpful wiki, and their long list of extensions.

Warden

Devise is actually built on top of warden. And warden is descendant from merb-auth (merb). So, if you’re like me and quite familiar with merb-auth and it’s flexibility, you’ll feel right at home with warden. I use warden when I need just the basics, login / logout / signup. Also, if you happen to be doing some custom work when logging in, such as interfacing with an API or another system, warden is built to handle it.

See Mike Thomas’ post about getting up and running with rails and warden

check out the code in github

and the rails plugin

Roll your own

Some people prefer to write their own authentication system. Personally, I avoid reinventing the wheel when flexible solutions like warden and devise exist. However, there are some railscasts covering this topic in detail, if you’re interested to see how authentication works.

railscasts: authentication from scratch

railscasts: rails 3.1 authentication

Recommendations

Use devise if you’re running a standard rails stack with activerecord or mongoid (or couch or datamapper if you try their extensions).

If you need to do some particularly strange things with authentication, I’d recommend warden. It’ll also work with any rack compatible web app (sinatra, and others).

Don’t bother rolling your own, warden and devise do a fine (and flexible) job, and there’s plenty of other authentication plugins not mentioned here.

Learn Emacs: Use Defadvice to Modify Functions

I use defadvice to change the behavior of zap-to-char. In it’s original form, zap-to-char deletes all characters up to and including the matching character. I prefer it to behave more like zap-up-to-but-not-including-char. It’s more intuitive to me.

More on defadvice here