我已将我的问题放在评论中。该代码支持进行休息调用。
// (1) Is appending Base to the name of base classes useful?
public abstract class RestCallBase : IRestCall
{
// (2) Is there a good way to decide on the ordering/grouping of members?
// I have seen code that uses #region for this, but I feel like it's not
// pervasive.
public string Url { get; set; }
public string Method { get; set; }
public string PostData { get; set; }
public string ResponseText { get; set; }
// (3) How do you feel about using the same name for type and identifier
// in cases like this? I go back and forth because on one hand it feels
// cleaner than using underscore (_MyPrivateProperty) or
// camel cased (myPrivateProperty).
private HttpWebRequest HttpWebRequest { get; set; }
// (4) Is it clear that the target of the lone verb comprising the method
// name is the noun which is the name of the class? To me, it is
// redundant to say 'PrepareForRestCall'.
protected abstract void Prepare();
public IRestCall Go()
{
this.Prepare();
HttpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
// (5) Here a region is used in place of a comment, but I have not
// seen any other code that uses regions this way. My thinking is
// that it brings the code one step closer to becoming a private
// method but can stay like this until it actually needs to be called
// from multiple points in the logic.
#region Add post data to request if present.
if (!string.IsNullOrEmpty(PostData))
{
// (6) I changed this from 'sw' to 'writer' after code review.
// Would you have as well?
using(StreamWriter writer = new StreamWriter(HttpWebRequest.GetRequestStream()))
writer.Write(PostData); // (7) Would you use curly braces for a single statement? I opt to save two lines so I can see more code on the screen.
}
#endregion
using (HttpWebResponse response = HttpWebRequest.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
ResponseText = reader.ReadToEnd();
}
return this;
}
}
最佳答案
(1) Is appending
Base
to the name of base classes useful?
这是个人喜好问题。我个人是受不了的。我讨厌看到像 RestCallBase instance = new SomeConcreteRestCall();
这样的代码 instance
是一个 RestCall
。期间。
(2) Is there a good way to decide on the ordering/grouping of members? I have seen code that uses
#region
for this, but I feel like it's not pervasive.
我喜欢看到相关的项目组合在一起。我讨厌 #region
(它只是一种隐藏代码的方式,它增加了试图保持所有 #region
井井有条的维护成本)并且我认为它是一种代码味道。
(3) How do you feel about using the same name for type and identifier in cases like this?
喜欢它。请看我之前的answer关于这个问题。
I go back and forth because on one hand it feels cleaner than using underscore (
_MyPrivateProperty
) or camel cased (myPrivateProperty
).
我不再喜欢前导下划线(我不得不承认我曾经喜欢)。现在我在成员前面加上 this
;这对我来说更清楚了。
(4) Is it clear that the target of the lone verb comprising the method name is the noun which is the name of the class? To me, it is redundant to say
PrepareForRestCall
.
在这个问题上我可以选择任何一种方式,但可能会倾向于更短的 Prepare
但如果有人认为 PrepareForRestCall
更清楚或询问什么是 Prepare
准备,我可能会承认这一点。
(5) Here a region is used in place of a comment, but I have not seen any other code that uses regions this way.
我讨厌地区。它在这里的使用方式很荒谬。
(6) I changed this from
sw
towriter
after code review. Would you have as well?
是的。
(7) Would you use curly braces for a single statement? I opt to save two lines so I can see more code on the screen.
哦,你最好相信我会。更清晰,降低维护成本。
关于c# - 关于此代码的最佳实践和编码约定的一些希望简单的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3961016/