6 ms·
I once worked maintenance on a large C++ program used in production by a lot of customers. It was odd in several ways, but the feature that stands out in my min
by mikelevins 9y ago
I once worked maintenance on a large C++ program used in production by a lot of customers. It was odd in several ways, but the feature that stands out in my mind was the numerous classes that were not defined anywhere in the source code or libraries.
If that sounds unlikely to you, it sounded unlikely to me, too. I wasted a lot of time trying to figure out where they were defined. I couldn't ask the original author of the code; he had moved on.
Eventually, I found them, sort of. They were being defined by a sed script that ran during the build process. It read the sources before they got to the compiler, constructed class definitions on the fly, and injected them into the code before it was fed to the compiler. So the definitions were right there in the code that the compiler saw; they just weren't anywhere in the code that humans could see.
Why was it done that way? I have no idea.
- mabbo 9y agoI have a simple rule about adding "magic" like that- if you can't make it immediately obvious to the readers of the code just what it is the magic is going to do, don't do it. AOP Java lead me to that rule because of too many obscure annotations that did insane things to help one developer avoid some minor nuisance.
- paulie_a 9y agoI agree with that rule. Sometimes you have to get the job done in a "less than ideal" way. But a lot of documentation/comments should be left to justify and more importantly explain how it works.
- ndh2 9y agoI disagree that you shouldn't do it. The way you do it is to add a comment // This %thing% was generated from %template% using data from %data%. If your language is too restrictive to add sensible tools, you should write the tools yourself. As with any code, write it in a way such that other people will be able to understand it. There is no magic, it's just code. I wish people would stop using that word.
- msangi 9y agoThe original comment says "they just weren't anywhere in the code that humans could see." Where would you and the comment in a situation like that one? I don't have anything against generated code but it should be visible and, as you said, it should be crystal clear where it come from
- ndh2 9y agoI think in this instance it's also a matter of tools. Visual Studio/Visual Assist would have been able to find the class definition, and that's where the comment would go. For cscope it's a matter of configuration, which should be auto-generated by the build tool. If the class is being used, then the definition has got to be somewhere, hopefully in a header. It's simply a file that is included somewhere. And it will be human readable. There is no magic.
- mikelevins 9y agoThe class definitions were not in any file. That was the entire point. They didn't exist at all except as transient build artifacts.
- ashleyn 9y agoIf I had to guess, it was a hamfisted way of doing JS-style objects in C++. They could just slap properties onto objects, etc, and the build process will determine what each class needs. Clever, but terrible.
- mikelevins 9y agoFor what it's worth, it was before JS existed.
- C4stor 9y agoHonestly, that sounds pretty normal, or at least ok, to me. Auto-generation of code is one of my primary daily tools, and I think it's just right for whole categories of problems. I currently generate a good third of my compiled sources, and also generate a rudimentary typescript library out of my source code to be used by the my colleagues. That being said, I usually work in Scala which provides language-based tools for that, so it definitely helps with avoiding the "dark magic" sentiment you may have had. But why was it done that way ? It reduces boilerplate, copy-paste errors, code duplication, in ways sometimes not made possible by inheritance or composition.
- mikelevins 9y agoHey, I'm a common lisp programmer. I'm down with automated code generation. But in Lisp or in Scala, as you say, it's in-language, so you can see what's going on by reading the code. This was different.
- ups101 9y agoDid you consider the point about code being defined only at build-time, not being available for inspection by the developer? That sets it apart from most auto-generating code scenarios I've seen.
- mirceal 9y agoWhat’s your opinion of languages that can do stuff like this, dynamically at runtime? And it’s even better: you can define, augment, redefine or remove classes at runtime.