Table of Contents
Question content
Solution
Home Java Sonar issue: define a constant instead of copying this literal

Sonar issue: define a constant instead of copying this literal

Feb 10, 2024 am 08:15 AM

Sonar problem has always been one of the problems that programmers often encounter in PHP development. When working with text, many developers are used to copying and pasting text, but there are some potential problems with doing so. PHP editor Xiaoxin suggests that in order to avoid these problems, it is best to define the text that needs to be reused as a constant. This will not only improve the maintainability of the code, but also reduce the possibility of errors and improve the quality of the code. In this article, we will discuss the sonar issue in detail and provide some solutions to help developers better deal with this problem.

Question content

The java file in my project has the following log line 3 times==

1

2

3

4

log.info(

                    "Queuing workflow message with ID {} and shardedDatabaseId {}",

                    workflowQueueMessageString,

                    shardedDatabaseId);

Copy after login

This creates a serious problem for sonar==

Define a constant instead of repeating this text "Queue workflow message using id {} and shardid {}" 3 times.

I can understand the reason for this error. But the string is not actually a constant and has placeholders like {}. So how to correctly resolve this sonar reporting issue.

Solution

This string is the format of the log message. Even if the message itself is not constant, the format is constant.

Just declare it (the format) as a variable - final, static, whatever suits the use case.

1

2

3

4

5

6

String messageFormat = "Queuing workflow message with ID {} and shardedDatabaseId {}";

log.info(messageFormat, workflowQueueMessageString, shardedDatabaseId);

...

log.info(messageFormat, workflowQueueMessageString, shardedDatabaseId);

...

log.info(messageFormat, workflowQueueMessageString, shardedDatabaseId);

Copy after login

The above is the detailed content of Sonar issue: define a constant instead of copying this literal. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)