Live data from Hacker News

How to make fewer errors at the stage of code writing

viva64.com

21–24 of 24 posts

Re: How to make fewer errors at the stage of code writing

#21
post #8

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…

I wouldn't bundle C and C++ in the same group in memory handling aspects. C++'s destructors and RAII make memory management rather easy (I actually think that it's easier than garbage collection, as I know with some certainty when some memory is allocated and freed. No need to trust some magical black box that GC is.

Re: How to make fewer errors at the stage of code writing

#22
A better approach to #5 (Do not copy a line more than once) is to make a "template" copy of the line, which will fail to compile, and then make a bunch of copies of that.

Example:

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

#23
post #13

Tangentially 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…

you have a declaration without initialization there. Variables should always be initialized - this removes another large potential source of bugs.

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

#24
post #7

For 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?

Don't get me wrong, lambdas are really useful and elegant, I just think they should be used when there is a purpose other than simply making the code succinct - such as limiting scope. I've seen people use them instead purely to reduce the number of lines of code, which I think in the end makes it less readable and harder to debug.
Post reply on HN