linagora / tmail-backend

GNU Affero General Public License v3.0
41 stars 22 forks source link

Refactor codebase to use textblocks instead of multiline string concatenation #1186

Open HoussemNasri opened 1 month ago

HoussemNasri commented 1 month ago

The codebase contains many instances of multiline string concatenation, especially in testing classes when hardcoded JSON strings are defined. This leads to hard to read/maintan code like this:

"{" +
        "  \"newUser\":\"geraldine\"," +
        "  \"oldUser\":\"user\"," +
        "  \"status\":{" +
        "    \"A\":\"DONE\"," +
        "    \"B\":\"WAITING\"," +
        "    \"C\":\"FAILED\"," +
        "    \"D\":\"ABORTED\"}," +
        "  \"timestamp\":\"2018-11-13T12:00:55Z\"," +
        "  \"type\":\"UsernameChangeTask\"" +
        "}"

Text blocks is a new feature introduced in Java 15 that fixes this exact problem, turning the above string to:

"""
            {
              "newUser":"geraldine",
              "oldUser":"user",
              "status":{
                "A":"DONE",
                "B":"WAITING",
                "C":"FAILED",
                "D":"ABORTED"},
              "timestamp":"2018-11-13T12:00:55Z",
              "type":"UsernameChangeTask"
            }"""
""""                  

Automated mass refactoring tools such as Rewrite could be of use, especially the UseTextBlocks recipe.

When text blocks are used one could use IntelliJ's language injection feature to add syntax highlighting and error detection when dealing with JSON strings.

chibenwa commented 1 month ago

+1

Needs to idealy be discussed on the James mailing list.

Ok as long as we do not rely on Java string templates whose JEP is removed in Java 23.