1. Stupid .NET Tricks #11

    February 26, 2008 by Craig

    TextWriter is an abstract class that has heavily-overloaded Write() and WriteLine() methods for all of the more basic types such as Int32, Boolean, and String. The idea is that it takes these values, converts them to character representations (given a specified Encoding), and then outputs the results (usually to a StringBuilder or a Stream). It’s equivalent to Java’s PrintWriter. However, unlike PrintWriter, there’s no underlying output source / OutputStream: you’re free to do whatever you like with the resulting characters.

    The stupidity lies in the interface and it’s accompanying documentation. TextWriter is an abstract class, but the only abstract member is the Encoding property’s get accessor; you get to decide how and when you want the Encoding to be specified. All of the Write() and WriteLine() methods, however, are not abstract: they’re simply overrideable. That’s not necessarily bad, but there’s little to no documentation on what any of these 36 methods actually do (specifically, whether they call any of the other Write() methods and thus don’t need to be overridden in any subclass). For example:

    • Write(char[]):
      • “This method does not search the specified String for individual newline characters (hexadecimal 0x000a) and replace them with NewLine.” (Gee thanks. Does it also not cure cancer?)
      • “This default method calls Write and passes the entire character array.” (If it’s passing the entire character array to Write(), doesn’t that mean it’s recursive? I can guess that they mean it passes each member of the array to Write(char), but that’s not what the documentation actually says.)
    • Write(Decimal): “dd” (And that’s all it says.)
    • Write(Double): “The text representation of the specified value is produced by calling ToString.” (But once you do that conversion from Double to String, where does the String go? Is it used anywhere or simply dropped?)
    • Write(String): “This version of Write is equivalent to Write .” (Yes, the URLs are identical. I’m glad to see they understand the Reflexive Property.)
    • WriteLine(char) states that it (effectively) calls Write(char) and then WriteLine(), which is exactly what it should be doing. Unfortunately, none of the other WriteLine() overloads state this; all they say is that they write the converted characters plus the newline (not necessarily calling the appropriate Write() methods). Thus you should be overriding these methods too.
    • Write(char): “This default method does nothing, but derived classes can override the method to provide the appropriate functionality.” (If it does nothing, then why not make it an abstract method and communicate the intent explicitly?)

    The documentation for the TextWriter class itself is no help either:

    A derived class must minimally implement the Write method to make a useful instance of TextWriter.

    (The link points to the list of all the Write() overloads, not one specific method.)

    All Microsoft needed to do was:

    1. Make Write(char) abstract, to show that derived classes need to do something with a character
    2. Document that Write(char[]) calls Write(char) for every element in the array
    3. Document that Write(String) calls Write(char[]) after converting the String to a char[]
    4. Document that all of the other overloads call Write(String) after calling .ToString() on the target
    5. Document that all of the WriteLine() methods call their equivalent Write() method and then call WriteLine() to write the newline character.
    6. Leave all the Write() and WriteLine() methods as overrideable, to allow for changes in behavior / optimizations.

    Then, a TextWriter implementation would only have to override one method (ie: Write(char)) and reliably get all of the conversion behavior for free. That’s almost exactly what PrintWriter already does (except that it defers to an aggregated Writer/OutputStream to write its characters rather than deferring to its own abstract method). Instead, we get this mess.

    Bonus Points: Write(Object) simply does a .ToString() on the given object, which is reasonable. Most of the other overloads (for Boolean, Double, Int32, Int64, Single, UInt32, UInt64, and probably Decimal) state that they do exactly the same thing… thus making them unnecessary. It’s clear that the .NET API designers were trying to emulate Java’s PrintWriter, which is forced to create the equivalent overloads due to the separation between primitive and Object types. Of course, if you’re implementing your own TextWriter, you still have to override all of the superfluous methods (and don’t forget the WriteLine()s!).

    Extra credit: There’s Write(String, Object) to format the Object argument given the formatting String argument (which is a very common way to write out dates and numbers in varying ways). There’s also a Write(String, Object, Object) method that does the exact same thing but with two Object arguments, and then a Write(String, Object, Object, Object) that does the same thing but with three Object arguments. There’s no method that takes four arguments though; I guess they considered that excessive.

  2. Market Capitalization

    February 25, 2008 by Craig

    What kind of world do we live in where Visa is worth only 1/3 of Yahoo?

  3. A Perfectly Cromulent Topic

    February 24, 2008 by Craig

    When does a word become a word?

    Today Laura described something as “strambotic”. I blinked when I heard this. I have a pretty big vocabulary but Laura does, on occasion, use an English word that I’m not familiar with. This is pretty rare though, so I was curious to see if this was one of those times.

    So I asked her to Google it.

    Results 1 – 10 of about 180 for strambotic.

    Did you mean: estrambotica.

    Note the lack of definition link for “strambotic.” Given this Googlethority I claim that “strambotic” is not a valid English word.

    Now, could it be?

    • As I originally suspected, Laura was transliterating a valid Spanish word “estrambotica” (it means “pompous”) into English. The rules for doing this include “drop the leading “e” before an “s”” and “drop the last letter if it’s an “a” or “o””. It works a surprising amount of the time… which is why its failures are notable (and often humorous).
    • It’s not a raw invention; “estrambotica” a valid Spanish word with a defined and accepted meaning. There’s a pretty good chance that there’s similar words in Portuguese, Italian, and French.
    • There’s other cases (180 according to Google) of people using it. There’s a demand.
    • I’ve heard it said that dictionaries describe a language but do not define it. (Note that this is different than what elementary school would have you believe, and doesn’t necessarily apply for regulated languages like French). That is, once a word becomes “official” by entering the common speech, it is included in dictionaries… not the other way around. Thus, “strambotic” isn’t invalid just because it doesn’t have a dictionary entry.

    I guess “valid” or “invalid” isn’t applicable for English words. What’s really important to ask is “will people understand what I mean when I use it?” Since “strambotic” isn’t common and isn’t easy to look up, the answer is “no”, and thus you probably shouldn’t use it… at least not yet.

  4. The Power of Words

    February 15, 2008 by Craig

    “Sometimes you just need to believe in the power of words: cheap, cheap words.”

    From Australia Apologizes To Aborigines on The Onion.

  5. Wikipedia Tourism 5

    February 10, 2008 by Craig

    Elmo, as in the Muppet.

    Elmo is the only Muppet ever to testify before the U.S. Congress. At the request and with the assistance of Rep. Duke Cunningham, he testified before the House Appropriations Subcommittee on Labor, Health and Human Services and Education in April 2002, urging support for increased funding in music education.

    I guess Cheney’s hand up Bush’s ass doesn’t qualify.

  6. Wikipedia Tourism 4

    by Craig

    Platonia dilemma

    A similar game, referred to as a “Luring Lottery”, was actually played by the editors of Scientific American in the 1980s. To enter the contest once, readers had to send in a postcard with the number “1” written on it. They were also explicitly permitted to submit as many entries as they wished by sending in a single postcard bearing the number of entries they wished to submit. The prize was one million dollars divided by the total number of entries received, to be awarded to the submitter of a randomly chosen entry. Thus a reader who submitted a large number of entries increased his or her chances of winning but reduced the maximum possible value of the prize. It can be shown mathematically that one maximizes one’s average winnings in this game by submitting a number of entries equal to the total number of entries of others. Of course, if others take this into account, then this becomes a dubious strategy.

    Although the magazine had previously discussed the concept of superrationality from which the above-mentioned algorithm can be deduced, many of the contestants submitted entries consisting of an astronomically large number (including several who entered a googolplex). Some took this game further by filling their postcards with mathematical expressions designed to evaluate to the largest possible number in the limited space allowed. The magazine was unable to tell who won, and the monetary value of the prize would have been a minuscule fraction of a cent.

    In related sightseeing, I just read that a real-life experiment found that 40% of participants will take the “cooperate” (ie: irrational) option in Prisoner’s Dilemma.

  7. Alberta Provincial Election

    February 4, 2008 by Craig

    Alberta Premiere Ed Stelmach has called a provincial election for March 3. I already am quite sure who I’m voting for: Mike Robinson, Liberal Candidate for Calgary-Foothills.

    I don’t know anything about Mike personally. I don’t know a heck of a lot about the Liberals either. I do know that what I’ve seen I don’t terribly like:

    • Their theme is “The Conservatives don’t have a plan, we do.” They’re (publicly) assuming that the province needs fixing and that the government needs to take action. I don’t necessarily agree with either of these points.
    • I also take issue with specific items in their platform:
      • Health care premiums should stay. They’re easily affordable for anyone with a solid income. There’s already price reductions for lower incomes. The absurd employee demand means that nearly everyone can have an income that can support health care (rent on the other hand is another matter entirely). Eliminating health care premiums won’t create much economic value (it’ll just save the collection costs), but instead would shift the cost onto the tax base. What problem would that solve? The Liberals don’t say. It’s just a slogan.
      • Electricity (and gas) rates aren’t unreasonably high; they’re a very minor portion of our overall expenses. I certainly don’t think they should be subsidized with tax money; the Liberals don’t say how else they would lower them. If anything, the rates should be higher, as that will give incentives to reduce demand and/or find alternative energy sources (which would help fight global warming). I don’t think there’s terribly good evidence to show that re-regulating the utilities would make them more efficient; I doubt that the existing “competitors” in the market are particularly regulation-free anyway.
      • Investing royalties “now before its too late” is a straw man plus fear mongering. Is there some investment apocalypse that I’m not aware of? It’s not like royalty money is going to sit idle anyway; as the overall tax/royalty surplus grows it’ll either get spent (which is what the Liberals mean by “investing” anyway), invested in securities (which is basically what happens by default), or reduced through tax cuts (which is a very economically sound option).
      • Providing hospitals and health care workers is a very good idea, but doing it “now” is exaggeration. You can start building & training now, but it won’t pay off for years.

      I’m neutral on the greenhouse gas cap; it’s not necessarily the best solution, but it may be better than nothing at all, so if it’s achievable then it’s worthwhole.

    • So why vote Liberal? I’m not upset with the provincial Conservatives; by and large I agree with their policies, and I think they should stay in power. However, I think that any government works best when it’s kept in check. Harper’s minority government has, in general, played ball with the rest of the political spectrum because of the real threat of a non-confidence vote. They’ve effectively been prevented from screwing things up too badly. I’d like to see that sort of balance in Alberta too. The Liberals have very little chance of winning the province, but they’re the only ones who are any threat at all. Thus, I’ll support them as much as I can (without trying too hard of course; they’re not going to see a dime of my money). If in some bizzaro universe they actually stood a chance of winning the whole thing, I’d be more careful when placing my vote.