DAB0mB/radial-snake

Further improvements

Closed this issue · 2 comments

Hello, I looked through your code and found some more places where you could improve / simplify things.

  1. game.js draw function: We don't need to implement double buffering in JS since it runs your code until completion until it shows your stuff on the canvas. This can be seen in this fiddle: http://jsfiddle.net/HYVLj/ Here you see a green rectangle which will be cleared, then a long loop will run and after that a blue rectangle will be painted. As you can see, there will be no flickering.

  2. snake.js draw function: Why does the snake have to know how to paint a line or a circle? Your geometry object have some behaviour (checking for collisions e.g.) why not have them draw themselves? This way you only have to invoke draw on every shape in the snake. This leads to the suggestion that the snake is no special object but a simple shape group. (See composite pattern - https://en.wikipedia.org/wiki/Composite_pattern). If you want your geometry shapes to be simple data objects, then you could take a look at the visitor pattern: You could implement a drawingVisitor that can draw alle the shapes. If you are going to add another shape you only have to adapt the Visitor

  3. Please never ever touch built in prototypes. Ever wonder, why ES7 brought array.includes and not contains? Because so many developers extended the prototype with contains with different behaviour than the final spec. Write your own utility functions and use them from your own namespace.

@andreasgruenh Your notes are true and important. I will take notes and fix them as soon as possible. Just for the record, this is an old project I wrote a long time ago when I only started programming. Some of the stuff I chose to ignore, and some of them I simply missed, but I will fix everything due time. Thank you for submitting this issue!

@andreasgruenh I fixed everything listed in your comment and I released a new version. If you have more suggestions I'd be happy to hear it. Please share this tutorial and spread free education :-D