Applying Best practices

Hello people !

I’ve been slowly making progress through Udacity’s Javascript patterns course.

It led me to build a MVC (sort of) little application called Cat Clicker. The goal of this exercice was to separate contexts this way :

  • Model can only communicate with Controller;
  • Controller can call Model and View functions;
  • View Can only communicate with Controller.

You can see this here

I’ve learn quite a lot doing this, and while the code is lenghtier than my first try I feel it is easier to understand how it works, and is more easily expandable (I think).

My question is : Is there any way to make this look better/cleaner without using third party library ?

I’ve gone the vanilla JS way and I know my code could have been less verbose by using Jquery, but I wanted to better understand DOM stuff beforehand.

Thanks !

Cool! I like it… I don’t think your code is verbose at all. In fact I think the idea going around these forums about making your code footprint as tight and compact as possible is just crazy, and harmful. Save the fancy code with all the little tricks for places like Codewars, leave the code others may want to read and learn from as simple as possible. If that makes for more verbose code - to heck with it! Readability, clarity, and ease of understanding is king.

By-the-way here is my CatClicker

I really enjoyed doing this as well…

2 Likes

I have to echo @rickstewart here. Your code is pretty well readable and modular, @Mizu . If you needed to, you could change the model to be a database, a text file, or a server call without affecting the rest of your code, so it’s a job well done. I can’t think of any way to improve on it.

The drive for writing the shortest code possible is, I think, the best reason for learning algorithm analysis and data structures, even though web developers hardly ever need to write new algorithms or choose from a wealth of data structures. Terser code isn’t going to run appreciably faster by virtue of removing a few lines, but it could suck away hours from someone’s life if they need to debug it.

2 Likes

I think that’s a bit unfair. The actual length of the code in this case doesn’t come from how long or short each statement is but from the separation of business and presentation logic and from making sure that each function deals with only one thing.

Thanks guys !

Yes, I can’t tell why I do aim for shorter code, it seems like that’s just something I did throughout FCC exercices. And I do agree that it does lose its readability. I’ve went back to my old code on FCC and some even doesn’t make sense today…