skip to Main Content

I am getting following code smell in my sonarqube dashboard, and I am not sure how to resolve such issues.

‘isInitialized’ is already declared in the upper scope.

Here’s my code-

let _isInitialized = false;

/**
 * Getter for _isInitialized
 *
 * @return {Boolean} _isInitialized
 */
export const isInitialized = function() {
    return _isInitialized;
};

/**
 * Sets isInitialized
 * @param {Boolean} isInitialized new value for inititialized
 */
export const setIsInitialized = function( isInitialized ) {
    _isInitialized = isInitialized;
};

Can anyone please explain what’s the problem in my code?

Don’t know what to try as my code is working correctly but getting this issue on sonarqube dashboard.

2

Answers


  1. Here you define a variable called isInitialized in the scope of the module using const

    export const isInitialized = function() {
    

    Here you define a variable with the same name in the scope of the function using an argument name.

    export const setIsInitialized = function( isInitialized ) {
    

    Don’t do that. Use unique names for variables.

    Login or Signup to reply.
  2. tldr: There isn’t any problem with the code.


    SonarQube is configured to check a rule similar to the no-shadow rule of ESLint. It basically checks that the code does not reuse a name when another object is accessible in the same scope using that name.

    The function setIsInitialized() uses the name isInitialized for its parameter but in its scope, the global const isInitialized is also visible.
    The function parameter isInitialized hides the global const, rendering it unusable in the function body.

    Your current code:

    let _isInitialized = false;
    
    export const isInitialized = function() {    -- this is not available here --+
        return _isInitialized;                                                   |
    };                                                                           |
                                                                                 |
    export const setIsInitialized = function( isInitialized ) {                 |
        _isInitialized = isInitialized;             ^                | <-------- +
    };                         |                    |               /
                               +-- this is this ----+
    

    You can rename the parameter of setIsInitialized() to have access to the global const isInitialized and this will make the message disappear.

    let _isInitialized = false;
    
    export const isInitialized = function() {
        return _isInitialized;
    };
    
    export const setIsInitialized = function( initialized ) {
        _isInitialized = initialized;
    };
    

    Your code does not need this change because the setIsInitialized() function does not have any reason to use the isInitialized() function.

    However, avoiding shadowing is generally considered a good practice; it prevents confusion and misunderstanding for the code readers.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search