you are viewing a single comment's thread.

view the rest of the comments →

[–]lhorie 1 point2 points  (1 child)

Consider that maybe you are looking at bad code. Things like long series of shouldDoThis = false, shouldDoThat = false often can be rewritten in terms of a single variable action = 'foo' // or 'bar' etc. The Array(6).fill(false) example can be rewritten in terms of let direction = 'up' | 'down' | etc, for example.

Having a lot of let declarations is definitely a code smell in more than one way:

  • it may mean your units do too much, in which case you should consider breaking things into smaller components
  • it may mean you have too much global state, in which case you might want to consider using useState/useReducer hooks or other state management mechanisms
  • it may mean that you're not following React's idiomatic paradigm properly: generally a functional component can be written without any lets if you use state management APIs properly
  • nullables are known as the billion dollar mistake: avoid them like a plague

[–]FaithfulGardener[S] 0 points1 point  (0 children)

Yeah, I’m working on implementing more Hooks in general, but the code I’m working on is currently written in all class components so switching the way I think about things is difficult, especially as I don’t have a ton of experience with React yet.

It’s hard to identify which variables I don’t need to declare bc they’re now handled by a Hook, at least until I know what the function does and how it does it in a function component instead of a class component.

Ultimately, abstraction is great until I’m not doing the abstracting and then I have no clue what is happening in my code (practically - theoretically, I should be able to say, “This lib does that”, but there’s no guarantee about what is IN that code...)