Home > Blockchain >  'confirmPassword' is assigned a value but never used.eslint
'confirmPassword' is assigned a value but never used.eslint

Time:12-13

I know the way to fix the "unused error in eslint" but I am wondering if the variable in the first destructuring part is mandatory or I can just leave it as const [, setConfirmPassword] = useState("");

Below is my code

const [confirmPassword, setConfirmPassword] = useState("");

Thank you and sorry for being a noob... #I am new and first time to ask here, sorry if disturbing your time helping me

I've tried to run my file and it works fine but I want to know the best practice and clear my doubt. From what I search from the other forum they said it might waste the space and affect the run of the program so I am confuse whether it is okay to leave it with unused OR is it correct if I leave it blank.

CodePudding user response:

Usually in ESLint you can prefix a variable with an underscore to show that you're deliberately assigning a value, but don't plan to use it. This is also useful in cases where you're ignoring the first argument in a function:

// don't need to read value of password
const [_confirmPassword, setConfirmPassword] = useState("");

// only need to use second argument in callback
someObject.runCallback((_, b) => {console.log(b)})

In most cases, assigning an extra variable has low performance costs relative to the overhead of the code being less readable. If you need to fix a bug, or rewrite some code, or add a new feature, and in the past used some workaround to avoid assigning the variable, and that adds an extra ten minutes to development time because the code's less readable, that's probably more value lost than assigning an extra variable.


In your code snippet specifically, it's not clear why you need a setter but not a getter. There's no larger context in your question but I suspect that as you work on this piece of code you'll find a reason that you do need to use confirmPassword somewhere.

CodePudding user response:

Like Dakeyras's answer says underscores can be used to ignore a variable, but you can also use your own regular expressions with the varsIgnorePattern attribute to achieve the same.

Documentation:

The varsIgnorePattern option specifies exceptions not to check for usage: variables whose names match a regexp pattern. For example, variables whose names contain ignored or Ignored.

Examples of correct code for the { "varsIgnorePattern": "[iI]gnored" } option:

/*eslint no-unused-vars: ["error", { "varsIgnorePattern": "[iI]gnored" }]*/

var firstVarIgnored = 1;
var secondVar = 2;
console.log(secondVar);

CodePudding user response:

In JavaScript, it is perfectly fine to have unused variables in your code. The interpreter will not throw an error if you have an unused variable. However, it is generally considered a best practice to avoid unused variables in your code. This is because unused variables can clutter your code and make it harder to read and understand.

In your specific example, you are using destructuring to assign the values of confirmPassword and setConfirmPassword to variables with the same name. Since you are not using the first destructured variable, you can simply omit it from the destructuring statement, like this:

const [, setConfirmPassword] = useState("");

This will not affect the behavior of your code, and it will make it easier to read. However, keep in mind that if you later decide to use the first destructured variable, you will need to add it back to the destructuring statement.

In summary, it is fine to have unused variables in your code, but it is generally considered a best practice to avoid them whenever possible to make your code easier to read and understand.

  • Related