Showing posts with label Rant. Show all posts
Showing posts with label Rant. Show all posts

Monday, May 4, 2009

Rails is awesome ... too bad the creater and much of the community are total dickwads.

In a moment of genuine unintended irony, I decided to start learning Rails at the exact moment that a huge uproar happened over a conference presentation that contained pornographic images and the following "don't hamper my freedom from expression/to look at naked chicks" response from much of the Rails community. Worse yet, DHH, creator of Rails and partner in 37 Signals, condoned the talk.

Honestly, I've mostly been avoiding going in depth into it because I know how much it would piss me the fuck off and turn me off from Rails and feeling safe and comfortable asking questions and just generally participating in the community. {:thought_bubble => 'fuckers' }

But recently there was some discussion on Systers (thank goodness for Systers!). I still am not really ready to formulate thoughts on this ... partly because I don't really feel like I'm part of the Rails Community at all (fyi - this didn't help and doesn't really give me much hope that I ever will be). Instead I'll just link to other people's thoughts on it.

Scott Hanselman's Computer Zen - one of the first posts I saw on the subject

The ladies respond ...
http://www.sarahmei.com/blog/?p=46 (contains slides and DHH's comments)
http://www.ultrasaurus.com/sarahblog/2009/04/gender-and-sex-at-gogaruco/ (read the comments - she has some good stuff to say about women adopting ruby)
http://dyepot-teapot.com/2009/04/25/dear-fellow-rubyists/ (really great thoughts on negotiating gender and sexuality in a male-dominated field)*
http://lizkeogh.com/2009/04/29/i-am-not-a-pr0n-star-avoiding-unavoidable-associations/ (really good thoughts on the affects of associations - subconscious or otherwise)**
http://www.blogher.com/tipping-point-women-tech-heres-hoping (good summary)
http://hackety.org/2009/04/29/aSelectionOfThoughtsFromActualWomen.html (summary of women's thoughts. my fav "What about a presentation about writing code on deadline: 'Delivering Like a Birth Mom.'")

On both the up and down side, Mike Gunderloy resigned from Rails Activists. On the one hand, I'm really happy that there is an important male figure in the Rails community who thinks this is serious. On the other hand, I can only imagine the kind of private conversations he had with other members of the community causing him to believe that "the difference between their opinions and mine is so severe that I cannot in good conscience remain a public spokesman for Rails." On another up side, the comments on this post were really supportive and understanding.


Last but not least, DHH has a post on getting more women into Rails. I agree that " simply refraining from having saucy pictures of pole dancers is going to do the trick". But let's not pretend that it won't help.



-------------
Ok. I can't help. I'm commenting:
* A commentor said this:
"Ruby, like most/all (?) current software projects is male dominated. This may or may not be because of an essential difference in the interests/motivations of the sexes - but this isn’t important - the only factor is the competence and passion of the individual.

Open-source projects, in particular because of their distributed nature, have values based around contributions. If you’re able to make good contributions, then regardless of your identity, you’ll do well.

This has always been my experience - coders listen to people whose code they respect."


As much as I wish I could, I know I will never ever be able to convince some dude who believes that just how fucking wrong they are. The fact is priviledge is really really hard to see and until you have coded something awesome and gotten no credit or had a guy you worked with get/be given all the credit or say something in a meeting only to be totally ignored until some guys says the exact same thing and everyone is like 'totally that's such an incredibly brillant idea', you won't get it. But maybe, if you're one of those people who believe that your coding is all that matters (not to mention the barriers to entry to be any kind of coder let alone a good one), you could just try really hard to pay attention to how you are treated vs how the women in the room are treated.


** I've seen in a couple places Matt (the presenter's) response was: “I would have hoped that people who were likely to be offended would have simply chosen not to attend my talk or read my slides on the internet”. The extent to which that is un-fucking-acceptable kills me. Who is most likely to be offended by this talk? Could it be the people whose gender is naked in half the slides? So I shouldn't get a chance to learn a technology (that itself isn't offensive) at a conference I've paid to go to to learn, because you're a frat-boy who doesn't know how to behave appropriatley in public? Again, this is just another example of the ways in which things like this discourage women from technology.


Monday, May 12, 2008

DBNull is my Nemisis: Part Deux

******
This is the second part of my rant about DbNull. Go here to read the first part.
******

Ok. So now you have a utility method that lets you check for null whether it's null, DbNull, or, if bound to a datagrid, " "

What if you want to Convert that object, which might be null or DbNull, to a Type? What if you'd like to write it in pretty code? Well then you'd use this lovely method:

public static T ConvertToType(object o)
{
if (o == DBNull.Value || o == null)
return default(T);
return (T)o;
}


There are few things to note about this method. The first is that it will throw an exception if o can't be cast to type T. This very possibly might be what you want.

The second thing to note is the use of default(T). The default method is a useful little thing that allows .Net Generics to work with both value (structs) and reference (class) types. Basically, default(T) will return the default value of a value type, i.e. 0 for int or System.Int32, or null for a reference Type.

What if you don't want the method to throw an exception when converting? What if you'd like to use the equivalent to the "as" operator?

You might replace the last line of the method with this:

return o as T;


Then you would get a compiler error. Why? It's quite simple really. The "as" operator only works on reference types because only reference types can be null. One solution to this problem is to return default(T) if the casting fails.

public static T ConvertToTypeOrDefault(object o)
{
try
{
return ConvertToType(o);
}
catch (Exception e)
{
//this will return null for nullable types & the default value for non-nullable types
return default(T);
}
}


However, what if you really really want the convert method to return null. The short answer is that you can't; the long answer is that you can. :)

While you can't make a method that could return value types return null, you can force the method to only take certain Types using Generic constraints. In this case, we'd want the method to only accept reference types.

I tried to get this to work using a variety of methods until my trial and error finally ended in success. First, I tried returning T? (i.e. Nullable) but since reference types are already Nullable and Generic Nullable class only takes value types. Bah. So I tried restricting T to System.Nullable. Nope - static classes can't be used as constraints. Then, I tried restricting T to System.Object. Nope - Object is a special class and can't be used in this way. (Awww, it's special) . Next, I tried constraining T to new(), which essentially constrains T to classes that have a default constructor. Still nada - constraining to new() had no effect on the compiler erroring when I tried to return null.

Finally, after skimming the MSDN introductory article on Generics, I landed on the solution! T can be constrained by "class" (and conversely, by struct).

public static T ConvertToTypeOrNull(object o) where T : class
{
if (o == DBNull.Value)
return null;
return o as T;
}



Viola!

It's true that the ConvertTypeToDefault method is essentially the same for ConvertTypeToNull when T is a class. One advantage of using the ConvertTypeToNull is that it doesn't require the overhead of exception handling.

Thinking about default and value types brings up another issue though: What if you want to know if something is null OR if it's the default value. In other words, you are converting the Potential-DbNull object to a struct and want to know if it has no value.

You could add an additional method:


public static bool isNullOrDefault(object o)
{
bool bIsNull = isNull(o);
if (!bIsNull && o is ValueType)
{
bIsNull = ConvertToType(o).Equals(default(T));
}
return bIsNull;
}

public static bool isNullOrDefault(object o, bool webBound)
{
bool bIsNull = isNull(o, webBound);
if (!bIsNull && o is ValueType)
{
bIsNull = ConvertToType(o).Equals(default(T));
}
return bIsNull;
}


Something to note is that I'm not using the == operator to compare the default type with what's returned by ConvertToType. Why? Simple - the compiler doesn't let me. Again, because at compile-time the type that T represents is ambiguous, the compiler doesn't know if T supports the use of the == operator so it complains. Fortunately, the Java style .Equals method, while a bit ugly, does the trick just fine.

Also, the above method will throw an exception if the object can't be converted to the type. It seems logical to me, but you could use a different ConvertToType* method if you wanted.

(If you want to know more about what the webBound bool is for, check out my first post on DbNull.)

So there you have it! A DbNullUtility class to make your life just a little bit easier. Hooray for me and fuck DbNull! (Bad Religion anyone??)

Saturday, May 10, 2008

DBNull is my Nemisis

Despite what I told interviewers when looking for a new job last summer, I definitely prefer C# over Java. Having learned to program mostly in C++ with all it's (dangerous) flexibility, the fact that Java did not implicitly convert primitives to their object counter-parts (i.e. double and Double) - not to mention the endlessly long names - filled me with endless internal rage. Endless. My rage was especially acute when dealing with Java web services talking to C# web services. I'm angry just thinking about it.

C# seems to be a happy medium between the hand holding of Java and the cryptic compiler errors of C++. (Oh those compiler errors! I think fondly back on the time I continued to get a vTable error even after removing all code from the class file.) I do miss operator overloading but I generally am able to live without it ... except in one very important circumstance: DbNull.

I hate DbNull with a passion not quite reaching my hatred for no primitive/object conversion in Java but relatively close. I'm sure it's because it reminds me of dealing with this annoying aspect of Java. If you are unfamiliar with this lovely aspect of the C# language, I hope you stay blissfully ignorant and stop reading now. However, in case you've decided against ignorance, DbNull is the object that is returned when a database cell is null or, in the helpful words of MSDN, "Represents a null value." That's right. DbNull is null. Now, as a logical person, you'd think that then this statement would evaluate to true:

//where the cell in the DataRow dr is null
if(dr[0] == null) {
... do something
}


You'd think it would evaluate to true. You'd think that the committee of dudes (and hopefully, some ladies!) would have thought that, in this instance, we should override the Equals method and make DbNull == null always evaluate to true.

You'd think so but you'd be wrong.

This means that any time you are checking for nulls in something populated from a database, you must check for equality with DbNull.Value. I guess this is ok if everything came out of the database and was never changed, but perhaps that's not the case; perhaps some of the data is from the database and some isn't; perhaps an action such as casting with 'as' is happening on some fields. Or perhaps you'd simply like to use the ?? operator instead of less readable ? : syntax. If you want to do any of those things or if you're just a general fan of logic and non-bloated, readable code, well too bad for you. Write ugly code or scrap your idea.

So I decided to rectify this situation as best I could by writing a DbNull Utility class. I've had this idea floating around in my head ever since I first encountered this bullshit, but haven't gotten around to doing anything about it until now. On the upside, in working on this little project, I got a chance to delve a little more into C# Generics and that was exciting.

The first order of business is to write a method that checks if an object is null or DbNull:


public static bool isNull(object o)
{
return (o == DBNull.Value || o == null);
}


Easy enough.

Maybe you decide to test this out. Maybe you use ASP .Net and a datagrid because it seems like a simple utility test. Maybe you write an ItemDataBound method to change the value of a cell when it's null. Maybe you run the code and find that the value isn't changed. Then you think "What the fuck?"

Little did you know that DbNull's stupidity continues. When a DbNull value is bound to a datagrid, it's value is changed to " " I'll admit, I do understand the logic behind this on some level - it ensures that the cell renders properly in all browsers (ahem, netscape). Nevertheless, I spent many an hours trying to figure out what the hell was going on. This was back in my days of VB .Net using .Net 1.1. To make it even awesomer, converting the object to a string and checking for equality with " " didn't work. Oh no, you had to convert it to a Char Array and check for length and each element. In .Net 2.0 that no longer seems to be the case, but I've included a check for this behavior anyway. (Note: I can't remember - and am too lazy to go dig up the code - if null is bound with the trailing semi-colon or not so I check either way. Feel free to leave this out.

public static bool isNull(object o, bool webBound)
{
if (webBound)
{
string s = o as string;
if (s != null)
{
if (s == " ")
return true;
Char[] c = s.ToCharArray();
if ((c.Length == 5 || (c.Length == 6 && c[4] == ';')) &&
(c[0] == '&') && (c[1] == 'n') && (c[2] == 'b') && (c[3] == 's') && (c[4] == 'p'))
return true;
}
}
return isNull(o);
}



Since this post is getting a bit lengthy (and because I fear I'm coming off as a rage-aholic), I'm going to end this post here and continue with the methods that convert to a type in the next post.