|
TCPMem wrote: Why is more apropriate to write
if(true == a)
Because the compiler will catch the error if you mistakingly use an assignment operator.
TCPMem wrote: if(a)
is avoidable. I mean not avoidable...
This is simply checking if a has a non-zero value (regardless of what type it is).
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
i personally dislike
if (a) because it isn't obvious at the very first look what exactly 'a' is, it could be a bool, an int, a pointer, an instance of a class with a bool operator, and it can be confusing if you are e.g. reading someone else's code and do not have an in-depth knowledge of what is what and why.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
Actually if you use this form strictly to check if a is true instead of for example checking if the integer value is 0 this form is very clear.
|
|
|
|
|
As Richard said, it is a matter of style really. That form can also be very easily readable with well named variables, e.g:
if (noMoreItemsLeft) EndOfItems(); looks quite obvious when you read through the code, but
if (!ptrToNextItem) EndOfItems(); you have to 'analyze' this, if not ptrToNextItem, this means, if ptrToNextItem is zero, then...
I also don't like situations like this:
class AClass
{
public:
int Integer;
bool Bool;
AClass(int i, bool b):Integer(i),Bool(b) {}
operator bool() const { return Bool; }
AClass &operator =(int i) { Integer = i; return *this; }
};
...
AClass A(0, true);
if (A) A = 3;
...
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
I'd say that was more a criticism of the inappropriate use of operator overloading rather than a problem with not comparing the results of logical expressions to true and false.
Cheers,
Ash
|
|
|
|
|
I have seen this being done (not by me). Anyways, am just trying to say that "in my oppinion" simply writing if (whatever) doit(); isn't always completely clear and obvious. But i am repeating myself . This is indeed a question of style and personal oppinion, i am not saying it is good or bad, just sharing my point of view.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
I mostly disagree with the others.
1.
You need better identifier names. Then the short form is the right one:
if (printingToFile) printToFile();
2.
it does not make sense comparing with true explicitly, as can be proven ex absurdo:
If you think
if (a==true) ...
is any good, then the following is even better:
if ((a==true)==true) ...
3.
when the variable's type is NOT boolean and you need an explicit constant value, then one can argue putting the constant first is safer, as it causes an error when accidentally = is used instead of ==. However, a decent compiler will generate a warning when you accidentally write:
if (myInteger=1) ...
|
|
|
|
|
I don't agree with your point 2 proof (though it is clever).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Agreed 100%.
Remember these ones:
BOOL bDoIt = ::ExternFunction();
if (bDoIt == TRUE)
where ExternFunction() according to documentation returns non-zero on success?
It's not a matter of style. It's a matter of stupidity.
|
|
|
|
|
Niklas Lindquist wrote: BOOL bDoIt = ::ExternFunction();
if (bDoIt == TRUE)
Niklas Lindquist wrote: It's a matter of stupidity.
May be I'm being a bit stupid but, what exactly is "stupid" in the code above?
if (bDoIt) would've looked nicer but if (bDoIt == TRUE) isn't bad either.
It's better to know some of the questions than all of the answers.
Pravin.
|
|
|
|
|
It's ultra-bad. Returning non-zero on success, might just as well mean returning 2, 3 or, God forbid, even 4. Comparing it to TRUE (i.e. 1) might not give the anticipated result. The only thing you can compare it to is FALSE, if (bDoIt != FALSE), would have been the correct usage. But that really doesn't help anyone, does it.
Good naming removes the need for such constructs. It's about readability.
Consider
if (signalIsOn) {}
if (signalIsOn == true) {}
The second construct is just plain silly, and actually reduces readability.
|
|
|
|
|
OK, now I get it. To be honest, I had never thought about this side effect. But, to me what seems to be the real problem here is the use of BOOL (which is essentially int )in place of bool . A bool bDoIt would have responded to any non-zero number by simply turning itself to true .
Does that mean we should avoid using BOOL in general?
It's better to know some of the questions than all of the answers.
Pravin.
|
|
|
|
|
PravinSingh wrote: the real problem here is the use of BOOL
I admit that this is slightly off topic, but still, I think it's worth mentioning.
Avoiding BOOL in general is a good thing. But in reality, you would probably use a mix if you are hacking Windows API. The language evolves, but sadly, a lot of features are not used using excuses such as 'we need to handle an old code base' or 'we need to support an ancient compiler'. They might be valid excuses, but you could be stuck at that forever. Whenever possible, embrace what the technology gives. Also, embrace boolean algebra and proper naming
|
|
|
|
|
This was adopted as a standard some time ago by many developers for these statements because there was actually a time when the compiler would not have complained about
if( isThisCool = true )
but most certainly would have complained about
if( isThisCool = a )
so it was a little bit of a defensive coding mechanism.
Nowadays, it really doesn't matter and I personally have gone to the
if( isThisCool )
syntax. It just reads nice that way.
|
|
|
|
|
Generally prefer the third form - if someone needs reminding that a logical expression resolves to true (non-zero in older languages) or false (0 in older languages) in an algol derived language then they need a refresher.
If you use decent names, know boolean algebra and wrap up complicated logical expressions in functions you can't go wrong. Or rather you probably won't go wrong, and if you did then direct comparison with true or false (either way around) isn't going to help you.
Cheers,
Ash
|
|
|
|
|
Hello everybody.
This is my problem:
I need to connect to https webpage which has a self-signed certificate.
Regular webbrowsers warn me when I open that page.
I want to connect to it and POST some data.
When I connect to the https webpage, I'm getting an error saying "The certificate authority is invalid or incorrect". Here's my code:
CInternetSession ises;
CFile* f=NULL;
f=(CFile*)ises.OpenURL(L"https://mysite.com/login.jsp");
...
Is there any way to accept that certificate or any other solution?
Thanks in advance.
|
|
|
|
|
Maybe this[^] can help.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
Code-o-mat, thanks for your reply.
I tried the code that should ignore the invalid certifacate as said in the link you gave.
But, it doesn't seem to work, the code's working, but my problem didn't get solved.
Please see the code:
CFile* f=NULL;
MyTry:
try{
f=(CFile*)ises.OpenURL(L"https://mysite.com/login.jsp");
}
catch(CInternetException* s){
if(s->m_dwError==ERROR_INTERNET_INVALID_CA){
DWORD dwFlags;
DWORD dwBufferLen=sizeof(dwFlags);
ises.QueryOption(INTERNET_OPTION_SECURITY_FLAGS,(LPVOID)&dwFlags,&dwBufferLen);
dwFlags|=SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
ises.SetOption(INTERNET_OPTION_SECURITY_FLAGS,&dwFlags,sizeof(dwFlags));
goto MyTry;
}
}
...
Is there any other way to workaround this problem?
|
|
|
|
|
I can't think of anything else other than trying to do the download without OpenURL, using CInternetSession::GetHttpConnection[^], CHttpConnection::OpenRequest[^] and CHttpFile[^], maybe it makes a difference...
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
Hi,
How to remove maximize button from frame window in SDI application?
|
|
|
|
|
I've never found a clean way to remove the maximise button but you can effectively disable it by handling WM_SYSCOMMAND/SC_MAXIMIZE and returning 0 from the window procedure.
Cheers,
Ash
|
|
|
|
|
Better to rest the WS_MAXIMIZEBOX style bit with a SetWindowLong , so that the gui will paint the button consistently.
By simply throwing away the command, you end-up with an inconsistent GUI showing an enabled command that doesn't behave as expected.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
Don't set (or remove ) the WS_MAXIMIZEBOX style. As Ash said, the maximize box will then be drawn disabled.
If you have none of WS_MAXIMIZEBOX and WS_MINIMIZEBOX styles, no size boxes are drawn at all.
cheers,
AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
|
|
|
|
|
I have a very frustrating issue that has been driving me nuts for a couple of years now.
We have a 3rd party application that is pretty much a high load Sybase database to which we have no visibility. My internal customer requires me to provide them with real time statistics using a web app to show the results.
In order to retrieve this real time data, we have an sdk provided by the vendor, who unfortunately no longer exist so it is completely unsupported.
This sdk has one big problem, it has a memory leak that causes the thread to grow by between 2 and 4k on each iteration.
My solution is an MFC application which consumes the sdk. In order to manage the memory space of the application and keep it from eating up unlimited resources, I have a Windows Service wrapping the application and monitoring the memory usage. When the usage gets to big, it signals the application to exist and then restart.
My issue is that periodically there is a memory error occuring within the sdk which I can't handle in my application. There is no pattern to the timing of this error. It could be five minutes, or five days, and the invalid memory address also doesn't follow any visible patterns.
Has anyone else had a similar issue with a memory exception in a third party sdk? How did you deal with it? I have spent years writing real time software and my threading knowledge is very good, so I have eliminated the possibilities of bad or missing locks and pointers in my side of the application.
modified on Wednesday, October 13, 2010 2:27 PM
|
|
|
|
|
If the memory leak and the memory access error are inside a third-party library that comes without sources, you have no chance to fix it. According to what you have already done, the only one approach that I could imagine is to use Structured Exception Handling (C++)[^] to catch the memory access violation event: this will give you the chance to exit the application without showing a dialog that reports the crash, the next step is obviously to restart the application.
|
|
|
|