5 ms·
There's another code smell that every programming whiz kid produces at some point: over-engineered. All code has a cost, primitives have a lower base cost beca
by dasil003 14y ago
There's another code smell that every programming whiz kid produces at some point: over-engineered.
All code has a cost, primitives have a lower base cost because they are universal in the language and thus every programmer will automatically know how to use them. Before introducing a custom object with yet another API to be learned, you need justification.
> Implement Money class if you need to deal with money, it’s much better than using floats all over the place. Don’t use Hash for configuration objects, use a custom Configuration class instead.
Let's pretend he said integer since floats for money is outright broken. Yes, Money is a clearcut case where having a money-specific API is both intuitive and immediately useful.
Configuration on the other hand is debatable. If you're just doing things a hash does then it's fine. If you find yourself with lots of helper methods or repeating the same transformations over and over then that's when you have a code smell that may warrant lifting configuration to its own level.
But the critical thing is to remember that there's a non-trivial cost to creating domain-specific objects.
- nwmcsween 14y agoThe justification is simple, to allow for testing concerns the code uses. I see domain specific objects the same way I see domain specific languages - if I cannot grasp it by looking at it then it's broken, meaning I should not have to read documentation but simply understand the domain the object / language represents e.g sinatra get()... etc.
- dasil003 14y agoYou state that as if it's a universal justification, but the same standard applies. If your code enough is simple enough to use primitives, then you're unit tests are testing the next higher level of abstraction. If the complexity rises to the level of justifying it's own class, then you should probably have unit test coverage on that. Also, regarding DSLs, there is a tradeoff there that's worth commenting as well. A DSL pays the most dividends when the domain is understood (such as HTTP in the case of Sinatra), but if it's custom business logic than there may not be enough common business understanding for a DSL to be intuitive, and in that case it's just another layer of indirection to follow through as you inspect the code to figure out what it's actually doing. Half-baked DSLs are harmful IMO.
- tzs 14y ago> Let's pretend he said integer since floats for money is outright broken. This reminds me of a story I read on Reddit that someone told about his dog. The first time his dog saw a horse, the dog was excited, and ran up to sniff the horse through the fence. It was an electric fence, and the dog touched it with his nose. The dog found this extremely unpleasant. Now the dog is deathly afraid of horses, and runs and hides whenever he sees a horse.
- dasil003 14y agoYou're being obtuse. What's your use case for money as floats?
- Negitivefrags 14y agoI think his point is that the OP got burned using floats for money. He thought the lesson was to use a custom class for money every time, but the actual lesson could have just been to use integers instead.
- yummyfajitas 14y agoRealtime trading systems. If your system is slow, you lose money. If your trading system says you earned $1,523,374.54, but your accounting system says you only earned $1,523,374.26, you don't care much.
- CodeMage 14y ago
- wglb 14y agoNow that is in interesting proposition: Let's pretend he said integer since floats for money is outright broken. On my 32 bit machine, enquire.c tells me that long integers have 32 bits of unsigned value. It also says that doubles have 53 bits of precision. So if you decide to use floats for money, you get bigger numbers than if you use long. Oh, and might I suggest that you consider doing it in pennies rather than fractional dollars? It will work out better. Better to understand the underlying territory, I think. That would not be over-engineering.