My own private WTF?

Thursday 31 March 2005This is more than 18 years old. Be careful.

As I’ve mentioned, I’ve taken over responsibility for a body of C# code. The layout code is filled with stuff like this:

this.Size = new Size(this.Size.Width,
   this.widgetsLabel.Location.Y + this.widgetsLabel.Size.Height);

To paraphrase Tevye, would it have spoiled some vast eternal plan to write this instead?:

this.Height = this.widgetsLabel.Bottom;

I spent quite some time today applying shrinking transformations:

x.Size.Height                             -->  x.Height
x.Location.Y                              -->  x.Top
x.Top + x.Height                          -->  x.Bottom
x.Size = new Size(x.Width, y);            -->  x.Height = y;
x.Location = new Point(x.Location.X, y);  -->  x.Top = y;

Believe me, it’s much more readable afterwards! Of course, I’ve a sneaking suspicion that a lot of this layout code could just be chucked by setting proper constraints in the forms designer! To quote another movie character, I can’t think about that today, I’ll think about that tomorrow.

I’ve slightly edited this post to remove asides about developers that were distracting from my point. Reactions from readers about those comments have also been removed.


Looks like their code was generated by a GUI page design mode.
"You are in a maze of twisty little passages, all different."
If you try to set widget.Location.Y I think it complains that Y is a read-only property, as I recall. Perhaps whoever wrote this assumed that the same would apply to widget.Top and never checked once they had found an incantation that works. I think I've done the same with some of this widget stuff.

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
Comment text is Markdown.