码迷,mamicode.com
首页 > 其他好文 > 详细

实验四 代码评审

时间:2020-05-14 01:25:38      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:打开   前置   alibaba   学习   外部   ram   lsp   功能   代码检查   

一、实验目的

1) 了解代码审查的含义;

2) 掌握相关编程规范检查工具的安装与使用;

二、实验内容及要求

Code Review中文应该译作“代码审查”或是“代码评审”或“代码复查”,是指通过阅读代码来检查源代码与编码标准的符合性以及代码质量的活动。Code Review主要用来在软件工程过程中改进代码质量,通过代码评审可以达到如下目的:

●在项目早期就能够发现代码中的BUG

●帮助初级开发人员学习高级开发人员的经验,达到知识共享

●避免开发人员犯一些很常见,很普通的错误

●保证项目组人员的良好沟通

●项目或产品的代码更容易维护

     代码评审主要内容是编程规范,重构方法,架构设计,性能安全,日志,可读性,扩展性等问题。通过代码评审可查找和修复引入到开发阶段的应用程序的错误,提高软件的整体素质和开发者的技能。代码评审的作用和意义已在很多技术团队内达成共识,可是很多时候并未被有效执行,甚至被认为是一项费时费力的工作。借助一些工具可以更容易,更有效率地来进行Code Review。

1、以小组形式,针对前面“实验一”中所完成的代码,进行代码评审(走查),重点检查以下情况。你也可有查询相关材料,建立更细化的检查清单(check list)

- 程序是否能正常工作,代码是否实现预期的功能,逻辑是否正确。

- 代码是否遵循的编程规范

- 代码是否尽可能的模块化

- 所有的数据输入是否都进行了检查

- 是否有注释,并且描述了代码的意图

- 代码的可理解性和可测试性

2、按“实验二”的分组方式,两人一组,随机分配另一组的代码作为本组评审和分析的对象

一些编码规范的检查工具如下,也可自行查找工具使用。

java语言      

- 采用使用eclipse Checkstyle插件 

- CheckStyle是SourceForge下的一个项目,提供了一个帮助JAVA开发人员遵守某些编码规范的工具。它能够自动化代码规范检查过程,从而使得开发人员从这项重要但枯燥的任务中解脱出来。它可以根据设置好的编码规则来检查代码。比如符合规范的变量命名,方法体的最大行数,重复代码检查等等。

如果你使用idea ,可以使用Alibaba Java Code Guidelines插件,参考链接如下:https://github.com/alibaba/p3c/wiki/IDEA%E6%8F%92%E4%BB%B6%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3

C++语言

- 可使用Google代码规范工具Cpplint。

- Cpplint是一个python脚本,Google使用它作为自己的C++代码规范检查工具,VSCcode可配置Cpplint对C++代码进行规范检查。

python

- 可采用pylint

- Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准(Pylint 默认使用的代码风格是 PEP 8,具体信息,请参阅参考资料)和有潜在问题的代码。目前在 eclipse 的 pydev 插件中也集成了 Pylint,VSCcode可安装pylint插件

    当发现了项目存在的问题后,可通过Bug跟踪系统向项目维护者反馈问题(issue),管理Issue的系统称为BTS(Bug Tracking System,Bug跟踪系统)。当今具有代表性的BTS有Redmine、Trac、BugZilla等。GitHub自身也加入了BTS的功能。在GitHub上,可以将它作为软件开发者之间的交流工具。通过github的issues功能开发者可以便捷的发现软件的BUG并报告,想向项目所有人询问或用来追踪各种想法探讨准备实施的任务。

三、实验过程

1、总述

检查项目

https://github.com/lizhenzhen0114/game

 

项目语言

C语言

 

代码规范软件

Cpplint

python脚本,Google使用它作为自己的C++代码规范检查工具

项目运行软件

VS2012、Python3.6

 

2、配置代码审查工具

前置要求:下载Python并配置环境变量

(1)从https://github.com/cpplint/cpplint下载cpplint项目。

技术图片

(2)解压压缩包,比如我解压到“E:\GitHub\Google_style”,在这个目录下就有了许多文件,包含我们所需的cpplint.py文件,点击 setup.py。

技术图片

(3)要想调用cpplint可以有两种方式,其一,直接命令行调用。

  1win+R,输入cmd,调用cmd.exe

  进入我们所需要检查的项目目录(我将项目保存在“E:\GitHub\game”)

  2E:

  3cd GitHub\game

  4python E:\GitHub\Google_style\cpplint\cpplint.py sm.c

技术图片

(4)其二,在vs里添加工具。打开VS,工具->外部工具,添加工具,按下表操作填写信息。然后就会看到在工具栏下有一个“Google_Style_Standard”的工具。

标题

Google_Style_Standard

随便起个名字

命令

C:\Program Files\Python36\python.exe

Python.exe的地址

参数

E:\GitHub\Google_style\cpplint\cpplint.py --output=vs7 --filter=-build/header_guard,-build/include,-readability/streams $(ItemPath)

前面是Cpplint.py的地址

初始目录

$(ItemDir)

点右边的三角形按钮,选择项目录

技术图片

3、结果记录

sm.c:0: No copyright message found. You should have a line: "Copyright [year] <Copyright Owner>" [legal/copyright] [5]
sm.c:6: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:6: Should have a space between // and comment [whitespace/comments] [4]
sm.c:7: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
sm.c:7: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
sm.c:8: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:8: Should have a space between // and comment [whitespace/comments] [4]
sm.c:9: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:9: Should have a space between // and comment [whitespace/comments] [4]
sm.c:10: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
sm.c:10: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
sm.c:11: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
sm.c:11: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
sm.c:12: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:12: Should have a space between // and comment [whitespace/comments] [4]
sm.c:13: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:13: Should have a space between // and comment [whitespace/comments] [4]
sm.c:14: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:14: Should have a space between // and comment [whitespace/comments] [4]
sm.c:14: Missing space after , [whitespace/comma] [3]
sm.c:15: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:15: Should have a space between // and comment [whitespace/comments] [4]
sm.c:16: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
sm.c:16: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
sm.c:18: { should almost always be at the end of the previous line [whitespace/braces] [4]
sm.c:19: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:19: Should have a space between // and comment [whitespace/comments] [4]
sm.c:20: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:20: Should have a space between // and comment [whitespace/comments] [4]
sm.c:21: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:21: Should have a space between // and comment [whitespace/comments] [4]
sm.c:22: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:22: Should have a space between // and comment [whitespace/comments] [4]
sm.c:23: Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3]
sm.c:25: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
sm.c:25: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
sm.c:26: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:26: Should have a space between // and comment [whitespace/comments] [4]
sm.c:27: { should almost always be at the end of the previous line [whitespace/braces] [4]
sm.c:28: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:28: Should have a space between // and comment [whitespace/comments] [4]
sm.c:31: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:31: Should have a space between // and comment [whitespace/comments] [4]
sm.c:32: { should almost always be at the end of the previous line [whitespace/braces] [4]
sm.c:33: Missing spaces around = [whitespace/operators] [4]
sm.c:33: Missing space after , [whitespace/comma] [3]
sm.c:34: Missing spaces around = [whitespace/operators] [4]
sm.c:35: Missing space before ( in while( [whitespace/parens] [5]
sm.c:37: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:37: Should have a space between // and comment [whitespace/comments] [4]
sm.c:38: Missing spaces around = [whitespace/operators] [4]
sm.c:38: Missing spaces around < [whitespace/operators] [3]
sm.c:38: Missing space before ( in for( [whitespace/parens] [5]
sm.c:38: Missing space after ; [whitespace/semicolon] [3]
sm.c:39: { should almost always be at the end of the previous line [whitespace/braces] [4]
sm.c:40: Missing spaces around = [whitespace/operators] [4]
sm.c:40: Missing spaces around < [whitespace/operators] [3]
sm.c:40: Missing space before ( in for( [whitespace/parens] [5]
sm.c:40: Missing space after ; [whitespace/semicolon] [3]
sm.c:41: Missing spaces around == [whitespace/operators] [3]
sm.c:41: Missing space before ( in if( [whitespace/parens] [5]
sm.c:43: Missing spaces around == [whitespace/operators] [3]
sm.c:43: Missing space before ( in if( [whitespace/parens] [5]
sm.c:47: Missing spaces around = [whitespace/operators] [4]
sm.c:47: Missing spaces around < [whitespace/operators] [3]
sm.c:47: Missing space before ( in for( [whitespace/parens] [5]
sm.c:47: Missing space after ; [whitespace/semicolon] [3]
sm.c:48: Missing spaces around = [whitespace/operators] [4]
sm.c:48: Missing spaces around < [whitespace/operators] [3]
sm.c:48: Missing space before ( in for( [whitespace/parens] [5]
sm.c:48: Missing space after ; [whitespace/semicolon] [3]
sm.c:49: { should almost always be at the end of the previous line [whitespace/braces] [4]
sm.c:50: Missing spaces around = [whitespace/operators] [4]
sm.c:50: Missing space after , [whitespace/comma] [3]
sm.c:51: Missing spaces around == [whitespace/operators] [3]
sm.c:51: Missing space before ( in if( [whitespace/parens] [5]
sm.c:52: { should almost always be at the end of the previous line [whitespace/braces] [4]
sm.c:53: Missing spaces around < [whitespace/operators] [3]
sm.c:53: Missing space before ( in if( [whitespace/parens] [5]
sm.c:54: Lines should be <= 80 characters long [whitespace/line_length] [2]
sm.c:54: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:54: Should have a space between // and comment [whitespace/comments] [4]
sm.c:54: Missing spaces around = [whitespace/operators] [4]
sm.c:55: Missing spaces around > [whitespace/operators] [3]
sm.c:55: Missing space before ( in if( [whitespace/parens] [5]
sm.c:56: Lines should be <= 80 characters long [whitespace/line_length] [2]
sm.c:56: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:56: Should have a space between // and comment [whitespace/comments] [4]
sm.c:56: Missing spaces around = [whitespace/operators] [4]
sm.c:57: Missing spaces around == [whitespace/operators] [3]
sm.c:57: Missing space before ( in if( [whitespace/parens] [5]
sm.c:58: Lines should be <= 80 characters long [whitespace/line_length] [2]
sm.c:58: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:58: Should have a space between // and comment [whitespace/comments] [4]
sm.c:58: Missing spaces around = [whitespace/operators] [4]
sm.c:60: An else should appear on the same line as the preceding } [whitespace/newline] [4]
sm.c:60: Missing spaces around == [whitespace/operators] [3]
sm.c:60: Missing space before ( in if( [whitespace/parens] [5]
sm.c:61: { should almost always be at the end of the previous line [whitespace/braces] [4]
sm.c:62: Missing spaces around == [whitespace/operators] [3]
sm.c:62: Missing space before ( in if( [whitespace/parens] [5]
sm.c:63: Lines should be <= 80 characters long [whitespace/line_length] [2]
sm.c:63: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:63: Should have a space between // and comment [whitespace/comments] [4]
sm.c:63: Missing spaces around = [whitespace/operators] [4]
sm.c:66: Missing spaces around = [whitespace/operators] [4]
sm.c:66: Missing spaces around < [whitespace/operators] [3]
sm.c:66: Missing space before ( in for( [whitespace/parens] [5]
sm.c:66: Missing space after ; [whitespace/semicolon] [3]
sm.c:67: Missing spaces around = [whitespace/operators] [4]
sm.c:67: Missing spaces around < [whitespace/operators] [3]
sm.c:67: Missing space before ( in for( [whitespace/parens] [5]
sm.c:67: Missing space after ; [whitespace/semicolon] [3]
sm.c:68: Missing spaces around = [whitespace/operators] [4]
sm.c:69: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
sm.c:69: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
sm.c:70: Missing spaces around == [whitespace/operators] [3]
sm.c:70: Missing space before ( in if( [whitespace/parens] [5]
sm.c:71: { should almost always be at the end of the previous line [whitespace/braces] [4]
sm.c:73: Missing spaces around = [whitespace/operators] [4]
sm.c:79: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:79: Should have a space between // and comment [whitespace/comments] [4]
sm.c:79: Missing space after , [whitespace/comma] [3]
sm.c:80: { should almost always be at the end of the previous line [whitespace/braces] [4]
sm.c:81: Missing spaces around = [whitespace/operators] [4]
sm.c:81: Missing space after , [whitespace/comma] [3]
sm.c:82: Missing spaces around <= [whitespace/operators] [3]
sm.c:82: Missing space before ( in for( [whitespace/parens] [5]
sm.c:82: Missing space after ; [whitespace/semicolon] [3]
sm.c:83: Missing spaces around <= [whitespace/operators] [3]
sm.c:83: Missing space before ( in for( [whitespace/parens] [5]
sm.c:83: Missing space after ; [whitespace/semicolon] [3]
sm.c:84: Missing spaces around == [whitespace/operators] [3]
sm.c:84: Missing space before ( in if( [whitespace/parens] [5]
sm.c:89: Weird number of spaces at line-start. Are you using a 2-space indent? [whitespace/indent] [3]
sm.c:89: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
sm.c:90: At least two spaces is best between code and comments [whitespace/comments] [2]
sm.c:90: Should have a space between // and comment [whitespace/comments] [4]
sm.c:91: Missing space after , [whitespace/comma] [3]
sm.c:92: Missing spaces around = [whitespace/operators] [4]
sm.c:92: Missing spaces around < [whitespace/operators] [3]
sm.c:92: Missing space before ( in for( [whitespace/parens] [5]
sm.c:92: Missing space after ; [whitespace/semicolon] [3]
sm.c:92: Missing space before { [whitespace/braces] [5]
sm.c:93: Missing spaces around = [whitespace/operators] [4]
sm.c:93: Missing spaces around < [whitespace/operators] [3]
sm.c:93: Missing space before ( in for( [whitespace/parens] [5]
sm.c:93: Missing space after ; [whitespace/semicolon] [3]
sm.c:94: Tab found; better to use spaces [whitespace/tab] [1]
sm.c:94: Missing spaces around = [whitespace/operators] [4]
sm.c:94: Consider using rand_r(...) instead of rand(...) for improved thread safety. [runtime/threadsafe_fn] [2]
Done processing sm.c
Total errors found: 155

4、结果分析

技术图片

  我们发现主要错误在一些符号附近没有添加空格,导致如for{;;}、a>b等代码挤在一起,极其难看,也不好检查错误。其次在注释上要与代码隔开,//与注释之间加空格,一行注释不宜过长。尤为注意的是一定要项目开头写好版权信息,如作者、时间、最后一次修改信息等。

技术图片

5、提交问题

 

技术图片

 

技术图片

 

 

四、实验小结

  通过本章的学习认识到了一些代码审查软件如cpplint、eclipse Checkstyle等。发现代码审查可以很好的检查自己代码的格式。

  在检查别人项目时发现错误还挺好改的,然而在修改自己Java项目代码格式时,极其难受,尤其是代码前不允许使用tab,且只能有两个空格,而软件默认又是6个空格(一个tab)。

实验四 代码评审

标签:打开   前置   alibaba   学习   外部   ram   lsp   功能   代码检查   

原文地址:https://www.cnblogs.com/DTer1999/p/12885751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!