3 ms·
I know it's a tradeoff but I think I fall on the side of I wish the language I was writing in required `this` or `self` for members/properties. C++ foo =
by gernb 4y ago
I know it's a tradeoff but I think I fall on the side of I wish the language I was writing in required `this` or `self` for members/properties.
C++
foo = bar \* 2;
Are foo and bar local variables or members of some instance?
vs
Python
self.foo = self.bar \* 2
100% clear. No naming convention needed.
I bring this up because `_foo` for members is a naming convention that wouldn't be needed in a language that required `self` or `this`
That said, I get that maybe refactoring some code from standalone function to class method is easier if you don't have to change the code as much but I'd be curious how often that's a net win.
- TillE 4y agoWhen writing idiomatic C++, you typically end up with mostly stuff like impl->foo anyway. The pimpl pattern is sort of a weird artifact of how the compiler works, but it generally works out as a smart way to structure your code.
- BenFrantzDale 4y agoIt really depends. I used PImpl today but it’s the rare exception. Between small inlinable classes and abstract base classes, there are lots of ways work. But that’s the beauty of C++: there are lots of ways to solve problems!
- mhh__ 4y agoAs a language design point it's also saying that (this simple modification to C++) void add(const this, int x) Is more readable than (IMO) void add(int x) const
- kccqzy 4y agoYou may like the "deducing this" proposal: http://wg21.link/P0847 http://wg21.link/P0847 (Scroll down to the "proposed syntax" section.)
- TeMPOraL 4y agoMight as well just always spell out the hidden "this" argument. Then all methods would look just like regular functions, allowing to simplify the language syntax and the standard, making it more concise and consistent, without sacrificing any functionality.
- pronlover723 4y agoThat can go both ways? I agree that I like when functions are not special so instance.add(10) Is just sugar for add(instance, 10) and you pass anything that fits as the first argument. But, following the "syntactic sugar" is okay rule class Foo { add(int v); } Is just syntactic sugar for void Foo.add(Foo this, int v); ... or something along those lines... ?
- ldh0011 4y agoI worked with C++ at my last job and now again for a side project with a partner. People not using `this` is one of my biggest pet peeves.
- ihatepython 4y agoThis.