Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add min lines configuration for no-identical-functions rule #320

Conversation

vanyaraspopov
Copy link

Fixes #225

@vanyaraspopov
Copy link
Author

Hello? Anybody home?
Please let me know about the term you can review my fix.

@vilchik-elena
Copy link
Contributor

Hi @vanyaraspopov,

We don't think it's a good idea to have this parameter configured by user.
What would be the use-cases when you would allow duplications of bigger functions?

@vanyaraspopov
Copy link
Author

Hi @vilchik-elena , thanks for your reply!

I'd like to leave small duplications rather than try to refactor that because it produces an extra code bigger than duplication itself or it takes a lot of time.

Generally I can say that I run into some issues with callbacks.

For example, react component in storybook.

const ComponentStories = () => {
  return (
    <div>
      {cases1.map((props, i) => (
        <div key={i}>
          <Component />
        </div>
      ))}
      {cases2.map((props, i) => (
        <div key={i}>
          <Component />
        </div>
      ))}
    </div>
  );
};

or this example

function foo() {
  const someVarWithLongName = "a";
  const anotherVarWithLongName = "b";
  baz(() => {
    console.log({
      someVarWithLongName,
      anotherVarWithLongName,
    });
  });
}

function bar() {
  const someVarWithLongName = "x";
  const anotherVarWithLongName = "y";
  baz(() => {
    //---^^ no-identical-functions
    console.log({
      someVarWithLongName,
      anotherVarWithLongName,
    });
  });
}

function baz(callback) {
  // do some stuff
  callback();
}

@vilchik-elena
Copy link
Contributor

@vanyaraspopov I understand that those are just examples and probably in real life the reported issues are harder to refactor but here I don't see any problem with refactoring.

const ComponentStories = () => {
  const componentInDiv = key => (<div key={i}>
          <Component />
        </div>);
  return (
    <div>
      {cases1.map((_, i) => componentInDiv(i))}
      {cases2.map((_, i) => componentInDiv(i))}
    </div>
  );
};
function helper(var1, var2) {
  const someVarWithLongName = var1;
  const anotherVarWithLongName = var2;
  baz(() => {
    console.log({
      someVarWithLongName,
      anotherVarWithLongName,
    });
  });
}

function foo() {
  helper("a", "b");
}

function bar() {
  helper("x", "y");
}

function baz(callback) {
  // do some stuff
  callback();
}

If it's hard to refactor or you don't want (e.g. you know you will change one duplicate later), you can disable the issue with a comment.

I am happy to hear more reasons to disable the rule for bigger functions if you have smth else in mind

@vanyaraspopov
Copy link
Author

vanyaraspopov commented Feb 1, 2022

@vilchik-elena Yes, those examples are simple just for demo but talking about first one I see no considerable difference. The second example came out oversimplified, my bad. I could think up a better one but it's not worth. The main idea I want to express is that small duplications are acceptable and I'd like to tune the size of it with the option.
Also it would be better to avoid using of suppressing comments because it may become a common practice.

@vilchik-elena
Copy link
Contributor

Finally our team decided to accept this config, I opened a PR #325 to include your contribution. Thanks!
Note that I had to adjust the options scheme as we want to accept single sonar-runtime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Minimum required functions line of rule no-identical-functions can be configured
2 participants