CCC2: InitializeComponent()

Call it a Clearest Code Challenge: What should InitializeComponent() look like?  What’s the best code you can imagine, that the WinForms designer should generate?


Let’s take a simple, concrete case: a form with an OK button. The button is anchored at the bottom right.


Let’s also pretend that the WinForms team has no trouble coding solutions to intractable problems.  For example, if you propose an InitializeComponent that solves the Halting Problem, your form will still show correctly in the designer.  This should free us from worry about what code analysis tools are capable of.


My last post gives us the seed code – the solution we produce today in Whidbey. 


May the Refactoring begin!

Comments (3)

  1. Dave says:

    You ask for the best code, and code which could be intractable. So I’m assumining that you’re actually asking for the ‘most readable’ code.

    Given that assumption, I’d say promote XML to a first class language construct (X#, Xen), and render InitializeComponent as XML. That’d look pretty nice.

    But partial classes are great too. 🙂

  2. jaybaz [MS] says:

    Woo, a response!

    I didn’t mean to ask for intractable code. I’m just saying you can ignore the limitations of the designer.

    XML is probably the right answer. I had a particular way I wanted this discussion to go, and XML completely diverts it & makes much more sense.

    My goal was to motivate a discussion about OO code. I imagined creating a nested class called "OKButton" that encapsulated the properties and behaviors of the button.

    Then I wanted to talk about how you might refactor from InitializeComponent to a nested class.

    I also wanted to rant about VB’s "with" statement, and make the case that you only need "with" if you aren’t writing good OO code.

    But you outsmarted me!

Skip to main content