为什么用代码写配置数据是一个坏主意? [英] Why is it a bad idea to write configuration data in code?

查看:64
本文介绍了为什么用代码写配置数据是一个坏主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现实生活中的案例(来自 caff )来举例说明简短的问题主题:

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' ];

然后输入代码:eval `cat $config`,访问%CONFIG

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

提供的答案不仅列出了具体示例,还列出了常见问题.

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

推荐答案

有很多原因可以避免在代码中进行配置,我在掌握Perl .

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

  • 任何配置更改均不承担破坏程序的风险.当然,它不应该冒着中断编译阶段的风险.
  • 人们不必编辑源代码即可获得不同的配置.
  • 人们应该能够共享同一应用程序而无需使用一组通用设置,而只需重新安装该应用程序即可更改配置.
  • 应该允许人们创建几种不同的配置并批量运行它们,而无需编辑源代码.
  • 您应该能够在不同的设置下测试应用程序,而无需更改代码.
  • 人们不必学习如何编程就可以使用您的工具.
  • 您仅应将配置数据结构与信息源松散地绑定在一起,以使以后的体系结构更改更加容易.
  • 您真的想要一个接口,而不是在应用程序级别直接访问.

我在 Mastering Perl 类中总结了这一点,告诉人们编程的首要规则是创建一种情况,使您减少工作量,而人们却让您独自一人.将配置放入代码中时,您将花费更多时间来处理安装问题和响应损坏.除非您喜欢这种事情,否则请给人们一种更改设置的方式,而又不增加您的工作量.

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.

这篇关于为什么用代码写配置数据是一个坏主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆