Refactoring: Replace Integer With Boolean

You have an integer variable holding one of two values used as a conditional flag.

Replace the integer with a boolean variable.

Motivation

The C language did not provide a true boolean type until C99[1], so integer variables containing 0 or 1 (or any non-zero value) are often used as a substitute. Programmers that have built habits from C compilers often use integer variables as boolean flags. In a language with true boolean types, such as C99, C++, C# and Java, a boolean type should be used along with the constants for true and false to more clearly convey the usage of the variable.

Read the rest of this entry »

Refactoring: Convert C to C++

You have C source code.

Create a C++ source file containing the C source code, modified to adhere to the rules of C++.

Motivation

The C language provides limited scoping and encapsulation of data and functions. It has no objects, but legacy systems often involve a large body of procedural code that could benefit from object-oriented refactorings. In order to apply object-oriented refactorings the code must first be ported to C++. Then the C++ code can be refactored from procedural code and data into objects. The objects can be further refactored with standard object-oriented refactorings.

C++ is syntactically compatible with C, but introduces new reserved keywords that are not present in the C language, such as template, typename, class and so-on. Because these additional keywords are reserved in C++ but not in C, you may need to apply Rename Function, Rename Field or Rename Variable refactorings in order to eliminate errors arising from compiling the source with a C++ compiler instead of a C compiler.

C++ is link compatible with C when external data and functions are declared with “C” linkage via the extern "C" in the C++ code. Data and functions made available to C compilation unit from a C++ compilation unit must also be declared extern "C" in order to be visible to the C code.

Read the rest of this entry »