by okasaki · 11y ago · 0 comments
for example
drawline(int,int,int,int); // obscure
drawline(Point,Point); // clearer
this is only "clearer" until you find yourself at the very common case where you use several libraries where each defines its own "Point".Now you're writing something like
drawline(Point(vec.x1, vec.y1), Point(vec.x2, vec.y2))
Pretty ugly!And of course drawline(Point, Point) isn't clearer at all imo. What's the coordinate system? What are the units? Any performance notes on this function? Etc. I'll be checking the documentation the first time anyway.
void do_something(vector<string>& v)
{
string val;
cin>>val;
// ...
int index = 0; // bad
for(int i=0; i<v.size(); ++i)
if (v[i]==val) {
index = i;
break;
}
// ...
}
this is shown to be inferior to the find() version. Again, it's only inferior until the very common case where you're doing something else with the index, which is pretty common during debugging. Then you'll be rewriting the above "bad" version anyway.Similar problems other higher order functions like for_each().
Good question. Where are you going to look that up? In the documentation of drawline? Should drawrect, drawcircle, etc..., also include documentation about the coordinate system/units? Of course not, that information should be centralized. That's where Point come in.
And that's just a detail, the main advantage of using a class is that is abstract details. In the Point class, you may want to switch from int to int64_t without having to revisit all the methods.
This kind of nonsense is just a recipe for everyone spending lines of code and neural cycles on a bunch of junk that will just be unpacked back into the underlying representation anyway in order to be used in the real world.
Don't do it. Simplicity trumps almost everything in real engineering. Abstract where abstractions add real value (which usually means "hide non-trivial amounts of code) and not where they do nothing but "document" stuff.
void drawLine(lib1::Point, lib1::Point);
void drawRectangle(lib2::Point, lib2::Point);
In this case the usage is clear (and IIRC I think most IDE's will avoid omitting lib1:: and lib2:: in compiled code even if you do). The whole point of namespacing is to avoid the name collision issues that are (were?) inherent to C.This kind of scenario isn't uncommon: https://developer.apple.com/library/ios/documentation/2DDraw...
If both libraries just passed two doubles around, you wouldn't be able to catch this at compile time.
That's the point. The compiler is useless here, and this kind of glue code is probably 60+% of what developers are actually writing in practice.
But this site wants us to sit down and write a ton of boilerplate to handle a "Point" abstraction (complete with a full operator suite I'm sure, yada yada) just to solve a problem that can't be solved in the first place.
That information can be encoded into the class type, in which case you get compile time checking. I wrote a blog post about this some time ago:
http://www.randomprogramming.com/2014/06/quote-of-the-week-t...
The summary is that we had two different coordinate systems in play and had many bugs caused by passing values in the wrong coordinate system around. By making the different coordinate systems different classes everything became explicit and checked by the compiler. We completely eliminated a whole class of bugs from our program.
The great thing about using an int is that it can be anything (integer). It can be an x coordinate, or the number of eggs or a selection between multiple options.
The bad thing about using an int is that it can be anything, and sooner or later you will pass the number of eggs into a function that really wants an x coordinate.
Distance d = feet(7);
d += meters(3);
std::cout << d.yards();
Distance d2 = 3; // I won't compile
This assumes that you want to mix units, not defeat it. It all ends up being very efficient; use MKS for the underlying representation. You can multiply, add, and so on, quite efficiently; the only conversions happen on input and output.This. I did the very same thing when dealing with some code that was frequently converting between three coordinate systems. Using Haskell code duplication was close to non existent and still type-safe. Not wanting to sound too fan-boyish ... but that was one of the first times that I realized that a good type-system can help you prevent whole classes of bugs.
True, though you can usually reduce the pain by implementing appropriate conversion operators and/or implicit constructors.
> Again, it's only inferior until the very common case where you're doing something else with the index, which is pretty common during debugging.
You can use subtraction to obtain the index from the iterators in the better version. In this case, find really is superior.
Plus, a lot of problems are solved by namespacing and something like this:
typedef std::tuple<int, int> Point;drawline({x0,x1}, {x2,x3})
drawline
You can always define an implicit constructor: Point::Point(const Library::Point &p)
Of course, you're now hiding a ticking time-bomb from a the next maintainer of that code.If you absolutely want the latter case, overloading is your friend:
drawline(int x1, int y1, int x2, int y2);
drawline(Point p1, Point p2) {
drawline(p1.x, p1.y, p2.x, p2.y);
}
This, in my opinion, is the most conducive to refactoring (and modularizing code).As an aside, you really should be doing:
drawline => drawlines
"Where's the one, there's many."Same problem where each library defines its own "String".
Would be nice if the standard libraries would define such basic types and everyone would just use the same ones.
This might work better without OO kitchen sink approach to types. Just standardize a simple data layout or interface and leave the possibility for libraries to define their own functionality in extension methods.
Still, as Herb Sutter asked at CppCon 2015: "How many of you have your own homegrown string types at your companies? ... Don't lie. ... Right. And those of you who didn't put up your hand and are just bashful, ... we know we have them." [1]
Only with C++11 did they add std::u16string, which is at least suitable for the (horrible) UTF16 encoding. Maybe we'll get an UTF8 capable std::string class in another 20 years.