It seems to me that the most common errors in C/C++ are memory leak and/or corruption issues. Coming from someone who's background is in managed/garbage collected languages, doesn't using a managed language help to avoid these common mistakes? I know that it's still possible to leak memory in C#, like not releasing event handlers, but it's very difficult, if not impossible, to corrupt memory without using unsafe code…
How to make fewer errors at the stage of code writing
21–24 of 24 posts
Re: How to make fewer errors at the stage of code writing
#22Example:
Template line:
EnableWindow(GetDlgItem(hwndDlg,),);
Copy/paste: EnableWindow(GetDlgItem(hwndDlg,),);
EnableWindow(GetDlgItem(hwndDlg,),);
EnableWindow(GetDlgItem(hwndDlg,),);
Final edit: EnableWindow(GetDlgItem(hwndDlg, IDC_PRIMARYSTATUS), TRUE);
EnableWindow(GetDlgItem(hwndDlg, IDC_CYCLETIMESPIN), FALSE);
EnableWindow(GetDlgItem(hwndDlg, IDC_CYCLETIME),);
Now when you try to compile, but forgot to update something (such as the missing boolean in the last line), the compiler will complain.Re: How to make fewer errors at the stage of code writing
#23Tangentially related (section 3 talks about this): why would you ever disable code using the && false idiom? You could just remove it (and rely on you source management system to get it back) or you comment it out (where you will at least get syntax highlighting to help you spot the dead code). It happened multiple times to me that I missed a tucked in &&false while I was porting older code to a new API. As the code…
UINT uFlags = DST_ICON | DSS_NORMAL;
or in C++
UINT uFlags( DST_ICON | DSS_NORMAL );
Also, declare variables const whenever possible.
Re: How to make fewer errors at the stage of code writing
#24For languages that support it, use lambda functions judiciously - too much of them make code really error-prone and hard to debug. (Not the most wide-spread problem, but came across this today) Also check for null!!! ;)
Your experience is the exact opposite of what I was promised by the functional programming community. Can you please elaborate on how lambdas are giving you trouble?