3 ms·
> You have a tendency to overengineer things. Overengineering is an actual problem. For a tiny example, I'll see things like: enum MAGIC = 67; // explana
by WalterBright 4y ago
> You have a tendency to overengineer things.
Overengineering is an actual problem. For a tiny example, I'll see things like:
enum MAGIC = 67; // explanation
...
foo(MAGIC);
The use of MAGIC is the only one, and is far removed. A better solution is:
foo(67); // explanation
because it improves locality.
I also see things like an object fleshed out with all kinds of member functions that are never used.
- maxbond 4y agoI think I can see what you're getting at that you view this as a premature abstraction, but I don't think I'd call it over engineering. I'd call it a stylistic difference. If `MAGIC` was sufficient to understand what the significance of the number was and `// explanation` was the citation or derivation, then I'd be fine with this, especially if there were a bunch of other constants with explanations with similar derivations or citations (eg a bunch of trig or constants from the same standard); then it'd be preserving a different sort of locality. If `MAGIC` wasn't sufficient to capture the purpose of the constant & such a name wasn't readily available, so that you're always going to want to read `// explanation` (rather than only in the case you think the value is wrong), then I'd agree with you. Concrete(ish) examples: if 67 terminated the Foo section of the Bar binary file format, even if we only used it once, I'd prefer to see END_OF_FOO rather than read a comment. Reading comments is context switching, at least for me. Names help me stay in the headspace of the programming language. If 67 was the result of an ad-hoc calculation which only made sense in the context of this particular program (how many angels can dance on the head of an Acme brand pin executing our trademarked choreography) while, and so there wasn't a great name for that calculation, or at least not a name that would make sense unless you've already read the derivation, then I'd agree in that case. That being said, I have absolutely seen overengineering or misengineering cause real problems.
- sokoloff 4y agoIn a code review, I’d given a junior programmer advice to avoid magic constants and use defines instead (c, not c++). Resubmission came back with: #define SEVENTEEN 17 Last I spoke with him, he was a Java instructor.
- zelphirkalt 4y agoIt is truly amazing. One can rave about OOP all day and not understand the basics of programming or good, readable code. Truly mindboggling.
- gymbeaux 4y agoAt the risk of sounding contrarian, I don’t think it’s mind-boggling at all. There are two types of software engineers out there: those who read to understand, and those who understand to read. What I mean by this is that I find most of my coworkers “read to understand”- they don’t know X (or, more commonly, they don’t understand X and want to prove the person who wrote “X” wrong), so they Google around until they find an SO that supports their stance, or gets them code that does what they want it to do…. Then there are those who understand that to be truly great is to read beyond the minimum needed to achieve the goal, be it to get software to do X, to prove someone wrong in code review, or something else… and to READ for pleasure and to quench the thirst of knowledge. The passionate folk, in other words. Most of the time you can find out which group a person falls into by whether they read about and write code in their free time. Kind of like leetcode, this litmus test I find has a low false-positive rate, but a relatively high false-negative rate, as there are many passionate engineers who don’t have the time or energy to read and write in their free time (typically a “wife and kids” scenario). E: in other words, those who code to live vs live to code.
- erehweb 4y agoThe truly brilliant thing to do would be #define SEVENTEEN 16
- chii 4y ago// the law changed, but we're too lazy to rename the constant
- stephenbennyhat 4y agoI worked for a while with a software engineer who insisted in encoding the value of the macro within the name: #define MAGIC_16 16 his rationale was that you had to go through all the uses and consider whether the change would cause an issue. It takes all kinds.
- seanhunter 4y agoI see where you're going. You never know when MAGIC will be needed and whether or not it will remain constant. It also doesn't scale horizontally. The right answer if you want to be fully robust is to make a Magic microservice that will return the magic number when needed and autoscale on demand. You could have the swagger doc for the service include the explanation.
- TuringTest 4y agoYou're joking, but your description of a prepare-for-any-circumstance mentality summarizes exactly the problem, you never know if MAGIC will be needed or not remain constant, so assuming it will not and making your program structure more complex for it is overengineering. The best approach if you want to be future-proof while would be to label the instance in-place, so that you can look later for places where #MAGIC is being used, without losing locality: foo(67); // #MAGIC explanation
- deleted 4y ago[deleted]