Hi all,
There's a certain amount of pride one gets from writing some concise code: that is, the least amount of repetitious and superfluous code that achieves the same thing.
I wrote this simple event block attached to a Button
and then realised that if written like this:
It's vastly more simple, and yet I prefer the first example because, well, I don't know why exactly. Perhaps because the coding will scale better, even though it will never need to. Perhaps because it has a few less characters or 'code' lines.
My question is: Given the choice, which example would you tend to in your projects?
There's a certain amount of pride one gets from writing some concise code: that is, the least amount of repetitious and superfluous code that achieves the same thing.
I wrote this simple event block attached to a Button
Code:
[navy][i]// for automatic syntax highlighting see faq102-6487
[/i][/navy][b]procedure[/b] TMainForm.OptionsButtonClick(Sender: TObject);
[b]const[/b]
arg : [b]array[/b][Boolean] [b]of[/b] String = ([teal]'Hide'[/teal], [teal]'Show'[/teal]);
[b]begin[/b]
TButton(Sender).Caption := arg[TopPanel.Visible] + [teal]' &Options'[/teal];
TopPanel.Visible := [b]not[/b] TopPanel.Visible;
[b]end[/b];
Code:
[b]procedure[/b] TMainForm.OptionsButtonClick(Sender: TObject);
[b]begin[/b]
[b]if[/b] TopPanel.Visible [b]then[/b]
OptionsButton.Caption := [teal]'Show &Options'[/teal]
[b]else[/b]
OptionsButton.Caption := [teal]'Hide &Options'[/teal];
TopPanel.Visible := [b]not[/b] TopPanel.Visible;
[b]end[/b];
It's vastly more simple, and yet I prefer the first example because, well, I don't know why exactly. Perhaps because the coding will scale better, even though it will never need to. Perhaps because it has a few less characters or 'code' lines.
My question is: Given the choice, which example would you tend to in your projects?