
By the nature of my work, I often meditate on various C ++ code. And, as they say, I have accumulated. I can not bear it anymore. Sorry, I'll share it with you now.
I propose the top 10 best "best" code fragments from the ones I have collected recently. These examples have a significant difference from those collected on the Govnokod.ru website. If there are many examples taken from student coursework and private little-known projects, then my examples are from reputable places.
These are well-known open-source applications and open libraries. We all have been regularly using these projects and libraries for more than one year. I will not give names, as I consider it unethical. The fact that the project got one particularly beautiful piece of code does not at all characterize the quality of the entire project.
')
10. Fair election of candidates parties TRUE and FALSE.
m_bTypeED = (nCmd == nCmd)? TRUE: FALSE;
9. Lord, let the database always open. And what should I look at "zyuki"?
wstring myDBFName;
...
printf ("Cannot open database% s", myDBFName);
8. But do not do such things using Ctrl-V to do.
fhead [11] = '\ 0';
fhead [12] = '\ 0';
fhead [13] = '\ 0';
fhead [13] = '\ 0';
7. A nice and detailed comment, this is great! Although of course the correct code is better.
/ ** Deletes all previous field specifiers.
* This should be used when dealing with clients that
* send multiple NEP_PACKET_SPEC messages, so only the last
* PacketSpec is taken into account. * /
int NEPContext :: resetClientFieldSpecs () {
this-> fspecs.empty ();
return OP_SUCCESS;
} / * End of resetClientFieldSpecs () * /
6. Simple and tasteful.
flag = true;
flag = false;
// no further flag is used
5. And what if the first time check does not work ...
if (freeze || winfreeze || (mainmenu && gameon) || (! gameon && gamestarted) ||
(mainmenu && gameon) || (! gameon && gamestarted) || (! gameon && gamestarted))
Note. gameon and gamestarted are the most common variables of type bool.
4. Original sin of using Basic.
t.printf ("% 10.5f,% 10.5f,% 10.5f, \ n% 10.5f,% 10.5f,
% 10.5f, \ n% 10.5f,% 10.5f,% 10.5f) ",
m [0, 0], m [0, 1], m [0, 2],
m [1, 0], m [1, 1], m [1, 2],
m [2, 0], m [2, 1], m [2, 2]);
3. Cycle to comprehend the Tao.
for (i = 0; i <num_tbl; i ++) {
* tbl ++;
}
2. But where does this file actually go? And so come down.
FILE * fp;
if (! (fp = fopen (filename, "wb")) == -1) {
perror ("opening image file failed");
exit (1);
}
Note. This condition is always false due to two errors. Find them, seeker of truth!
1. First place goes to the switch operator.
switch (LOWORD (wParam))
{
case (IDOK || IDCANCEL):
EndDialog (hDlg, TRUE);
return (TRUE);
break;
}
Conclusion
Actually the conclusion, as always, is one. Only the one who does nothing is not mistaken. Even highly skilled developers are not insured from mistakes, even stupid ones.