Game Development Reference
In-Depth Information
If you use a library, you avoid having to write all the code, but you're dependent on the library's
limitations . If you don't properly investigate beforehand whether the library will really solve your
problem, you may put a lot of effort into integrating the library into your code and then find out it
doesn't actually do the crucial thing you need it to do. Also, sometimes it's a good idea to write code
from scratch instead of using a library, because doing so forces you to understand the problem at a
deep level; therefore you may find a solution that is more efficient for your application. And finally, if
you write all the code from scratch, it's easier to extend or modify the code, because you wrote it.
All things considered, as a developer you have to develop a sense for which parts of the game you
want to program yourself (which takes time but leads to better understanding) and which parts you
want to use a library for (which gives results faster but may not be a perfect fit for your needs).
Efficiency of Code
JavaScript programs can run on many different devices, ranging from high-end desktop machines
to tablets and smartphones. This sometimes puts limits on the computational power that is available.
Therefore, it's crucial that JavaScript programs have efficient code. And this depends on how a
programmer solves a particular programming problem. Often, there are many possible solutions.
For example, consider the simple problem of creating an array and filling it with the numbers 0, 1, 2, 3,
and so on. Here is one way to do this:
var myArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
You can also use a for loop:
var myArray = {};
for (var i = 0; i < 20; i++)
myArray.push(i);
And here's yet another solution:
var myArray = {0};
while (myArray.length < 20)
myArray.push(myArray.length);
Each of these solutions delivers an array of size 20 containing the numbers 0-19. But which
solution you choose may depend on the context. The first solution (writing out the array) is very
straightforward, and by looking at the code it's immediately clear what the contents of the array will
be after the code has been executed. For smaller array definitions, this approach works very well.
However, this doesn't work if you need an array of size 300. The second solution, which uses a for
loop, is much more suitable in that case, because changing the desired length of the array only
requires changing one number in the header of the for instruction. The third solution uses a while
loop to solve the problem. It avoids having to declare a counter variable ( i ). However, this solution is
probably less efficient than the second solution, because in each iteration the length of the array has
to be retrieved twice (once in the header and once in the body).
 
Search WWH ::




Custom Search