2. Use of inappropriate data structures
New programmers often focus heavily on algorithms. While it's great to be able to choose the right algorithm and use it effectively, simply memorizing them won't make you a programming genius.
On the other hand, memorizing the strengths and weaknesses of different data structures, in your programming language can definitely improve your skills as a developer. Using an inappropriate data structure is a clear sign of beginner's code. Here are a few examples:
Regular array or associative array?
The most common mistake is using regular arrays instead of associative arrays to store a list of records.
Ordinary array:
Associative array:
That's right, you should use associative arrays to store a list of records. By "list of records," we mean records that have an identifier. Arrays are justified for storing scalar values and if you plan to actively use methods like push, pop, shift, unshift, which address records not through the key, but through the order in the list.
The point is that searching inside an associative array is much faster than in a regular one. This is especially true for large collections, but it's better to keep this in mind when working with small ones as well.
Stack or recursion?
Optimizing recursion can be quite challenging, especially in a single-threaded environment. It becomes even more difficult if the recursive function calls itself two or more times.
Instead of recursion, you can use a data structure such as a stack. Put function calls on the stack and then retrieve them when the turn comes.
Last updated