| Conditions | 2 |
| Paths | 16 |
| Total Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* TODO |
||
| 51 | app.get('/grades/', function (req, res, next) {
|
||
| 52 | if (program.fulllog) {
|
||
| 53 | var start = new Date(); |
||
| 54 | console.log((timeStamp() + 'Started to query the grades: ').cyan + req.query.id.yellow); |
||
|
1 ignored issue
–
show
|
|||
| 55 | } |
||
| 56 | access.login(req.query.id, req.query.pwd, res, function (headers, iires) {
|
||
| 57 | program.fulllog && console.log((timeStamp() + 'Successfully logged in.').green); |
||
|
1 ignored issue
–
show
|
|||
| 58 | var ret = {};
|
||
| 59 | var $ = cheerio.load(iires.text); |
||
| 60 | ret.name = escaper.unescape($('.block1text').html()).match(/姓名:.+</)[0].replace('<', '').substring(3);
|
||
| 61 | ret.id = req.query.id; |
||
| 62 | |||
| 63 | // 进入成绩页面 |
||
| 64 | superagent.get(base + $('li[title="我的成绩"] a').attr('href'))
|
||
| 65 | .set(headers) |
||
| 66 | .end(function (err, iiires) {
|
||
| 67 | if (err) {
|
||
| 68 | console.log((timeStamp() + 'Failed to fetch grades\n' + err.stack).red); |
||
|
1 ignored issue
–
show
|
|||
| 69 | ret.error = '获取成绩失败'; |
||
| 70 | return next(err); |
||
| 71 | } |
||
| 72 | program.fulllog && console.log((timeStamp() + 'Successfully entered grades page.').green); |
||
| 73 | |||
| 74 | $ = cheerio.load(iiires.text); |
||
| 75 | |||
| 76 | // 获取成绩列表 |
||
| 77 | let grades = {};
|
||
| 78 | let failed = {};
|
||
| 79 | $('#dataList').each(function (index) {
|
||
| 80 | // cheerio没有实现jQuery的lt |
||
| 81 | if (index >= 2) |
||
| 82 | return; |
||
| 83 | $(this).find('tr[class!="theadCss"]').each(function() {
|
||
| 84 | // 这段写得真是要吐血了 |
||
| 85 | let subject = escaper.unescape($(this).find('td[align="left"]').eq(1).text());
|
||
| 86 | if (subject) {
|
||
| 87 | let score = $(this).find('font');
|
||
| 88 | if (score.text()) |
||
| 89 | grades[subject] = score.text(); |
||
| 90 | if (score.css('color'))
|
||
| 91 | failed[subject] = score.text(); |
||
| 92 | } |
||
| 93 | }); |
||
| 94 | }); |
||
| 95 | ret.grades = grades; |
||
| 96 | ret['subject-count'] = Object.getOwnPropertyNames(grades).length; |
||
| 97 | ret.failed = failed; |
||
| 98 | ret['failed-count'] = Object.getOwnPropertyNames(failed).length; |
||
| 99 | |||
| 100 | // 完成所有工作后,登出 |
||
| 101 | access.logout(headers, res, function() {
|
||
| 102 | // 第五步:返回JSON |
||
| 103 | res.send(JSON.stringify(ret)); |
||
| 104 | program.fulllog && console.log((timeStamp() + 'Successfully logged out: ').green + req.query.id.yellow + (' (processed in ' + (new Date() - start) + 'ms)').green);
|
||
|
1 ignored issue
–
show
|
|||
| 105 | }); |
||
| 106 | }); |
||
| 107 | }); |
||
| 108 | }); |
||
| 109 | |||
| 128 | console.log((timeStamp() + 'The server is now running on port ' + port + '.').green); |