Crazy Things I Did Before Generic Variance

public class stuff : IEnumerable<Thing>, IEnumerable<IThing> { … }
public static void Main() {
  stuff s;
  var names = s.Select(t=>t.Name);

And even though System.Linq is imported, I still get an error “stuff" does not contain a definition for Select and no extension method ‘Select’ accepting a first argument of type ‘stuff’ could be found (are you missing a using directive or an assembly reference?)

Well, no, but LINQ and extension methods on IEnumerable<T> doesn’t work when the T is ambiguous. If I were to cast the above “s” to one of IEnumerable<Thing> or IEnumerable<IThing>, then I could use LINQ, but until I do, I get nothing. The error message isn’t all that helpful either.

My general recommendation: don’t do this.

Method I Wish Was There: IEnumerable.IsNullOrEmpty

This one is a bit controversial because what I’m really saying is that I want to call an extension method on null and have it return true, rather than a Null Reference Exception. This violates the best practice of keeping null semantics on extension methods, but it makes my code so easy to write, so I don’t care, I wish it was there.

public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
  if(source ==null) return true;
  var collection = source as ICollection<T>;
  if (null != collection && collection.Count==0) return true;
  var enumerator = source.GetEnumerator();
  return enumerator.MoveNext();

I say that it makes my code writable because I can write what I’m thinking.

if (somedata.IsNullOrEmpty()) takeAction(); else takeOtherAction();

Method I Wish Was There: string.Join

Yes, I know it is there as a static, but I want it as a method on IEnumerable<string>.

public static string Join(this IEnumerable<string> source, string joiner)
            if (source.Count() == 0)
                return "";
            return source.Aggregate((a,b) => a+ joiner + b);

So that when I’ve got some strings I can join them 🙂

var logmessage = people.Select(p=>p.Firstname).Join(“, “);

Method I Wish Was There: PipeTo

Too much F# rots the brain…

Ok, maybe not, but too much F# really changes your C#. I find myself wanting to |> something that isn’t really linq-able (not IEnumerable).

After talking it over with my LINQ guru – Chris Marinos – for a good name for this operation, we both agreed that PipeTo is a good name.

public static void PipeTo<T>(this T source, Action<T> action)

Sure, its a dead simple implementation, but it means that I can do this:


Real IPv6, Here I Come

For a few months now, my entire home has been on the ipv6 internet via Hurricane Electric’s free tunnel service. It has been very cool and I’ve learned a bit about IPv6 in the process.

Today I was happy to see an email from Comcast about their IPv6 trial program. I don’t have direct IPv6 just yet, but this was the first time I had to agree to Terms of Service.

Confidentiality.  While the conduct of the Trial, the nature and quality of the Trial Service and any Trial Equipment you receive constitute Comcast confidential information, one of Comcast’s objectives is to assist the general Internet community in preparing for IPv6 and to encourage widespread IPv6 deployment across the entire Internet.  Thus, you are authorized to discuss details of the trial with non-participants, such as members of the Internet Engineering Task Force, and to post information about your participation on web-based forums, email discussion lists, social media networks, etc. However, you agree not participate in any media interviews that involves disclosure or discussion of any details of the Trial with media representatives, including but not limited to professional bloggers, print media, online newspapers and magazines, radio, and television, without the prior written approval of Comcast.

I have to admit, these terms aren’t too bad. I can blog about it, tweet about it, talk about it all I want. I can be as mean or as nice as I want. But… “no interviews” 🙂

Teaching New Programmers

I teach at a local community college. This semester I’m teaching one of a handful of sections called “Introduction to Computer Science”. This course is supposed to be a bridge course before throwing new students directly into the typical C++ Programming course.

The thought is that some students need a little extra help before taking that first C++ course. This course is intended to give them some intermediate information and some very basic programming introduction.

Since there are other sections to this course I’ve not had to make my own course material. I’ve used PowerPoint created by others. A few times I’ve had to say things like “Just ignore that bullet, its only true from a certain point of view.”

It wasn’t until the introductory C++ slides that I blew a gasket.

After a slide on “Documentation” with these bullets:

•A well-documented program is easier to understand and modify
•You use comments to document programs
•Comments should appear in a program to:
?Explain the purpose of the program
?Identify who wrote it
?Explain the purpose of particular statements

I advanced to this slide with an example of “good” comments:

int feet; //variable to hold given feet
int inches; //variable to hold given inches
int totalInches; //variable to hold total inches
double centimeters; //variable to hold length in centimeters

At this point I lost my professionalism a bit and cursed quite a lot. I ranted about how this was useless. I feel like a channeled a little bit of everyone who I’ve heard say “comments are evil”.

When I finally got to a complete demo program instead of a code snippet, It was time for conniption.

//named constants
const int CENTIMETERS_PER_INCH = 2.54;

int main() {
  //declare variables
  int feet, inches;
  // statements

For the love of programming will someone explain to me what value any of these comments have?

I can only hope that my ranting was not so extreme that students stopped listening. Hopefully they will all have a better understanding of comments and what NOT to do.

hexane-free veggie burger recipe

my aunt actually asked me for my recipe, so I figured i’d post it.

start with 1lb of non-GMO soybeans. bring them to boil with 2 quarts of water. Let simmer for 2-2.5 hrs, adding another pint of water if necessary.

Use a wand blender to puree the soybeans. Cook until soft. Let them cool.

Take 1 pint of the soybeans and add 1 finely cut onion and 3 cloves of garlic, add 1/2 cu of whole wheat flour, 1/2 cu of oats, 1/8 cu of flax meal (optional), 2 tsp salt and 1tsp of cumin. Mix thoroughly.

Form into patties and cook ’em up.

Method I Wish Was There: RegexIsMatch

I don’t like the verbosity of a couple days ago use of Regex(). That is not to say that I don’t still find the Regex extension method useful, but if I’m just matching I should say so.

var re = @“\d+-\d+-\d+-\d+”.Regex();
var match = re.Match(“things 10-12-5-1”);
//do things with match
var othermatch = re.Match(“stuff 1-6-8-9”);

Regex extension method is great. But for a Boolean statement I should be able to just say “RegexIsMatch?”

if ( someText.RegexIsMatch(@”\d+\.\d+”) ) …

Method I Wish Was There: ContainsDigit

Sometimes when dealing with lots of strings or parsing text, I don’t always want to turn directly to regular expressions. I might want to just say “if this thing has a digit”.

public static bool ContainsDigit(this string source) {
  return @"\d".Regex().IsMatch(source);

This uses yesterday’s Regex() extension method on string and simply returns true or false, exactly as it reads.

string field1 = GetFieldFromSomeParserOrSomething();
if (field1.ContainsDigit()) …