重构代码 --- 计算长函数工具的实现(附源代码)
2011-12-12 16:45
274 查看
长函数代表着腐化的代码,难以修改和扩展。在网上搜了竞没找到相关工具,于是就自己手动写了一个。
计算函数行数,找出长函数,这样就可以开始重构代码了。
工具:fl.py
说明:计算函数长度,目前只支持.c文件和.cpp文件,支持其他文件类型,只需将代码中.cpp改为对应的文件类型即可
环境要求:安装Python 2.7
使用方法: ./fl.py max_lengthrelative_dir
Max_length:大于该长度的函数则输出
Relative_dir:目录相对路径
使用例子:./fl.py 300101_StarZoneServer/
@IED_40_125_sles10sp1:~> ./fl.py 300 101_ZoneServer/
Lines > 300 , Search in dir: 101_StarZoneServer/
101_ZoneServer/DropEngine/DropFramework.cpp : line 295 total : 563 namespace DroppedThingPrivateFunction
101_ZoneServer/GameMaster/PetAttributeCommand.cpp : line 41 total : 341 int CPetAttributeCommand::SecurityCheck()
101_ZoneServer/GameMaster/RoleAttributeCommand.cpp : line 186 total : 320 int CRoleAttributeCommand::SecurityCheck()
101_ZoneServer/GameMaster/PetEnhanceCommand.cpp : line 36 total : 403 int CPetEnhanceCommand::SecurityCheck()
输出第一行为参数,函数长度大于300,搜索目录名。
后面输出为:
文件名 函数起始行号 函数总长度函数名称
如果有什么不足或BUG,欢迎交流反馈和完善。
如果有什么不足或BUG,欢迎交流反馈和完善。
计算函数行数,找出长函数,这样就可以开始重构代码了。
工具:fl.py
说明:计算函数长度,目前只支持.c文件和.cpp文件,支持其他文件类型,只需将代码中.cpp改为对应的文件类型即可
环境要求:安装Python 2.7
使用方法: ./fl.py max_lengthrelative_dir
Max_length:大于该长度的函数则输出
Relative_dir:目录相对路径
使用例子:./fl.py 300101_StarZoneServer/
@IED_40_125_sles10sp1:~> ./fl.py 300 101_ZoneServer/
Lines > 300 , Search in dir: 101_StarZoneServer/
101_ZoneServer/DropEngine/DropFramework.cpp : line 295 total : 563 namespace DroppedThingPrivateFunction
101_ZoneServer/GameMaster/PetAttributeCommand.cpp : line 41 total : 341 int CPetAttributeCommand::SecurityCheck()
101_ZoneServer/GameMaster/RoleAttributeCommand.cpp : line 186 total : 320 int CRoleAttributeCommand::SecurityCheck()
101_ZoneServer/GameMaster/PetEnhanceCommand.cpp : line 36 total : 403 int CPetEnhanceCommand::SecurityCheck()
输出第一行为参数,函数长度大于300,搜索目录名。
后面输出为:
文件名 函数起始行号 函数总长度函数名称
如果有什么不足或BUG,欢迎交流反馈和完善。
#!/usr/bin/python # Create by douglaschen, 2011.12.12. All right Reserved. import os import string import sys import linecache __DEBUG = 0 def fl(FILENAME, LENGTH): sysSed="sed -e '/^{\|^}/=' -n " sysSed += str(FILENAME) result = os.popen(sysSed).read() resultlist = result.split('\n') # print len(resultlist) for i in range(0, len(resultlist)-1, 2): start = int(resultlist[i]) end = int(resultlist[i+1]) if ((end - start) < LENGTH): continue print FILENAME, ": line", start - 1, "total :", (end - start), print linecache.getline(FILENAME, start - 1), def walkdir(directory): if __DEBUG != 0: import pdb pdb.set_trace() # print directory for root, dirs, files in os.walk(directory): # print "root : ", root # print "dirs : ", dirs # print "files :", files for file in files: # print file string = str(file) if not (string.endswith(".cpp") or string.endswith(".c")): continue file = os.path.join(root, file) # print "file is : ", file fl(file, LENGTH) for dir in dirs: # print dir walkdir(dir) if __name__ == '__main__': # fl("UseThing.cpp", 50) LENGTH=150 DIR=os.path.abspath('CandyAT/Bin/Server_proj/') argc = len(sys.argv) if 3 != argc: print "argc :", argc print "Usage : ./fl.py max_length relative_dir" sys.exit(0) if (argc > 1): LENGTH = int(sys.argv[1]) if (argc > 2): DIR = sys.argv[2] print "Lines >", LENGTH, ", Search in dir:", DIR walkdir(DIR)
如果有什么不足或BUG,欢迎交流反馈和完善。
相关文章推荐
- MATLAB实现层次分析法计算权重的代码函数实现
- git是一种分布式代码管理工具,git通过树的形式记录文件的更改历史,比如: base'<--base<--A<--A' ^ | --- B<--B' 小米工程师常常需要寻找两个分支最近的分割点,即base.假设git 树是多叉树,请实现一个算法,计算git树上任意两点的最近分割点。 (假设git树节点数为n,用邻接矩阵的形式表示git树:字符串数组matrix包含n个字符串,每个字符串由字符'0
- C#代码行数的计算工具(递归的实现)
- 第七章 php自定义函数实现代码
- 介绍如何通过代码实现模拟按键的函数
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- Magento购物车价格计算的功能实现之代码理解
- php shell超强免杀、减少体积工具实现代码
- 用nodejs实现PHP的print_r函数代码
- javascript trim 去空格函数实现代码
- 计算一个文本文件有多少行(利用标准IO 函数实现)
- 2016书单总结--重构改善既有代码的设计--重新组织函数
- 自己模拟strcmp库函数写的功能函数my_strcmp实现strcmp的功能,完整代码
- js实现addClass,removeClass,hasClass的函数代码
- 关于矩阵计算的java 代码实现----笔记
- js实现的日期操作类DateTime函数代码
- 编写代码实现strpbrk()函数。
- 实现功能点1: 对 Maria 代码 git ssh 相关接口进行重构
- TGraphicControl(自绘就2步,直接自绘自己,不需要调用VCL框架提供的函数重绘所有子控件,也不需要自己来提供PaintWindow函数让管理框架来调用)与TControl关键属性方法速记(Repaint要求父控件执行详细代码来重绘自己,还是直接要求Invalidate无效后Update刷新父控件,就看透明不透明这个属性,因为计算显示的区域有所不同)
- calloc函数的实现代码