New Concurrency Support in .NET 4

I’ve been reading about some of the new support for parallelism and concurrency in version 4 of the .NET Framework, and came across a really good paper on parallel design patterns by Steven Toub (most or all of which seems to also be covered in the Parallel Programming with Microsoft .NET book).  There are some really convenient additions to the framework in .NET 4 that help make parallelism easier and less error-prone.  The point of this post is to illuminate a good example of how this is so.

About a year ago, I was working on a class library that was supposed to do some filtering on streams – something along the lines of Boost.Iostreams which gives you the ability to create pluggable, customized “sources” and “sinks” (producers and consumers) and filters set up in a stream pipeline.  One simple application of such a pipeline (and apparently Toub’s favorite) is that of compressing and encrypting some data.

Input is read from the source stream, passed through the filters, and written to the output stream.

I found Toub’s MSDN Magazine article describing how one could parallelize this, and incorporated the sample into my class library.  The basic idea is that you set up a blocking queue of chunks of data, and then use that queue (or queues) to connect the inputs and outputs of the streams and filters, and just let it fly.  The queue serves as the synchronization mechanism, fulfilling the producer role required by the input side of each piece, and fulfilling the consumer role required by the output side of each piece.  Below is the sample code.

Continue reading

Posted in Uncategorized | Tagged , , | Leave a comment

Re: Do I Need To Learn Microsoft Technologies

A long time ago, in a galaxy far, far away (Stackoverflow.com, November 2008), a university junior posted this question:

Most jobs speak of C#, Visual C++, .NET, Java, etc etc Where as I am mainly using Java, C++, Perl, Python and programming to the standard Unix standards, would I be better off ditching Linux and spending my last year of University brushing up on Windows based technologies, languages and API’s, would this increase my chance of getting into the industry?

A fellow named Andy Lester responded with this:

I suspect that your “most jobs” observation is from looking in the wrong places.

Whether or not “most jobs” are using MS technologies, would you WANT to work with MS technologies? If you went and boned up on your .NET and Visual C++ and had to use Windows all day, would that be the kind of job you wanted? If not, then it doesn’t matter if that’s what “most jobs” call for, because those aren’t the jobs for you.

There are not hundreds of jobs out there available for you that are a good match for you, and for which you are a good match. Don’t worry about the broad playing field of the job market, but instead focus on the jobs that DO interest you.

I responded:

I think this is stupendously bad advice. Of course you should bone up on Microsoft technologies. The chances of you making it through a 40-year career in technology without having to work with MS stuff is slim to none. Of course, the real answer is…focus on what you’re learning in school first.

Now recently, I was busy doing something else and I had long forgotten all about this exchange, and Andy has replied back:

Ben’s right, you’re likely to have to use Microsoft technologies, if that’s how you want your career to take you. What I think we’re seeing here is the difference in viewpoints between someone like Ben who seems to think primarily in terms of maximum salary and maximum employability, and someone who thinks about the importance of loving what it is that you do for a job.

He then spends the rest of his post regurgitating his original point: focus on the jobs that interest you.

Firstly, to Andy: I don’t think your point is a bad one, but I still think it was a bad answer to a 21-year-old kid trying to figure out how to make himself marketable.  Of course you should be brilliant at the things that interest you – but it’s also wise to be familiar with tools and techniques that don’t, because they may very well allow you to sustain a situation in which you are allowed to do things that interest you.

My college situation was very similar to the OP’s: I went to a good computer science school, but all the tools totally revolved around Java, C, and Unix.  I never learned any of the Win32 API, and only after my Junior year did I really get a chance to learn anything about Microsoft developer technologies.  That was a huge deal.  It helped broaden my horizons a bit, even if my first job out of school turned out to be a realtime embedded systems job.  But you know what?  After a few years I decided that I wasn’t terribly interested in what I was doing at that job any more, so because I was familiar with tools that I didn’t typically use, I had the flexibility to quit that job and find another one pretty quickly.

Another reason why it would have been smart for the OP to bone up a bit on .NET development or the Windows APIs is simply that he may not really know what he is interested in.  If all you’ve ever done is Java on Linux, how could you?  It could also be that what he’s really interested in is distributed algorithms (or any technology-agnostic application domain), in which case the particular platform or toolchain involved is immaterial.  If that were the case, then it’s a no-brainer: get familiar with new stuff before you graduate, and you’ll be more attractive to potential employers.

So, simply put: my point wasn’t that you shouldn’t pursue the things that interest you, but that it’s wise (particularly for a new grad) to round out your skill set to increase the odds that you’ll get to do the things that interest you.

Just ask a Lisp programmer.

Posted in Uncategorized | Tagged , | Leave a comment

Happy Pi Day

Posted in Uncategorized | Tagged , | Leave a comment

The Decade IS ending

There are some folks out on the internet preening about their realization that “decade” means 10 years, and there was no year zero, thus decades range over the calendar years [xx01 - xx10].

Marc Theisson posts:

2010 is the last year of the decade. The new decade begins in 2011!

Some dude named Phil Plait weighs in:

What people were arguing over were things like centuries and millennia, and how there was no year 0, and therefore the last day of the decade is actually December 31, 2010. But that’s not relevant because we don’t measure decades the same way we do centuries.

And then this guy thinks decades end at the end of xx10 because when you count, you count from 1 to 10:

Quick. Count to 10.

OK, now think back. When you were counting, what was the first number you used? What was the last?

That’s right. You began with “1” and you ended with “10.” That is something that we know before we begin first grade.

As a math-y, computer science-y kind of guy, I have to point out: accurate counting begins with zero.  By that light, the fact that there was no year “0″ is an error in our calendar system, not a feature.  Why then would we want to perpetuate the mistake throughout all time instead of just calling it a 9-year decade, and a 99-year century and be done with it?  The people who lived in those times are long dead and don’t care anyway.

UpdateFred Schwarz chips in -

…there’s no “technically” or “officially” about it. A decade begins or ends whenever you want it to, and one system is as right or wrong as another…The lengths of a day and a year might seem to be unalterably fixed by nature, but in fact they are not…So if the length of a day, a month, and a year can vary, why not the length of a decade? Why not define the first decade A.D. to be nine years long, ending in the year 9, and follow the natural 0-to-9 pattern from then on?

Exactly right, but you heard it here first.

Posted in Uncategorized | Tagged , , | Leave a comment

VSeWSS: Value does not fall within the expected range.

On occasion, particularly if you’re moving files around or working on the structure of your project inside Visual Studio 2008, you’ll try to deploy your SharePoint project using VSeWSS 1.3 (March 2009 CTP) and get the following error:

Value does not fall within the expected range.

It’s cryptic and unhelpful. What value? What expected range? Experience has taught me that VSeWSS can only be deblogged, not debugged.

Anyway, this error usually means there is a file listed in the solution (as in WSP solution, not Visual Studio solution) manifest that doesn’t exist on the filesystem. Maybe SVN sucks (it does), or maybe you clicked the wrong button. Head down that path and you’ll likely find the problem.

A dead obvious giveaway is shown in this image.  The little ‘hazard’ icon indicates a file included in the project is missing from the filesystem.

Posted in Uncategorized | Tagged , , | Leave a comment

Internet Freelancing

In a previous post about an online independent contracting portal, I complained about the quality of the work to be done. In a more general sense, this is really the bottom line when it comes to the difficulties in doing part-time side contracts:

InternetContractorRates

The hourly rates are in increments of $5

I don’t know about anyone else, but I simply can’t be bothered to work for $5 or $10 per hour. Really. I could hold sandwichboard signs for that much, and skip the whole process of begging for work on the internet. $15 and $20 is where the rates start to cross into my “I can suck it up for a while” territory, but even there I would expect that $20/hr rate to land me customer relationships or provider ratings that would enable me to land higher rates.

In a purely emotional sense, it feels unfair that I have to compete with individuals in developing countries who are willing to do labor for so little money.

However, it’s not unfair – that’s just the way the market works. I have to remind myself that the “hire a freelancer” sites aren’t the whole market; they only appeal to exactly the market subset that I am least equipped to operate in: the one in which employers only care about the hourly rate, and nothing else. I simply can’t compete with a 5$ per hour code monkey who doesn’t speak english. Employers who want to hire that guy want to hire him because they explicitly don’t want to hire me at a higher rate. My advantages in traditional markets are not as advantageous in the online freelancing market:

  • I have a breadth of knowledge and experience that most people don’t. This is offset in the online market because one can hire a different individual for $5/hour for as many skills as you need, and you might still be able to underbid me.
  • I have experience in the defense world. Defense contractors and embedded systems developers have structural impediments to playing in the online contracting market
  • I have a strong educational background that plays well into particular application domains that don’t typically present themselves in online markets

Coupled with this, I have two very important (although linked) disadvantages: living expenses are higher here than in most developing countries, and the labor market rates in my market here are much higher than what is available in online markets.

Don’t get me wrong – I’m not whining (ok, just a little bit), but I think I’ve finally come to the conclusion that online freelancing markets are just not for me. Sadly.

Posted in Uncategorized | Tagged , | Leave a comment

Lambdas in data-binding expressions

For a recent project, I wrote a templated web part (similar to the Smart Part) to do a search on a custom list and display the results (all using AJAX updates made available by the UpdatePanel), and I found myself unable to accomplish what I needed to accomplish in data-binding expressions inline in the user control loaded by my web part. I needed to be able to do multiple steps to condition the bound data before rendering it (for example, splitting a single field into multiple distinct fields that should be rendered differently).

For example, one of the bound fields I needed to render was a string formatted like this:

"http://www.website.com/page.aspx, Name of the Website"

I needed to render this as a link. Obviously, it won’t directly render properly. It needed to be split, so it could be rendered something like this:

<a href="www.website.com/page.aspx">Name of the Website</a>

For this simple example, you can do it all inline without lambdas by taking advantage of the ability to pass an array as a params parameter:

String.Format("<a href=\"{0}\">{1}</a>",
              ((SPListItem)Container.DataItem)["WebPage"]
               .ToString().Split(new string[] { ", " }, 
                                 StringSplitOptions.RemoveEmptyEntries))

However, for a more complex case in which the bound data needs conditioning that can’t be done inline, typically you would do your conditioning and rendering in server side code1.

The problem with that is that you then have to write a bunch of methods that kind of pollute your other classes, and your logic is divided (some in the user control, some in a class). Enter lambdas.

Data-binding expressions are somewhat restrctive in that they can only evaluate DataBinder.Eval and DataBinder.Bind. DataBinder is the context of the expression. However, you can create a new context by writing an inline lambda, like this:

() => 
{
  string url = ((SPListItem)Container.DataItem)["WebPage"].ToString();
  string toks[] = url.Split(new string[] {", "},
                      StringSplitOptions.RemoveEmptyEntries);
  return String.Format("<a href=\"{0}\">{1}</a>", toks[0], toks[1]);
}

Now you can see more clearly what it is you’re doing, and as an added bonus, you can actually set breakpoints on each individual statement and inspect local variables (an option that’s unavailable with chained expressions).

There’s a problem, though. The result of this expression is a lambda, not a string. When the page is hit and the server tries to compile this, it may fail. Even if it didn’t, the rendered result would not be desirable2. Somehow, we have to cause this method to be invoked.

My suggestion is to write a server-side method for invoking the lambda.

public delegate string DbExprDelegate();
 
public static string DbExpr(DbExprDelegate lambda)
{
  var obj = lambda.Invoke();
  return obj;
}

To get the type system to go along, you have to define a delegate type for the lambda. In my case, I knew I would be taking no arguments and returning no strings. For more complex cases you may be able to make good use of generics and type inference to write a more useful generalization. Since lambdas are implicitly convertible to compatible delegates, you can then use the static method DbExpr in your data binding expression to invoke the lambda. I defined DbExpr in my web part class, but I could have put it just it just about anywhere because my user control has no reference to the web part object that loads it, which requires my DbExpr to be static. In other contexts where data binding expressions are used, you may have references to instance objects in your context and may not need to make your DbExpr static.

Finally, we now have the plumbing we need to make the data binding expression work:

<%@ Assembly Name="MyAssembly, etc, etc." %>
<%@ Import Assembly="MyAssembly" Namespace="MyNamespace" %>
<asp:Repeater ...> <!-- or some other data binding control -->
<%# 
MyWebPartControl.DbExpr(() => 
  {
    string url = ((SPListItem)Container.DataItem)["WebPage"].ToString();
    string toks[] = url.Split(new string[] {", "},
                              StringSplitOptions.RemoveEmptyEntries);
    return String.Format("<a href=\"{0}\">{1}</a>", toks[0], toks[1]);
  })
%>
</asp:Repeater>
  1. Of course, the term “server-side” here doesn’t imply that inline expressions are executed on the client – they aren’t. It’s just a way to distinguish between code organized into class libraries and inline expressions embedded into ASP.NET files, like user controls. []
  2. the lambda’s .ToString() method would be called, and the result would be what the end-user would seee []
Posted in Uncategorized | Tagged , , , | Leave a comment

Chrome OS: Much Ado About Nothing

Folks, let’s don’t get too excited about Google’s announcement of a “Chrome” operating system. They’ve already said that it’s oriented toward netbooks and other entry-level PC devices, and that most of the interactivity will be web-based.

In other words – it’s not a desktop operating system as you understand the term. It’s not going to compete with windows. It’s going to be a glorified browser than doesn’t need Windows to run.

Is that significant? Yes. But it’s not a Windows (or Linux)-killer.

Posted in Uncategorized | Tagged | Leave a comment

Sololance

I’ve got a full-time job with benefits, and it’s a blessing to me and my family because it allows me to provide for them in full. We want for nothing, really.

However, I do someday want to start my own business. I have some ideas, but it will take time and even if I bootstrap and patiently build something worthwhile, it will still take money. I’ve been looking for small but worth-it side jobs to do to generate a little cash for a new business (e.g., purchase a license for basic developer tools, pay for basic hosting, etc). I’ve registered at a few of these “freelancer” work websites, thinking that if I fish around, I shouldn’t have much trouble picking up a job a month and netting an extra $100 or so to put away for a startup. Seems like a reasonable way to start bootstrapping something, no?

You know the sites: sologig.com, elance.com, guru.com, and most recently, werkadoo.com.

I’m entirely frustrated. Werkadoo looked promising, but since it launched it looks like a huge flop – I can only ever find one job listing, and it’s a very generic “send us your resume and we’ll be in touch” kind of listing. Not only that, but a full membership on Werkadoo costs $20/month. Sorry. Cash flow is the whole reason to do it in the first place, and when I really only envison doing 1 job per month, a cost of $20/month just to be there is not doable. Sologig.com? Just a feeding ground for employment companies and recruiters – which I’m entirely uninterested in. Sologig is a waste of time.

Don’t even get me started on rent-a-coder and get-a-freelancer and the like. Most of the projects are junk, and the employers that post non-junk projects are swarmed by bidders who can’t communicate, have no idea how to bid a project, or both.

Elance.com and guru.com are probably the best remaining options. Both have free membership options, but they limit your ability to bid and communicate with clients. They aren’t as bad as the other sites, but they still have their share of junk projects (you know the kind: “I need a complete clone of eBay for $500″). Those two sites don’t seem too bad, but bidding on a free membership plan is difficult because you’re not allowed to ask questions, and the projects posted are rarely detailed enough to elicit a complete bid. I’m basically structurally unable to compete because I’m not a paid member, but it’s not economical for me to shell out for a paid membership.

The other problem I have is that most of the software development projects are web applications. I’m not incapable of doing those projects (web applications are what I do for a living now), but the ideal project is one for which I can draw on my experience, and most of my experience is in distributed systems, clusters, backend server applications, systems programming, and embedded systems. Those projects are few and far between (and only on guru.com, as far as I can tell).

*Sigh*. Does anyone else have this problem? I want to find small, discrete chunks of work to do to make a little extra cash on the side, and in principle, the freelancing sites seem like a good avenue for finding such work, but in practice, they aren’t working well.

What is a freelancer with minimal availability to do?

Posted in Uncategorized | Tagged , , | 1 Comment

A simple weblocks application

Introduction

I’ve been following the Weblocks web framework for about a year now, and it’s my opinion that Weblocks is the best Common Lisp web framework available. One of the difficulties with Weblocks right now is a lack of documentation and example code. This will be a contribution to that need. I have created a simple blog application based on Weblocks. I started with the blog that Evan Monroig put together as a simple tutorial, but wanted to go a little further with it. This will be a multi-part screencast tutorial, but rather than spreading it across multiple posts, I”ll just update this one as I have new material to post. I apologize in advance if there are very dry moments in these videos; I’m new to screencasting and am using free tools. You get what you pay for :-)

So let’s start…

Setup

I use the Linux-Emacs-Slime-SBCL stack for the screencasts.  Slime, Emacs, and SBCL are functional on Windows, but I don’t know that Weblocks (or, more importantly, it’s dependencies) will.  I haven’t tried.  I prefer Franz’ Allegro Common Lisp, but Weblocks is targeted specifically at the Hunchentoot webserver. There is a very good Lisp environment screencast by Marco Berringer, and an accompanying reference by Peter Christensen.  I highly recommend them for learning more about how to get your Lisp environment set up. There are many other references on the web that will help you with getting Lisp set up on your machine.  I use the Gentoo distribution of Linux.  Gentoo has a very good package management system called “Portage”, which works something like the FreeBSD/OpenBSD ports system, but better (there are also drop-in replacement systems that improve on Portage in different ways, but I haven’t had a need for them; Portage works just fine for me).  One of the more recently developed features of Portage is the concept of “overlays”, which provide the ability to more easily customize the set of packages available at a particular location or over a particular subset of categories.  There is a Common Lisp overlay that provides fairly up-to-date releases of most of the useful Common Lisp libraries available (the overlay contains 200+ ebuilds), and I’ve found that to be adequate for my needs.  Thus I avoid all the teeth-gnashing over Lisp library management and installation. For me, I just needed to install Emacs, Slime, paredit, SBCL and finally Weblocks (which pulls in a lot of other libraries as dependencies) through the portage system.  The only thing I needed to do to make things work was to edit my .emacs and .swank.lisp files.   My .emacs file needed this:

(load "/usr/share/emacs/site-lisp/site-gentoo" nil t)

and my .swank.lisp (the user lisp initialization file for swank, the Slime backend).

(defun project-setup ()
  (mapcar #'(lambda (path)
              (push path asdf:*central-registry*))
          '(
            #p"/home/bc/proj/weblocks-dev-aggieben/"
            #p"/home/bc/proj/simple-blog/"
            )))
(project-setup)

As you can tell (hopefully), the PROJECT-SETUP function merely adds my local project directories to the ASDF path so I can simply use (require :simple-blog) to load my application (SBCL’s REQUIRE function uses ASDF to find packages). The two directories I’m adding to the ASDF path are key to this tutorial:  I am a Weblocks contributor (barely), so I run a local development version of Weblocks in my project directory.  Most users can just install Weblocks however is best supported by their Linux distribution, through ASDF-INSTALL, or manually.  This tutorial was created and tested on the latest development version.  It may or may not work on the last stable (0.8, I think).  Also, I added the ‘simple-blog’ directory to the list because that’s where we will create our application.

Creating a Weblocks Application

After making sure my Lisp environment is set up, I can start everything up:

At this point, we have the skeleton of a Weblocks application. It only writes a little text on the site, but all the plumbing is in place. Since we’re building a blog, we need to design a simple object model to represent it:

Now we have our object model in place and have shown how to use gridedits and default views for viewing and data entry, as well as the limits of the default views. In order to successfully input data, we need to customize the form views:

Now that we have data entry working, we need to build more interesting pieces of our blog. Firstly, how do we display a post to a user? We can write a custom widget to do that:

We’d like to be able to show more than one post to a user. We’ll construct a custom widget to represent the blog itself:

Check back for updates….when I get time, I’ll post screencasts to demonstrate further improvements to the blog, including graphical styling, navigation, author login, and tags.

Posted in Uncategorized | 10 Comments