Archive for November, 2009

Vim splits, an introduction.

November 18th, 2009

First off, lets get some test files:

for i in foo bar cat dog ; do echo $i > $i ; done;

This creates 4 files named  foo, bar, cat and dog. Each file has a single line that contains the file’s own name.

Let’s open the first file:

vim foo

vim with single file

This would be the familiar vim with one file open view. Now to open a new split and open the bar file inside it:

:sp bar
vim with two splits

Focus is in the new split initially. To move between splits first press Ctrl-w (I remember this by Control Window, I’m not sure what the official mnemonic is) Then press a directional key to move the cursor to the split you’re interested in. Directional key could be the arrows or my preferred home row method.

We can split again and open the cat file:

:sp cat
vim with three splits

By now you may have noticed the every time you open new split all splits get an equal amount of screen real estate. The size of the current split can be adjusted by using Ctrl-w + and Ctl-w – (+ increases the split size by one line, – reduces the split size by one line) If the idea of bumping the size of the split one line at a time doesn’t sit well with you, prefix +/- with a multiplier. For example to increase our current split (which is the cat split) by 5 lines run the following:

Ctrl-w 5+
vim with adjusted split size

To quickly “maximize” the current split:

Ctrl-w _
vim with 3rd split maximized

And to return to equalized splits:

Ctrl-w =
vim with three splits

So far we have only been working with horizontal splits. Vim also supports vertical splits. To split the current split again, only vertically (and at the same time open the file named “dog”) run:

:vsp dog
vim with vertical split

Of course you can keep splitting until your head hurts. Vim even allows you to split the same file multiple times and it will automatically keep the contents in sync. This is very handy for referencing one section of a file while editing another.

Crazy vim splits

Split related commands:

Command Action
:sp filename Open filename in horizontal split
:vsp filename Open filename in vertical split
Ctrl-w h
Ctrl-w ←
Shift focus to split on left of current
Ctrl-w l
Ctrl-w →
Shift focus to split on right of current
Ctrl-w j
Ctrl-w ↓
Shift focus to split below the current
Ctrl-w k
Ctrl-w ↑
Shift focus to split above the current
Ctrl-w n+ Increase size of current split by n lines
Ctrl-w n- Decrease size of current split by n lines

Success!

November 12th, 2009

While searching through quotes to include in our company newsletter, I came across this:

“Any fact facing us is not as important as our attitude toward it,

for that determines our success or failure”

Read that again…

“Any fact facing us is not as important as our attitude toward it,

for that determines our success or failure

The mind is a very powerful thing.  For example, I don’t believe I’m the only one to experience being in a store next to a visibly sick individual and after they sneeze I start to feel like I’m getting sick.  It’s impossible to get sick that quickly but my mind starts thinking of the possibility of catching the virus this stranger had… and how I have so many plans, I can’t be sick… what am I going to do… and instantly I start to think that I’m sick.  The mind has the ability to quickly convince us something is happening that isn’t!

The thoughts in your head, positive or negative, directly affect your attitude.  Your attitude directly affects how others react to you.  Those closest to you, your family and your co-workers, are able to tell what type of thoughts are going through your mind by a simple look at your face and they react accordingly.

So what is my point?  Take a self inventory!  Are most of the thoughts that go through your mind positive or negative?  The situations that you face on a daily basis are NOT as important as how you decide to handle them.  When a difficult situation arises and you start to see it in a negative light, remind your self…

——————————————-

“Any fact facing us is not as important as our attitude toward it,

for that determines our success or failure”

-Norman Vincent Peale

——————————————-

Think positively… Aim for success!

You may be amazed at the outcome when you do!

PL/SQL Variables and Connection Pooling

November 11th, 2009

I recently had to implement a common feature across multiple applications and app servers, all of which point to the same Oracle database. For reasons unrelated, I chose to implement this feature using PL/SQL. You can all stop laughing now. I ended up with something resembling:

CREATE OR REPLACE PACKAGE BODY MY_PKG IS
   g_enabled BOOLEAN := TRUE;
 
   PROCEDURE enable_sp() IS
      BEGIN
         g_enabled := TRUE;
      END;
 
   PROCEDURE disable_sp() IS
      BEGIN
         g_enabled := FALSE;
      END;
 
   FUNCTION is_enabled_fn() IS
      l_return_value NUMBER;
 
      BEGIN
         IF g_enabled THEN
            l_return_value := 1;
         ELSE
            l_return_value := 0;
         END IF;
 
         RETURN l_return_value;
      END;
END MY_PKG;

Seems pretty innocent, doesn’t it?

So, I write unit tests in java to test everything. It all seems to be working. It goes through code review. Nothing more serious than cosmetic issues were found. It goes through the qa cycles. It all seems to be working. It gets deployed to production. At first, it seems to be working. Then I start to see some interesting behavior.

Lesson #1:

PL/SQL data types aren’t as straight forward as I would have liked. The variable g_enabled is of type BOOLEAN. This is a data type built into PL/SQL. It is not a data type in Oracle’s version of SQL. Why is this significant? Consider the two following scenario’s and their result (assuming that is_enabled=TRUE) -

BEGIN
 
  DBMS_OUTPUT.put_line(my_pkg.is_enabled_fn());
 
END;

This will print out a 1 to the standard output.

SELECT my_pkg.is_enabled_fn() FROM dual;

This query will return 0.

This was not what I expected. I would have expected the second query to return a 1. The reason it doesn’t, is that the query is not run in a PL/SQL block. This results in the BOOLEAN data type to not be defined and the if statement in is_enabled_fn() to always evaluate to false.

Lesson #2:

Package variables are connection private. I create two connections, A and B, to the database. If I call disable_sp() in connection A, is_enabled_fn() will still return a 1 in connection B. The value of the variable is not shared across the connections. If I were then to create a connection C, is_enabled_fn() will return 0. This is because connection C was created after the value was set in connection A. While it wasn’t what I initially expected, it makes some sense in that the connections don’t share memory. The really big problem comes into play when app servers pool connections and might not close them for weeks at a time.

Monte-Carlo Localization in a Nutshell

November 7th, 2009

If you’re a nerd like most of us here at Source Allies, you probably think robots are cool.  One of the most important part of robotics is teaching the robot to find its location on a geographic map – a process known as  “localization.” One such algorithm for solving this problem is known as Monte Carlo Localization. When talking about this algorithm, we typically use a a notion commonly referred to as “particles.”  These particles generally can be thought of as virtual manifestations of the robot within some computer.  They are postulations about the robot’s location, orientation, and certainty of this information on a geographic map.  With that in mind, the Monte Carlo Localization algorithm in plain English is as follows:

  1. Initialize set of particles (or beliefs about the robot’s location.) Depending on what problem you’re trying to solve, the set of particles can either be random or already localized.
  2. Gather data about the physical environment by interacting (taking in sensor information, moving around)
  3. Look at each particle in your set of particles and assign a weight to each based on how well that particle fits with the the data gathered in step 2. Basically, our certainty about whether a particle is actually representing the robot’s location and orientation determines the “weight.”
  4. Create new set of particles by resampling from particles with greater weights. This is sort of a Darwinian, survival of the fittest, particles with higher weights repopulate the set for the next round.
  5. Replace old set of particles with a new set and start again at step 2.

So that was a very basic, watered down version of the algorithm that omits many important statistical calculations, but hopefully it gets most of the main idea across.

In case there is any confusion, let’s walk through an example.  Let’s say I am a robot. For obvious reasons, I was kidnapped by some ninjas. The ninjas then released somewhere in downtown Des Moines I have no idea where I am at first.  Fortunately, I am a robot and have a perfect map of downtown Des Moines, so I initialize in my head a set of postulations about my position and orientation. These beliefs are completely random and distributed fairly evenly over all of downtown Des Moines and all have different random orientations.

First thing I do is open my eyes and look around, perhaps I’ll take a few steps in any direction and continue gathering visual information.  I see a Smokey D’s.  Then I look at all the particles in my set of particles and determine which of those particles also would see a Smokey D’s.  I decide that those particles are better than all the other particles and assign them greater weights.  Then I go through my set of particles again do some ninja-statistics that I learned while I was kidnapped to decide which particles can make it to round two and which cannot. What I’d end up with is two clusters of particles around the two points, appropriately titled “A” and “C.”

There would be a cluster of particles around A and C

There would be a cluster of particles around A and C

Then I’d go back to step 2, interact with my environment some more, find out that I’m actually indoors and that fits better with the particles around A – the Smokey D’s in the skywalks. Perhaps I’d repeat from step 2 a few more times and eventually weed out particles until I have a full set of particles that are all in a very similar spot around the Smokey D’s in the Skywalks. Then I know where I am and I’m a happy robot.

My Biggest Fan

November 6th, 2009

Who am I to argue?

She is proud of her dad

Agile Conversations

November 4th, 2009

Everyone, especially project managers, is in love with Agile Development. And why wouldn’t they be? Under the old school system, you’d end up with developers either sitting around uselessly, or drafting up prototypes that will only be thrown away. Agile allows for parallel design and development, wasting less time and money. But there’s always a tradeoff. In this case, I’m thinking of commonality of design. Let me explain by example:

I’m currently working an Agile project with three developers including myself. We’ve divied up the tasks so that one is doing the JSF database plumbing, and the other two are creating the web services that sit on the back end. We started development without having a certain idea of certain database keys, so we naturally wrapped the keys in an object that we could easily change when the decision was made. It let us move forward with development and had a very low cost for change when business came back with a decision. But we each implemented it in a difference way, using a different nomenclature and at different times. So now we have three objects that perform identical tasks, and require a translation process when our respective parts interact with each other. It’s not broken, but it’s definitely messy.

And it’s a natural fallout of the tendency to think that Agile means you start developing right away, and things like requirements and interfaces can be laid on top of the code later. How do we prevent it? Sadly, communication is the only answer. Code reviews early on could have prevented this situation before it would have been a pain in the tuckus to refactor. A project wiki exists, and even has a section that lays out common objects and interfaces. But the wiki was infrequently referenced for matters of actual code implementation; it was for the documents and the Agile storyboards.

In the end, it’s important to recognize that Agile development has taken the large chunk of communication out of the beginning phase of a project. But that communication isn’t gone, it’s been spread out over the duration of the project, and in most cases that means there going to be a lot more of it. As developers we eagerly embrace Agile projects since it means we don’t spend two months in design meetings and can’t indulge our love of code early on, but we have to realize that it comes with a cost. We actually have to talk to each other.

Sorting your Beans

November 3rd, 2009

Need to sort a list of custom objects?   Instead of coding up a custom implementation of your favorite sorting algorithm, you can simply use the tools provided in the Collections class (Collections framework) paired with the BeanComparator class (Apache project).

Simple example: you have a List of Person objects and you want to sort via the lastName.

List<Person> people = ... ;
Collections.sort(people, new BeanComparator("lastName"));

Sorting complete!  And good news, the sort method guarantees n log(n) performance through a special implementation of merge sort.

Is your custom object more complicated?  Perhaps you encapsulated all the name information (first, middle, last) in another object within Person, called Name.

Collections.sort(people, new BeanComparator("name.lastName"));

Want the list sorted in descending order?

Collections.sort(people, new BeanComparator("name.lastName"));
Collections.reverse(people);

There are some other great utilities in there including methods to find the min and max based on a given field, as above.  You could even search for a single element using their binary search implementation, but beware: your list should already be sorted!

Issue Tracking Process

November 2nd, 2009

After several weeks of performing support duties at a larger company I have begun to wonder about support practices and what could make the process better.

The process in which I currently work goes as follows:
• Field employees call the marketing team and describe the problem.
• Marketing team calls the Help Desk and re-describes the problem.
• Help Desk creates and issue ticket and assigns the ticket to a team.

It seems like there might be a quicker option.

My responsibilities include monitoring all the tickets that come to our team, solving what I can, and passing on ones for other applications. Frequently these tickets are mis-assigned or contain incomplete or inaccurate information which only creates more work for the support team. Because our support team does not contact customers, if we need more information we must go back through the support team.

I’m sure there are ways to improve this process, but I’m curious what other peoples experiences are with support processes at larger employers.

Calculating business days…

November 2nd, 2009

Today is already becoming “one of those days” ;)

A loosely written specification provided me with an ambiguity I did not account for:  the client wanted expiration dates and ordering dates to be based upon business days, whereas the specification said “days” only.  So, I found myself scrambling to figure out the best way to perform date functions against a business day concept.  I found some solid information here: http://stackoverflow.com/questions/1044921/a-good-business-calendar-library-in-java – how is everyone else solving this issue?

Add some “magic” to PHP apps: Dynamic properties

November 1st, 2009

There are no doubts that PHP made a huge impact on Web development industry. Because of it’s flexibility, PHP finds it’s usage in many areas, starting from simple Web sites and ending with complex Web applications (i.e. frameworks, shopping carts, etc…).

So, today I’d like to share with you how can a simple feature bring back/more fun to daily development. Let’s talk about “magic methods”.

“Magic methods” are class methods that start with double-underscores “__”. PHP has more than 1 of them, however today we’ll make usage only 4 of them: __set, __get, __isset, __unset.

So, what’s so special about them? You could use them to make your object more adaptive, you could have dynamic and unlimited properties.
For example we have a simple User class:

class User {
private $name;
}

At the time of it’s writing I don’t know how many properties it will have, and later when I’ll need new properties, I’ll have to add them, right?. However there is another option, to have a super class that will handle calls to unknown/non-existing properties. The idea would be to store all data in an associative array (a map if you want) and place this functionality into a super class that we’ll extend.

class AbstractModel {
  private $data = array();
 
protected function __set($name, $value) {
  $this-&gt;data[$name] = $value;
}
 
protected function __get($name) {
  if (array_key_exists($name, $this-&gt;data)) {
     return $this-&gt;data[$name];
  }
  return null;
}
 
protected function __isset($name) {
  return isset($this-&gt;data[$name]);
}
 
protected function __unset($name) {
  unset($this-&gt;data[$name]);
 }
}

__set method will be called when we’ll try to assign a value to a non-existing property.

$obj-&gt;nonExistingProperty = "Some value";

will trigger

__set("nonExistingProperty", "Some value");

__get method will be called when we’ll want to retrieve value of our property.

echo $obj-&gt;nonExistingProperty;

will trigger

__get("nonExistingProperty");

Similar to __set and __get, __isset and __unset will be triggered when we’ll use isset and unset functions on non-existing properties.

Usage of our “magic”.

class User extends AbstractModel {
}
include "User.php";
define ('NL', '<br>');
 
$u = new User();
 
$u-&gt;name = "John Rambo";
$u-&gt;age = 87;
 
echo $u-&gt;name . NL;
echo $u-&gt;age . NL;
 
if (isset($u-&gt;name)) {
  echo "Name is set" . NL;
}
 
if (isset($u-&gt;age)) {
  echo "Age is set" . NL;
}
 
if (!isset($u-&gt;email)) {
  echo "Email is NOT set" . NL;
}

Output:

John Rambo
87
Name is set
Age is set
Email is NOT set