我需要逐行解释代码。 [英] i need explanation of code line-by-line .

查看:156
本文介绍了我需要逐行解释代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

asasssasdasndsndjnsajdfnasjndasndjasnjdsjdnsnds

解决方案

您是否知道逐行解释代码的工作量是多少?

每一行都需要一个段落解释!例如:

 int next = r.Next(); 



创建一个名为next的新变量,它可以包含一个整数值。从先前声明的Random实例r,调用Next方法获取一个新的随机数,并将其分配给next变量。



可以你想象我们需要多长时间才能解释一个像你的例子一样的非常短的代码片段,一行一行?



不会发生这种情况。如果您有特定问题,请询问有关它的问题。但首先考虑一下 - 您是否希望坐下45分钟并且没有充分的理由键入逐行描述?


没有特别的理由这样做。这是一场噩梦:)

(Convert.ToInt32(c [0])> = 65&& Convert.ToInt32(c [0])< = 90 || Convert.ToInt32(c [0])== 95 || Convert.ToInt32(c [0])> = 97&& Convert.ToInt32(c [0])< = 122)代码片段试图检查char数组的第一个字符或字符串是字母还是下划线。但它很糟糕...... :(

完全不需要这样的转换。就这么简单:(c [0]> ='A'& ;& c [0]< ='Z')||(c [0]> ='a'&& c [0]< ='z')|| c [0] == '_' ...甚至更简单的Char.IsLetter ......



但是正如我在修订历史中所看到的,其余的代码也充斥着废话...



由于mehod的标识符是 CheckIdentifier ,它应该做它告诉了什么,但是例如C#接受标识符中的unicode和数字,它不是C#标识符测试器。

对于C#来说就是这么简单:



  public   static   bool  CheckIdentifier( String  i){
CodeDomProvider provider = CodeDomProvider.CreateProvider( C#);
return provider.IsValidIdentifier(i);
}


我看到的代码比这更差 - 所以,不要让你对一些好人的评论放下。 .. ;-)



您的逐行解释请求超出了 QA - Quick 回答



在这种情况下以及将来如何分析自己这样的代码?

一种可能的方法:



  1. 如果有特定功能块的文档,请阅读文档 - 您是否了解文档它的目的是什么?实际上,项目文档很少......
  2. 阅读代码文档(类或函数的注释):目的,参数,返回值,约束的注释。如果任何代码注释是最新的,那么这就是那个。
  3. 尝试理解类,函数和参数名称。一个体面的编码为实体提供了合理的名称。不幸的是,实际上,平均质量代码通常缺乏合适的命名,例如 int F1 (int a1 ,int a2 ){int temp = a1; ...}
  4. 我看到的很多代码都是过度设计的:历史上增长,对于给定问题的直接和最简单的解决方案知之甚少。在这种情况下,请密切注意。
  5. 构建已知模式的工具箱,惯用的构造,了解库和框架。在这种情况下:什么是类转换适用于什么?您可能来自使用ASCII代码检查字符的背景。这至多是第二个最佳选择,因为它假定您作为程序员知道每个字符的字符编码。在这里,一种符号方法(即通过符号而不是通过编码来命名字符)更为充分。检查文本模式的更好方法是 Regex (参见下面的建议解决方案)。
  6. 如果要求您进行分析然后重构,请创建测试用例首先介绍当前的功能。一旦你有一套体面的测试用例来检查所需的功能,你可以自行决定重构该功能。





对于给定的情况,如果它匹配某些 ASCII [ ^ ]编码的文本模式。对于这种情况,我使用 Regex

在您的代码中,普通数字最有可能是ASCII代码,如下所示:



普通号码 解释为ASCII码
48 '0'
57 '9'
65 'A'
90 'Z'
95 '_'
97 'A'
122 'z'




这个函数可能包括以下内容:

  bool  IsIdentifier( string  strWord)
{
return Regex.IsMatch(strWord ?? string .Empty, @ ^ [A-Za-z _] [0-9A-Za-z_] *

asasssasdasndsndjnsajdfnasjndasndjasnjdsjdnsnds

解决方案

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:

int next = r.Next();


Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?


There is no particular reason for doing such thing. It's a nightmare :)
The (Convert.ToInt32(c[0]) >= 65 && Convert.ToInt32(c[0]) <= 90 || Convert.ToInt32(c[0]) == 95 || Convert.ToInt32(c[0]) >= 97 && Convert.ToInt32(c[0]) <= 122) code snippet is trying to check if the first character of a char array or a string is letter or underscore. But it is awful... :(
Such conversions are not needed at all. It would have been that simple: (c[0]>='A' && c[0]<='Z') || (c[0]>='a' && c[0]<='z') || c[0]=='_'... or even simpler with Char.IsLetter...

But as I see in the revision history, the rest of the code is also full of nonsense...

As the mehod's identifier is CheckIdentifier, it should do what it tells, but as C# for example is accepting unicode and digits in the identifiers, it is not C# identifier tester.
For C# it would be that simple:

public static bool CheckIdentifier(String i) {
 CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
 return provider.IsValidIdentifier (i);
}


I've seen much worse code than that - so, don't let you put down by some comments of some nice fellows... ;-)

Your request for a line-by-line explanation is beyond a QA - Quick Answer.

How can you analyze such code yourself in this case and in the future?
One possible approach:


  1. Read the documentation if there is one for the particular function block - do you understand the documented purpose of it? In reality, project documentation is scarce...
  2. Read the code documentation (comments of classes or functions): purpose, parameters, return value, remarks on constraints. If any code comment is to be up-to-date, then this is the one.
  3. Try to make sense of the class, function, parameter names. A decent coding gives reasonable names to the entities. Unfortunately, in reality, average quality code often lacks decent naming, e.g. int F1(int a1, int a2) { int temp = a1; ... }
  4. A lot of code I see is over-engineered: grown historically with insufficient knowledge of the straight and simplest solution for a given problem. Sharpen your eyes for such situations.
  5. Build up your "toolbox" of known patterns, idiomatic constructs, knowing the libraries and frameworks. In this case: what is the class Convert good for? You may come from a background where characters were checked by ASCII codes. This is at best the second best choice since it assumes you as a programmer know the character encoding of each character. Here, a symbolic approach (i.e. name the characters by their symbol and not by their encoding) was more adequate. An even better approach for checking text patterns is Regex (see suggested solution below).
  6. If you are asked to analyze and then refactor, create test cases first to cover the current functionality. Once you have a decent set of test cases that check for the required functionality, you are free to refactor the function at your discretion.



For the given case, it looks like you are checking some string if it matches some ASCII[^] encoded text pattern. I use Regex for such cases.
In your code, the plain numbers stand most likely for the ASCII codes as shown below:

Plain numberInterpreted as ASCII code
48'0'
57'9'
65'A'
90'Z'
95'_'
97'a'
122'z'


This function might be covered by the following:

bool IsIdentifier(string strWord)
{
    return Regex.IsMatch(strWord ?? string.Empty, @"^[A-Za-z_][0-9A-Za-z_]*


这篇关于我需要逐行解释代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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