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

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

问题描述

真实案例(来自 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

提供解决一般问题的答案,而不仅仅是针对示例.

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.

  • 任何配置更改都不应带来破坏程序的风险.它当然不应该承担破坏编译阶段的风险.
  • 人们不应该为了获得不同的配置而编辑源代码.
  • 人们应该能够在不使用一组通用设置的情况下共享同一个应用程序,而只是为了更改配置而重新安装应用程序.
  • 应该允许人们创建多个不同的配置并批量运行它们,而无需编辑源代码.
  • 您应该能够在不同设置下测试您的应用程序,而无需更改代码.
  • 人们不应该学习如何编程才能使用您的工具.
  • 您应该只将配置数据结构与信息源松散地联系起来,以便以后的架构更改更容易.
  • 您确实需要一个界面,而不是应用程序级别的直接访问.

我在我的掌握 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天全站免登陆