3 ms·
I've recently programmed quite a lot using 'Visitor'. But looking at my code a few months later it seemed hard to understand. Why? I think it is because the ab
by galaxyLogic 5y ago
I've recently programmed quite a lot using 'Visitor'. But looking at my code a few months later it seemed hard to understand. Why? I think it is because the abstraction level is too high. The names in the code don't refer to anything that has to do with the problem the code is trying to solve.
For instance I needed to traverse some directories and do something with the files and subdirs in them. So I used my parametric Visitor class.
But when I now read that code it has calls to visit() etc. and when I debug the code it never mentions 'files' and 'folders'. That has been abstracted away into parameters. And that makes it difficult to understand what the code is actually doing, what it is accomplishing.
So, I've found it gets easier if I rewrite the code to use the vocabulary of the problem domain. It still follows the same (Visitor-) "pattern". The pattern is there but code does not use the vocabulary of the pattern, but of the concrete things it is dealing with.
And maybe that's why Design Patterns are important. Not as a code-library but as explanations of "patterns" that are useful when writing software.
- antman 5y agoNaming in a certain way is mostly for explanatory reasons in the tutorials and minimally for actual code. I just name the top one Visitorxxx and the rest as per their actual function. And when reading about them I also could not remember until using them multiple times. But at the end of the day, you learn each one, and then don’t need to remember how the respective parts of the code work. Else after a few months I would need to go line by line of probably worse code, unless in a happy accident I had reinvented them.