I am fixing the legacy code base and there I faced this sonarlint reported issue whether a same string is initialized multiple times. so this code smell is generated by sonarlint.
What is the issue :
Same string is initialized multiple times.
Why we should fix this :
- so if we want to update we have to update at all the occurrences, but that process is not efficient
What is the fix :
- Create a constant and use that instead , in this way we will update only at one location and it will be changed in all the locations.
in the below mentioned screenshot we can see the same string is declared multiple times. Lets make it a constant and use it through out the file
fix is this
- A constant with the error string and it can be used to log the error message anywhere.
private static final String ERROR_RETRIEVING_FIELD="Error retrieving field layout.";