26 May 2011

Seven Attributes of Job Evaluation

I recently came across Erik Pettersen's "The Four Legs of Job Satisfaction", I have a similar list of my own which I have been using for the last few years to assess possible jobs when I am trying to decide about taking a new position and thought this was a good opportunity to share them.  Here they are in non-metaphorical form and in no particular order:

  • Compensation
  • Commute
  • Culture and management quality
  • The work you are doing
  • The people you work with
  • The weekly hourly load/life Impact
  • The physical work environment

Compensation is pretty strait forward and will include salary and benefits or strait fee or hourly rate for contractors.

Commute is generally thought of in terms of time, but can include cost and hassle, do you have to take two buses and transfer twice on the train?  Also parking probably falls under this one as well, but could fall under compensation if it’s included.

The work you are doing is in my opinion one of the more crucial attributes. Are you doing work that will move you and your career forward? Is it interesting and challenging? This one can vary over time depending on the phase of the project or which project you are working on at a given time.

Culture and management quality - I am lumping these two together, as they often go hand in hand but not always. Is your manager competent and/or a decent person? This can make a big difference in your day to day stress and enjoyment of the work you are doing. Also culture can affect quality of life as well.

The people you work with are pretty important and will be more of a distribution than an absolute value. If you are lucky the people that you mostly see day to day are people whose company you enjoy.

The weekly hourly load/life Impact - this one can overlap with commute but the bottom line is how much potential overtime will you get hit with and how often? Also the life impact can be emotional in that a frustrating situation can be draining and affect your quality of life and eat into your personal time if you are thinking about work when you aren't working.

The physical work environment is important.  Are you in a cube or office?  Do you have a quiet work environment?  Do you have any natural light? I also lump work neighborhood into this one. 

Now of course you can break these things out however you want, for example where would telecommuting fit in that list?  It could easily fit under commute or work environment.  The nice thing about this type of approach is that you can use it analytically to compare two jobs.  When I am planning a job change I often list these items out and give each one a delta value to compare them, for example a previous transition looked like this:

Compensation - Slight decrease in hourly rate
Commute + Still able to bike commute, five miles shorter. Same road danger factor.
Culture and management quality 0 About the same
The work you are doing +++ Went from doing legacy support work with Struts 1 to green fields RestfulSpring 3 & Hibernate
The people you work with 0 About the same
The weekly hourly load/life Impact 0 About the same
The physical work environment + Cube slightly worse but much better work neighborhood with good restaurants

Unfortunately it’s often hard to tell about some of these before you start but you have to do the best you can and you can ask about some of these like work environment, if you think about it.  Since the pay cut didn’t put me in a negative financial situation, the above transition was a no brainer.

I have wanted to share this in my blog from the time when I was just thinking about writing it and it’s a nice light break from some of my heavier topics.

17 May 2011

The Combinatorial and Other Math of the Java Collections API

The Java Collections API both involves some explicit Math and presents some nice artifacts which parallel other Math concepts.

Combinatorics is probably one of the more fundamental Maths for Computer Science and is also prevalent throughout other Maths. In one example of Combinatorial counting problems, there is a collection of items, say a deck of cards, from which we want to determine the number of selections, say hands. The formula to compute the possible Combinations is written as:

This can also be stated as “n choose k.” My mnemonic for remembering this, and it is worth remembering, is to look at the left side above note that the n is on top and the k is on the bottom, which visually maps the n! to the numerator and k! to the denominator, and then rotate the left part by 90 degrees counter clockwise so that it forms (n - k)! and put it in the denominator. Also every number calculated by this equation is a Binomial Coefficient found in Pascal’s Triangle, which in my opinion is one of the most amazing mathematical structures.

Our example for possible five card hands from a deck of cards computes as follows:

Combinations give you the number of elements with no repetitions and where order does not matter. The possible cases and Mathematical structures for the criteria of order and repetition are as follows:

  Order Does not Matter Order Matters
No Repetitions Combination/Set Permutation
Repetitions Multiset/Bag Enumeration/List/String

The Combinatorial equations are as follows:

  Order Does not Matter Order Matters
No Repetitions
Repetitions

 

The interesting thing about lists is that if you make lists of varying lengths of the following characters [0,1,2,3,4,5,6,7,8,9] , you have the natural numbers in base ten, so a list of length 3 gives you 103 or 1000 possibilities, which are: 000-999. Also lists have the highest cardinality of the four:

|List| > |Multiset| > |Permutation| > |Combinitation|

Where |object| means the cardinality or size of the collection, which is the number of elements in the set, list, etc.

The corresponding Java classes/interfaces are:

  Order Does not Matter Order Matters
No Repetitions java.util.Set java.util.SortedSet
Repetitions org.apache.commons.collections.Bag java.util.List/java.lang.String

Sadly the Java Collections API is incomplete in this regard. Also this example is somewhat abstract but it does demonstrate how to view these objects from a Combinatorial perspective, which in turn can help with choosing the appropriate data structure, also it gives insight into possible ways that they may be populated and perhaps even tested.

This is an introductory example to Combinatorics, for more perspective there is an expansion on this known as the Twelvefold way.

The next example is more concrete, in fact it comes directly from the java docs for Comparator and Comparable:

For the mathematically inclined, the relation that defines the imposed ordering that a given comparator c imposes on a given set of objects S is:
{(x, y) such that c.compare(x, y) <= 0}.
 
The quotient for this total order is:
{(x, y) such that c.compare(x, y) == 0}.
 
It follows immediately from the contract for compare that the quotient is an equivalence relation on S, and that the imposed ordering is a total order on S. When we say that the ordering imposed by c on S is consistent with equals, we mean that the quotient for the ordering is the equivalence relation defined by the objects' equals(Object) method(s):
{(x, y) such that x.equals(y)}. 

I have to admit when I first read that a few years back, I was like, WTF does that mean! Obviously whoever wrote that was thinking deeply and mathematically about the underlying principals here which is clearly needed if you are designing fundamental low level framework aspects of base classes and collections.

Comparators and Comparable are about ordering, so what is being defined above is a Total Order which means that all elements can be compared to every other element, compare this to a Partial Order where elements may or may not be comparable to each other, both of these fall under the more general Order Theory. The Quotient aka Equivalence Class concept is a bit more complicated and beyond what I am trying to talk about here.

I believe the above Comparator Javadoc quote was written by Joshua Bloch, the following is from his book Effective Java:

The equals method implements an equivalence relation. It is:
  • Reflexive: For any non-null reference value x, x.equals(x) must return true.
  • Symmetric: For any non-null reference values x and y, x.equals(y) must return true if and only if y.equals(x) returns true.
  • Transitive: For any non-null reference values x, y, z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) must return true.
  • Consistent: For any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) must return false.

The first three are the Mathematical definition of an Equivalence Relation (which relates back to the Equivalence Class) can be found in “Abstract Mathematics” or “Transition to Advanced Mathematics” type books which describe Functions and Relations. CS Majors usually are exposed to these concepts as part of the intro into discrete math.

What I find interesting about these concepts is how the comparator/comparable and equals relation and their interrelationship for the individual object map back to control the collections behavior of ordering and repetitions.

04 May 2011

Q:Are we not engineers?


A:We are developers.



Worst Post Ever


When I originally wrote this post I was trying to poke fun at a couple of posts that I thought were pretty lame, I have since come to the conclusion that my resulting post came out pretty bad, terrible in fact.


I also feel that within the original post was a kernel of a good idea that was bogged down by the rest of it, so I have rewritten this post using just the good parts.


Another unfortunate consequence of this post is that a Russian guy translated it and used it on his blog and linked back to it.  Due to the nature of the Page Rank Algorithm it made this post one the top results for Googling my name, as a result people interested in me were being directed to my worst post. This effectively became my Google problem.


The rewritten version is here.


If you are interested in the original post you can find the Russian version here, it’s pretty awesomely bad if you translate it, parts of it are quite funny actually.

02 May 2011

Monoid for the Masses

If you don’t know what a Monoid is, it is an Algebraic Structure, and before we go any further, let me just say: Don’t Panic! If you have a negative or skeptical reaction because the idea of Algebra makes you think of something that you learned in middle school and don’t really use, then you’ll be relieved to hear that you mostly don’t even need any prior knowledge of Algebra to understand the concept that I want to talk about here. In fact if you are programmer you probably already know it, you just don’t know that you know it.

So what is this mysterious Monoid that you already know? It is String concatenation. Take the following Java code for example:

String string1 = "aaaa";

String string2 = "bbbb";

String string3 = string1 + string2;

See I told you that you already knew it.

If your CS Math education was similar to mine, which I hope for your sake it wasn’t, you may have learned about Group Theory and it seemed completely abstract and unrelated to the programming related classes. Well it turns out it is relevant. Also whether you learned this or not I am hoping that you can follow what I am presenting here without any prior knowledge.

A Monoid can be thought of as a simpler version of a Group, and we will define it more formally, later. Our Monoid, strings under the operation of concatenation uses the plus sign [+] operator and a String will be bounded by quote marks ["], pretty standard stuff, and it isn’t even hard.

To talk about this properly we will restrict the character set to strings that only contain "a" or a "b", so that any string will only contain these two characters, in fact think of all strings that are all the possible combinations of only these two characters, so the Set is all strings that can have an "a" or "b" in any position, some examples are:

"a","b","ab","abb", "aaa", "bbb", "baba",...

You get the idea. This forms a Set of all possible "ab" string combinations. This Set is Closed under + (concatenation), which means if you concatenate any two strings in the set you get a string that is still in the set. So every new string is still made up of only a’s and b’s, for example:

"aa" + "abab" = "aaabab"

Concatenation is Associative, for example:

("aa" + "bb") + "aba" = "aa" + ("bb" + "aba") ="aabbaba"

Concatenation has an identity element, an Identity means that when you combine it with any element of the set you get the same element back, it’s like zero in addition, the Identity element is the empty string "", think of it like the string version of zero, after all it has zero length, for example:

"ab" + "" = "ab"

One thing of note is that the Set of all "ab" strings is actually all strings with either "a" or "b" plus (Union with) the empty string meaning that it also includes the empty string "", this is an important distinction. I did not mention it earlier to simplify the initial explanation.

To recap, Strings under concatenation form a Monoid and a Monoid is defined as:

  • A Set and a Binary Operation where the Operation is Closed on the Set.
  • The Operation is Associative.
  • The Set contains an identity element under the Operation.

So why isn’t this a Group? A Group requires an Inverse, in a sense a Group is a Monoid with an Inverse and there is none here, for example can you think of an X for:

"ab" + X = ""

Additionally you should note that concatenation is not Commutative, for example:

("aa" + "bb" ≠ "bb" + "aa") ⇔ ("aabb" ≠ "bbaa")

However, the concatenation of the identity is Commutative, it also referred to as a double sided identity:

"" + "ab" = "ab" + "" = "ab"

Why only [a,b]? Well to keep it simple, this is referred to as the alphabet and you can define your alphabet as anything you want such as [0,1], [a-zA-Z], all ASCII characters, etc.

The really cool thing about this Monoid is that it’s the basis for Formal Language Theory, and actually this is really a first lesson in Formal Language Theory and it is also good introduction to a basic concept from Abstract Algebra. Not to mention this could probably be taught in middle or even grade school.