C# 关于此代码的最佳实践和编码约定,有许多简单的问题
我把我的问题放在评论里。代码支持进行rest调用C# 关于此代码的最佳实践和编码约定,有许多简单的问题,c#,coding-style,C#,Coding Style,我把我的问题放在评论里。代码支持进行rest调用 // (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 see
// (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;
}
}
尽管没有人喜欢与StyleCop相关的所有规则,但我们的团队决定采用的是这样,至少简单的事情在我们的代码库中是一致的
我将尝试按顺序回答这些问题:
RestCallBase instance=…
而不仅仅是RestCall
很奇怪streamWriter
,因为这更清楚Base
有用吗
这是个人喜好的问题。我个人无法忍受。我讨厌看到像RestCallBase instance=newsomeconcreterestcall()这样的代码
实例
是一个RestCall
。句号
(2) 是否有一种确定成员顺序/分组的好方法?我见过使用#region
的代码,但我觉得它并不普遍
我喜欢看到相关的项目组合在一起。我讨厌<代码>区域> /代码>(这只是隐藏代码的方法,它增加了维护成本,试图保持所有的<代码>区域>代码>,我认为它是代码气味。
(3) 在这种情况下,使用相同的名称作为类型和标识符,您感觉如何
我喜欢。关于这个问题,请参阅我的上一篇文章
我来回走动是因为一方面它比使用下划线(\u MyPrivateProperty
)或驼峰式(MyPrivateProperty
)感觉更干净
我再也不喜欢开头的下划线了(我不得不承认我以前喜欢)。现在我在成员前面加上this
;这对我来说更清楚了
(4) 包含方法名的单独动词的目标是否明确是作为类名的名词?对我来说,说prepareforestcall
是多余的
在这一点上,我可以选择任何一种方式,但可能倾向于较短的Prepare
,但如果有人认为prepareforestcall
更清晰,或者问我准备做什么,我可能会承认这一点
(5) 这里使用了一个区域来代替注释,但我还没有看到任何其他代码以这种方式使用区域
我讨厌地域。这里使用它的方式很可笑
(6) 在代码审查之后,我将其从sw
更改为writer
。你也要吗
对
(7) 你会对一个语句使用大括号吗?我选择保存两行代码,以便在屏幕上看到更多代码
哦,你最好相信我会的。更清晰,降低维护成本。#5-我将用名为
AddPostDataToRequestIfPresent
的方法替换该区域作为注释,以不同意关于问题6的共识:
(6) 在代码审查之后,我将其从sw更改为writer。你也要吗
我不会的。sw
的范围只有两行代码,在如此小的空间中,简洁(可阅读的代码更少)和长名称(定义就在这里)的好处是少
在更大的范围内,反映对象使用情况的较长名称(而不是其类型)会更好(例如postContent
)writer
或streamWriter
是一个比冗长的匈牙利语稍差的名字,因为它只传递变量类型
(5) 这里使用了一个区域来代替注释,但我还没有看到任何其他代码以这种方式使用区域
我想说这很有趣。以下是我对原因的看法
可能的好处:
- 如果评论解释了你在做什么,那就足够了