How not to Vue
A list of bad things I’ve found on my new job
Click here to share this article on LinkedIn »
Not so long ago, I got a new job. And when I saw the code base for the first time, it terrified me. Here I want to show the things you should avoid in Vue.js applications.
Static properties in data/computed
There are no reason to pass static properties to data
and especially in computed
. When you do it, Vue makes these properties reactive but it’s unnecessary.
Thinking that non-reactive data will be reactive
Remember: Vue is not wizard. Vue doesn’t know when your cookies update.
I mentioned cookies because my colleague spent 2 hours to understand why his computed property doesn’t update.
Also, I recommend you not to use any side-data in computed properties. There should be no side effects in your computed properties. It will save you a lot of time. Believe me
Mixins with things that should be done once
Mixins are fine *somebody closes post right now*… Mixins are fine in some cases:
- Creating plugins that modifies Vue instance, giving new features
- Using common specific methods in different components/throughout the application
But one man made a global mixin with very slow actions in mounted
hook. Why not? Because it is being called on each component mount but may be called only once as well.
I won’t show this code. Instead, to make it clearer, I’ll show you a simpler example.
Incorrect work with setTimout/setInterval
At an interview, one of frontend developers in my team asked me if we can use timeouts/intervals in components. I answered “Yes” and wanted explain how to do it correctly but I was blamed as incompetent.
I dedicate this part to a man after whose code I have to maintain now
Mutating parents
Thing that I really don’t like in Vue and want to be removed (Evan, please)
I see no real use cases to use $parent
. It makes components inflexible and may confuse you with unexpected problems.
If/else form validation
I was really confused when I’ve found forms with manual validation. It generates lots of useless boilerplate code.
Instead of conclusion
It’s not all sins that junior Vue.js developers do, and I’m sure that this list may be infinite. But I think it‘s quite enough.
So, what “interesting” things did you see in Vue.js projects? Respond here if you have what to tell :)
Thanks for reading! And don’t repeat dumb errors :)
Special thanks to people who contribute to carbon.now.sh. It’s really nice!