Hi everyone, i am making a statistic about coding habit of unused variables, will you replace the unused variables by _
or just forget it and fix it until you make the production build. Forgetting that the unused variables will influnce the incremental compilation speed, i’m curious about that, please let me know.
Can’t you just remove an unused variable? Any examples?
The only unused variables in my code are from values returned by side-effects (for example, promises which have been resolved). I pipe those to the ignore
function to make the ignoring more explicit.
Aside from that, I just remove unused variables whenever the compiler warns me about them.
In my bsconfig unused values are configured to be treated as errors, sometime it can be annoying for prototyping but I really dislike dead code.
So inside a function I would use as _
for labelled parameters.
But outside prototyping, it can be necessary to ignore the return value, I used to do ->ignore
and now I find it cleaner to assign it like so let _ =
@ollehar Hi, i have created a hook which can generate state, setState and stateRef, the most convenient point of it is that you can get the stateRef updated immedatily when you called setState. This is very useful when you need the latest state value in both event callback and viewer.
let useStateRef = value => {
let (state, setState) = React.useState(_ => value)
let stateRef = React.useRef(value)
React.useEffect1(() => {
stateRef.current = state
None
}, [state])
(state, setState, stateRef)
}
Well, it causes a problem, i will write the whole expression when i am using this hook. Just because you don’t know wheter you only need this state value in event callback or just the viewer, or need the two both.
And you also need to repalce some of it by _
if your prototype does not need it. The frequent changes of prototype makes a lot of unused variable warnings, it annoyed me much… and it did influnces the incremental compilation speed seriouslly, so i choose to shut the warning check down.
You can also return a record in your hook, instead of a tuple, and use just the things you need.
Using the _
pattern is the normal way to ignore parts of a tuple.
let (_, _, x) = f()
Using records instead definitely makes it easier if you do it a lot, though.
Prefixing with underscore also works to silence warning but keep the name hint.
In my experience unused variables are very case to case and do require some finesse that can make it hard for junior engineers to address quickly.
The most outstanding for me are for functions implementing a common shared type including named arguments for a strategy/policy pattern. I would like to prefer removal of unused arguments, then underscore, then ignore, and this case can only really be resolved with ignore.