Browse by Tags

Tagged Content List
  • Blog Post: Uses and misuses of implicit typing

    One of the most controversial features we've ever added was implicitly typed local variables , aka "var". Even now, years later, I still see articles debating the pros and cons of the feature. I'm often asked what my opinion is, so here you go. Let's first establish what the purpose of code is in the...
  • Blog Post: Danger, Will Robinson!

    As long-time readers of this blog know, I am often asked why a particular hunk of bad-smelling code does not produce a compiler warning. "Why not?" questions are inherently hard to answer because they turn causation on its head; normally we ask what caused a particular thing to happen, not what caused...
  • Blog Post: All your base do not belong to you

    People sometimes ask me why you can’t do this in C#: class GrandBase { public virtual void M() { Console.WriteLine("GB"); } } class Base : GrandBase { public override void M() { Console.WriteLine("B"); } } class Derived : Base { public override void M() { Console.WriteLine("D"); base.base. M(); // illegal...
  • Blog Post: Graph Colouring With Simple Backtracking, Part One

    As regular readers know, I'm interested in learning how to change my C# programming style to emphasize more concepts from functional programming, like use of immutable rather than mutable data structures and use of declarative control flow like LINQ queries instead of imperative control flow in the form...
  • Blog Post: Making the code read like the spec

    As I mentioned a while back, there are some bugs in the compiler code which analyzes whether a set of classes violates the “no cycles” rules for base classes. (That is, a class is not allowed to inherit from itself, directly or indirectly, and not allowed to inherit from one of its own nested...
  • Blog Post: Style follows semantics

    Which is better style? bool abc; if (Foo()) abc = Bar(); else abc = false; vs bool abc = Foo() && Bar(); ? To me, this comes down to the question “is Bar useful solely for obtaining its value, or also for its side effects?” The stylistic choices should typically be driven by a...
  • Blog Post: Continuing to an outer loop

    When you have a nested loop, sometimes you want to “continue” the outer loop, not the inner loop. For example, here we have a sequence of criteria and a sequence of items, and we wish to determine if there is any item which matches every criterion: match = null; foreach(var item in items) { foreach...
  • Blog Post: Always write a spec, Part Two

    Upon submitting that specification for review, even before seeing my code, Chris found a bug and an omission. The omission is that I neglected to say what happens when the ref variable is an index into a fixed-size array buffer. As it turns out, that case is also rewritten as a pointer dereference...
  • Blog Post: Always write a spec, part one

    Joel had a great series of articles many years ago about the benefits of writing functional specifications , that is, specifications of how the product looks to its users. I want to talk a bit about technical specifications, that is, a specification of how something actually works behind the scenes....
  • Blog Post: Events and Races

    Here’s a question similar to one I saw on stackoverflow the other day. Suppose you have an event: public event Action Foo; The standard pattern for firing this event is: Action temp = Foo; if (temp != null) temp(); What the heck is up with that? Why not just call “ Foo() ” ? First off...
  • Blog Post: Good Names

    Imagine a door with an unusual handle. The handle is five feet off the ground and rotates upwards to open the door. The door has no lock. Is this a good door design? Sorry. That’s not an answerable question. The purpose of almost every door is to prevent something from going through it without preventing...
  • Blog Post: Every Problem Looks Like A Nail

    I wish all the questions I got were this straightforward: “I need to compare two strings for non-culture-sensitive equality. I notice that there are methods String.Equals and String.Compare which can both do that. What is the guideline on which one I should use?” I’ll answer your question, but...
  • Blog Post: Santalic tailfans, part two

    As I have said before many times , there is only one sensible way to make a performant application. (As an aside: perfectly good word, performant, deal with it!) That is: Set meaningful, measurable, customer-focused goals. Write the code to be as clear and correct as possible. Carefully measure your...
  • Blog Post: Properties vs. Attributes

    Here is yet another question I got from a C# user recently: I have a class that represents a business rule. I want to add some rule metadata that could be used by consumers to retrieve a friendlier rule name, description, and anything else that makes sense. Should this information be exposed as an attribute...
  • Blog Post: Future-Proofing A Design

    Last time on FAIC a user asked for guidance on the potential pitfalls of refactoring an automatic property into a regular explicit property. This is just an example of a far more general problem: how can we design programs so that they are easy to get right when things inevitably change in the future...
  • Blog Post: Automatic vs Explicit Properties

    Here's a question I got from a C# user last year, a question I get fairly frequently: User: With “regular” explicit properties, I tend to use the private backing field directly from within the class. Of course, with an automatic property, you can’t do this. My concern is that in the future, if I decide...
  • Blog Post: Arrays considered somewhat harmful

    I got a moral question from an author of programming language textbooks the other day requesting my opinions on whether or not beginner programmers should be taught how to use arrays. Rather than actually answer that question, I gave him a long list of my opinions about arrays, how I use arrays, how...
  • Blog Post: Vexing exceptions

    Writing good error handling code is hard in any language, whether you have exception handling or not. When I'm thinking about what exception handling I need to implement in a given program, I first classify every exception I might catch into one of four buckets which I label fatal , boneheaded , vexing...
  • Blog Post: High maintenance

    The other day I went to buy some snack from the snack machine in the kitchen. The snack I wanted was in slot B-10, so I put in my coins, press B - one - zero, hey wait a minute there's no zero button! And why is it serving me up the snack on the left end of the machine instead of the right? Aha, there...
  • Blog Post: Reading Code Over the Telephone

    In my youth I once attended a lecture given by Brian Kernighan on the subject of code quality, which was very influential on my attitudes towards writing legible code. One of the things that Kernighan recommended was to endeavour write code that was so clear that it could be easily read over the phone...
  • Blog Post: Translating intentions and mechanisms

    Before I get into today's blogging, a quick note about my recent post on How To Not Get A Question Answered . That was certainly not intended to be fishing for compliments or chiding people for never acknowledging help ten years ago; that said, I appreciate both. Thanks to everyone who made thoughtful...
  • Blog Post: Bad Names

    Over the past year and a half of working on the C# compiler we’ve refactored a lot of crufty old code. But a surprising number of the “refactorings” have actually simply been renaming a structure, class, or method to something more sensible. The compiler is showing its age; there are a lot of structures...
  • Blog Post: Should we produce warnings for unused/uninitialized internal fields?

    You may have noticed as you've been developing C# applications that if you have an internal field of an internal type which you never use, we give you a warning about it. In fact, there are a number of warnings that we give for such fields, depending upon whether the field was never read or written,...
  • Blog Post: Write-only variables considered harmful? Or beneficial?

    I haven't forgotten that I promised to describe one more place where we insert an explicit conversion automatically. But before that, a quick digression. Reader Phil Haack has written some good tips for writing clear code and quotes an old post of mine . However I thought that I would talk a bit about...
  • Blog Post: Error messages: diagnostic is preferable to prescriptive

    The new LINQ features are going to create new failure modes for the compiler, so we're going to need to create some new error messages. The compiler development team got together the other day to discuss what makes an error message good or bad. I thought I'd share with you guys what we came up with....
Page 1 of 2 (29 items) 12