Sunday, October 13, 2013

What would make a sustainable genetically modified organism?

As a policy, I try very hard not to listen to political campaigns. Mailers and other propaganda go straight to the trash. This does not mean I don't care. Several weeks before ballots are due, I look at the candidates and issues. Given my point of view, will reading this bias your views?

Nonetheless, I've heard that the GMO debate rages in my beloved Washington State. Many of my dear friends in my hippy sustainable green echo chamber seem to care deeply about this.

What are we arguing about?  Initiative Measure No. 522, AN ACT Relating to disclosure of foods produced through genetic engineering. So, what is a GMO and how does it how does it differ from regular selective breeding? 522 uses a technical definition:

(3)(a) "Genetically engineered" means any food that is produced
from an organism or organisms in which the genetic material has been changed through the application of ... recombinant deoxyribonucleic ... protoplast fusion ... in a way that does not occur by natural multiplication or natural recombination."


In essence, changing plants and animals much faster than can be achieved using older techniques. Cross species.

I don't want to analyze the arguments for an against 522. Briefly, While most of my echo chamber thinks GMOs are evil, I think GMOs could be great, but currently aren't.  Why aren't GMOs great? Patenting, terminator seeds, monopolization, plants that increase use of pesticides, farmers using them are only motivated by crop yield, companies producing them are only motivated by profit.

An aside: as a software engineer, I wondered why genetic modification doesn't have have open source. In software, closed source, licensed software (Microsoft, Apple) is balanced and challenged by open source (think linux, javascript, and other software).  Of course: software is very easy to read, copy, alter, distribute and even reverse engineer. DNA takes a little more work. Perhaps we'll have more open source genetic modification when gene readers and writers come down a little in price. I wonder if kids will be able to create toy genomes as easily as they can create toy computer programs?

Getting back to the point, I think that the organic, sustainable food movement needs to embrace genetic engineering as an essential control to it's implementation. What would a "sustainable" genetically engineered food system look like?

Open source. Free as in speech, if not free as in beer. Transparency through the entire process. Stop suing farmers whose crops are corrupted by your intellectual property.

No terminator seeds.

Slow, careful pilots and studies. The roll out of GMOs has been too fast for some peoples' comfort. Dismissing concerns about nutrition, affects on humans and affects on the environment reduces time to market but scares people.

Solve problems for people and the environment, not companies' profits. Can we make more drought resistant plants? Can we reduce pesticide use? Can we put our biological resources and efforts into understanding the systems that allow for healthy food production? We've accelerated the pace of climate change so much, that the environment needs as much help as it can to adapt.

Can this happen in the current economic and political climate? As long as the only ones in the game are aggressive, profit driven companies, probably not. We need more idealistic minds to think creatively about how genetic engineering our food production can help us solve the world's problems. We need to think of it as just a faster way to turn teosinte into modern day corn. The danger, and the opportunities, lie in "faster".







Saturday, June 01, 2013

The question, Is this worth automating?

No-one likes to do repetitive tasks. Well, perhaps some people do, but programmers should not. We like to automate the tasks. But when should we not? The stock answer would be to turn to a return on investment calculation.

However, I'd suggest that even if a task could be done manually in 30 minutes, where it would take 2 hours to script, you should automate it. If a process could be done in 5 minutes a day, but it would take 2 days to create a program to do it, write the program.

Why? Do you not care about your companies bottom line? Why, thank you for asking: Each time you script or automate something, you get better and faster at automation. What would have taken 2 hours to script the first time will take 1 hour the second, and a few minutes subsequent attempts. Consider what you learn in your decision to automate something.

And, of course, writing a script is much more entertaining than install the ruby devkit manually on 25 windows build agents.

Friday, June 01, 2012

Dispatches from DevTeach Vancouver

DevTeach in Metrotown 

A few months ago, my company offered me the opportunity to choose a software conference to attend. After browsing some conference listing sites, I chose DevTeach Vancouver, despite it's hokey website. I've had enough of Las Vegas -- it also had an impressive and relevant session list. I hopped a train in Seattle with my bicycle. From the Vancouver station, I rode on Vancouver's amazing network of bike trails to the conference 10k out in Metrotown, Burnaby.

Metrotown is ... eclectic. A substantial Asian mall -- almost a proper china town -- called "Crystal Palace", was attached to the conference hotel. (Great food, there). Metrotown boasts one of the Vancouver areas largest shopping malls. I got lost just finding my way back from the restrooms. For a break from the bustle and commercialization, I could easily ride my bike to two substantial parks. Deer Lake Park surprised me. Riding down through suburban sprawl, I discovered kilometers of wetland trails and boardwalks around a beautiful lake. I tried not to run over the goslings huddled on the trail.

(Insert picture here when I get to a computer with some image editing software.)

Some memorable anecdotes from DevTeach

“Objects are the way God thinks!”  Ted Neward in his multi-paradigmatic C#.

"If I looks like a duck, quacks like a duck, and eats batteries, you probably have Liskov violation." Joel Hebert, from his presentation on Design by Contract.

Don't borrow men's phones was the most important lessons learned from Tom Lewis in his talk about Responsive Web Design. Liberally paraphrased: "We shouldn't assume that mobile devices are something you use when walking to a meeting ... when asked where they used their phones, 80% of men admitted to using them on the toilet." ( Where and how people use their mobile browsers was relevant to an argument that we should use same site for mobile devices as their main site. Increasingly people use their phones like their computers. ) 

Dispatch 1: Design by Contract (and how to use it without Visual Studio Ultimate)

Joel Hebert started his discussion with a review of SOLID, including the most memorable description of the Liskov Substitution Principle, which I have only been able to explain for 5 minutes after reading more formal definitions. That was the ducks, above.

Contracts have been a first class construct of the .Net framework since v4 (we can say "since v4" now the 4.5 is coming soon). How does this relate to the Liskov substitution principal? It is quite difficult to implement a class hierarchy that adheres to Liskov-- where derived classes can substitute for base classes -- without Contracts. The best tech speakers also know that if the open with clever descriptions of comp sci concepts, they'll get there audiences attention.

You can read about code contracts on MSDN. What contracts provide are enforcement preconditions, postconditions, and invariants. You can apply contracts on an interface in the style of aspect oriented programming.

Why is this cool? Quality! It makes discovering and preventing defects much easier. Code that calls a method with an invalid value would get caught at compile time via static checking. How many basic validation unit tests would that make unnecessary? Manual testers, system and integration tests that stumble over contract violations will get a very specific error.

While you can annotate your code with contracts with just the v4 SDK, they won't do anything. One tool provides static checking of contracts at compile time (cccheck.exe). That is, it looks for contract violations that it can see in the code, and spits out warnings. Another rewrites the IL to inject the contract assertions (ccrewrite.exe). This throws exceptions if a contract violation occurs in your running code. Visual Studio Ultimate Edition gives you a nice UI to configure this. 

Oh, you don't have Visual Studio Ultimate? Joel did. Too bad I only have Premium. According to documentation, any edition other than Express should show this property pane, but I have only seen it in Ultimate editions. Fortunately, we are all hackers here. I've posted an explanation and example of how to leverage Contracts with lesser versions of Visual Studio using the build targets. This is probably not violating any licenses, as it the pretty UI you are paying for. Without it, you have to edit the build files yourself.


A warning about the static checking and compilation. It increased the build time for a tiny project from 1 second to 12 seconds. I could execute a lot of unit tests in that time. 

Dispatch 2: You don't need to design your code well if you are filthy rich

Ah, the advantages of being rich. Schooling, buying laws, poor folk have to design code well to unit test it. They put interfaces around expensive components, use Dependency injection, and replace them with mocks or stubs using open source tools such as Rhinomocks or Moq. If you've got money, you don't need to do that.

Mocking frameworks that are based on the .Net profiler API can mock any class or method, including static and framework methods. We've created our own Time implementation, so that we can control the time in our unit tests. Mocking frameworks that use the .Net Profiler API could even replace the return from DateTime.Now. However, you pay quite a bit.
  • Moles from Microsoft (this is the only one framework that you don't have to pay extra for, but it is extremely awkward in its current incarnation).
  • JustMock from Telerik ($400 each)
  • Isolator from TypeMock ($800 dev + $2500 build server. We have 20 build servers. I hope there is volume licensing.)
Here is what pressed my button: the presenter suggested that these have the advantage that you did not need to use dependency injection and IoC containers to enable unit testing. He argued you using DI and IoC to enable unit testing is bad, because New() is faster than IoC resolution. While IoC resolution is in fact slower, this is irrelevant. You shouldn't resolve in a tight loop. We typically create interfaces in front of components  where the call is expensive, significantly more expensive than new(). File systems, databases, web services, and so forth. There are many other reasons that dependency injection plays a part in good design, and many other reasons to have a layer between your code and its dependencies.

(Is it obvious that I let a red herring distract me from the content of an otherwise well delivered presentation?)

There are few cases, such as DateTime.Now, where the capability to mock the behavior of statics is nice. For everything else, these tools allow developers to unit test poorly written code. While the I'm don't think these tools are worth the price, people who write terrible code have a right to test it, too.

More later (maybe)

I covered a lot of ground at my time at DevTeach, and I'll post more later. I also reserve the right to got back to my regular life and not post. 
  • The first day had a Windows 8 promotion key note.
  • Design patterns for web services. Moving to Resource APIs. I finally grok Hypermedia as the Engine of State. 
  • Several presentations on NOSQL, including some discussion of why defining some by what it is not is nonsense. Volvos, dogs, and my left foot are also not SQL.
  • You can write C# in 5 paradigms.
  • Source control branching, Shared components, and deployment
  • I went to several HTML presentations and an MVC presentation. While I once (1990s) worked a lot in HTML, I have only dabbled in the last decade. I now feel inspired to dabble more dabblishly. Maybe I'll write a complete HTML app in couchdb while sitting on my futon. Apparently, you can write data tier, service tier, and presentation tier in one process!
  • I refreshed my knowledge of Scrum and estimation. Always good to review the basics.
  • Kinect for windows is cool ... but still definately v1.
  • I got schooled by Kathleen Dollard on .Net Geeky stuff. 
I'll send this post to my mother and father. They also say they don't really know what I do. 

Monday, March 07, 2011

If heat rises, why is it colder at the top of mountains?

This blog will not answer the title question, but answer a much more relevant question: how and why should we hunt knowledge every day. It is inspired by my son, who asked the question and James S Bach’s “Secrets of a Buccaneer Scholar”.

First, about my son, then about James Bach. At six, my son loves science. Every time he asks questions like “why is the sky blue?” and “how long would it take to drive to the moon”, my wife and I dig into the answer with gusto. He loves math for much the same reason. We revel in his curiosity and encourage it. I have learned a lot teaching him.

Now, about James Bach. I found his book while looking for books about Software Testing. While James Bach also writes books about software testing, this book was about learning. It describes how, after dropping out of school with only an 8th grade diploma, he learned Assembly Language Programming, became a software tester, and now consults other software testers. It was his habit of self-education that gave him the edge over his college educated competition.

I’ll elaborate on just a few of his “great secrets”.

To learn something, it should be relevant. He calls these Authentic Problems. While I’ve studied several foreign languages in school, the 2-3 that I speak well I learned while living abroad, and are not the languages I learned in school. Likewise with software. The most useful software knowledge that I’ve gained has been because I needed the knowledge at work to solve a problem. I take the problem home and beat it into submission.

Cognitive Savvy. You have to understand how you learn. Everyone will learn differently. I learn well by processing things in multiples ways. For example, this blog entry will be my Toastmaster’s speech Tuesday. I know that I often fragment ideas as I restructure on the fly, so I videotaped myself reading it to ensure it flowed well.

Other minds. Woodrow Wilson said, “I not only use all the brains that I have, but all that I can borrow.” Bach refers to hunting knowledge in packs. I refuse to work alone because I want the ability to come up with crazy ideas. Some will be brilliant, some will be chaff. Other minds attacking my ideas allow me to be brilliant.

I will conclude by how I answered my son’s query: I dove in and made up a plausible answer on the spot. Diving in is always the first step across a river. Perhaps I could have solved it on my own, but I thought it would be more fun to hunt down the answer with my pack of friends and relatives. The most amusing answer was, “Because you’re further away from hell.” The most thorough and correct answer came from my brother, who has climbed all of the 14ers in Colorado. This was a very authentic problem for him. Now that I know the answer, I’m trying to understand it well enough to explain it to my six year old son. Finding the answer was a great hunt.

Thursday, October 28, 2010

How I am voting in the 2010 November election.

No, silly, I am not going to tell you what candidates I choose and what position I take on ballot measures, but I will tell you my methodology. In elections, as in all else, “It ain't what you don't know that gets you into trouble. It's what you know for sure that just ain't so.” (Mark Twain). More money is spent convincing you of things that just ain’t so than in almost any other choice you make. If the electorate abided by the principals that jurors use in the justice system – we’d all need to be excused.

Step 1: Avoid learning things that just ain’t so. It's much easier than unlearning them. I abandoned television news several years ago and have never been better informed. Or, at least, less misinformed.

Step 2: Don’t start looking at issues and candidates until your voters guide arrives. It really shouldn’t take more than a few weeks to make an informed decision. Often, I can take a stand from the statements in the voters guide alone (see Step 3!). The massive amount of money spent in campaigns infuriates me. Ignoring it all gives me the satisfaction of wasting the money of the interest groups. It’s my statement that I’d like to see campaign season reduced to a two week period.

Step 3: For each issue, list what what you have heard before you opened the voters guide. In all likelihood, it just ain’t so. 30 second commercials and slogans on yard signs can’t articulate anything in depth. The format requires misinformation.

Step 4: Establish reliable – or at least honest -- sources of information for issues that you need more information on. Check out fact checking sites such as http://politifact.com to appreciate just how difficult this is. I look for endorsements from groups that are thoughtful and honest about their biases; that tell me why they are endorsing an issue. Even if I disagree with them, I’ve got a clearer picture. This is especially important for complex ballot measures.

Grammatical and rhetorical corrections are welcome.

Tuesday, August 04, 2009

“I found $x very bizarre while learning powershell”

Coming from most other directions, powershell is truly bizarre. I started this a few years ago when I started learning PowerShell because I suspected that I would not be able to remember why powershell is so non-intuitive.

Confusing: Verb-Object?

I can’t be the only programmer who finds the syntax backwards. In almost every other language I’ve worked with, you have an object (or module) and call a method on it.

new EvilService().CreateRandomEvilPlan()

You can do that in powershell, but it’s not the powershell way.

Add-EvilPlan

The paradigms will get confused in most code, as anything that doesn’t have a commandlet uses the standard paradigm. You can access normal .NET objects (or com objects)

$proxy = New-WebServiceProxy -URI $uri -Class "DSP" -Namespace "Daptiv"

$proxy.

Whoa … Functions, and everything else, can be treated like a file.

Get-Item -Path function:

new-item -path function: -name csrss -options readonly -value {get-process csrss}

Everything in the output of this powershell method can be treated like a file system:

get-psdrive

For example:

get-item env:C*

dir env:C*

dir variable:ps*

A lot of functionality in powershell is exposed through providers with consistent syntax, revolving around items. This is a bit mind-blowing at first: I work with functions, the registry and certificates just like files? Then, it becomes fairly powerful.

Piping objects? Think reduce, not pipes

Shell script authors know pipes, but these are typically text or byte pipes, not object pipelines. Object pipelines are cool because 1) it’s really easy to distinguish one object from the next. No worry about terminators. 2) You can act on those objects, walk graphs within the pipeline (it’s not always a good idea, though).

Functional programming languages has the concept of reduce. I am happiest thinking of pipelines in functional programming terms.

Yeah … slices and easy list types!

While experimenting with python for operational purposes, I found the list handling superbly powerful. Defining a list of servers to act on, tuples to quickly group data, all were great. Some of these things can also be done in powershell. The operations aren’t made as easy in real languages, for fear of nasty performance problems working with long lists.

See help_about_array. Highlights.

$webservers= "webapp1","webapp2", “webapp3”

or strongly typed:

[string[]] $webservers= "webapp1","webapp2", “webapp3”

Slice it to get a part (webapp1, webapp2)

$webservers [0..1] = "webapp1","webapp2", “webapp3”

Use the plus operator (+) to combine two arrays

$dbservers= “db1”,“db2”

$allservers=$webservers + $dbservers

Dictionaries are as easy as I hoped. See help about_hash_tables

$lookup = @{coffee="bitter";apple="sweet";peanut="crunchy";banana="sweet"}

With a surprising access mechanism -- not $lookup(“peanut”), but

$lookup.peanut

Sorting and filtering lists and dictionaries is simpler and more powerful than most languages, because it ties into the powershell object pipeline.

$lookup = @{coffee="bitter";apple="sweet";peanut="crunchy";banana="sweet"}
$lookup.getenumerator() | sort-object -property key
$lookup.getenumerator() | where-object {$_.Value -eq "sweet"}

One liners can be more than one line

In summary, here is one “line” of powershell:

$x=(@("Verb-Object semantics",
    "everything is a drive",
    "piping objects",
    "slices and lists",
    "powershell line continuation",
    "string interpolation") `
    | Where-Object {!$_.Contains("lists")}); $y=[String]::Join(",",$x); "I found $y bizzare truly bizzare while learning powershell."

Thursday, July 30, 2009

Respect, don't trust: A little philosophy on the emotional life of the software team.

I haven't heard much talk about the emotional world of the software engineer, so I'll offer some late night ruminations on the topic.

The deliberate conflict between the engineers making software and quality assurance fascinates me. Here's why: While software engineers passionately try to make things work, QA engineers diligently work to prove that the software is horrible. If you put this behavior in another context there would be pain and strife. One person persistently pointing out someone else's flaws. How rude!

In a healthy team, this is not discord, it is work and sometimes even fun. It's beautiful to watch. The graceful dialectic offers some life lessons and gives me hope in human nature. We can be honest and frank, we can seek truth and scorn "white lies". We can do this respectfully, with passion, and without hurt.

Here is how software teams make this work.

Respect, don't trust 

QA never trusts development. A QA who trusts the developer would believe -- without empirical proof -- that the software works. That would be silly. QA's role is to aggressively and skeptically to prove the developers' work flawed. QA seeks to prove. While mistrusting, the QA team respect the developers intelligence, creativity and passion. Respect does not require trust in this relationship - It a relationship where mistrust and skepticism are possible, emotionally, because of the teams' strong mutual respect. 

Trust and faith are good for your wife and your religion. Anyone or anything less than these gods deserves respectful skepticism. 

Be fearless of mistakes - mistakes and corrections make the world work. 

There are few situations in life where products must be right the first time. This is true in assembly line manufacture and live virtuoso performances. Most of life is about mistakes and corrections. In a conversation, if we don't understand each other, we dialog and correct. If I don't catch a fish on the first cast, I can try again. Bikes constantly wobble a little as we balance from side to side.  

Productive software engineers don't fear mistakes. Engineers who fear mistakes hoard their work for weeks, keeping it to themselves. Finally, pride satisfied, they throw an overwhelming amount of change of the wall to QA. QA needs weeks to validate - the cycles take many times longer than they should and the product is invariably late, rough in the places that need to be smooth, and polished in places where it doesn't matter.

Software works better when the developer fearlessly checks in small pieces of work continually. A little due diligence - send to to quality assurance. The right things are fixed and fast. Back to the physical world analogies: A bicycle with small wobbles moves fast. The only bike that does not wobble is probably smoothly falling over. 

Back to respect: The developer can fearlessly check in because of the respect offered from QA. QA respects the daring of the developer showing her raw work to the team.

( The final product is of course flawless. Mistakes the customer sees are bad. I'm talking about the early life of software. Typically, we make hundreds or thousands of bad builds before we make the final release build. So, the customer sees the one "right" build, but the life of software development is a story of continual mistakes and corrections. )

A defect in software is not a defect of the author.

Defect systems offer a powerful emotional tool - we don't talk about the systems this way very often. I rarely hear, in software, QA walk up to an engineer and say, "You stupid lout. You made the CSS wrong and the now the border of this table clips the text."

Instead, the developers does there best to make something pretty. QA digs into it, fines a tiny flaw, and files a bug report into a bug database. The bugs get prioritized, assigned out and fixed. The separation means that the flaw never gets personal. The author can handle a statement like "There is a flaw in the software" much better than "You broke the software."

All software teams think defect systems are essential for organization - I argue they are also essential to avoid getting distracted by the emotions of making mistakes.

Tuesday, July 28, 2009

My Simple 3 Step IIS Setup System

This is my secret system for getting IIS to work on new computers (servers or desktops). Permissions, default identities and user interfaces change with each OS. I'm not going to write a book on all the things that can go wrong with IIS. I've got got 3 simple steps that shake out different types of problems very fast.

Audience: Software Engineers or QA Engineers don't want to reconfigure IIS more than a few times in a year. The folks in operations probably have a powershell script to do this.

Three steps and a tip

Starting point. New server? Installed W7 on your development machine and it isn't serving up your site any more? It's all the same problem.

Step 0: Start with a sanity check and Run Compmgmt.msc (computer management). Do you see Internet Information Services? Any suspicious icons?

Step 1: Drop a static web page into the site and browse to it.a For example: <html><body>Hello</body></html>

Step 2: Drop an a very simple ASPX page into the site and browse to it. Example: Response.Write("Hello"). My dynamic test page is a more complex than this, to report host header, virtual directory, and so forth.

Tip: Verify this site in multiple browsers. Even better, chose a browser with a “Private” mode. In some configurations (or mis-configurations) you might send NTLM credentials and the site impersonates your credentials, masking permissions problems.

Step X: What you don't want to do.

Don't debug basic IIS setup problems in a complex site. For example, daptiv will have a few dozen confounding factors: from special handlers to run of the mill database and storage configurations to several choices of authentication.

OK, here are some details.

But really, don't clutter up your mind by memorizing these. They are examples. IIS 6 configuration does not resemble IIS 7 configuration.

Step 0 problems - check the obvious

Are you missing an IIS component? It's easy to do. The components vary from release to release. On Windows 2003 x64 – installing SMTP requires CAB file hacking and you might have hacked it wrong.

By opening IIS Management, you verified that the metabase is readable. I've got a short list of known ways to corrupt the IIS MetaBase and longer list of unknowns. SysPrep, for example, can someimes mangles the machine keys IIS uses to secure its data. BTW, I don't know how to resolve metabase problems. The only method known to work without an overpaid IIS Consultant nearby is to uninstall IIS and reinstall.

Step 1 problems – static page

Is the web site running? Check IIS management console. The website might also be stopped because configuration isn't valid. Two sites on port 80 with the same url? Invalid application pool? These problems are easy because event log tells you the problem.

Permissions? If I can't see the static page on a new IIS instance, it is usually because of permissions at the virtual root. I get this often, as I’ll create webs at odd paths such e:\src\3\root\SecretProduct\www, and refuse to make IIS_IUSRS an admin on my box. Permissions should be easy: Add users one at a time in a sensible manner until it works. Remove them until it stops working. Now you know what needs access.

Account names will change between IIS, windows, and .NET versions. Typically I need to add permissions to the folder for:

  • Application pool identity – needs at least read access: My app pool usually runs as Network Service or a domain account.
  • The account used for anonymous access. (IIS_IUSRS or IUSR, IUSR_COMPUTERNAME, or something fancy)

Step 2 problems – ASPX fails

The ASP.NET extension might not be enabled. This is configured in entirely different places on IIS 6.0 (Web Service Extensions) and IIS 7 (ISAPI and CGI restrictions). It's disabled by default on some versions of Windows, ever after IIS is installed.

Of course, this is a good time to run aspnet_regiis.exe. If any identity (Application Pool) is not the default, use aspnet_regiis.exe -ga. Use aspnet_regiis -i with total abandon.

Summary

That's all I need to know to get most web servers running IIS. There is a basic troubleshooting principal here: start simple and build up facts.

Sunday, July 12, 2009

Czerneda - In the Company of Others

Another science fiction devoured. (Why review books?)

http://www.czerneda.com/novels.htm

The book:

Smart savvy herione (Earther Gail Smith) played off against a street smart heroes Pardell and Malley from the Thromberg station. The solar system and earth are quarantined against the vague Quill (what is it? the book leads off pointing out that humans are clearly the only intelligent life in the universe). Gail is from once side of the Quarantine, Malley and Pardell from the other. It all weaves nicely together. 

The experience:

Let's talk about Sunborn by Jeffrey Carver, because I picked up in the Company of Others after tossing aside Sunborn. Sunborn reminds me of a poorly written mockery of science fiction that one might see on a mediocre Saturday Morning cartoon trying to make fun of sci fi. Stilted characters. The author seeks to create a "rich" world by injecting new bizarre traits or situations every other page. He doesn't have the creativity to pull it off. The pages read like they were rolled off an assemlby line of the bizarre where the workers were having a bad day. Page one. Let's stamp something bizarre. OK, here's a telepathic link. Page two ... low on the bizarre. How about an intelligent sun? Page three ... oh pick a random page of P K Dick. An so on.

My reading of In the Company of Other's benefited greatly from the relief of coherence and quality it provided. I devoured it overnight, and had an unpleasantly tired Tuesday as a consequence. To add to the amusement of this book, at around 2AM I looked through the front window to see the (unabashed) neighbor boy sneak out with a gaggle of friends. Not large enough to be a posse and they look unaltered by sensibility reducing substances -- so it looked like good clean mischief. I just smiled with fond memories of those summer's of freedom that precede financial independence.

I suspected, as I read it, that I've read the sequel. The story tempo and characters seemed extremely familiar. I can't find a sequel, so it's entirely possible that I swallowed it whole before and forgot most of it. Overall, a perfect pulp sci fi experience.

Sunday, May 10, 2009

My Books to Read page: May 2009

Why?

Books to read for fun

Ursula Leguin

Ursula LeGuin makes me proud to spend most of my free time with nose in sci-fi speculative fiction books. Looks like she finally produced a new book!

* Lavina

Kage Baker

Where can I find the Children of Company novels, in order? Is there an order?

Patrick Rothfuss

Name of the Wind hooked me. Where is the next one? Wake me up when it's done.

David Gerrold

Maybe Rothfuss will finish his books before David Gerrold finishes the war of the Chtorr. I think I grew out of that series, and then grew up enough that I could read it anyway. Damn artistes.

Serious Reading

Trigonometry for dummies (in progress)

Backlog of serious reading

  • Either "7 Habits of Highly Effective People" or "Getting Things Done: The art of stress-free productivity".
  • Mathematical Methods in the Physical Sciences: Need to keep the math fresh.
  • NHibernate in action. (In progress.) NHibernate only seems necessary to me because other people think it's necessary. I've never had a problem it purports to solve. The unit of work and caching in it is decent, but too tied into other crap to be be useful. Is there any ROI? Have to learn a clunkly framework that will be legacy in a few years.
  • I'll probably find a good ASP.NET MVC book and read it before I force myself to dig into NHibernate.

Read and digesting

  • Domain Driven Design, Eric Evans: This is the classic
  • Applying Domain-Driven Design and Patterns, Nilson: A good folks read, which just enough to disagree with.

Books on my Safari account - used for occasional skimming

  • Clean Code (great)
  • IronPython in Action: Just started
  • C# 3.0 in a nutshell: Perhaps Worth buying?
  • Deploying .NET applications: Disappointing.

Applying Domain-Driven Design and Patterns, Nillson (read ... drafting review)

Serious reading, looking for

  • Books on running and exercise that are not lame.
  • Books on finance.
  • Behavior Driven Design.
  • Content management, collaboration, OpenID

Hello, World in msbuild

I haven't blogged in a while. Here is a msbuild "Hello, World" for you.

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build;Test" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WhoToGreet>World</WhoToGreet>
</PropertyGroup>

<Target Name="Hello">
<Message Text="Hello, $(WhoToGreet)" Importance="high" ></Message>
</Target>

<!-- A build target is required. -->
<Target Name="Build" DependsOnTargets="Hello"/>
<!-- A test target is required. -->
<Target Name="Test"/>
</Project>


Execute as : msbuild helloworld.proj



or : msbuild helloworld.proj /p:WhoToGreet="Moon"

Saturday, November 29, 2008

Don't get too serious

DSC_0030-1

We serious folk can ponder deep meanings for a long time. As an antidote to this, I like a frequent trip to the zoo or a playground. It doesn't take much to live life to its fullest. Having a two year old helps you remember the technique. Grandma has to borrow ours on occasion.

DSC_0024

Saturday, August 02, 2008

A deep dive into COM, ApartmentStates and VS 2005 unit testing

I’ve had some adventures in what is clearly “Legacy” code recently, and had the pleasure on learning all about STA vs MTA apartment states, and what this means for unit testing in VS 2005. Let me warn you that this is an obscenely technical post. Mainly, I’m posting it because I’m certain to forget the details next week. If you want something non-technical, go ogle my children.

Summary: Do not use MTA, there are good reasons STA is the default.

Update 2008-08-19: OK, if you are in a hurry, do use MTA. Consider creating two test projects, one for STA and another for MTA. Getting the tests to work in STA is "proper", but not easy.

Scenario: An eager TDD developer wants to create unit tests for a .NET application that uses COM component, for example, a Microsoft office application. The woes you experience are described in threads such as this (MSDN forums) with misleading answers.

Side note: A test project where you are referencing COM objects and MS office applications is not a unit test project. Unit tests cover functionality with no or few external dependencies. You are writing very valuable functional or integration tests in the style of unit tests. You still need true unit tests.

What is happening?

Usually, a COM component such as an MS Office Application is heavy and slow to create. You want to share an instance to avoid creating it for each test. Reread the document for apartment states carefully. Note that the default, “single threaded apartment”, does not allow you to share a COM object across threads. VS 2005 test executes test initialization, assembly initialization, and tests on different threads. Ergo, you can't create a COM object in test initialization and use it (directly) in your tests if you run with Single Threaded Apartments.

Also note that STA is the default. Many COM components will not work with Multi Threaded Apartments. For example, the .WebBrowser control in .NET 2-0. Even the trivial .NET file open dialog requires STA.

More background: about COM runtime callable wrappers.

One answer to this is to execute your tests with Multi-Threaded Apartments. Having tried this, I strongly discourage it. A few hours of test support wrappers can get around the STA problems. MTA problems:

1) A lot of the COM components require STA threading. You’ll get errors such as “ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.”
2) If the actual application will run in STA, a test using MTA is an invalid test. Assuming this is an automated integration test (as it interacts with heavy COM dependencies, it is not a unit test), you want it to exercise the code in a realistic manner. A different threading model will lead to false failures and successes.

My solution:

To start with, build this out with Test Driven Development. What do you need to do? You need a test initialization method that creates some COM objects, you need to access objects during tests, and clean them up afterwards. So start with the following :
  1. test class that will demonstrate the ability to "share" a scenario using COM objects.
  2. a test initializer
  3. two test methods that access the objects. Interestingly, the second test failed until I got it write. Order didn't matter.
  4. a test cleanup method

Problem: I don’t want to create a heavy office application for each test.

Solution: While you can't share COM RCWs across threads, most heavy COM objects such as MS Office applications allow you to get an existing process via Marshal.GetActiveObject(). I create an Office Application wrapper and a builder responsible for its creation. The tests call something like Builder.GetApp(). This gets a new COM RCW that might be an existing MS Application process or a newly launched Application.

Also note that even in Single Threaded Apartment state, you can share these wrappers and COM RCWs in a thread static variable.

Problem 2: What about all those other smaller com objects? I want to set up a scenario during test initialization with lots of cells or ranges, then use those little COM objects in my tests.

Solution: Stores a way to find the COM object during test initialization (store the cell location, a task uniqueid, or something like that), rather than the actual COM RCW object. Then write a utility that provides a quick way to get the COM object for the test from the static variable. For example Wrapper.GetCellForActiveBook (identifier).

Problem 3: But GetActiveObject doesn't always work ...
Right, you can't always use the GetActiveObject trick for all types of objects. I'm still working on this. For example, I haven't been able to create "document" level objects (MSProject.Project, Excel.Workbook) in test initialization yet, and grab that specific instance in the tests (on a different thread). Solutions include.

  1. For some reason, the ActiveObject (ActiveWorkBook, ActiveProject) property gives me a document that I can create in initialization and use through the tests. This limits the test suite to a single document, and there is a risk that another document becomes active, breaking the test. I'm uncomfortable with this ... but using it successfully.
  2. Create a new instance per test. Use helper methods to repeat scenario initialization. While a little slow, I'm prefer this approach, as it keeps the tests clean.
  3. Create a document in initialization and save it to disk. Pull it from disk for each test - this creates the COM RCW on the test thread. I haven't pursued this approach.


Finally, be careful to close those COM objects! It's often harder than you think.

If you are playing with COM from .NET, read and understand Marshal.ReleaseComObject and the very handy Marshal.FinalReleaseComObject. In the solution I describe, you will have to carefully manage MS Office application instances or your test suite will leave dozens (hundreds?) of open MS office applications.

ReleaseComObject is very necessary after working with something like WindowsInstaller.Installer.OpenDatabase. The author of these automation interfaces neglected to implement the close method for database objects, while the C API requires that you close the database. Consequently, ReleaseComObject is the only way to avoid leaking resources and leaving a lock on the MSI file you read.

Other details - In a case where I am either creating a new Office app or stealing an existing process with Marshal.GetActiveObject, it's important for the wrapper to know if it created the Com object (you need to close the application process that you created) or stole it (you should only release the com object).

In summary, I hope I can't remember the details next week.

Anyway, there is lots of complexity in closing COM objects. For this reason, I package all heavy com objects in wrappers. A Builder pattern handles creation, and the IDisposable pattern handles cleanup. Easy. Once done, I can forget about this whole sorry technical deep dive by next week, and think about more important things.

Wednesday, March 05, 2008

The problems of excessive literacy

Many other avid readers probably share my problem: It's 5PM on Friday, the library closes in 1 hour. I have two kids in tow with the puzzles of SPL children's section on the top of their agenda. How do you remember what books you've read, and what was the 4th Kage Baker company novel, anyway?

So many books to read, so little time. Book time is precious and to be wasted carefully. I do want to get my fiber books read (Economics?) while I "accidentally" pick up total pulp science fiction every now and then. The balance requires a little thought about the books I read - even if that thought was, "What delightful pap that was!"

To keep track of

  • Fun reading: Where am I in an epic sci fi?
  • Fun reading: I catch an excellent short story by a new author, and want more ... oops, i forgot the authors name.
  • Serious reading: There was a topic I needed to read.
  • Serious reading: Inspires lots of thoughts that I leave half baked.

I've experimented some with online services such as google books and worldcat and they are quite useful, but don't really hit the spot. They support my book lust well, but are far too structured for me to use effectively.  Same with facebook and linked in pages. That I will try now, is to use the blog like an online whiteboard.  Here is my book list.

Tuesday, July 04, 2006

Cliff and Tenzing

 Posted by Picasa

Tenzing on Trike

He may seem like the rough type, but he's got a soft spot for flowers. Posted by Picasa