C#代码审查(C#入门) [英] C# code review (starter in c#)

查看:97
本文介绍了C#代码审查(C#入门)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我本周开始使用C#进行编程.始终使用PHP编程,但我渴望学习另一种语言.因此,希望你们,高级C#可以审阅我的代码.我希望从中学到新的东西.

提前谢谢.

Hi,

I started programming in C# this week. Always programmed in PHP but I am eager to learn another language. So hopefully you guys, the senior C# can review my code. I hope to learn new things from it.

Thanks in advance.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebService {
    public partial class IncidentCreator : Form {

        public IncidentCreator() {
            InitializeComponent();

            VisibleFalse();
        }

        private void VisibleFalse() {

            //TextLabels
            labelTextDomain.Visible = false;
            labelTextUserID.Visible = false;
            labelTextShortDesc.Visible = false;
            labelTextDescribe.Visible = false;

            //ComboBox
            comboBoxDomain.Visible = false;

            //TextBox
            textBoxShortDesc.Visible = false;
            textBoxUserID.Visible = false;
            textBoxDescribe.Visible = false;

            //Button
            buttonCreateIncident.Visible = false;

        }

        private void VisibleTrue() {
            labelTextDomain.Visible = true;
            labelTextUserID.Visible = true;
            labelTextShortDesc.Visible = true;
            labelTextDescribe.Visible = true;

            comboBoxDomain.Visible = true;

            textBoxShortDesc.Visible = true;
            textBoxUserID.Visible = true;
            textBoxDescribe.Visible = true;

            buttonCreateIncident.Visible = true;
        }

        private void comboBoxIdentUser_SelectedIndexChanged(object sender, EventArgs e) {

            if (comboBoxIdentUser.SelectedItem.Equals("User X")) {

                Size = new Size(334, 376);

                VisibleTrue();

                var a = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
                textBoxUserID.Text = a;

            } else if (comboBoxIdentUser.SelectedItem.Equals("User Y")) {

                VisibleFalse();
                Size = new Size(334, 110);

            }
        }

        private void buttonCreateIncident_Click(object sender, EventArgs e) {

            buttonCreateIncident.Enabled = false;
            comboBoxIdentUser.Enabled = false;
            comboBoxDomain.Enabled = false;
            textBoxShortDesc.Enabled = false;
            textBoxDescribe.Enabled = false;

            //Reference Code
            WebReference.Now_incident soapClient = new WebService.WebReference.Now_incident();
            System.Net.ICredentials cred = new System.Net.NetworkCredential("admin", "xxx");
            soapClient.Credentials = cred;

            WebReference.insert insert = new WebReference.insert();
            WebReference.insertResponse response = new WebReference.insertResponse();

            var a = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
            var x = a;

            var b = x.Replace("Domain" + (char)92, "");

            insert.caller_id = b;
            insert.short_description = textBoxShortDesc.Text.ToString();
            insert.comments = textBoxDescribe.Text.ToString();

            try {

                response = soapClient.insert(insert);
                MessageBox.Show("Your Incident has been created: " + response.number + "\n");

            } catch (Exception error) {

                this.textBoxDescribe.Text = error.Message;

            }
        }
    }
}

推荐答案

1)注释!添加它们.使用XML注释工具,它将您的方法和字段添加到Intellisense.
2)不要使用魔术数字"-(char)92吗?为什么? 334、376、110?什么?改用常量,并给它取一个明智的名称,这样我就不必去寻找它的功能了!
3)
1) Comments! Add them. Use the XML comment facility, it gets your methods and fields added to Intellisense.
2) Don''t use "Magic Numbers" - (char)92 ? Why? 334, 376, 110? What? Use a constant instead, and give it a sensible name so I don''t have to go hunting for what the heck it does!
3)
var a = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
var x = a;
var b = x.Replace("Domain" + (char)92, "");

为什么要全部创建"x"?您再也不会使用它了.
4)就个人而言,我讨厌将"var"用于定义的标准类型:ToString始终返回字符串,因此使用
会更清楚

Why create "x" at all? You never use it again...
4) Personally, I hate "var" being used for defined, standard types: ToString always returns a string, so it it clearer to use

string a = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
string b = a.Replace("Domain" + (char)92, "");


5)命名:为什么要称呼它为"a"或"b":读起来更明显"name"和"localName".

可能还有更多,但是那一定会作为一个开始! :laugh:


5) Naming: Why call it "a", or "b": "name" and "localName" are much more obvious to read.

There is probably more, but that''ll do for a start! :laugh:


如果您正在寻找代码审查工具,则市场上有很多,但是我发现有些是由Microsoft本身提供的.而且根据可靠性和审核标准,这些是最接近的.
如果要在编译后进行查找,这是第一个选项 FxCop
在必须由开发人员自己或在开发阶段由技术负责人进行审查的情况下,这是另一种选择
If you are looking for the Code Review Tools, then there are many in market, but i have came across to some which are provided by Microsoft itself. And as per reliability and audit standards these are the closest.
If you are looking for after compilation review, this is the first option FxCop
And in the cased where review has to be done by developer him/her self or by tech.lead at Development Stage, here is the other option StyleCop


我尝试避免事件处理程序(buttonCreateIncident_Click)中的代码.我将代码作为一个函数编写,然后从事件处理程序中调用它(如果我需要在另一个地方...)
I try to avoid code in Event Handlers (buttonCreateIncident_Click). I write the code as a function, and call it from the event handler (if i need it on another place ...)


这篇关于C#代码审查(C#入门)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆