Postări

  Efficient coding technique with pointers When we use pointers in C++, more precisely when we use them as index variables in a for loop to traverse an array, there are two ways to move to the next position:  p++  (post-increment) and  ++p   (pre-increment). At first sight they look almost identical, but in reality there is an important difference between them. With  p++ , the program makes a copy of the pointer, - which in case of complex stuctures call an init method which could cause side-effects - and then increment the old value. By using  ++p , the program first increases the pointer and then returns the new value. This leads to the conclusion that, when trying to optimize code, using  ++p  is more practical and efficient. At first glance, in simple programs, the difference may not be visible, but in large loops this trick can save both time and memory. An example: for (int *p = v; p < v + n;  ++p ) {      ...

Static code analysis for CONDITION COVERAGE

  Condition coverage is a software testing metric that ensures each atomic condition in a decision (e.g., a > 0 , b == 5 ) is evaluated to both true and false at least once, regardless of the overall outcome of the full expression. It helps identify whether all logical components of a conditional statement have been tested individually. This program performs a simple static analysis by counting the number of if statements in the source file and identifying the line numbers where they appear. The goal is to help locate decision points in the code. Each if may contain one or more sub-conditions that need to be tested for condition coverage. By identifying the number and location of if statements, testers can better understand the logical complexity of the code and plan test cases to ensure adequate coverage.       Condition Coverage (%) = (Number of conditions evaluated to both true and false) / (Total number of conditions) × 100 #include <iostream> #in...

STATEMENT COVERAGE

STATEMENT COVERAGE APPLICATION - COUNTING ELSE INSTRUCTIONS  Statement Coverage is a fundamental software testing metric. It measures the percentage of executable statements in your code that are executed at least once by your test suite. Its primary goal is to ensure that every line of code that can be run has been touched by a test, helping to identify untested or "dead" code. In the following program, I tried to analyse the statement coverage by counting the else instructions - static code analyse: #include <iostream> #include <fstream> #include <string.h> using namespace std; ifstream fin ("STATEMENT COVERAGE.in"); ofstream fout ("STATEMENT COVERAGE.out"); int main() {     char linie[100];     int nrtot_else=0, nr_linie=0;     while(!fin.eof())     {         fin.getline(linie,100);         nr_linie++;         if (strstr(linie,"else")>0 && !(strstr(li...
Imagine
Technical Analysis: Premature Optimization Donald Knuth’s principle, “Premature optimization is the root of all evil,” warns against the inefficiency of focusing too much on perfecting a single component of a project at the expense of completing the whole. The image of the unfinished horse is a representative illustration of this concept; in it, effort was invested in deeply detailing one section, while the basic structure of the rest was neglected. In software development, this translates into optimizing an algorithm down to the microsecond while critical application functionalities remain unimplemented. From my point of view, it is far more valuable to have a product that works from end to end, even if it still needs adjustments, than to have a single perfect component in a project that, overall, cannot be used.

Program that counts "while" loops (excluding do-while loops)

  #include <iostream> #include <fstream> #include <string.h> using namespace std; ifstream fin ("whilecount.txt"); int main() {     char linie[100];     int nrtot_while=0, nr_linie=0;     while(!fin.eof())     {         fin.getline(linie,100);         nr_linie++;         if (strstr(linie,"while")>0 && !( strstr(linie,";")>0))         {             nrtot_while++;             cout << nr_linie << endl;         }     }         cout << "Number of 'while' instructions in the file: " << nrtot_while;     return 0; } //This program was designed in CodeBlocks - version 20.03 in the C /C++ language.

Program that counts all "for" instructions from a file

#include <iostream> #include <fstream> #include <string.h> using namespace std; ifstream fin ("in.ppc"); int main() {     char linie[100];     int nrtot_for=0, nr_linie=0;     while(!fin.eof())     {         fin.getline(linie,100);         nr_linie++;         if (strstr(linie,"for")>0)         {             nrtot_for++;             cout << nr_linie << endl;         }     }         cout << "Number of 'for' instructions in the file: " << nrtot_for;     return 0; } //This program was designed in CodeBlocks - version 20.03 in the C /C++ language.

Counts ascending FOR loops

According to the following rule (AVR035: Efficient C Coding for AVR, Rev. 1497D–AVR–01/04) : " 9. Use descending loop counters and pre-decrement if applicable."  , reducing code of the Atmel ( NXP ) company on AVR series microcontrollers, using the appropriate for loops, helps optimize the code at the file size level.  // THIS PROGRAM COUNTS THE NUMBER OF ASCENDING FORS AND OUTPUTS THE LINE WHERE THEY BELONG #include <iostream> #include <fstream> #include <string.h> using namespace std; ifstream fin ("file_in.ppc"); int main() {     int ct=0,lin=0;     char linie[101];     while(!fin.eof())     {         fin.getline(linie,100);         lin++;         if((strstr(linie,"for")>0) and (strstr(linie,"++")>0))         {             ct++;             cout<<lin<<endl; ...