我使用 Visual Studio Code Metrics 做了一些测试。由于我可以计算圈复杂度,因此每个if, while, for- 运算符都会将复杂度增加 1。我有下一个简单的方法:
static bool ContainsNegative(int a, int b, int c, int d)
{
if (a < 0 || b < 0 || c < 0 || d < 0) return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
但对于它来说,圈复杂度是 5,而不是 2(1 表示方法 + 1 表示if)。我的问题是 - 这是因为代码指标将if运算符中的每个条件计算为不同的if?即我的方法相当于:
static bool ContainsNegative(int a, int b, int c, int d)
{
if (a < 0) return false;
if (b …Run Code Online (Sandbox Code Playgroud) 我需要将我的类转换为 JSON 并使用 Json.NET。但我可以有不同的 JSON 结构,例如:
{
name: "Name",
type: "simple1",
value: 100
};
Run Code Online (Sandbox Code Playgroud)
或者
{
name: "Name",
type: {
optional1: {
setting1: "s1",
setting2: "s2",
///etc.
},
value: 100
};
Run Code Online (Sandbox Code Playgroud)
我的 C# 代码是:
public class Configuration
{
[JsonProperty(PropertyName = "name")]
public string Name{ get; set; }
[JsonProperty(PropertyName = "type")]
public MyEnumTypes Type { get; set; }
public OptionalType TypeAdditionalData { get; set; }
[JsonProperty(PropertyName = "value")]
public int Value { get; set; }
public bool ShouldSerializeType()
{
OptionalSettingsAttribute optionalSettingsAttr …Run Code Online (Sandbox Code Playgroud)