您的位置:首页 > 编程语言

结对代码复审结果

2015-09-29 23:57 274 查看
Q:是否需要有代码规范

需要,我认为非常需要,虽然现代曾经也举办过乱码大赛。但是,代码作为一种车鞥徐语言的同事也是程序员之间的交流的媒介,可读性强可移植性强是非常重要的。就我个人而言,我觉得,我更喜欢每行代码之间都有空行,大括号之后要有换行,程序要换行和缩进非常明显和统一,这样看着才会增加可读性,便于理解。

这些规范都是官僚制度下产生的浪费大家的编程时间、影响人们开发效率, 浪费时间的东西? 并不是啊。。。如果你这样的话,等你走上工作岗位上看到一个程序要上万行的代码的时候,我们都是知道的,每一个软件开发的背后都是一个团队,而不是一个人,当你看着那上万行别人留给你的代码让你继续写的时候,难道你愿意看着那密密麻麻的代码毫无章法的格式,根本呢份不穷哪里是哪里更别提去好好的理解,所以应该有规范的格式。

我是个艺术家,手艺人,我有自己的规范和原则? 不赞同,所谓的艺术家页数创造美,而不是胡乱的堆砌自以为的东西,没有人会认为那个代码很乱的代码是美的,我们只能说他思路很清晰怎么写都不会乱,但是其实他并不美,杂乱无章,何谈艺术?

规范不能强求一律,应该允许很多例外? 规则都是人定的,象这个大括号之后回车缩进和回车不缩进其实都可以达到便于分辨的目的,这些都是活的,不一定要严格要求,这应该就是所谓的例外吧。但是不能乱,不能让人家看不懂看不明白,这样就不应该允许了。

我擅长制定编码规范,你们听我的就好了? 我不赞同,因为在大前提不变的情况的前提下,每个人有每个人的习惯和特点的,我们称之为代码风格。所以我们不应该强求别人遵从你的指定的规范,这是不合理不自由的!

Q:代码复审

在代码复审之前,我要表达一下欣慰之情。首先,因为我能和我认识的小伙伴结对做作业,省去了尴尬、认识、熟悉这些阶段,能够快速的开始任务。其次,在我看了结对伙伴的代码之后,除了发现我俩的编程风格相似之外,我俩的结构设计也很像,所以我认为在读对方的代码时应该完全没有问题。以下是我给结对伙伴的代码复审表。

General

Does the code work? Does it perform its intended function, the logic is correct etc.

正常工作,并没有异常,但是每次输出的东西都是一样的,在后边给出了自己的分析

Is all the code easily understood?

代码风格很清晰,设了很多类,名字都很容易理解

Does it conform to your agreed coding conventions? These will usually cover location of braces, variable and function names, line length, indentations, formatting, and comments.

我们代码风格不一样,他是对象化编程我是过程化编程,风格迥异

Is there any redundant or duplicate code?

个别一些地方比如ifelse哪里换成case也许会简化一些

Is the code as modular as possible?

已经很模块化了,但是program这里的运算还是可以也做成函数调用的。

Can any global variables be replaced?

没有全局变量

Is there any commented out code?

没有

Do loops have a set length and correct termination conditions?

是的

Can any of the code be replaced with library functions?

不能

Can any logging or debugging code be removed?

有的被注释到了

Security

Are all data inputs checked (for the correct type, length, format, and range) and encoded?

是的

Where third-party utilities are used, are returning errors being caught?

在输入时没有catch exception

Are output values checked and encoded?

是的

Are invalid parameter values handled?

有效

Documentation

Do comments exist and describe the intent of the code?

有注释的,可以

Are all functions commented?

有的没有

Is any unusual behavior or edge-case handling described?

没有

Is the use and function of third-party libraries documented?

是的

Are data structures and units of measurement explained?

重要的都解释

Is there any incomplete code? If so, should it be removed or flagged with a suitable marker like ‘TODO’?

answersfile计算未完成,没考虑负数

Testing

Is the code testable? i.e. don’t add too many or hide dependencies, unable to initialize objects, test frameworks can use methods etc.

程序可测试,各个模块运行正常

Do tests exist and are they comprehensive? i.e. has at least your agreed on code coverage.

程序中没有包含测试用例

Do unit tests actually test that the code is performing the intended functionality?

程序中没有包含unit tests,需要人工测试

Are arrays checked for ‘out-of-bound’ errors?

数据结构多使用Queue,使用中没有遇到out-of-bound问题

Could any test code be replaced with the use of an existing API?

程序中没有包含test code

总体来说,我的结对伙伴的程序完成了主要功能,代码清晰易读易理解,还可以进一步的优化。

1.代码可以正常的从文件中读入和输出到文件中数据。一共生成了100个练习表达式和100个答案。并且答案都是正确的。而且程序和函数的逻辑也是对的。但是有一个问题就是,输出的结果都是10个一样的答案和表达式,并且每次重新生成工程也都是一样的结果。所以真对于这个代码

public Create(int n, int r){
Random random = new Random();
quantity = n;
maxrange = r;
signNum = random.Next(0,4);
sign.Add('+');
sign.Add('-');
sign.Add('*');
sign.Add('/');
}

可以看出生产随机数的代码本身没有问题,用Random.Next产生随机数。问题的原因是每次都new一个初始化对象时,对象都是使用当前时间生成一个随机数的种子,这就意味着在循环时可能得到相同种子的Random实例。

解决办法可以如下:

private static readonly Random random = new Random();
private static readonly object syncLock = new object();
public static int RandomNumber(int min, int max)
{
lock(syncLock) { // synchronize
return random.Next(min, max);
}
}

private static readonly ThreadLocal<Random> appRandom = new ThreadLocal<Random>(() => new Random());

public static int GetRandomNumber()
{
return appRandom.Value.Next();
}
既然用了lock就有可能影响到性能。可以把随机数实例放到ThreadLocal中来保证每个线程一个Randonm实例。
2.所有的代码都比较容易理解。
3.比较符合我的代码编码约定,尤其是写完了大括号之后有换行和缩进,也不存在多行代码写在一行的情况。整个代码命名了很多类,起的名字都直观易懂,将整个程序非常好的模块化,对象化的完成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: