# $emit-ing Events from Outside AngularJS I was working with on a little JavaScript project, and after being sufficiently annoyed and having to manage the DOM directly, decided to drop AngularJS in there. Oh. My. God. So much better. But I didn’t want to restructure everything, only where there was pain. Pre-angular, there was some code like this in the app: function draw() { // _many_ lines of nasty third-party blech updateStats(); // added by me } function updateStats() { // do some math // render some DOM } I definitely didn’t want to reimplement the whole draw function, but I did want to get that updateStats function into an angular controller, so I could use its goodness to actually do the DOM stuff. This is where I wanted to go: function draw() { // _many_ lines of nasty third-party blech$scope.$emit('draw'); // this super doesn't work } // angular setup ... app.controller('Ctrlr', function Ctrlr($scope) {
$scope.on('draw', function updateStats() { // do some math$scope.stats = ...;
});
// other stuff ...
}

The question was “what actually needs to be there on line #3?” After a bit of digging around, I found it was pretty easy to make it work. I made a top-level $emit function to encapsulate the behaviour: function$emit() {
var scope = angular.element('body').scope();
scope.$emit.apply(scope, arguments); } function draw() { // _many_ lines of nasty third-party blech$emit('draw'); // this works!
}
// angular setup ...
app.controller('Ctrlr', function Ctrlr($scope) {$scope.on('draw', function updateStats() {
// do some math
$scope.stats = ...; }); // other stuff ... } …. except that my event handler doesn’t execute w/in a digest loop, so while the scope is updated, the DOM doesn’t repaint until something else applies it. Easy enough to fix with a little tweak to $emit:

function $emit() { var args = arguments; var scope = angular.element('body').scope(); scope.$apply(function () {
scope.$emit.apply(scope, args); }); } Perfect! I oversimplified a bit, however. The draw function is not just called from extra-angular code, some of that “other stuff” in the controller does too. You can probably see the problem: the controller functions are already w/in a digest loop, so $apply can’t be invoked again. This one, unfortunately required poking my nose into the angular internals (the stuff prefixed with $$), but for this project it is a reasonable choice to help bridge the conversion window. Angular keeps track of which phase it is currently in, and that’s exactly the piece of info we need. Here is the final version: function emit(name, eventArgs) { var args = Array.prototype.slice.call(arguments, 0); var scope = angular.element('body').scope(); var phase = scope.parent.$$phase;
if (phase == '$apply' || phase == '$digest') {
scope.$emit.apply(scope, args) } else { scope.$apply(function () {
scope.$emit.apply(scope, args); }); } } Seamlessly emitting events into my angular app from non-angular code, properly bound to the digest cycle, and allowing for re-entrant invocations/dispatch has already made the migration much simpler. I’d held off because I thought it was going to be complicated, but with 10 fairly simple lines of JS getting it done, I was a fool to wait. # Advent of Code Last December, Brenna did a bunch of Advent of Code, and on a number of problems I played mentor while she worked through them. My central tenet was that the hard part of programming is the part you do before you put your fingers on the keyboard. It’s really tempting to read through the problem and jump right into the code part and start laying out functions and stuff to solve it. It doesn’t work so well though. The essence of good software design is effective abstraction. Abstracting things is not terribly difficult, but effective abstractions are rather trickier. As a programmer, you have to take human/business “stuff” and translate it into something so simple a computer processor can understand it. The bottom of that process is all taken care of for you by various compilers/assemblers/linkers/interpreters/etc., but that’s the uninteresting (and thus easily automated) part. The technique that we repeatedly used was simple, but quite effective. It’s also totally obvious, so much so that it seems almost pointless: Your first version should be English. Start by writing prose in comments, and make it really high-level prose. I’m talking “the whole program is described in 10 lines” high-level. Once that’s done, it’s easy to pick out the nouns and verbs, which indicate where you probably need to make an abstraction. A noun represents a data structure of some sort. Declare the type somewhere, and reference it in your comment. A verb represents a function of some sort, and the object(s) will be the function’s parameter(s). Declare that function somewhere, and reference it in your comment. Now you’ve got some types and functions, which are exactly the same as your program as a whole, so you approach the same way: write prose in comments for what they do. Find the nouns and verbs and repeat. At some point you’ll get to functions which are either provided by your language runtime or libraries, or can be expressed directly in your language’s syntax. Once that happens, your comment can just be written as the equivalent line of actual code, at which point you’re done. And I don’t mean “done with the first draft”, I mean actually done as in “all the code is written.” This is recursion, of course! Undoubtedly the scariest concept in programming, and one of the most useful. Part of the reason this works so well is that it forces you to name everything. Naming stuff is a) really hard, and b) really, really useful. By having good names for stuff, it becomes possible to have natural-language conversations about the “stuff” in your program, at any level of abstraction. Even the best of programmers don’t think like computers, they think like humans, and humans use natural language to converse, even when they’re talking to themselves. By having good names in your programs, you make it much easier to talk about – and therefore think about – what is actually happening when they run, especially if there is more than one human involved. As one specific example, consider this type I created when working on Day 6 of the Advent of Code: type LightGrid [][]bool This might not seem very useful: what is wrong with just passing [][]bool around everywhere? But consider the question “how many lights in the grid are turned on?” for a moment, and then look at these two effectively-equivalent function signatures and ask yourself which one is more obviously the way to answer that question: func CountTurnedOn(LightGrid) int func CountTurnedOn([][]bool) int I hope you would agree with me that the former is much more obvious about what it’s doing, and this is an unusual case where “turned on” sort of makes sense for boolean values. If you’re familiar with Go, you’ll not be surprised that in my actual solution the first one was a method on the LightGrid type, not a standalone function. By having the LightGrid type, we can talk about it with other humans (or ourselves) without having to understand that it’s a two-dimensional slice of boolean values. That’s irrelevant if you’re talking about the grid as a whole, and the names in your program should reflect that. I.e., the name provides encapsulation. In Brenna’s specific case, a rather significant part of the problem was that she’s primarily developed using CFML in her career. CFML doesn’t provide a good way to create lots of data types to help with this problem. As a result, you have lots of signatures that use array, struct, or query in their types, and who knows what their semantics are. Which means you often have to go digging around in the implementation to deduce the semantics, which can be quite time consuming and completely breaks up the thought flow. Using CFCs and having good function names can help, but the function names in particular often end up being unwieldy, because they have to express argument type info as well as the verb in question, not to mention the extreme verbosity and runtime penalty of CFCs. If you’ve done your job right, after you’re all done with your program, you final shipping version should still be pretty darn close to English. The grammatical rules mandated by your compiler/interpreter are probably rather different than English, and the punctuation rules certainly will be, but with a little effort it’s surprising how close you can get. Everyone who has to read the code later (especially Future You) will thank you. # Information Transfer I have some information in my mind. I want to transfer it to another person’s mind. This is a surprisingly subtle problem. Obviously I can walk over to them and start taking to them. Chances are good that I’ll be able to get a pretty respectable information transfer, especially if we both are competent users of the same spoken language. But what if we’re not? If it’s information about the physical world, I can probably gesture with my hands, or maybe even point at stuff if it has to do with things that are close by. If not, maybe a picture is a better choice, especially if I can use color and/or rudimentary animation (a la comic book panels). Or perhaps non-language sounds will fit the bill. If my information is anything more interesting than whether I use 1% or 2% milk on my cereal, chances are good that this will evolve into a conversation, where we are both communicating information with each other (and requesting specific information from each other). We might even move through different media as we go, adapting to the specific information being transferred at any given time.. But what happens when I only want to share part of the information in my mind? Now we have something interesting. Any given piece of information can be communicated in a huge number of ways. For example, I don’t like dill pickles. If you asked me, I’d tell you. But let’s say we go have lunch together, and I leave my pickle spear behind. The same information was transferred, though certainly with less clarity. If I have lunch at the same place and I always leave my pickle spear behind, should an attentive server who waits on me numerous times stop bringing me a pickle spear? Hmmm. If I go in one day and get served without a spear, is the right thought “the server is stalking me” or just “oh, they must have forgotten”. Perhaps the right thought is “they’re having supply chain problems, and are out of pickles”. Hmmm. Once I got asked “Do you eat ice cream?” I answered like a smart ass, because a) I’m a smart ass, and b) everyone likes ice cream. A split second later, my brain actually decoded the information being transferred in that question: “I don’t like ice cream, but I don’t know if you do, so I’m offering to get you some even though I don’t want any.” Hmmm. Somewhere in there is a line which delineates what is an appropriate use of transferred information, because we all transfer far more information out of our minds than we think it is acceptable for those around us to use. Obviously the line is contextual: you would probably expect your spouse to react to the tone of “I’m good, how are you?”, but you would be appalled at the new supermarket checker who took it anything other than literal face value, regardless of inflection, tone, or body language. To say that another way, it is shared context which determines the meaning of any communication. Yet another way, language only governs a tiny part of the information transferred through the use of the language. Let alone the information transferred through other media, like action. Or through lack of action. “Implicit consent” is the term for that one: they didn’t object, so it must be okay. Our society runs on an economic basis. Those with the money make the rules, those without the money can either accept it or get some money so they can help make rules. But in reality, money is a highly non-interesting economy. The real economy is based on information. The relative value is far less tangible, and it inherently has huge differences between different entities. Having a currency just means you can interact indirectly – the lack of a currency means everyone is directly involved with everyone else. If I know fact X about person Y, I can barter that information for information about other people. This is called “gossiping.” We all do it. If you listen well, you get a double deal, because not only do you learn about the other people, but you also learn about the person you’re trading with. What do they consider important? What information do they value? Police informants are good at this game, but they have a high-risk job with poor pay. Which is, in itself, and interesting bit of information to internalize. What it means is money is not the currency of the information economy, even by proxy. The currency of the information economy is power, whether you’re in the board room, on the playground at a preschool, or running a siege of an enemy in war. If you have power, you can buy information. If you have information, you can trade for power, but the exchange rate is horrible. What is truly amazing about all of this, is that every two-year-old grasps this concept. “If you don’t give me the ball, I’ll tell mom you stole candy.” Knowledge is power, and so it is reasonable to expect those with the most knowledge will have the most power. But it’s not that way at all. Usually the opposite. The people with the least hesitation to use their knowledge to grab power will have the most power as long as the economy is stable. If the economy is unstable, things are different; knowledge is power at the edges of the stability envelope. When things are unstable people’s inhibitions fade, and they act more aggression and less restraint. Between a constantly shifting set of shared contexts with other people (and other entities) and just their sheer number, it is effectively impossible for an individual to do the calculus of information transfer in real time. Fortunately, the past few years have provided us with a wealth of tools (Facebook, Twitter, etc.) that can not only increase the number of people we’re frequently interacting with, but also increase the shiftiness of those shared contexts. I will leave as an exercise to the reader what three conclusion paragraphs contain. : ) # Mercenaries I am a mercenary. This is a weird revelation, as “mercenary” usually carries a “solder” connotation with it. And usually across national boundaries and/or affiliations. But it perfectly matches what I am: I solve problems the highest bidder can’t solve on their own. There is no allegiance in either direction; there is nothing beyond utility. No mercenary dies and has their employer mourn. No mercenary succeeds and gets an award. This seems at odds with the “craftsman” label I’d previously adopted as an apt descriptor, but I’m hesitant to disagree. An traditional artist/craftsman who works on commission is exactly the same: a mercenary sans violence. I have a certain set of skills. They don’t happen to be martial in nature, but they are certainly worth people paying to have at their disposal. Where is the line? What is the differentiator? # Fractions! Fractions can be confusing. But there there are a few simple rules which encompass everything you could ever want to do with fractions. In order to use the fraction rules, you have to be able to add, subtract, and multiply ordinary whole numbers. You’ll also want to keep in mind two rules about arithmetic which are very helpful with fractions. ## Arithmetic Rule #1 Any number stays the same when zero is added to it or subtracted from it. $3 = 3 + 0 = 3 - 0$ ## Arithmetic Rule #2 Any number stays the same when multiplied or divided by one. $5 = 5 \times 1 = 5 \div 1$ Fractions have a few specific terms which are important to know as well. Here are the big ones: ## Glossary • numerator: the number on the top of a fraction • denominator: the number on the bottom of a fraction • improper fraction: a fraction with a larger numerator than denominator • reduced fraction: a fraction that cannot be equivalently written with smaller numerator and denominator • mixed fraction: a fraction with a whole number part and a fractional part On to the rules, of which there are six, but only the last two are particularly interesting. ## Fraction Rule #1 Any fraction with $0$ as the denominator is undefined (illogical, nonsense, error). $undefined=\frac{2}{0}=\frac{0}{0}$ ## Fraction Rule #2 Any fraction with the same number for both numerator and denominator is equal to $1$. $1 = \frac{1}{1} = \frac{8}{8} = \frac{12345}{12345}$ ## Fraction Rule #3 Any fraction with $0$ as the numerator is equal to $0$ (unless the denominator is also $0$ - see rule #1). $0=\frac{0}{1}=\frac{0}{7}=\frac{0}{456}$ ## Fraction Rule #4 Any simple number can be converted to a fraction by placing it over $1$. $2 = \frac{2}{1}$ This is the same as the division version of arithmetic rule #2, since fractions are just division problems. ## Fraction Rule #5 Fractions with the same denominator are added by adding their numerators and copying the denominator. Fractions with different denominators cannot be added together directly (see below). $\frac{1}{5}+\frac{3}{5}=\frac{1+3}{5}=\frac{4}{5}$ ## Fraction Rule #6 Fractions can be multiplied by multiplying the numerators and denominators separately. $\frac{2}{3}\times\frac{1}{6}=\frac{2\times1}{3\times6}=\frac{2}{18}$ # Using the Rules ## Division Division is often a useful thing to do with fractions, and it’s easy. Just turn the divisor fraction (the second one) upside-down and multiply. This is commonly referred to as “invert and multiply”. $\frac{1}{3}\div\frac{2}{5}=\frac{1}{3}\times\frac{5}{2}=\frac{1\times5}{3\times2}=\frac{5}{6}$ ## Improper Fractions Improper fractions can be made proper by repeatedly pulling $1$ (expressed as a fraction) out of the fraction to create a proper mixed fraction. Remember that every fraction has a “ones” place to the left of the actual fraction to make it a mixed fraction. $\frac{5}{2}=0\frac{5}{2}$ The $1$ that we want to pull out when converting an improper fraction to proper form is based on the denominator. By addition rule #1, we can subtract $0$ from something without changing it. And we know that a fraction with the same numerator and denominator is equal to $1$ by fraction rule #2. So if we subtract a one-equivalent fraction and then add $1$ back to it, it won’t have changed. $\frac{5}{2}=\frac{5}{2}-\frac{2}{2}+1=\frac{3}{2}+1=1\frac{3}{2}$ In this case we still have an improper fraction, so we’ll repeat the process again (and continue until we no longer have an improper fraction). $1\frac{3}{2}=1\frac{3}{2}-\frac{2}{2}+1=1\frac{1}{2}+1=2\frac{1}{2}$ So the improper fraction $\frac{5}{2}$ reduces to the proper mixed fraction $2\frac{1}{2}$. ## Mixed Fractions Sometimes you have a mixed fraction and want to make it a pure improper fraction. Get rid of that pesky whole number part. Improper fractions are usually easier to manipulate than mixed fractions, so it’s common to make everything improper, solve the problem, and then make the answer proper again. The process of converting a mixed fraction to an improper fraction is quite simple. Remember that a mixed fraction is the same as the fractional part plus the whole number part. $2\frac{1}{2}=2 + \frac{1}{2}$ If the whole number is converted to a fraction, we can just add them and get a fractional result. We know how to do that because rule #4 says we can slap a $1$ under any whole number. The denominators won’t match, but arithmetic rule #2 says we can multiply by $1$ (a special form – namely the denominator over it self – see rule #2) without affecting anything. $2 + \frac{1}{2}=2\times1 + \frac{1}{2}=\frac{2}{1}\times\frac{2}{2}+\frac{1}{2}=\frac{2\times2}{1\times2}+\frac{1}{2}=\frac{4}{2}+\frac{1}{2}=\frac{5}{2}$ This process is a special case of adding fractions with different denominators discussed next. ## Adding Fractions With Different Denominators Adding fractions with different denominators requires multiplying one or both fractions by a special form of $1$ first (which doesn’t change anything – see arithmetic rule #2). The trick is in picking the right version of $1$ to use. The goal is to get both fractions to have the same denominator. The easiest way is to multiply each fraction by the denominator of the other fraction over itself (which is a way of writing $1$ – see fraction rule #2). For the problem $\frac{1}{3}+\frac{1}{2}$ we want to do this $(\frac{1}{3}\times\frac{2}{2})+(\frac{1}{2}\times\frac{3}{3})$ Doing multiplication according to rule #6 yields this $(\frac{1}{3}\times\frac{2}{2})+(\frac{1}{2}\times\frac{3}{3})=\frac{1\times2}{3\times2}+\frac{1\times3}{2\times3}=\frac{2}{6}+\frac{3}{6}$ Now that the denominators match (and they always will by following this process) we can add according to rule #5 without difficulty. $\frac{2}{6}+\frac{3}{6}=\frac{2 + 3}{6}=\frac{5}{6}$ An there we go, adding two fractions with different denominators by multiplying the parts by a clever form of $1$. $\frac{1}{3}+\frac{1}{2}=\frac{5}{6}$ ## Reducing Fractions (Part 1) Answers to problems, whether in math class or the real world, should almost always be given in reduced fractions. No one likes to be told $\frac{5}{10}$ when they ask how much pizza is left. Much better is $\frac{1}{2}$, but how to get from one to the other? Since you know that the above rules describe everything you’ll need to do with fractions, it’s probably not surprising that a special form of $1$ is integral to the answer. Finding the right form is tricker than for addition above, but there is still a foolproof process. The first step is to find a non-trivial common factor for the numerator and denominator. A factor is a number that can be multiplied by another number to get the answer. For example, $2$ and $3$ are two factors of $12$. The “non-trivial” means “not including one” since that’s a factor of every number and per addition rule #2 and fraction rule #4 dividing by $1$ doesn’t change anything. We don’t want just any non-trivial factor, however, we need one that is a factor of both the numerator and the denominator (a common factor). factors of 5: 1, 5 factors of 10: 1, 2, 5, 10 Since $5$ is prime, the only options are $1$ and itself. $10$ has a couple more options, but the only non-trivial common factor of both numerator and denominator is $5$. If you’re reducing a fraction and there are multiple choices for common factors, always pick the largest one. If you don’t pick the largest one, when you’re done, you only have a partially reduced fraction, not a fully reduced one (and you’ll have to do the process again). If there aren’t any non-trivial common factors, meaning $1$ is the only common factor, then the fraction is already in it’s reduced form. Now that we have $5$ as our factor, we can rewrite our problem by “pulling out” that factor according to rule #6: $\frac{5}{10}=\frac{1\times5}{2\times5}=\frac{1}{2}\times\frac{5}{5}=\frac{1}{2}\times1=\frac{1}{2}$ Thus we know that $\frac{1}{2}$ is a reduced version of $\frac{5}{10}$. Further, since $5$ was the largest common factor, we know that it cannot be reduced any further. ## Reducing Fractions (Part 2) A quick way of describing the above process is to say “divide the same number out of top and bottom and repeat you can’t do it anymore”. It’s still the same process, but mashing a few steps together and not worrying so much about doing it all in one pass. As an example, reduce $\frac{18}{24}$. Top and bottom are both even, so let’s divide out $2$. $\frac{18}{24}=\frac{18\div2}{24\div2}=\frac{9}{12}$ Still not reduced, but $3$ will divide out of each. $\frac{9}{12}=\frac{9\div3}{12\div3}=\frac{3}{4}$ Alright, no other numbers can be divided out, so it must be reduced. If we’d picked $6$ ($2\times3=6$) initially, we’d have been done in one step, because $6$ is the largest common factor of $18$ and $24$ as discussed above. Doing it this way meant we had to go through the process a couple times, but we didn’t have to figure out the list of factors and then figure out the largest common factor. Both approaches are 100% correct and will yield the same result every time. If you’re doing the reduction in your head while baking, chances are you’ll do the quick’n’dirty way. If you’re doing the reduction while designing a bridge strut, you’ll probably do it the longer way. Regardless which you choose, the answer will be the same. ## Reducing Proper and Improper Fractions Reducing fractions and switching from proper and improper forms are all simple conversions. They’re not changing the fraction’s value, just the way it’s written. Thus if you have to do both, it’s completely irrelevant which order you do them. Since a proper fraction’s whole number part is always reduced (whole numbers are as reduced as they come), doing the reduction second is often easier because the numbers in the fraction will be smaller. But it doesn’t change the math at all; the result is the same. # Groovy’s Collection.minus(Collection) If you’ve done much Groovy development, you’ve probably used all the handy helper methods that it provides to augment the core JRE classes. Collections, in particular, have some really helpful additions in the form of operator extension. For example, the minus operator works like this: assert [1, "a", true, true, false, 5.3] - [true, 5.3] == [1, "a", false] This is all sorts of handy, but I found an interesting bug in Groovy 1.8.4′s implementation. Behind the scenes, it uses two different Comparators to do the equality checks needed to perform the subtraction. However, the Comparators are a little off… The first Comparator is actually just the DefaultTypeTransformation.compareTo method, and it gracefully handles non-Comparable, non-equal objects with the same hashCode. It also handles nulls (which are always first), and a bunch of standard type conversions (e.g., char -> String when compared to a String). The second Comparator, however, it less well behaved. It’s called NumberAwareComparator, and delegates to DefaultTypeTransformation.compareTo first. But if that fails, it falls back to a hashCode comparison, which creates a very interesting bug: if two non-equal objects have the same hashCode, NumberAwareComparator will return zero from it’s compareTo method, which will be interpreted as equality in a variety of contexts. The workaround that I’ve found is to explicitly implement Comparable in the domain model at the appropriate level(s) so that you always have a mutually-comparable collection, and you can implement compareTo in a way consistent with equals. Implementing Comparable like this will ensure that DefaultTypeTransformation.compareTo will successfully handle the comparison, thus preventing falling through to NumberAwareComparator. Sometimes, it is the library. : ) # Domino Functions At Gram and Gramp’s this weekend we played dominos, and the question came up of counting a set of dominos. In particular, Gramps wanted to know how many dominos there were in a set with double twelves. After we were done playing, we lined them all up in the “domino triangle” with and Emery and I figured it out. The domino triangle is simply an arrangement of dominos with double zero in one corner, the zero-one next to it, followed by zero-two (etc.), and with the double one at the end, followed by the double two (etc.). Here is a partial rendering of the triangle for our double nine set: +-+ +-+ +-+ |0| |0| |0| +-+ +-+ ... +-+ |0| |1| |9| +-+ +-+ +-+ +-+ +-+ |1| |1| +-+ ... +-+ |1| |9| +-+ +-+ ... +-+ |9| +-+ |9| +-+ To start, let $n$ equal the number of the largest double (twelve, if our case). We’re looking for $f(n)$, which is the number of dominos in a set with largest double $n$. Simply counting the dominos in the triangle gives us a values for our function, but not the one we want:  n | f(n) -----+------- 0 | 1 6 | 28 9 | 55 12 | ?? Before dealing with $f(n)$, let $g(n)$ denote the number of doubles in the set. A quick glance at the triangle will show that the formula for this function is $g(n)=n+1$ because there is one double for each $[0, n] = \{x \in \mathbb{N} \,|\,0 \le x \le n\}$ Now thinking back to the “domino triangle”, it’s easy to see that $g(n)$ is both the width (base) and the height of the triangle. If you recall from planar geometry, the area of a triangle is half it’s base times height: $\frac{base \times height}{2}$ So a first guess at $f(n)$ would be this: $f(n)=\frac{(g(n))^2}{2}$ But that isn’t correct. Thinking back to the domino triangle quickly shows why. If you were to take two copies of the triangle to make a rectangle (the reason the triangle area formula works), you’d see that you’d have to offset them by one domino in one direction, so the rectangle is actually $g(n)$ by $g(n) + 1$. So the right formula is this: $f(n)=\frac{g(n)\,(g(n) + 1)}{2}$ Simplifying by inlining the formula for $g(n)$ yields: $f(n)=\frac{(n + 1)(n + 2)}{2}$ This turns out to be the correct formula, and yields 91 when its argument is twelve. Thus a set of double twelve dominos will have 91 dominos. What was particularly striking as Emery and I worked through the formulas, is that Emery had more trouble with the larger multiplication (which he hasn’t done in school) than following the function notation, doing the step-wise evaluation of the functions, and even the function nesting. # Branching and Refactoring Mark Mandel made an interesting post about branching strategies when you have both feature development and refactoring to perform together. Specifically, he’s wondering how best to ensure you don’t orphan the refactoring if you abandon the feature, since the refactoring might be useful elsewhere. I posted my comments, and you should too. Effectively using your tools is vital to success in the software world, and no tool has more potential value (and potential pitfalls) than source control. Everything you care about having around tomorrow should be stored in source control. That obviously means your source code, but also includes your build/deploy scripts, editor/IDE config files, design documents, etc. # Hql For Groovier Hibernate If you’ve done much with Groovy, you probably know about the slick Sql class that it provides for interfacing with a SQL datastore across JDBC. It’s not perfect, but it’s certainly easy. I was working on a Hibernate project and wanted the same thing for my HQL queries, so I threw together a simple Hql class to provide the same sort of behaviour. Like Sql, it can be parameterized with a connection source (a SessionFactory in this case) or a specific connection (a Session). If given a SessionFactory, it’ll use getCurrentSession() to grab a Session when one is needed, which allows you to wire a single Hql instance up in your Spring config, supply it your SessionFactory, and then inject the Hql into all your persistence-aware beans and just use it. It also provides a public getSession() method for returning the current session to those same beans, which means you can just inject the Hql instance and not have to also inject the SessionFactory instance if you’re feeling lazy (or doing some quick prototyping). I will leave whether that’s an appropriate long-term approach for a different discussion. Here she be: import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; /** * I aim to be sort of like Groovy's builtin Sql class, except for Hql * @author bboisvert@gmail.com * */ class Hql { private Session __sess private SessionFactory __sf def Hql(SessionFactory sf) { __sf = sf } def Hql(Session s) { __sess = s } SessionFactory getSessionFactory() { __sf } Session getSession() { __sess ?: __sf.currentSession } Object unique(String hql, List params=null) { new Statement(hql, params).query(session).uniqueResult() } Object unique(GString gstring) { new Statement(gstring).query(session).uniqueResult() } List list(String hql, List params=[], Integer firstResult=null, Integer maxResults=null) { new Statement(hql, params, firstResult, maxResults).query(session).list() } List list(GString gstring, Integer firstResult=null, Integer maxResults=null) { new Statement(gstring, firstResult, maxResults).query(session).list() } private class Statement { String hql List params Integer firstResult Integer maxResults def Statement(String hql, List params, Integer firstResult=null, Integer maxResults=null) { this.hql = hql this.params = params ?: [] this.firstResult = firstResult this.maxResults = maxResults } def Statement(GString gstring, Integer firstResult=null, Integer maxResults=null) { def hql = new StringBuilder() def params = [] def prefix gstring.strings.eachWithIndex { str, i -> if (i > 0) { def v = gstring.getValue(i - 1) if (v != null) { hql.append(prefix).append("?") params += v } else { if (prefix.trim().endsWith('=')) { prefix = prefix.replaceFirst(/=(\s*)$/, ' is\$1')
}
hql.append(prefix).append("null")
}
}
prefix = str
}
hql.append(gstring.strings.last())
hql = hql.toString()
this.hql = hql
this.params = params
this.firstResult = firstResult
this.maxResults = maxResults
}

Query query(Session sess) {
def q = sess.createQuery(hql)
params.eachWithIndex { o, i ->
q.setParameter(i, o)
}
if (firstResult != null) {
q.firstResult = firstResult
}
if (maxResults != null) {
q.maxResults = maxResults
}
q
}
}
}

# I am a Programmer Luddite

Several years ago I decided that I didn’t want to use the ‘barneyb.com‘ domain for my personal blog anymore.  I’m not sure what the catalyst was, or even whether there was a catalyst, but I do know that there were two primary factors at play.  Those factors have remained constant (if not increasing in relevance) over the intervening years, and I am now happy to say I have a new blog here at programmerluddite.com.  And yes, ludditeprogrammer.com will also work for those of you who, like me, can never get the words in the right order.

The first factor was the morass that barneyb.com had become.  It has housed between seven and twelve WordPress blogs, several large-scape web apps, a pile of little utilities and demo apps for various purposes, all of my self-contained open source projects, and eight years worth of blog posts.  The domain has lived on an old Dell laptop, an old workstation in my basement, a massively shared PHP host, a dedicated server, and now on EC2.  This has yielded a URL structure which is somewhat convoluted and an Apache HTTPD configuration which was even more insane.

The second factor was that the history of BarneyBlog (my blog at barneyb.com) indicated a trajectory which didn’t really match real life.  I don’t think it accurately reflects the developer that I am, let alone the person I am.  Which isn’t to say that it doesn’t represent reality, of course.  At the micro level, each individual post is a valid glimpse into my world, but the whole doesn’t seem to match up at the macro level.

The net of it was that I wanted a new projection of myself onto the web.

About the same time I realized just how little affinity I have for technology in general.  Technology is a tool, and while it can be a useful tool, no tool can hope to exist for it’s own sake.  Tools “buy” their existence through utility; non-useful tools aren’t tools, they’re just stuff.  A huge swath of technology falls into this latter category, and I seem to be fairly unique amongst my peers in judging such tech purely on utility.  The “luddite” moniker is not a perfect fit, but it certainly conjures up the appropriate imagery.

The other half of the name was somewhat more difficult to pin down.  I’ve spent a lot of time over the past five or seven years thinking about what label best describes what I do in my chosen career.  None of them seem to match up perfectly.  I’ve adopted the term “software craftsman”, but calling this site “software craftsman luddite” seemed rather pretentious.  “Programmer”, I think, indicates a similar love of the act of programming.  I.e., the love of programming itself, not the creation of software (which necessarily has an external purpose).  Yes, I appreciate the irony.

I write code to fulfill needs.  I create tools, some for me and some for others.  But my love of software development is selfish.  Meeting people’s needs with software is pleasing; crafting beautiful software is satisfying.  I solve problems with technology.  I eschew technology for it’s own sake.  I am a programmer luddite.