| Conditions | 34 |
| Total Lines | 114 |
| Code Lines | 68 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 67 |
| CRAP Score | 34 |
| 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 = i + 1 |
|
| 28 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 29 | } |
||
| 30 | |||
| 31 | // Add points by age (newest is better) |
||
| 32 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 33 | 1 | return ghData[i].Age < ghData[j].Age |
|
| 34 | }) |
||
| 35 | 1 | greetings += fmt.Sprintf("* The newest project is `%s`\n", ghData[0].Name) |
|
| 36 | 1 | for i := range ghData { |
|
| 37 | 1 | ghData[i].PlacementAge = i + 1 |
|
| 38 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 39 | } |
||
| 40 | |||
| 41 | // Add points by number of commits (more commits is better) |
||
| 42 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 43 | 1 | return ghData[i].TotalCommits > ghData[j].TotalCommits |
|
| 44 | }) |
||
| 45 | 1 | greetings += fmt.Sprintf("* The project with more commits is `%s`\n", ghData[0].Name) |
|
| 46 | 1 | for i := range ghData { |
|
| 47 | 1 | ghData[i].PlacementTotalCommits = i + 1 |
|
| 48 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 49 | } |
||
| 50 | |||
| 51 | // Add points by average contribution in days (longer is better) |
||
| 52 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 53 | 1 | return ghData[i].AverageContributionPeriod > ghData[j].AverageContributionPeriod |
|
| 54 | }) |
||
| 55 | 1 | greetings += fmt.Sprintf("* The project with biggest average contribution period is `%s`\n", ghData[0].Name) |
|
| 56 | 1 | for i := range ghData { |
|
| 57 | 1 | ghData[i].PlacementTotalCommits = i + 1 |
|
| 58 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 59 | } |
||
| 60 | |||
| 61 | // Add points by number of tags (more tags is better) |
||
| 62 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 63 | 1 | return ghData[i].TotalTags > ghData[j].TotalTags |
|
| 64 | }) |
||
| 65 | 1 | greetings += fmt.Sprintf("* The project with more tags is `%s`\n", ghData[0].Name) |
|
| 66 | 1 | for i := range ghData { |
|
| 67 | 1 | ghData[i].PlacementTotalTags = i + 1 |
|
| 68 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 69 | } |
||
| 70 | |||
| 71 | // Add points by Top10 contributors followers |
||
| 72 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 73 | 1 | return ghData[i].Top10ContributorsFollowers > ghData[j].Top10ContributorsFollowers |
|
| 74 | }) |
||
| 75 | 1 | greetings += fmt.Sprintf("* The project made by most notable top contributors is `%s`\n", ghData[0].Name) |
|
| 76 | 1 | for i := range ghData { |
|
| 77 | 1 | ghData[i].PlacementTop10ContributorsFollowers = i + 1 |
|
| 78 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 79 | } |
||
| 80 | |||
| 81 | // Add points by Top10 contributors followers |
||
| 82 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 83 | 1 | return ghData[i].ClosedIssuesPercentage > ghData[j].ClosedIssuesPercentage |
|
| 84 | }) |
||
| 85 | 1 | greetings += fmt.Sprintf("* The project with best errors resolving rate is `%s`\n", ghData[0].Name) |
|
| 86 | 1 | for i := range ghData { |
|
| 87 | 1 | ghData[i].PlacementClosedIssuesPercentage = i + 1 |
|
| 88 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 89 | } |
||
| 90 | |||
| 91 | // Add points by commits by day (more commits shows good healthy community) |
||
| 92 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 93 | 1 | return ghData[i].CommitsByDay > ghData[j].CommitsByDay |
|
| 94 | }) |
||
| 95 | 1 | greetings += fmt.Sprintf("* The project with more commits by day is `%s`\n", ghData[0].Name) |
|
| 96 | 1 | for i := range ghData { |
|
| 97 | 1 | ghData[i].PlacementCommitsByDay = i + 1 |
|
| 98 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 99 | } |
||
| 100 | |||
| 101 | // Add points by active forkers (more active forkers shows good open source spirit of the community) |
||
| 102 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 103 | 1 | return ghData[i].ActiveForkersPercentage > ghData[j].ActiveForkersPercentage |
|
| 104 | }) |
||
| 105 | 1 | greetings += fmt.Sprintf("* The project with the most active number of forkers is `%s`\n", ghData[0].Name) |
|
| 106 | 1 | for i := range ghData { |
|
| 107 | 1 | ghData[i].PlacementActiveForkersColumn = i + 1 |
|
| 108 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 109 | } |
||
| 110 | |||
| 111 | // Add points by returning contributors (more returning contributors shows good open source spirit of the community) |
||
| 112 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 113 | 1 | return ghData[i].ReturningContributors > ghData[j].ReturningContributors |
|
| 114 | }) |
||
| 115 | 1 | greetings += fmt.Sprintf("* The project with biggest number of returning contributors is `%s`\n", ghData[0].Name) |
|
| 116 | 1 | for i := range ghData { |
|
| 117 | 1 | ghData[i].PlacementActiveForkersColumn = i + 1 |
|
| 118 | 1 | ghData[i].PlacementOverall = ghData[i].PlacementOverall + i |
|
| 119 | } |
||
| 120 | |||
| 121 | // Assign places to projects by all metrics |
||
| 122 | 1 | sort.Slice(ghData[:], func(i, j int) bool { |
|
| 123 | 1 | return ghData[i].PlacementOverall < ghData[j].PlacementOverall |
|
| 124 | }) |
||
| 125 | 1 | greetings += fmt.Sprintf("* The best project (taking in account placements in all competitions) is `%s`\n", ghData[0].Name) |
|
| 126 | 1 | for i := range ghData { |
|
| 127 | 1 | ghData[i].PlacementOverall = i + 1 |
|
| 128 | } |
||
| 129 | |||
| 130 | 1 | return greetings |
|
| 131 | } |
||
| 132 |