|
Today i come across this two lines of code (i changed the naming of the functions):
if(GetA() && GetB())
*GetA() = *GetB();
took me nearly two hours of figuring out WHY DOES THIS CODE WORK, since i realised thatGetA() returns a Pointer and Pointers are IValue...
Looked deeper inside the code, i found a SetA(int) -Method...
Why to use something like
*GetA() = *GetB() if you could use
SetA(*GetB())
|
|
|
|
|
A possibly better (and much clearer) implementation would be to have GetA() and GetB() return references. You could then write the code without the idirection.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
That's even worse:
GetA() = GetB();
And due to if statement we must assume that A and B can be nulls. GetA and GetB should return const pointers.
|
|
|
|
|
My bad; I missed the if statement.
IMO, if code needs such a test, it probably has a poor architecture to begin with. It is much better to have a special dummy instance of a type to represent 'not present', and reserve the null pointer for truly bad situations that crash the program.
Reasonable people may differ on this.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
Sorry, that's not my code, an i cant change it. I just saw it in some code and where surprised what's that
Returning references would make it much harder to read:
GetA() = GetB()
looks like What the hell? Assignment to a Function?
|
|
|
|
|
C3D1 wrote: Returning references would make it much harder to read
We'll just have to agree to disagree on that. IMO, this is a quite useful C++ paradigm.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
Daniel Pfeffer wrote: IMO, this is a quite useful C++ paradigm. ...and that makes me even happier to barely being able to read C++
|
|
|
|
|
De gustibus non est disputandum
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
That's what I like about this forum. You go in thinking programming and you get a bit of Latin training. Hope I remember what the phrase means next time I see it.
|
|
|
|
|
Me too. There are plenty of sites around that merely answer programming questions, but only a few with the range of highly intelligent, humorous, and opinionated contributors that you find here. Amazingly, the amount of sniping and backbiting is kept to a minimum!
Long may it last!
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
Beautifully expressed and I agree totally.
"Rock journalism is people who can't write interviewing people who can't talk for people who can't read." Frank Zappa 1980
|
|
|
|
|
Daniel Pfeffer wrote: Amazingly, the amount of sniping and backbiting is kept to a minimum! Hear Hear (Or is that Here Here? ) Sometimes I find myself biting my tongue, but not on this thread. (Yet, and this thread is starting to grow a beard)
|
|
|
|
|
Daniel Pfeffer wrote: De gustibus non est disputandum I.e. C++ is the Durian (stinkfruit) in Software Development.
|
|
|
|
|
I agree, but I definitely would not name such a function "GetA".
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
Learn to read code.
Assignment to a function would be
GetA = GetB() which would mean GetB() returns a function pointer rather than an int* and GetA is function pointer rather than a function.
Learn to read code.
#SupportHeForShe If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
Only 2 things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein
|
|
|
|
|
You can't read C++ code without knowing the types. Who knows if equals was overridden, everything could happen.
Even then you should read, GetA() return something, then operator = is called on this something.
|
|
|
|
|
In two hours you can have a great lunch, jogging, some fun with your partner, some nice song listening/playing and also a shower.
All that cannot be done because the language is cryptic: that's why I hate C/C++!
Good post, though...
|
|
|
|
|
That's my issue with the language too. I once spent a day trying to understand a piece of code from a heavily modified Diku-based MUD when I was in college after a friend begged for help to fix some issues on his MUD.
|
|
|
|
|
Mario Vernari wrote: that's why I hate C/C++! Yea, I kind of agree with that sentiment, however, I have yet to run across the language that will supply me with a great lunch, but I do admit to sometimes having fun with it. (Does that mean I'm cheating on my partner?)
|
|
|
|
|
Mario Vernari wrote: that's why I hate C/C++! Yea, I kind of agree with that sentiment, however, I have yet to run across the language that will supply me with a great lunch, but I do admit to sometimes having fun with it. (Not C++ and does that mean I'm cheating on my partner?)
|
|
|
|
|
A good example of why you should use const:
const int* GetA() const { return &m_a; }
*GetA() = *GetB(); // compile error - use the Set function you numpty
|
|
|
|
|
totally agree with you.
But always use const can cause some other problems.
I personally try to avoid "pointer-getter-functions" but when i have to use, i do something like this:
class Foo
{
private:
int m_nA;
public:
int* GetAPtr() { return &m_nA; }
const int& GetA() const { return m_nA; }
void SetA(int nA) { ASSERT(IsValid(nA)); m_nA = nA; }
};
And if i have to use the pointer-getter i have to write Ptr explicit. So i see extremly fast that
*GetAPtr() = *GetBPtr();
is nonesense, and if you try to do
GetA() = GetB();
you get a compiler-error
Maybe my way overshot the mark a litte bit.
|
|
|
|
|
My only problem with that is that GetA and GetB are being called twice each. There is, of course, no guarantee that the second call to either will return the same value as the first. Further, there is not guarantee that either isn't an expensive operation.
I would probably go for the much clearer:
A* pa = GetA();
B* pb = GetB();
if (pa != NULL && pb!= NULL)
*pa = *pb;
Unfortunately, this method would always call GetB once, while the original would never call GetB if the first call to GetA returned NULL, so determining which is more efficient depends of how expensive the call to GetB is, and the likelihood than GetA returns null.
Which would give us this:
A* pa = GetA();
if (pa != NULL)
{
B* pb = GetB();
if (pb != NULL)
*pa = *pb;
}
Which, despite being the most keystrokes, would be the best method in terms of speed efficiency, memory efficiency (fewest assembly instructions), and code clarity.
In other words, just freakin' learn to type.
Truth,
James
modified 26-Jun-15 11:48am.
|
|
|
|
|
Amen Brother!
Was saying something similar this morning. Our Java guys have all these layers Spock, Groovy, Gradle, etc on top of the Java code to make life "simpler" and "easier". They spent the same, if not more, amount of time learning about those things as they would have just doing whatever it was by hand.
But code in Delphi mainly so what do I know?
Back when I was coding in C I was smart, now it doesn't feel like it so much because the languages take care of too much stuff for you and you don't have to think about it.
|
|
|
|
|
Ah Delphi, the evolution of my first true love. Where nothing is left to chance for knowing the types and how things are evaluated... I miss Delphi. I loath Fortran even F2003. Such is the life of maintaining aerospace code.
|
|
|
|