Let's distinguish between data structure manipulation and logic code.

What I felt when I started writing source code using STL (Standard Template Library) in C ++ language was that the data structure operations were prepared in the standard library, so the logic part became easier to understand. That's right.

** Library of unique data structure **

When I read code that used its own pre-STL data structures, I found that the indistinguishability between data structure operations and logic hindered my understanding of the code. For example, even if there is a code that uses an array of structures for which an area has been reserved, the code for assigning the initial value, the code for assigning a valid value to the i-th array element, and how many valid values are included. The magic number that tells you where the value is and where it is invalid, which means that if you are manipulating the structure solidly, you will not be able to distinguish between manipulating the data structure and the logic you are implementing.

** Common operations required after all **

Therefore, even if you use your own data structure such as a structure array, you must eventually create a function that adds data, assigns data, copies data, displays data, deletes data, and so on. Furthermore, do not copy structure data with memcpy (Note 1). Even when coding within the scope of C, it is essential to implement (encapsulate) and test operations (de facto methods) on those data structures. By testing such a method, you can use the structure array with confidence (Note 2).

** Separate data structure operations and logic **

When such de facto methods are in place, data structure operations and logic code can be clearly distinguished, making the logic of the program easier to understand. So let's write the source code to distinguish between data structure manipulation and logic code.

** Legacy code improvements **

When improving your legacy code, start with refactoring. Do not suddenly try to rewrite with STL. Add unit tests to your legacy code. Make sure your code is correct by unit testing. Don't try to use the STL to rewrite what your legacy code is doing before it's fully trivial.

Postscript: For me as a Python shop, Python's standard data structures, lists (C ++) By using the equivalent of std :: vector in C ++ and the equivalent of std :: unsorted_map in C ++, it can be said that the standard data structure was used. It can also be said that Python's rich standard library group had the same effect as using C ++'s Boost library and using a standard rich and reliable library that is OS-independent. "Sometimes you have the urge to rewrite everything from scratch, but you have to overcome that temptation." Source [[06] Things to watch out for when refactoring](http: // xn- -97-273ae6a4irb6e2hsoiozc2g4b8082p.com/%E3%82%A8%E3%83%83%E3%82%BB%E3%82%A4/%E3%83%AA%E3%83%95%E3%82%A1 % E3% 82% AF% E3% 82% BF% E3% 83% AA% E3% 83% B3% E3% 82% B0% E3% 81% AE% E9% 9A% 9B% E3% 81% AB% E6 % B3% A8% E6% 84% 8F% E3% 81% 99% E3% 81% B9% E3% 81% 8D% E3% 81% 93% E3% 81% A8)

Postscript: When developing a program, it is easier to determine the code for data structure operations and logic code for data structure operations. As the manipulation of the data structure becomes final, the code tends to become sufficiently dead code. On the other hand, the algorithm is likely to be changed many times due to trial and error unless the algorithm that has already been completed is ported. Therefore, it is advisable to keep the data structure operations in a separate file.

Note 1: With memcpy, it is easy to make an unintended shallow copy, and the character string of a certain structure (pointer of the start position) and the character string of the copy destination structure (pointer of the start position) become the same. If you change one, the other will also change. In particular, memcmp should not be used to compare structures. EXP04-C. Do not compare bytes including structures.

Note 2: My personal opinion is that STL should be used. Replacing a struct array with an STL in an existing program can be tricky. In such cases, I think it's a good idea to add de facto method functions to reduce the manipulation of bare data structures on struct arrays.

Recommended Posts

Let's distinguish between data structure manipulation and logic code.
Python data structure and internal implementation ~ List ~
Python data structure and operation (Python learning memo ③)
Let's do MySQL data manipulation with Python
Exchange encrypted data between Python and C #
Let's visualize the relationship between average salary and industry with XBRL data and seaborn! (7/10)
Distinguish between numbers and letters with regular expressions
Relationship between Firestore and Go data type conversion