Good code can be refactored.

As I mentioned in my previous article, my recent work has focused on architectural refactoring and code refactoring. So, as always, I wrote a tool to help me do that. That way, I can finish the relevant work more quickly next time.

Before that, there were plenty of tools that could do similar things. If our company has already opened source Tequila (github.com/newlee/tequ…) Such architecture, dependency analysis tools. However, a simple architectural analysis is not enough for my needs. And, in the spirit of writing tools is to earn experience, I decided to write a tool of my own.

Introduction of Coca

From my practical experience, I divide refactoring into four types:

  • Hierarchical architecture reconstruction. Make adjustments to the code architecture without changing business logic. According to the principle of single responsibility and dependency inversion, the system modules are split and merged to clarify responsibility and reduce coupling degree. Reprogramming packages to divide the boundaries between packages and reduce coupling between code.
  • Schema refactoring. Design patterns are used to improve extensibility and readability against the bad taste of a particular pattern.
  • Model refactoring. In the case of including tests, aggregate the behavior into the model by identifying and discovering the behavior of the model.
  • Micro refactoring. For minor code bad smells, IDE refactorings can be used to quickly improve existing code without affecting business functionality.

Refactoring: Improving the Design of Existing Code focuses on microrefactoring. Because of the difficulty of refactoring projects, it is often easier for inexperienced individuals and teams to rewrite than to refactor.

So, BASED on my needs, I wrote my own tools to improve the design of code as it exists:

Coca is a Swiss Army knife for legacy refactoring. It can analyze badsmells in code, line count statistics, analyze calls and dependencies, do Git analysis, automate refactoring, and more.

GitHub address: github.com/phodal/coca

Installation:

  1. go get -u github.com/phodal/coca
  2. Download the executable directly from GitHub

Before executing all commands, Coca Analysis needs to be executed to generate the corresponding dependency data.

So, what features does Coca 1.0 include?

API call

This feature, currently developed primarily for Spring, seems to be the standard for backend services in the Java world.

REST API to generate

Just executing the Coca API produces the result we want:

  1. Function call diagram
  2. API call hierarchy

Here are the API statistics:

SIZE METHOD URI CALLER
36 GET /aliyun/oss/policy controller.OssController.policy
21 POST /aliyun/osscallback controller.OssController.callback
17 GET /subject/list controller.CmsSubjectController.getList
17 GET /esProduct/search search.controller.EsProductController.search
17 GET /order/list controller.OmsOrderController.list
17 GET /productAttribute/list/{cid} controller.PmsProductAttributeController.getList
17 GET /productCategory/list/{parentId} controller.PmsProductCategoryController.getList
17 GET /brand/list controller.PmsBrandController.getList
17 GET /esProduct/search/simple search.controller.EsProductController.search

Corresponding panorama:

Of course, you can easily filter the content you want through the parameters.

The goal of this feature is to build out bounded contexts when refactoring to DDD in the future.

Call diagram

You can also look at just one part of the dependency graph:

coca call -c com.phodal.pholedge.book.BookController.createBook -r com.phodal.pholedge.
Copy the code

Enter the full method name and the contain you want to remove:

Reverse dependency diagram

It can also generate the corresponding reverse call diagram:

coca rcall -c org.bytedeco.javacpp.tools.TokenIndexer.get
Copy the code

The results are shown below:

Behavior analysis

Since the code is just another part of the reflection system, we have to get more information from the version management tool, which leads to:

coca git
Copy the code

File modification statistics

Coca Git-t: coca Git-t: coca Git-t: coca Git-t: coca Git-t

ENTITYNAME REVSCOUNT AUTHORCOUNT
build.gradle 1326 36
src/asciidoc/index.adoc 239 20
build-spring-framework/resources/changelog.txt 187 10
spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java 170 10
spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java 159 15

Yes, these are the most frequently modified files in the Spring Framework, and the first three seem reasonable, but annotationutils.java is clearly not.

The corresponding DefaultListableBeanFactory. Java has about 2000 + lines.

They are god classes in terms of the number of lines of code and the number of changes, and are often buggy.

Code age statistics

Well, there are common features like coca Git-a:

+----------------------------------------------------------------------------------------------------------------------- --+-------+ | ENTITYNAME | MONTH | +----------------------------------------------------------------------------------------------------------------------- - + -- -- -- -- -- -- -- + | helloworld. Go 2.04 | | | README. Md 2.04 | | | imp imp. / go 2.04 | | |. Gitignore 2.01 | | | CMD/root go | 2.01 | |test/ coca_suite_test. Go | 1.94 | | learn_go_test. Go 1.94 | | | core/languages/Java/JavaLexer tokens 1.84 | | | The core/languages/Java/java_lexer. Go 1.84 | | | examples/step2 - Java/domain/ValueObjectD Java 1.84 | |Copy the code

Other features

There are basic analysis functions such as coca Git-b

+-----------+--------+
| STATISTIC | NUMBER |
+-----------+--------+
| Commits   |    350 |
| Entities  |    514 |
| Changes   |    255 |
| Authors   |      2 |
+-----------+--------+
Copy the code

You can explore for yourself if you are interested.

Knowledge abstraction

Well, that’s a feature I plan to implement next year.

Methods to extract

As a first step for this feature, I would like to extract the word from the code: coca Concept:

+------------------+--------+
|      WORDS       | COUNTS |
+------------------+--------+
| context          |    590 |
| resolve          |    531 |
| path             |    501 |
| content          |    423 |
| code             |    416 |
| resource         |    373 |
| property         |    372 |
| session          |    364 |
| attribute        |    349 |
| properties       |    343 |
| headers          |    330 |
+------------------+--------+
Copy the code

As the first simple version, I just did word segmentation and statistics, and the next step was professional statistics. It is then used to generate domain-specific specific languages.

Submission information extraction

You know, there’s plenty of information in there.

TBD

Extract Chinese annotations

Next, I should do something similar, hahaha

Bad taste recognition

This is a very generic feature that you can find in a variety of tools. So, I didn’t go out of my way to enhance it or add too many features — because I knew they were better than my tools.

Coca BS will get a JSON file with proposed changes:

{
   "lazyElement": [{"File": "examples/api/model/BookRepresentaion.java"."BS": "lazyElement"}... ] }Copy the code

Combined with the Fanta tool I wrote, corresponding suggestions can be generated.

Batch refactoring

It is mainly used to combine the analysis results of the above tools to achieve automatic mass correction through artificial + intelligence.

Do not use the API in production because it is in the experimental stage. Supports the following functions:

  • Batch rename
  • Batch moving files
  • Delete unused import files in batches
  • Delete unused classes in batches

The way to use it is very simple:

coca refactor -m move.config -p .
Copy the code

You can try it, haha

Code statistical analysis

Code statistics tool, using SCC:

scc is a very fast accurate code counter with complexity calculations and COCOMO estimates written in pure Go

Cheerfully: Coca CLOC, then:

─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ Language Files Lines Blanks Comments Code Complexity ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ the Go 58, 31763, 7132, 890 23741 2847 Java 44 971 208 21 742 62 Markdown 8 238 75 0 163 0 Gherkin Specificati... 2 32 2 16 14 0 Document Type Defin... 1 293 36 0 257 0 License 1 201 32 0 169 0 SQL 1 2 0 0 2 0 SVG 1 199 0 34 165 0 Shell 1 3 1 1 1 0 XML 1 13 0 0 13 0 Gitignore 49 0 1 61 8 4 ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ the Total 119, 33776 7494 966 25316 2909 ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ Estimated Cost to DevelopThe $803,822 Estimated Schedule Effort 14.120551 months Estimated People Required 6.743156 ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─Copy the code

Other functions can be found in the official documentation of the SCC tool.

Refactoring fitness assessment

TBD

other

This is the first tool I wrote using Golang, so hopefully my usage is enough Go Style.

Home page: coca. Migration. Ink /

GitHub address: github.com/phodal/coca

Go get -u github.com/phodal/coca