进行代码重构时需要隐藏多少信息?
需要隐藏多少信息? 在删除记录之前,我有样板代码,它看起来像这样:
public override void OrderProcessing_Delete(Dictionary<string, object> pkColumns)
{
var c = Connect();
using (var cmd = new NpgsqlCommand("SELECT COUNT(*) FROM orders WHERE order_id = :_order_id", c)
{ Parameters = { {"_order_id", pkColumns["order_id"]} } } )
{
var count = (long)cmd.ExecuteScalar();
// deletion's boilerplate code...
if (count == 0) throw new RecordNotFoundException();
else if (count > 1) throw new DatabaseStructureChangedException();
// ...boiler plate code
}
// deleting of table(s) goes here...
}
注意:样板代码是代码生成的,包括“using (var cmd = new NpgsqlCommand( ... )”
但我正在认真考虑重构锅炉 的重构代码的方式(使用扩展方法变得更好(不是唯一的原因;))
using (var cmd = new NpgsqlCommand("SELECT COUNT(*) FROM orders WHERE order_id = :_order_id", c)
{ Parameters = { {"_order_id", pkColumns["order_id"]} } } )
{
cmd.VerifyDeletion(); // [EDIT: was ExecuteWithVerification before]
}
我希望将执行标量和样板代码放入扩展方法中。
板代码,我想要一个更简洁的代码。这就是我设想 上面的代码,是否需要代码重构/信息隐藏?我的重构操作看起来是否太不透明?
How much information hiding is necessary? I have boilerplate code before I delete a record, it looks like this:
public override void OrderProcessing_Delete(Dictionary<string, object> pkColumns)
{
var c = Connect();
using (var cmd = new NpgsqlCommand("SELECT COUNT(*) FROM orders WHERE order_id = :_order_id", c)
{ Parameters = { {"_order_id", pkColumns["order_id"]} } } )
{
var count = (long)cmd.ExecuteScalar();
// deletion's boilerplate code...
if (count == 0) throw new RecordNotFoundException();
else if (count > 1) throw new DatabaseStructureChangedException();
// ...boiler plate code
}
// deleting of table(s) goes here...
}
NOTE: boilerplate code is code-generated, including the "using (var cmd = new NpgsqlCommand( ... )"
But I'm seriously thinking to refactor the boiler plate code, I wanted a more succint code. This is how I envision to refactor the code (made nicer with extension method (not the sole reason ;))
using (var cmd = new NpgsqlCommand("SELECT COUNT(*) FROM orders WHERE order_id = :_order_id", c)
{ Parameters = { {"_order_id", pkColumns["order_id"]} } } )
{
cmd.VerifyDeletion(); // [EDIT: was ExecuteWithVerification before]
}
I wanted the executescalar and the boilerplate code to goes inside the extension method.
For my code above, does it warrants code refactoring / information hiding? Is my refactored operation looks too opaque?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想说,如果您的新单行代码替换了程序中许多地方的几行代码,那么您的重构非常好。 特别是因为所有这些地方的功能都是相同的。
追随你并查看你的代码的程序员将简单地查看扩展方法的定义来找出它的作用,现在他知道这段代码是在一个地方定义的,所以它不可能与其他地方不同放置。
I would say that your refactor is extremely good, if your new single line of code replaces a handful of lines of code in many places in your program. Especially since the functionality is going to be the same in all of those places.
The programmer coming after you and looking at your code will simply look at the definition of the extension method to find out what it does, and now he knows that this code is defined in one place, so there is no possibility of it differing from place to place.
如果必须的话可以尝试一下,但我的感觉是,这不是简洁的问题,而是你是否想每次或大部分时间强制行为。 推而广之,如果验证条件发生变化,它可能会全面改变。
基本上,减少一小块样板代码并不一定会让事情变得更简洁; 这只是开发人员必须深入了解和理解的一点抽象。
作为一名开发人员,我不知道“ExecuteWithVerify”是什么意思。 我们到底要验证什么? 我必须查一下并记住它。 但通过样板代码,我可以查看代码并准确理解发生了什么。
通过不将其简化为单独的方法,我还可以针对需要针对不同条件抛出异常的情况调整样板代码。
Try it if you must, but my feeling is it's not about succinctness but whether or not you want to enforce the behavior every time or most of the time. And by extension, if the verify-condition changes that it would likely change across the board.
Basically, reducing a small chunk of boiler-plate code doesn't necessarily make things more succinct; it's just one more bit of abstractness the developer has to wade through and understand.
As a developer, I'd have no idea what "ExecuteWithVerify" means. What exactly are we verifying? I'd have to look it up and remember it. But with the boiler-plate code, I can look at the code and understand exactly what's going on.
And by NOT reducing it to a separate method I can also tune the boiler-plate code for cases where exceptions need to be thrown for differing conditions.
当您提取或重构代码时,它不会隐藏信息。 仅当您在重构后开始限制对扩展定义的访问时,它才隐藏信息。
It's not information-hiding when you extract or refactor your code. It's only information-hiding when you start restricting access to your extension definition after refactoring.
应不惜一切代价避免类中的“new”运算符(构造函数除外)。 这就是您需要在这里重构的内容。
"new" operator within a Class (except for the Constructor) should be Avoided at all costs. This is what you need to refactor here.