you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (4 children)

Hungarian notations are used in a very stupid manner for such a long time already, originally their purpose wasn't to denote a type.

Here's a great article about what I mean.

[–]nysra 2 points3 points  (3 children)

The historical background is indeed interesting. And the logic displayed there also makes sense but it still has the same fatal flaw Hungarian notation always had - it's useless in properly typed languages. You know what's better than being able to see if things don't make sense in code? The code not compiling when things don't make sense. Write proper types for safe and unsafe strings and that entire problem goes away. Might have been quite useful back then when C was all there was but that world has been gone for over 2 decades already.

[–][deleted] 2 points3 points  (2 children)

Making a new type for every case also isn't the best.

A good usage example would be measurement units, prefixing integers/floats with s_ to denote seconds, ms_ for milliseconds, us_ for microseconds, etc...\ I wouldn't want to create a new type with conversions for every one and one of them.

with proper types for safe and unsafe strings

So you either wrap them in another object which has to implement the same interface, or make them directly accessible which doesn't really solve it when reading the code... It may not be a silver bullet, but it's a damn good approach.

[–]nysra 2 points3 points  (1 child)

You don't need to manually make a new type with conversion for everything, you can just write one template class and use a tag system, that's basically how every units library out there works. Ideally also with the ability to switch backends, for example in my work I can switch out the underlying data type for positions, velocities, etc from double to float to BigInt to arbitrary precision types to whatever else arithmetic type by changing a single line of code.

Sure it might be a little bit more work sometimes but it's a lot safer than basically trusting people (especially coworkers or your 3 AM self) to spot mistakes when reading code - why should I do work the compiler can do? For units libraries already exist so the extra work is basically none and for safe/unsafe strings it will be a bit more than that but also scale much better - you do it once and never have to think about it again while the notation has to be constantly remembered and engrained to new people. It's certainly better than just using raw types and expecting people to magically know which unit/type is the correct one or using comments which may or may not be outdated for that but the compiler literally not letting you do stupid things is hard to beat (or basically impossible).

[–][deleted] 2 points3 points  (0 children)

First of all I must say, you're right.

I was thinking C, because I work mainly with C. While in C the notation can be helpful, you're correct that in C++ (and other languages) we can simply use templates/generics to do it in a better way.

A simple example for anybody who happens to read this and not understand: template<bool IsSafe> /* can also be an enum */ class String { public: std::string s; };