Tuesday, May 27, 2008

Introducing BDD with RSpec

Behavior Driven Development (BDD) is an approach for software development combining the advantages of Test Driven Development and Domain Driven Design. It helps the team of the project focus on the exact requirements of the client in a verifiable manner.

In TDD, we had acceptance tests that verify the correctness and completeness of the feature once they pass. Those tests are written by the developers at the beginning of an iteration. A weak point though is that those tests were written in a programming language (java, ruby, ..) nothing that the customer can understand. BDD introduces a common vocabulary among all people, that is simple text, something that even the customer can understand and write.

RSpec is a ruby framework that helps with the implementation of BDD. It is really an interesting project.
RSpec started with what they call now the "spec framework", this framework describes pieces of the application model through by specifying characteristics and actions that can be verified through the code. check the example at the rspec site

The second part of rspec is called the "story framework".. the story framework is the same old rbehave project that was started by Dan North back in June 2007. Five months later, rbehave joined rspec and became the story framework of rspec.

The story framework enables the team to document user stories in simple text format written in a specific structure. A story is described and a set of scenarios are written to describe how the system should behave. The scenario is composed of several steps that can be programmatically implemented, so we end up in having an executable user story.

The example at the rspec homepage shows how this framework works.

the scenarios are written using a small set of keywords "Given, And, When, Then" that describe the steps of the scenario. "Given" declares the steps that describe the context, it can be used to setup that context for our test. "When" describes the action that is made. "Then" describes the expected results or expected final state the system after the action, "Then" steps implementation usually have the "actual.should ==expected" lines that are very similar to the ordinary assert instructions in most testing framworks.

One important point to consider is that the Scenarios should be written in a very specific manner. The developers won't be able to implement steps of abstract nature. for example:
when you are writing scenarios that describe that validation of a user model

DO NOT write:
Given the user has some invalid values
When the user submits
Then the user should be invalid

Instead, write:
Given the user entered his email as invalidemail
And the user left his name blank
When the user submits
Then the user should be invalid

David Chelimsky, one of rspec core team shows how to write plain text stories having the implementation of steps in a separate file which makes the stories look more elegant.

Monday, May 19, 2008

Posting Messages Using Jaiku API through Ruby console

Jaiku is considered a nice communication method among a group of friends or colleagues in a small company or so. One major strength of jaiku is that it has a lot of notification methods about updates. users can receive notifications through sms, IM or throught jaiku website.

I wanted to take a look at jaiku API. so i can use it in any application to send notifications to the rest of my colleagues. the user uses his username and API key to authenticate operations through the API

Jaiku provides two interfaces that can be used to read/write jaikus. the XML-RPC and the JSON interface. xmlrpc behaved little strangly always responding with "XML-RPC server accepts POST requests only." even when i'm sending POST requests.

i managed to use the json interface though

just send a post request to api.jaiku.com/json with the parameters(username, api key, method, message)

throught Ruby's irb
i can send a note by typing:

irb(main)> require 'net/http'

irb(main)> data = 'user=username&personal_key=999999999&method=presence.send&message=#channel This Message goes to everyone on that channel'

irb(main)> http = Net::HTTP.new('api.jaiku.com')

irb(main)> result = http.post('/json',data)

irb(main)> result.body

=> "{\"status\":\"ok\"}"

I believe this can be a valuable feature for some websites

of course you can make similar functionality through a simple html page with the following code

< form action="http://api.jaiku.com/json" method="post">
      Message to Jaiku< input name="message" type="text" value="">
      < input name="user" type="hidden" value="username">
      < input name="personal_key" type="hidden" value="fd36a832c09ed8765">
      < input name="method" type="hidden" value="presence.send">
      < input type="submit" value="send message">
< /form >

Whatever the way u choose to do this.. an important note is that you have to use the POST HTTP method. and you have to use the method=presence.send of course with your username and personal key

Wednesday, May 14, 2008

Overriding input components in active scaffold

One of the wonderful facts about Rails is that most stuff you need is already created for you, keeping you free to customize as you want.

ActiveScaffold is one of the most powerful and widely used plugins in rails projects because it simply provide you with a complete CRUD for your model with minimum coding.

The default view of the active scaffold is adequate for some applications, but not the most pretty. so is the input types used for the model attributes. Active Scaffold team helps us by providing and easy way to customize by overriding active scaffold defaults.

In my application i had a boolean attribute "admin" in my model "user". that was showed as a drop down list having the values of true & false. Pretty ugly.

I used partial form overrides to make it the more logical check box by adding
with the following content:

< dl >
  < dt >
    < label >is admin ?< /label >
  < /dt >
  < dd >
    <%=check_box :record, :admin, :name => 'record[admin]'%>
  < /dd >
< /dl >

Now when i create a new user, i can see a check box at the admin field. instead of the old ugly dropdown list.

A glimpse of fizo

Sitting there at the tram on my way to work, when i saw fizo.. a 70 years old fizo.

Well, it wasn't our own fizo himself. It was an old man who seemed to be like a future image of fizo. He was thin, had a white mustache and mixed (white, gray) mid long hair on the sides of his head, wore black framed glasses and clean tidy outfit, and moreover, his fingure nails looked something close to those of foze.

just wondering how would i look like if i was to reach that age..

Thursday, May 08, 2008

16 Ways to Keep A Razor- Sharp Focus at Work

I came across this nice post about focusing at work, i'd like to share..

Focus is something of a novelty these days. We’ve got cellphones for texting and calls, IM, Twitter, Email, RSS feeds, Facebook, Myspace… the list goes on and on. If you don’t have ADD before you start working online, it seems it’s almost inevitable thanks to these inputs. If you’re a web worker who uses the Internet for the majority of the day, you’re especially at risk for losing focus.

Focus is something that must be fought for. It’s not something that automatically switches on when you want to. You have to make sure your surroundings are perfect for working if you want to be focused. Here’s a few ways I’ve found this to work:

  1. Use offline tools. Paper products, pens, and other physical tools are a Godsend for those of us who have a hard time focusing throughout the work day. They’re so simple that we can use them quickly, without having to worry about becoming distracted.
  2. Take more breaks. More breaks = More productivity. It may sound wrong, but it’s true. Breaks allow us to re-group our thoughts and focus for the task at hand. They also keep us fresh so that we don’t end up burning out after only a few hours work.
  3. Smaller tasks to check off. When you’re planning your day, make sure that your “action steps” (aka items in the checklist) are small actions. Instead of “Paint living room”, try breaking it down into many tasks, like “buy paint, buy rollers, pick colors” etc.
  4. Keep a steady pace. Don’t try to do to much. Keeping the pace manageable allows you to keep your focus. Unfortunately, people can confuse this with “Work till you drop without breaks”. See number 2.
  5. Keep a daily “purpose” card. It’s pretty easy to get lost staring at the computer all day long. We’ll find rabbit holes to wonder down (ie. Youtube, Myspace, etc.) if we’re not careful. Having your daily purpose card gives you clarity and a reminder as to what you’re doing today.
  6. Develop the mindset that the computer is only a tool. It’s easy to try and use the computer for too much. At its core, the computer is merely a tool (albeit a freakin’ awesome one) that allows to do work more efficiently. If we’re using it as something more than that, (like as a solution for your life), you’ll ultimately fail. It’s like trying to eat a steak dinner with only a spoon.
  7. Plan your day to the T. If you’re finding sporadic periods of laziness throughout the day, it could be because you don’t take enough breaks (see #2), and you don’t have the day mapped out as efficiently as you could. Make sure your list of todos has lots of small, actionable steps that can be done quickly. This will gives a really satisfying feeling when you’re crossing things off your list like crazy.
  8. Notice your lazy routines. Everyone has recurring lazy spots throughout the day. Plan to have your breaks for those times. You’re going to be lazy then anyway, right?
  9. Plan the night before. Planning the night before is a great way to really get focused on the next day. “Sleeping” on your tasks and goals for the following day can really help your mind expect what’s going to happen the next day. Essentially, you’re preparing your mind for the following day. Advanced focus.
  10. Turn off extra inputs. These are IM and email for me, but we all have our Achilles heel. Completely turn off any distracting piece of technology that you own. Every one of these inputs tries to steal bits of your focus. And they won’t rest until they do.
  11. Set time limits for tasks. There’s no motivation like a deadline. Giving yourself real deadlines is a great way to stay motivated and focused on the task. Given the fact that we human are natural procrastinators, it’s no surprise that we’ll take as long as we’re allowed to finish something. Setting real but attainable limits is a great way to keep the project humming, so to speak.
  12. Keep a journal of what you did throughout the day. I like to use a moleskine notebook for my lists just so I can go back and review it every now and again, to see what I’ve done. Knowing how far you’ve come can keep you sharp and motivated to finish.
  13. Use programs to track where you spend your time. This is a real eye-opener. Knowing just how much time you spend every day/week/month on a certain site or with a certain program can quickly show you where your priorities lie. I recommend Rescue Time, but there are many others.
  14. Visualize the day in the morning, before it starts. A little pre-work meditation on the day’s events is a great way to start the day off focused and productive. Don’t worry about a full 30 minute session, a quick review before you start the day is fine.
  15. Start the day right. Starting the day with a good breakfast, some quiet time and/or exercise is a great way to set your day up for success. Sounds like a cliche, but it really works.
  16. Clean yourself up. It’s why my track coach in high school made us dress up for big races: you perform the way you feel. And if you feel polished, groomed and ready, you’ll be more likely to be productive. For me this is just taking a shower, brushing my teeth and putting on casual clothing. I used to work all day without taking a shower in my PJ’s, but I never got much stuff done. Let’s be honest here… if you’re dressed really casually, odds are you’ll be working really casually. Just taking the time to clean up a bit before you buckle down for the day is never a bad idea.

For more from Glen, check out his great productivity blog, LifeDev. He’s also the co-owner of the LifeRemix blog network.