Posts Tagged ‘Programming’

Keeping Roles in Mind When Writing User Stories

Saturday, May 2nd, 2009

I have been writing the user stories for both the game as well as the web site for the game.

As I do so, I have been paying more attention to the roles of my user stories. That is, usually the intended role is just “a user,” which is very generic and not very interesting.

However, if you have more specific roles, such as “a first-time visitor who has never heard of the game before,” you can have much clearer intentions in your stories.

I have been asking myself, “who is the intended user, and why would he/she want to perform this action?” I find it helps me to better determine what the action should be, and the purpose for that action.

Something else that might help is to come up with the role and what that role wants to do, and then determine the best way for the role to accomplish that goal.

For instance, if a visitor comes to the game’s web site who is interested in keeping up-to-date about it, what is the best way to accomplish that goal? For now, I determined it would be to subscribe to a mailing list, where I can periodically update them. Alternatively, I might just direct them to the forums, but I like the mailing list better because it’s on my initiative to update them, instead of theirs to remember to come back.

Here are some examples:

As a visitor who has never heard of the game before
I want to view the front page
So that I can learn what the game is about.
 
As a tester
I want to view the front page
So that I can login.
 
As an interested visitor
I want to subscribe to the mailing list
So that I can keep up-to-date about the game.
 
As an interested visitor
I want to visit the forums
So that I can check on the progress of the game.
 
As a tester
I want to visit the forums
So that I can give feedback about the game.

As for the game itself, roles could include “a pilot,” “a general,” “a non-registered user,” or “a registered user,” just as a few examples.

For instance, if I am a pilot sitting in an airplane, and I want to take off, how can I accomplish that goal? Most logically, I can accomplish it by starting the engine, among other steps.

So, it is important to keep your intended users in mind when writing user stories, so that the application actually allows them to accomplish their goals.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Vim: Seven habits of effective text editing

Sunday, April 19th, 2009

Vim: Seven habits of effective text editing.

It can be tough to get started in Vim. This article provides some useful tips for working in Vim more effectively, and guidelines for learning more commands.

All I can say is, make good use of :help. It is very good at figuring out what you’re looking for.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Some of the Reasons I Love Programming in Ruby

Saturday, April 18th, 2009

Today, I wrote a really long post on the SitePoint forums. I think that it can benefit other people as well, so wanted to post it here.

Someone asked whether it is worthwhile learning Ruby and Rails, or if they should learn a framework in PHP. This is my response, modified a bit from my post in the thread.

I would go with Ruby and RoR, mostly because it has so many useful, and in my opinion necessary, tools that PHP really doesn’t have, such as RSpec, Cucumber, and Webrat, all for BDD (behavior-driven development — see Introducing BDD). After using these tools, I just couldn’t imagine working without them, at least without introducing more bugs.

Rails is rather easy to get started in, too, and simple to deploy. To me, Ruby is much mor eintuitive than PHP.

There are several things in Ruby that just would take more work in PHP. In PHP, it feels like OO has just been hacked into the language. They try to fix that a bit in PHP5 but it doesn’t negate the mess created in the previous versions.

In contrast, in Ruby, absolutely everything is an object. It makes it much more intuitive to work with the language, because objects are an integral part of the language.

Also, I love blocks/procs/closures.

Example:

(1..10).select { |num| num % 2 == 0 }
#=> [2, 4, 6, 8, 10]

Or:

(1..10).to_a.delete_if do |num|
  num > 3
end
#=> [1, 2, 3]

Sorting is very flexible:

class Widget
  attr_accessor :name, :price
  def initialize name, price
    self.name, self.price = name, price
  end
end
 
def print_widgets widgets
  widgets.each do |widget|
    puts "#{widget.name}: #{widget.price}"
  end
end
 
# Creating some widgets
widgets = []
widgets << Widget.new('Foo', 4.99)
widgets << Widget.new('Bar', 2.98)
widgets << Widget.new('Baz', 9.99)
 
print_widgets widgets
# Foo: 4.99
# Bar: 2.98
# Baz: 9.99
 
# Sort by price
widgets.sort! do |w1, w2|
  w1.price <=> w2.price
end
 
print_widgets widgets
# Bar: 2.98
# Foo: 4.99
# Baz: 9.99

Another cool thing is that you can extend classes. I don’t know the proper word for it. I don’t mean inheritance, but you can actually add methods to an existing class.

class String
  def foo
    puts 'I am foo. I have infultrated String.'
  end
end
 
"hello".foo
# Outputs: "I am foo. I have infultrated String."

There are also mixins, the ability for classes to include and extend modules.

module Foo
  def do_something
    puts "I am doing something."
  end
end
 
module Calculator
  def add a, b
    a + b
  end
end
 
class Bar
  include Foo
  include Calculator
end
 
bar=Bar.new
bar.do_something
# Outputs: "I am doing something."
bar.calc 1, 2
#=> 3

And there is so much more that I love about Ruby. The syntax is just so much better. After working in it for a while now, I couldn’t imagine going back to PHP.

Of course, RSpec does a lot to make Ruby favorable for me as well. I really love using it, and it really improves the quality of my code.

Of course, that’s only Ruby, and I haven’t discussed anything from Rails, which is really great in its own right. There are frameworks inspired by Rails in PHP, but Rails does everything much more elegantly, because PHP just isn’t capable of some things that Ruby is. In PHP, there is nothing really resembling blocks/procs in Ruby. They can really simplify code.

I also love that you can overload operators in Ruby, because most operators are just methods of an object. Ruby doesn’t have the limitations that PHP does in the characters you can include in a method name. Many methods include the characters ? or !. For instance, the String class has a method called include? to check if the given string is contained within it.

foo.include? 'o'
#=> true

Again, none of this even touches Rails, which is an amazing framework itself. It is better than any framework I have used in PHP.

If nothing else, I think you should learn it just for the experience of another language. I don’t think there’s ever harm in learning a new language and perspective on programming.

For more information on Ruby and Rails, see the following resources:

If you enjoyed this post, make sure you subscribe to my RSS feed!

Learning Vim and Loving It

Thursday, April 16th, 2009

I recently (read: two days ago) decided to switch to vim.

I was using another editor called E-TextEditor, because it was the best editor I could find for Ruby. It has auto-indent, but the coolest thing was the snippets it has, such as creating the skeleton for a method just by typing def and pressing tab. There were other neat features as well, of course.

However, one thing that really annoyed me is that it doesn’t have a way to reindent code. Autoindent is great, and for the most part I didn’t have to think about the indentation, but when I refactor, which is quite often (as it should be), I copy and paste blocks of code into other classes, methods, etc. These new locations often have a different indent level than the former location, so I wanted a way to fix the indentation. This editor has no such feature, so I had to select the entire block of pasted code, and hit tab or shift-tab the appropriate number of times.

That was especially hard when the block spanned many lines, say 20 or more. What you have to realize is that it takes longer for me to select large blocks of text (unless it is select all, of course), because I have to use the arrows, instead of the mouse. Combine that with the fact that my down arrow has been acting up, so it doesn’t always work, and you can see it was getting quite annoying.

I thought of using emacs. It is the only editor that I ever got comfortable with on Linux. Anytime I got a VPS or server, my first action was to install emacs.

I had read that emacs had a ruby mode, so I thought about trying to use this. However, using emacs on Cygwin, I ran into many problems.

The mode just wouldn’t work. I don’t know why, and don’t know much about lisp so couldn’t debug it. Further, many keybindings that are supposed to work with emacs, weren’t working. I couldn’t set a mark with C-@ or C-SPC, for instance. The key combination for indenting a region, which I don’t remember at the moment (I think C-M-\?), wouldn’t work. Of course I could use the long form, but that was just annoying.

Exasperated, I thought of what else I could use. No Windows editor seemed to include everything I wanted, so I was still looking for something I could use on Cygwin.

The only other decent editor I know of is vim. I wasn’t sure if I wanted to try it, though, as it always seemed so difficult and unintuitive. Of course, my experience was limited to occasionally accidently being thrown into vim when I forgot to set the EDITOR environment variable, and quickly executing the only command I knew, :q. I soon learned to use :q! instead, because usually when this happened, I started trying to navigate or type without realizing where I was, and that would invariably execute all sorts of unwanted commands on the buffer.

So, needless to say, my relationship with vim had a rocky start. However, everyone I knew of who used vim, swore by it. I thought this rather odd.

So I decided to read up on it, because I knew it supported ruby. There were so many commands! I read about how to use it for several hours, in the middle of the night, which probably limited my capacity for absorbing the information.

But I got the basic navigation commands, and some additional useful things as well. I opened up vim to try some of them out, and was rather impressed at how simple it was.

Then I had to recompile it with ruby support, which took me a while to figure out because it looked so complicated. Fortunately I got that without too much trouble.

Then I found the plugins. I found a plugin for rails, and several related plugins to make it much easier to use vim for a Rails project. That made it significantly better, especially since this Rails plugin has limited support for RSpec.

I’ve really enjoyed it so far. I’m still getting used to all of the commands, because I’ve done limited coding in it so far. I’ve spent the last two days configuring my .vimrc.

However, last night I actually did some coding in it, and found it to be quite nice. Again it will take some getting used to, but there was something really nice about typing cw followed by the new text to insert, to replace a word, for instance. Or, finding the next method just by typing /def . Before, in the other editor, I would just keep arrowing down until I heard def, because the find dialog really didn’t work that great for me with JAWS. I prefer staying in the main window whenever possible, so it’s nice just to initiate finding text just by typing /.

Oh, and the thing that really clinched it was when I wanted to see the arguments to redirect_to, so I pressed C-], and ended up on the method definition, with the rdoc comments right above. After finding what I was looking for, I pressed C-t to get back to where I was.

Then, when I wanted to jump from the spec to the controller it was spec’ing, I typed :AS, and there I was in the controller in a separate window. I changed the spec, typed :w to save, and my autospec script running in a separate window rran to execute the controller specs. Someone once described it as zen, and I agree that that is the only suitable adjective. It’s just so much lighter and more efficient than the large and bulky editors I have used in the past, yet there really is no compromise on features; I would even say there are more features, such as that reindent.

Oh, yes, the reindent. gg to go to the top of the file, and =G to reindent from the cursor to the end of the file. So incredibly simple.

I’ll update with more information as I learn more, and will probably make a post soon about the plugins that I find useful.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Use XPath with Namespaces

Wednesday, February 11th, 2009

As I mentioned yesterday, I exported my Excel file of practice data to XML so that I could navigate it with PHP.

I wanted to be able to navigate it using XPath. I couldn’t figure out how to use it at first, though, because of the namespaces defined in the document.

However, I found this useful method, SimpleXMLElement->registerXPathNamespace(). Apparently you have to manually register the namespaces, even if they are already defined in the document, which I think is kind of stupid.

Thanks to a comment on that manual page, though, I figured out how to automatically register the namespaces with the prefixes already defined in the document. I extended it a bit to support the default namespace, as well, by registering it with the prefix “default” whenever I found an empty prefix.

Unfortunately, you have to register these namespaces for every element you want to use SimpleXMLElement->xpath() on, so I created a function to encapsolate the code, as follows:

function registerNamespaces(SimpleXMLElement $xml)
{
  $namespaces = $xml->getDocNamespaces();
 
  foreach ($namespaces as $prefix => $ns)
  {
    if (empty($prefix))
    {
      $xml->registerXPathNamespace('default', $ns);
    }
    else
    {
      $xml->registerXPathNamespace($prefix, $ns);
    }
  }
}

If you enjoyed this post, make sure you subscribe to my RSS feed!

Convert Excel Time to Timestamp

Tuesday, February 10th, 2009

I’ve been trying to automate the template of my practice journal posts. I store all of the data (times, lengths, and statistics), in excel for easier calculation, but couldn’t figure out a way to access that data with PHP.

I found that you can export to XML (save as XML Spreadsheet 2003), so I could read the XML data with SimpleXML.

The problem is with the times. When there is just a time with no date, it stores it starting at December 31, 1899. PHP does not support dates that long ago, so I had to create a function to modernize the date a bit.

I just added 70 to the year, and 1 to the day, so as to start from January 1, 1970. Really, it would read December 32, 1969, but thankfully mktime() corrects such dates.

I actually had to use gmmktime(), because otherwise PHP would take the timezone into account, which I don’t want.

The function is below.

function excelTimeToTimestamp($time)
{
list($datePart, $timePart) = split('T', $time);
list($year, $month, $day) = split('-', $datePart);
list($hour, $minute, $second) = split(':', $timePart);
 
$year += 70;
$day += 1;
$second = round($second);
 
return gmmktime($hour, $minute, $second, $month, $day, $year);
 
}

If you enjoyed this post, make sure you subscribe to my RSS feed!

Practice Schedule Creation Script

Friday, February 6th, 2009

I just made a script that takes a list of things I want to work on in my practice sessions, each having a certain weight, and creates a practice schedule out of them.

The algorithm is something like the following. Note that a focus is something to be worked on in a practice session, such as a piece, or technique.

  1. Create two temporary lists of focuses
  2. While the main focus list is not empty:
    1. Take the first focus of the main list:
      1. If one of the temporary lists has fewer items than the other, add the focus to that list.
      2. Otherwise, randomly choose one of the temporary lists, and add the focus to that list.
    2. Remove the focus from the main list.
  3. If either temporary list has more than one item, repeat from step #1 for that list.
  4. Merge the two lists and assign it to the main focus list.

In this way, focuses usually are dispersed throughout the schedule instead of being grouped together.

You can see the result of this script here. Really, it is another script that reads the resultant schedule text file, which I upload to the server, and makes it into a list for the blog.

The code for the practice schedule creator is below.

<?php
  $focuses = array(
array(
'name' => 'Saint-Saëns: Piano Concerto No. 2 in G minor, Op. 22: 1. Andante sostenuto',
'weight' => 10
),
array(
'name' => 'Scales',
'weight' => 9
),
array(
'name' => 'Arpeggios',
'weight' => 9
),
array(
'name' => 'Rachmaninoff: Polichinelle in F-sharp minor, Op. 3, No. 4',
'weight' => 8
),
array(
'name' => 'Scarlatti: Keyboard Sonata in E major, K.380/L.23/P.483: Andante comodo',
'weight' => 4
),
array(
'name' => 'Piano Ensemble',
'weight' => 3
),
array(
'name' => 'Bach: English Suite No. 2 in A minor, BWV 807: I. Prélude',
'weight' => 2
),
array(
'name' => 'Brahms: Rhapsody in B minor, Op. 79, No. 1',
'weight' => 5
),
);
 
$sessions = array();
foreach ($focuses as $focus)
{
for ($i = 0; $i < $focus['weight']; ++$i)
{
array_push($sessions, $focus['name']);
}
}
 
$sessions = createSchedule($sessions);
 
$fp = fopen('C:' . DIRECTORY_SEPARATOR . 'Users' . DIRECTORY_SEPARATOR . 'Brandon' . DIRECTORY_SEPARATOR . 'Documents' . DIRECTORY_SEPARATOR . 'piano' . DIRECTORY_SEPARATOR . 'schedule.txt', 'ab');
 
foreach ($sessions as $session)
{
fwrite($fp, $session . "\n");
}
 
fclose($fp);
 
function createSchedule(array $sessions)
{
 
$list1 = array();
$list2 = array();
 
while (count($sessions))
{
 
$value = array_shift($sessions);
 
if (count($list1) < count($list2))
{
array_push($list1, $value);
}
elseif (count($list2) < count($list1))
{
array_push($list2, $value);
}
else
{
$num = mt_rand();
if ($num % 2 == 1)
{
array_push($list1, $value);
}
else
{
array_push($list2, $value);
}
}
 
}
 
if (count($list1) > 1)
{
$list1 = createSchedule($list1);
}
 
if (count($list2) > 1)
{
$list2 = createSchedule($list2);
}
 
return array_merge($list1, $list2);
}
 
?>

If you enjoyed this post, make sure you subscribe to my RSS feed!

Christmas Break At Last

Thursday, December 18th, 2008

Yeah, I guess I’ve not posted for a while. I’ll try to update this blog about what’s been going on in the next few days.

I just got home for Christmas break yesterday, and I couldn’t be happier. There was a while where I thought the end of the semester would never come.

But now, I’ve gotten through all the finals, and I’m home for break!

I studied really hard for the chemistry final, because I knew I wouldn’t do well on the physics final, and wanted to offset it a bit. Unfortunately, I still got a C+ on the chemistry final, but I still got a B in the class, thankfully, and just barely!

So now I’m relieved, and just relaxing, practicing, programming, and waiting for next semester, when I’ll be back in the music school.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Budgix – Budgeting Web Site

Wednesday, September 10th, 2008

I’m very meticulous about budgeting. I’ve been using a program called You Need a Budget, which does work pretty well.

However, I thought that I could probably make something better that used similar rules, but added a lot of additional things, too. I had also been checking out GnuCash, and thought that a mix of many of the features from both of these programs would be perfect.

I excel at making web sites, so thought I’d make a budgeting web site, with the features I’ve mentioned, as well as some articles about budgeting. If it was on a web site, too, people could add transactions and execute other actions via SMS, which seems rather convenient.

So, that is what I am working on now. Well, I’m trying to work on it. I made some progress before school started, but now my free time has significantly diminished. However, I’m going to try to work on it at least on weekends, and any free time I can manage throughout the week.

The project is called Budgix. It’s nice and short, and a bit catchy, too.

I’m really excited about the project, so I do want to get working on it. Hopefully I can at least get some basic features finished soon. I’ll try to post updates on my blog every once in a while about it.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Free Upgrade to Visual Studio 2008 Professional

Tuesday, August 12th, 2008

As I mentioned recently, I was looking at including a database with a Windows application. I had downloaded SQL Server Compact Edition 3.5. I came to find out that it did not work with Visual Studio 2005. It was looking for the 3.0 version of the libraries. I realized that I would either have to downgrade to 3.0, or upgrade Visual Studio to 2008.

I decided to download the trial version to test it out. I was going to download Visual Studio 2008 Standard, but realized that 2008 has unit testing, so decided to download that. I figured I might be able to gather enough money before the 90-day trial was over.

I then found Dream Spark. Basically, if you are a student, you can get many programs from Microsoft for free, without a catch. This includes Visual Studio 2008 Professional, SQL Server Developer, and a few other things I wasn’t interested in.

The thing is that Duquesne isn’t included in their list. I was about to give up, when I found a link to another company with whom you can verify your student status. They also couldn’t find me, even though they listed Duquesne, so I had to send verification. They wanted a student ID, or a schedule. I didn’t have an ID because Duquesne takes it at the end of every year. However, I could login and take a screenshot of my schedule, which I did. Luckily, a few days later, they emailed me saying I was verified.

I then got access to Dream Spark. I downloaded Visual Studio 2008 Professional, but it would not accept the key. I figured it was because I had the trial installed, so I tried the reinstall option. Still, it would not work.

I contacted support. They said to uninstall it and then install the version from Dream Spark. I tried it. It worked! I think I will install SQL Server Developer, but I think I will wait until I can use my new laptop, because hopefully I will only be using this one for another couple of weeks at most.

If you enjoyed this post, make sure you subscribe to my RSS feed!