socket.on('event', function(){ ... } or some other functionality, I would literally try to do everything in one function, and it would grow and grow infinitely. Functions 200-300 lines of code long wouldn't impress me one bit back then. Now I would grab my head and probably start pulling hair off seeing such thing.It was hard to read. I remember that feeling. It was really tiresome to read all that action happening in once place.
It was hard to maintain.
It was hard to understand after a while.
We, coders, are writers. We write stuff. We read stuff. We read more stuff than we write. And without proper training and approach we tend to screw things over and over.
I've been trying to change the way I write code. I try to divide code into smaller parts (functions) with meaningful names. Sometimes it's difficult to choose a good and short name for a function. It requires some active thought process. Now when I read my code, I notice most functions are quite small, 1-5 lines of code. Some are bigger (10-40) or even 80 but the general tendency is to make things smaller (I'm going to have another look at those 80 lines).
Now, atomic code is not to be confused with compact/short code. 1 letter variable names are good for minified, production ready code, but not for maintaining. It is good to know some shortcuts, sometimes they are useful, but it is important to remember that we write code mainly for humans to read and only sometimes for machines to execute.
Also, don't be afraid to split your code into separate files. I find it best to put 1 'class' into a file (+ dependencies etc) and name the given file with the name of the class.
That's it for this post.
Brak komentarzy:
Prześlij komentarz