Share via


Clearest code challenge

This is a real problem I had recently.

Suppose you have a form with OK and Cancel buttons. You want the buttons to be placed in the bottom-right corner of the form. Normally you'd place them where they go, then set their Anchor properties to Bottom, Right.

However, today I'm going to place an arbitrary limitation. You can't set the positions of the button controls in the designer, you have to write it in code.

            Button _buttonCancel;

            Button _buttonOK;

            void PlaceButtons()

            {

                  this._buttonCancel.Location = ???;

                  this._buttonOK.Location = ???;

            }

I think the Cancel button should be 12 pixels from the bottom right corner. The OK button should be 12 pixels to the left of the Cancel button.

But here’s the real challenge: write the clearest code possible.

I know my solution. It’s clearer than what I would have written a few years ago, but I wonder if I could have done better. I’ll post mine in a few days, I guess.