Skip to content
Advertisement

How to refactor this kind of code to fix codeclimate duplication report?

I use react & flux to develop my frontend web app:

So I define the constants like this:

/js/constants/AppConstants.js

const KeyMirror = require('keymirror');

module.exports = {
  PayloadSources: KeyMirror({
    SERVER_ACTION: null,
    VIEW_ACTION: null
  })
};

/js/constants/ProductConstants.js

const KeyMirror = require('keymirror');

module.exports = {
  ActionTypes: KeyMirror({
    GET_PRODUCT: null,
    UPDATE_PRODUCT: null,
  })
};

This totally works and is correct, then I push these codes to Github (with Codeclimate integration).

Codeclimate says:

Similar code found in 1 other location (mass = 54)
const KeyMirror = require('keymirror');

Obviously, we see that this line const KeyMirror = require('keymirror') was defined in 2 different files, and Codeclimate thinks this should be changed. But I was thinking, this is just a statement to import the library.

How do you think? How should I need to refactor this?

Advertisement

Answer

I agree with you, this is just a statement to import some other code, and it needs to be there whenever you want to use the functionality. So, the only possible refactoring would be to merge the two files AppConstants and ProductConstants. But this is probably not a good refactoring because it sounds like a good idea to separate the app constants and the product constants.

I would rather raise an issue with codeclimate than try to refactor this.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement