Lol that’s what I noticed too.
One side wants less people to die, the other side wants fascism and racism. Please help me compromise.
So are you saying that me pushing a pregnant woman down the stairs is the same as doing so to a non pregnant woman?
Imo, no. Pushing a woman is assault, pushing a pregnant woman is assault and something else (another post suggested something akin to manslaughter, which I think fits if the assault causes a miscarriage)
the pro-life response is simply that the unborn child doesn’t get a say in the matter.
Correct. An unborn child doesn’t get a say in whether they are aborted or born. They have no opinions, they have no wants. The unborn child cannot consent to being aborted but they cannot consent to being born either. The only valid opinion and choice is that of the mother, because it’s the mother’s life that is very physically (and eventually also mentally, socially, etc) affected by the pregnancy.
Which is also why I said that pushing a pregnant woman should have harsher penalties than just assault: it also endangers or destroys something whose state of being only the woman should be in charge of.
It’s like if I pickpocket your wallet that’s stealing, but if I steal the wallet from your house that’s also breaking and entering.
Meh. Been developing professionally with C++ for 10 years at this point. I’m one of the weird people that kinda likes C++ and its pragmatism despite all its warts.
I’d like C++ better if it didn’t have inheritance. There are better solutions to model interfaces, and without inheritance people can’t write class hierarchies that are 10 levels deep with a different set of virtual functions overridden (and new virtual functions added) at each level.
And yes, that is not hypothetical. Real codebases in the real world shipping working products do that, and it’s about as nice as you can imagine.
You do have a terminology mismatch. In C++, an abstract class is a class with at least one pure virtual method.
Such classes cannot be instantiated, so they are useful only as base classes.
An interface is more of a concept than a thing.
Sure you can say that Iterable
is an interface that provides the Next()
and Prev()
methods and you can say that Array
is an Iterable
because it inherits from Iterable
(and then you override those methods to do the correct thing), and that’s one way to implement an interface in C++.
But you can also say that Iterable<T>
is a class template that provides a Next()
and Prev()
methods that call the methods of the same name on the type that they wrap (CRTP aka static polymorphism).
Or you can say that an algorithm that scans a collection T
forward requires the collection to have a Next()
method by calling Next()
on it.
And I can think of at least 2 other ways to define an interface that isn’t using abstract classes.
And even if using abstract classes, inheriting from them is definitely the least flexible way to use them to define an interface, because it doesn’t allow one to do something like mocking functionality in tests, because it’s not possible to redefine the class to be tested to inherit from the test interface implementation with mocked functionality, so one still needs something to the effect of dependency injection anyway.
So yeah, abstract class is very different from inheritance, and it’s also very different from interface, even though it relates to both.
Since my previous example didn’t really have return value, I am changing it slightly. So if I’m reading your suggestion of “rewriting that in 3 lines and a single nested scope followed by a single return”, I think you mean it like this?
int retval = 0;
// precondition checks:
if (!p1) retval = -ERROR1;
if (p2) retval = -ERROR2;
if (!p3 && p4) retval = -ERROR3;
// business logic:
if (p1 && !p2 && (p3 || !p4))
{
retval = 42;
}
// or perhaps would you prefer the business logic check be like this?
if (retval != -ERROR1 && retval != -ERROR2 && retval != -ERROR3)
{
retval = 42;
}
// or perhaps you'd split the business logic predicate like this? (Assuming the predicates only have a value of 0 or 1)
int ok = p1;
ok &= !p2;
ok &= p3 || !p4;
if (ok)
{
retval = 42;
}
return retval;
as opposed to this?
// precondition checks:
if(!p1) return -ERROR1;
if(p2) return -ERROR2;
if(!p3 && p4) return -ERROR3;
// business logic:
return 42;
Using a retval has the exact problem that you want to avoid: at the point where we do return retval
, we have no idea how retval
was manipulated, or if it was set multiple times by different branches.
It’s mutable state inside the function, so any line from when the variable is defined to when return retval
is hit must now be examined to know why retval
has the value that it has.
Not to mention that the business logic then needs to be guarded with some predicate, because we can’t early return. And if you need to add another precondition check, you need to add another (but inverted) predicate to the business logic check.
You also mentioned resource leaks, and I find that a more compelling argument for having only a single return. Readability and understandability (both of which directly correlate to maintainability) are undeniably better with early returns. But if you hit an early return after you have allocated resources, you have a resource leak.
Still, there are better solutions to the resource leak problem than to clobber your functions into an unreadable mess. Here’s a couple options I can think of.
defer
Example of option 1
// precondition checks
if(!p1) return -ERROR1;
if(p2) return -ERROR2;
if(!p3 && p4) return -ERROR3;
void* pResource = allocResource();
int retval = 0;
// ...
// some business logic, no return allowed
// ...
freeResource(pResource);
return retval; // no leaks
Example of option 2
// same precondition checks with early returns, won't repeat them for brevity
auto Resource = allocResource();
// ...
// some business logic, return allowed, the destructor of Resource will be called when it goes out of scope, freeing the resources. No leaks
// ...
return 42;
Example of option 3
// precondition checks
void* pResource = allocResource();
defer freeResource(pResource);
// ...
// some business logic, return allowed, deferred statements will be executed before return. No leaks
// ...
return 42;
Example of option 4
int freeAndReturn(void* pResource, const int retval)
{
freeResource(pResource);
return retval;
}
int doWork()
{
// precondition checks
void* pResource = allocResource();
// ...
// some business logic, return allowed only in the same form as the following line
// ...
return freeAndReturn(pResource, 42);
}
deleted by creator
deleted by creator