代码重复,代码可读性和代码可维护性

时间:2019-01-16 11:39:41

标签: code-readability code-maintainability

我在这里有一个非常笼统的问题。我总是对删除代码重复的代码审查注释感到困惑。让我们以下面的示例为例:

                public static void RunFunction1(TraceWriter log)
                {
                    ITelemetryLogger appinsights = new ApplicationInsightLogger(INSTRUMENTATIONKEY);
                    DateTime startTime = DateTime.Now;
                    log.Info($"RunAudit Job is triggered at {startTime}");
                    IRestClient client = new RestClient();
                    client.BaseUrl = SERVICE_BASEURL;
                    IRestResponse response = client.Execute(new RestRequest("API/RunFunction1", Method.POST));
                    if (response.IsSuccessful)
                    {
                        DateTime endTime = DateTime.Now;
                        log.Info($"RunFunction1 Job completed successfully at {endTime}, Time of Execution is {endTime - startTime}");
                    }
                    else
                    {
                        log.Error($"RunFunction1 Job Failed with message {response.Content.ToString()} and Status Code {response.StatusCode}");
                        Dictionary<string, string> properties = new Dictionary<string, string>();
                        properties.Add("MethodName", "FunctionRunFunction1");
                        properties.Add("ErrorMessage", $"RunFunction1 Job Failed with message {response.Content.ToString()} and Status Code {response.StatusCode}");
                        appinsights.LogError("ScheduledJobFailed", properties);
                    }
                }



                public static void RunFunction2(TraceWriter log)
                {
                    ITelemetryLogger appinsights = new ApplicationInsightLogger(INSTRUMENTATIONKEY);
                    DateTime startTime = DateTime.Now;
                    log.Info($"RunAudit Job is triggered at {startTime}");
                    IRestClient client = new RestClient();
                    client.BaseUrl = SERVICE_BASEURL;
                    IRestResponse response = client.Execute(new RestRequest("API/RunFunction1", Method.POST));
                    if (response.IsSuccessful)
                    {
                        DateTime endTime = DateTime.Now;
                        log.Info($"RunFunction2 Job completed successfully at {endTime}, Time of Execution is {endTime - startTime}");
                    }
                    else
                    {
                        log.Error($"RunFunction2 Job Failed with message {response.Content.ToString()} and Status Code {response.StatusCode}");
                        Dictionary<string, string> properties = new Dictionary<string, string>();
                        properties.Add("MethodName", "FunctionRunFunction2");
                        properties.Add("ErrorMessage", $"RunFunction2 Job Failed with message {response.Content.ToString()} and Status Code {response.StatusCode}");
                        appinsights.LogError("ScheduledJobFailed", properties);
                    }
                }

现在,我的问题是,如果您在两个函数中都看到重复的代码。我们可以将此代码移到一个可以处理通用代码的通用函数中。但是我的问题是,我们是否不通过使它们调用公共函数来将两个函数逻辑相互耦合? 1.)代码重复,代码可读性和代码可维护性之间的细线是什么?

2。)是否有人可以遵循任何准则?

3。)某人应遵循的最佳实践是什么?

0 个答案:

没有答案