这种重构有什么好处吗?
有一个类通过每个表有 1 个 string[] 变量来定义各个表中的主键。例如:
static string[] my_table_foo_TablePrimaryKeys = new string[] { "primary_key1", "primary_key2" }
static string[] my_table_bar_TablePrimaryKeys = new string[] { "user_id", "customer_number" }
我发现这有点混乱,并且对于我们稍后添加第三个表并且我们想要回到此类来定义新的第三个表的主键的情况来说,这有点混乱并且可扩展性较差。所以,我将其重构为如下所示:
static Dictionary<string, string[]> tablePrimaryKeys = new Dictionary<string, string[]>()
{
{"my_table_foo", new string[] { "primary_key1", "primary_key2" }},
{"my_table_bar", new string[] { "user_id", "customer_number" }}
};
你们认为这是一个不错的重构更改吗?为什么?
此外,在我看来,引用主键的地方也更干净一些。示例:
在前一种情况下:
DoStuffWithPrimaryKeys( my_table_foo_TablePrimaryKeys, "other stuff", 1000 );
在后一种情况下:
string[] keys = tablePrimaryKeys["my_table_foo"];
DoStuffWithPrimaryKeys( keys, "other stuff", 1000 );
如果有人还想提及“可接受的重构”的原则是什么,以及如何知道什么可以重构,什么不可以,那将是伟大且非常有教育意义的。
我正在使用 C# 和 .NET 3.5。
There is a class that defines primary keys in various tables by having 1 string[]
variable per table. For example:
static string[] my_table_foo_TablePrimaryKeys = new string[] { "primary_key1", "primary_key2" }
static string[] my_table_bar_TablePrimaryKeys = new string[] { "user_id", "customer_number" }
I find this to be a bit messy and less extensible for cases where we add a 3rd table later and we want to come back to this class to define the primary keys for that new 3rd table. So, I refactored it to look like this:
static Dictionary<string, string[]> tablePrimaryKeys = new Dictionary<string, string[]>()
{
{"my_table_foo", new string[] { "primary_key1", "primary_key2" }},
{"my_table_bar", new string[] { "user_id", "customer_number" }}
};
Would you guys consider this a decent refactoring change? Why?
In addition, it also is a little cleaner in my humble opinion where the primary keys are referenced. Example:
In the former case:
DoStuffWithPrimaryKeys( my_table_foo_TablePrimaryKeys, "other stuff", 1000 );
And in the latter case:
string[] keys = tablePrimaryKeys["my_table_foo"];
DoStuffWithPrimaryKeys( keys, "other stuff", 1000 );
If anyone also wants to mention what the principles are for "acceptable refactoring" and how to know what is acceptable to refactor and what is not, that would be great and very educational.
I'm using C# and .NET 3.5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对这种变化并不太着迷。这两种情况对我来说都很可疑,但在第一种情况下,至少你可以从编译器那里获得一定程度的安全性。只要变量初始化时的键拼写正确,无论何时使用它们,它们都会按预期工作。
重构后,您将在任何地方使用字符串,任何地方的拼写错误都会导致运行时错误。
I'm not too crazy about that change. Both cases looks dubious to me, but in the first case at least you get some amount of safety from the compiler. As long as the keys are spelled correctly where the variables are initialized, they will work as expected whenever they are used.
After you refactored you will be using strings everywhere, any a spelling error somewhere will cause a runtime error.
这些对我来说都不好看。我不知道您的系统的要求是什么,但我会在这里查看更大的图景,看看是否有更好的方法来完成此任务。至于这里的重构,我不会打扰,并没有真正为更改的风险增加任何大的好处。
Neither of these look great to me. I don't know what the requirements of your system are, but I'd be looking at the bigger picture here to see if there is a better way to do this altogether. As for the refactoring here, I wouldn't bother, not really adding any big benefit for the risk of the change.
我想说这不是更好或更糟。您只是引入魔术字符串而不是变量名称。
I would say its not better or worse. You are just introducing magic strings instead of variable names.