| Conditions | 25 |
| Total Lines | 87 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 51 |
| CRAP Score | 25 |
| 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:
Complex classes like main.rateGhData often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | // Copyright 2018 Fedir RYKHTIK. All rights reserved. |
||
| 17 | func rateGhData(ghData []Repository) string { |
||
| 18 | |||
| 19 | 1 | greetings := "" |
|
| 20 | |||
| 21 | // Add points by repository total popularity (more popular is better) |
||
| 22 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 23 | 1 | return ghData[i].Watchers > ghData[j].Watchers |
|
| 24 | }) |
||
| 25 | 1 | greetings += fmt.Sprintf("* The most popular project is `%s`\n", ghData[0].Name) |
|
| 26 | 1 | for i := range ghData { |
|
| 27 | 1 | ghData[i].PlacementPopularity = ghData[i].PlacementPopularity + i |
|
| 28 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 29 | } |
||
| 30 | |||
| 31 | 1 | greetings += fmt.Sprintf("* The newest project is `%s`\n", ghData[0].Name) |
|
| 32 | 1 | for i := range ghData { |
|
| 33 | 1 | ghData[i].PlacementAge = ghData[i].PlacementAge + i |
|
| 34 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 35 | } |
||
| 36 | |||
| 37 | // Add points by number of commits (more commits is better) |
||
| 38 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 39 | 1 | return ghData[i].TotalCommits > ghData[j].TotalCommits |
|
| 40 | }) |
||
| 41 | 1 | greetings += fmt.Sprintf("* The project with more commits is `%s`\n", ghData[0].Name) |
|
| 42 | 1 | for i := range ghData { |
|
| 43 | 1 | ghData[i].PlacementTotalCommits = ghData[i].PlacementTotalCommits + i |
|
| 44 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 45 | } |
||
| 46 | |||
| 47 | // Add points by number of tags (more tags is better) |
||
| 48 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 49 | 1 | return ghData[i].TotalTags > ghData[j].TotalTags |
|
| 50 | }) |
||
| 51 | 1 | greetings += fmt.Sprintf("* The project with more tags is `%s`\n", ghData[0].Name) |
|
| 52 | 1 | for i := range ghData { |
|
| 53 | 1 | ghData[i].PlacementTotalTags = ghData[i].PlacementTotalTags + i |
|
| 54 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 55 | } |
||
| 56 | |||
| 57 | // Add points by Top10 contributors followers |
||
| 58 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 59 | 1 | return ghData[i].Top10ContributorsFollowers > ghData[j].Top10ContributorsFollowers |
|
| 60 | }) |
||
| 61 | 1 | greetings += fmt.Sprintf("* The project made by most notable top contributors is `%s`\n", ghData[0].Name) |
|
| 62 | 1 | for i := range ghData { |
|
| 63 | 1 | ghData[i].PlacementTop10ContributorsFollowers = ghData[i].PlacementTop10ContributorsFollowers + i |
|
| 64 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 65 | } |
||
| 66 | |||
| 67 | // Add points by Top10 contributors followers |
||
| 68 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 69 | 1 | return ghData[i].ClosedIssuesPercentage > ghData[j].ClosedIssuesPercentage |
|
| 70 | }) |
||
| 71 | 1 | greetings += fmt.Sprintf("* The project with best errors resolving rate is `%s`\n", ghData[0].Name) |
|
| 72 | 1 | for i := range ghData { |
|
| 73 | 1 | ghData[i].PlacementClosedIssuesPercentage = ghData[i].PlacementClosedIssuesPercentage + i |
|
| 74 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 75 | } |
||
| 76 | |||
| 77 | // Add points by commits by day (more commits shows good healthy community) |
||
| 78 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 79 | 1 | return ghData[i].CommitsByDay > ghData[j].CommitsByDay |
|
| 80 | }) |
||
| 81 | 1 | greetings += fmt.Sprintf("* The project with more commits by day is `%s`\n", ghData[0].Name) |
|
| 82 | 1 | for i := range ghData { |
|
| 83 | 1 | ghData[i].PlacementCommitsByDay = ghData[i].PlacementCommitsByDay + i |
|
| 84 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 85 | } |
||
| 86 | |||
| 87 | // Add points by active forkers (more active forkers shows good open source spirit of the community) |
||
| 88 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 89 | 1 | return ghData[i].ActiveForkersPercentage > ghData[j].ActiveForkersPercentage |
|
| 90 | }) |
||
| 91 | 1 | greetings += fmt.Sprintf("* The project with the most active community is `%s`\n", ghData[0].Name) |
|
| 92 | 1 | for i := range ghData { |
|
| 93 | 1 | ghData[i].PlacementActiveForkersColumn = ghData[i].PlacementActiveForkersColumn + i |
|
| 94 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 95 | } |
||
| 96 | |||
| 97 | // Assign places to projects by all metrics |
||
| 98 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 99 | 1 | return ghData[i].PlacementOverall < ghData[j].PlacementOverall |
|
| 100 | }) |
||
| 101 | 1 | greetings += fmt.Sprintf("* The best project (taking in account placements in all competitions) is `%s`\n", ghData[0].Name) |
|
| 102 | |||
| 103 | 1 | return greetings |
|
| 104 | } |
||
| 105 |