SlideShare une entreprise Scribd logo
1  sur  3
Télécharger pour lire hors ligne
Knee-deep in C++ s... code
Author: Andrey Karpov

Date: 01.03.2011

Perhaps many of you know the site govnokod.ru. If not, I recommend those of you who think of
themselves as programmers to visit it. Good mood is guaranteed! This is how the site describes itself:

Found or squeezed out code you can't call fine, code you can't help laughing at? Do not rush at deleting
or refactoring it - post it at govnokod.ru, let's laugh together!

I, due to my occupation, spend much time on meditation upon very diverse C++ code. So, as they say, I
have just enough of that stuff stored up. I can't carry this anymore inside me. Excuse me, but I will share
it with you too.

Here you are the Top 10 of the "best" code fragments I've collected recently. These samples have one
significant difference from those collected at the site "Govnokod.ru". While many of those samples are
taken from students' works and private little-known projects, my samples are taken from reputable
sources. These are well known open-source applications and libraries. Everyone have been using these
projects and libraries regularly for many years. I won't name them since I believe it's unethical: one
especially beautiful code fragment you might encounter in some project doesn't mean the bad quality of
the whole project.


10. Fair election of candidates from parties TRUE and FALSE.
m_bTypeED = ( nCmd == nCmd ) ? TRUE : FALSE;


9. Oh Lord, let the database always open - what is all that "frobnitz" for?
wstring myDBFName;

...

printf("Cannot open database %s",myDBFName);


8. You'd better not do such things with Ctrl-V.
fhead[11] = '0';

fhead[12] = '0';

fhead[13] = '0';

fhead[13] = '0';
7. A nice and detailed comment is a good thing! But 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 smart.
flag = true;

flag = false;

// flag is not used further anywhere


5. What if check fails at the first time...
if(freeze||winfreeze||(mainmenu&&gameon)||(!gameon&&gamestarted)||

    (mainmenu&&gameon)||(!gameon&&gamestarted)||(!gameon&&gamestarted))

Note. gameon and gamestarted are common variables of the bool type.


4. Original sin of 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. A loop to conceive the Tao.
for (i = 0; i < num_tbl; i++) {

    *tbl++;

}
2. Well, where will this file go after all? That'll do.
FILE *fp;

if (! (fp = fopen(filename, "wb")) == -1) {

    perror("opening image file failed");

    exit(1);

}

Note. This condition is always false thanks to two errors. Come on, find them, you pursuer of truth!


1. The first place goes to switch operator.
switch(LOWORD(wParam))

    {

        case (IDOK || IDCANCEL):

         EndDialog(hDlg,TRUE);

         return(TRUE);

         break;

    }


Conclusions
Well, there is one conclusion as always: he who makes no mistakes, makes nothing. No developer, even
a skillful one, is secure from even silliest errors.

Contenu connexe

Tendances

Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
Jeya Lakshmi
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
jahanullah
 

Tendances (20)

Block introduce
Block introduceBlock introduce
Block introduce
 
CoffeeScript: JavaScript, but Better!
CoffeeScript: JavaScript, but Better! CoffeeScript: JavaScript, but Better!
CoffeeScript: JavaScript, but Better!
 
Koha Cronjobs
Koha CronjobsKoha Cronjobs
Koha Cronjobs
 
block introduce
block introduceblock introduce
block introduce
 
Compiladoresemulador
CompiladoresemuladorCompiladoresemulador
Compiladoresemulador
 
Instalación de emu8086
Instalación de emu8086Instalación de emu8086
Instalación de emu8086
 
Lesson 2 starting output
Lesson 2 starting outputLesson 2 starting output
Lesson 2 starting output
 
Lenguaje ensamblador EMU8086
Lenguaje ensamblador EMU8086Lenguaje ensamblador EMU8086
Lenguaje ensamblador EMU8086
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
 
Emulador de ensamblador EMU8086
Emulador de ensamblador EMU8086Emulador de ensamblador EMU8086
Emulador de ensamblador EMU8086
 
Telegram bots
Telegram botsTelegram bots
Telegram bots
 
Looping in C
Looping in CLooping in C
Looping in C
 
Design problem
Design problemDesign problem
Design problem
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
Loops
LoopsLoops
Loops
 
Emscripten, asm.js, and billions of math ops
Emscripten, asm.js, and billions of math opsEmscripten, asm.js, and billions of math ops
Emscripten, asm.js, and billions of math ops
 
Let's Go-lang
Let's Go-langLet's Go-lang
Let's Go-lang
 
Test Fest 2009
Test Fest 2009Test Fest 2009
Test Fest 2009
 
For Loop
For LoopFor Loop
For Loop
 
UTAU DLL voicebank and ulauncher
UTAU DLL voicebank and ulauncherUTAU DLL voicebank and ulauncher
UTAU DLL voicebank and ulauncher
 

En vedette

как загрузить файл на форум
как загрузить файл на форумкак загрузить файл на форум
как загрузить файл на форум
skatelal
 
Running Technique and Gear
Running Technique and GearRunning Technique and Gear
Running Technique and Gear
nobody020
 
Schulden und Vermögen in Österreich und international
Schulden und Vermögen in Österreich und internationalSchulden und Vermögen in Österreich und international
Schulden und Vermögen in Österreich und international
Michael Girkinger
 
Storyboard Aa
Storyboard AaStoryboard Aa
Storyboard Aa
tariq.s
 

En vedette (20)

как загрузить файл на форум
как загрузить файл на форумкак загрузить файл на форум
как загрузить файл на форум
 
Millennium city park, kaifeng (開封清明上河園)
Millennium city park, kaifeng (開封清明上河園)Millennium city park, kaifeng (開封清明上河園)
Millennium city park, kaifeng (開封清明上河園)
 
Zagreb 2013 FER
Zagreb 2013 FER Zagreb 2013 FER
Zagreb 2013 FER
 
Security Scanner Design am Beispiel von httprecon
Security Scanner Design am Beispiel von httpreconSecurity Scanner Design am Beispiel von httprecon
Security Scanner Design am Beispiel von httprecon
 
Art of eXperiential Learning
Art of eXperiential LearningArt of eXperiential Learning
Art of eXperiential Learning
 
Running Technique and Gear
Running Technique and GearRunning Technique and Gear
Running Technique and Gear
 
Moș Crăciun
Moș CrăciunMoș Crăciun
Moș Crăciun
 
Estrutura - submissão de artigo para revista
Estrutura  - submissão de artigo para revistaEstrutura  - submissão de artigo para revista
Estrutura - submissão de artigo para revista
 
Fentect liminar
Fentect liminarFentect liminar
Fentect liminar
 
Lo mejor en electrocomesticos
Lo mejor en electrocomesticosLo mejor en electrocomesticos
Lo mejor en electrocomesticos
 
Bo 23 01-2012-3
Bo 23 01-2012-3Bo 23 01-2012-3
Bo 23 01-2012-3
 
Anti cut protections
Anti cut protectionsAnti cut protections
Anti cut protections
 
Teenosaurus
TeenosaurusTeenosaurus
Teenosaurus
 
Team phsk part3
Team phsk part3Team phsk part3
Team phsk part3
 
Russia e commerce update june 2013
Russia e commerce update june 2013Russia e commerce update june 2013
Russia e commerce update june 2013
 
FIGURINES. Color
FIGURINES. ColorFIGURINES. Color
FIGURINES. Color
 
Schulden und Vermögen in Österreich und international
Schulden und Vermögen in Österreich und internationalSchulden und Vermögen in Österreich und international
Schulden und Vermögen in Österreich und international
 
Samsung Shows How to Bring IoT to Life at IFA 2015 v. Patents
Samsung Shows How to Bring IoT to Life at IFA 2015 v. PatentsSamsung Shows How to Bring IoT to Life at IFA 2015 v. Patents
Samsung Shows How to Bring IoT to Life at IFA 2015 v. Patents
 
Big deciamal
Big deciamalBig deciamal
Big deciamal
 
Storyboard Aa
Storyboard AaStoryboard Aa
Storyboard Aa
 

Similaire à Knee-deep in C++ s... code

Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
gerrell
 

Similaire à Knee-deep in C++ s... code (20)

Grounded Pointers
Grounded PointersGrounded Pointers
Grounded Pointers
 
Analyzing FreeCAD's Source Code and Its "Sick" Dependencies
Analyzing FreeCAD's Source Code and Its "Sick" DependenciesAnalyzing FreeCAD's Source Code and Its "Sick" Dependencies
Analyzing FreeCAD's Source Code and Its "Sick" Dependencies
 
Analyzing the Blender project with PVS-Studio
Analyzing the Blender project with PVS-StudioAnalyzing the Blender project with PVS-Studio
Analyzing the Blender project with PVS-Studio
 
Miranda NG Project to Get the "Wild Pointers" Award (Part 1)
Miranda NG Project to Get the "Wild Pointers" Award (Part 1) Miranda NG Project to Get the "Wild Pointers" Award (Part 1)
Miranda NG Project to Get the "Wild Pointers" Award (Part 1)
 
The Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and EverythingThe Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and Everything
 
The Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and EverythingThe Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and Everything
 
Zero, one, two, Freddy's coming for you
Zero, one, two, Freddy's coming for youZero, one, two, Freddy's coming for you
Zero, one, two, Freddy's coming for you
 
Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...
 
PVS-Studio vs Chromium. 3-rd Check
PVS-Studio vs Chromium. 3-rd CheckPVS-Studio vs Chromium. 3-rd Check
PVS-Studio vs Chromium. 3-rd Check
 
Archeology for Entertainment, or Checking Microsoft Word 1.1a with PVS-Studio
Archeology for Entertainment, or Checking Microsoft Word 1.1a with PVS-StudioArcheology for Entertainment, or Checking Microsoft Word 1.1a with PVS-Studio
Archeology for Entertainment, or Checking Microsoft Word 1.1a with PVS-Studio
 
Errors that static code analysis does not find because it is not used
Errors that static code analysis does not find because it is not usedErrors that static code analysis does not find because it is not used
Errors that static code analysis does not find because it is not used
 
PVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's code
 
PVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's code
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project Analyzed
 
Of complicacy of programming, or won't C# save us?
Of complicacy of programming, or won't C# save us?Of complicacy of programming, or won't C# save us?
Of complicacy of programming, or won't C# save us?
 
Checking VirtualDub
Checking VirtualDubChecking VirtualDub
Checking VirtualDub
 
Why Students Need the CppCat Code Analyzer
Why Students Need the CppCat Code AnalyzerWhy Students Need the CppCat Code Analyzer
Why Students Need the CppCat Code Analyzer
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
 
A Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source Code
A Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source CodeA Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source Code
A Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source Code
 
CppCat Static Analyzer Review
CppCat Static Analyzer ReviewCppCat Static Analyzer Review
CppCat Static Analyzer Review
 

Dernier

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

Knee-deep in C++ s... code

  • 1. Knee-deep in C++ s... code Author: Andrey Karpov Date: 01.03.2011 Perhaps many of you know the site govnokod.ru. If not, I recommend those of you who think of themselves as programmers to visit it. Good mood is guaranteed! This is how the site describes itself: Found or squeezed out code you can't call fine, code you can't help laughing at? Do not rush at deleting or refactoring it - post it at govnokod.ru, let's laugh together! I, due to my occupation, spend much time on meditation upon very diverse C++ code. So, as they say, I have just enough of that stuff stored up. I can't carry this anymore inside me. Excuse me, but I will share it with you too. Here you are the Top 10 of the "best" code fragments I've collected recently. These samples have one significant difference from those collected at the site "Govnokod.ru". While many of those samples are taken from students' works and private little-known projects, my samples are taken from reputable sources. These are well known open-source applications and libraries. Everyone have been using these projects and libraries regularly for many years. I won't name them since I believe it's unethical: one especially beautiful code fragment you might encounter in some project doesn't mean the bad quality of the whole project. 10. Fair election of candidates from parties TRUE and FALSE. m_bTypeED = ( nCmd == nCmd ) ? TRUE : FALSE; 9. Oh Lord, let the database always open - what is all that "frobnitz" for? wstring myDBFName; ... printf("Cannot open database %s",myDBFName); 8. You'd better not do such things with Ctrl-V. fhead[11] = '0'; fhead[12] = '0'; fhead[13] = '0'; fhead[13] = '0';
  • 2. 7. A nice and detailed comment is a good thing! But 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 smart. flag = true; flag = false; // flag is not used further anywhere 5. What if check fails at the first time... if(freeze||winfreeze||(mainmenu&&gameon)||(!gameon&&gamestarted)|| (mainmenu&&gameon)||(!gameon&&gamestarted)||(!gameon&&gamestarted)) Note. gameon and gamestarted are common variables of the bool type. 4. Original sin of 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. A loop to conceive the Tao. for (i = 0; i < num_tbl; i++) { *tbl++; }
  • 3. 2. Well, where will this file go after all? That'll do. FILE *fp; if (! (fp = fopen(filename, "wb")) == -1) { perror("opening image file failed"); exit(1); } Note. This condition is always false thanks to two errors. Come on, find them, you pursuer of truth! 1. The first place goes to switch operator. switch(LOWORD(wParam)) { case (IDOK || IDCANCEL): EndDialog(hDlg,TRUE); return(TRUE); break; } Conclusions Well, there is one conclusion as always: he who makes no mistakes, makes nothing. No developer, even a skillful one, is secure from even silliest errors.