Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

perl - Why is it a bad idea to write configuration data in code?

Real-life case (from caff) to exemplify the short question subject:

$CONFIG{'owner'} = q{Peter Palfrader};
$CONFIG{'email'} = q{peter@palfrader.org};
$CONFIG{'keyid'} = [ qw{DE7AAF6E94C09C7F 62AF4031C82E0039} ];
$CONFIG{'keyserver'} = 'wwwkeys.de.pgp.net';
$CONFIG{'mailer-send'} = [ 'testfile' ];

Then in the code: eval `cat $config`, access %CONFIG


Provide answers that lay out the general problems, not only specific to the example.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are many reasons to avoid configuration in code, and I go through some of them in the configuration chapter in Mastering Perl.

  • No configuration change should carry the risk of breaking the program. It certainly shouldn't carry the risk of breaking the compilation stage.
  • People shouldn't have to edit the source to get a different configuration.
  • People should be able to share the same application without using a common group of settings, instead re-installing the application just to change the configuration.
  • People should be allowed to create several different configurations and run them in batches without having to edit the source.
  • You should be able to test your application under different settings without changing the code.
  • People shouldn't have to learn how to program to be able to use your tool.
  • You should only loosely tie your configuration data structures to the source of the information to make later architectural changes easier.
  • You really want an interface instead of direct access at the application level.

I sum this up in my Mastering Perl class by telling people that the first rule of programming is to create a situation where you do less work and people leave you alone. When you put configuration in code, you spend more time dealing with installation issues and responding to breakages. Unless you like that sort of thing, give people a way to change the settings without causing you more work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...