Optimize Memory in C++: Doxygen Case Study

When the processes running on your machine attempt to allocate more memory than your system has available, the kernel begins to swap memory pages to and from the disk. This is done in order to free up sufficient physical memory to meet the RAM allocation requirements of the requestor.

Excessive use of swapping is called thrashing and is undesirable because it lowers overall system performance, mainly because hard drives are far slower than RAM. Continue reading “Optimize Memory in C++: Doxygen Case Study”

Discover your C++ project internals: POCO case study

The POCO C++ Libraries are a collection of open source class libraries for developing network-centric, portable applications in C++.

POCO stands for POrtable COmponents. The libraries cover functionality such as threads, thread synchronization, file system access, streams, shared libraries and class loading, sockets and network protocols (HTTP, FTP, SMTP, etc.), and include an HTTP server, as well as an XML parser with SAX2 and DOM interfaces and SQL database access.

The modular and efficient design and implementation makes the POCO C++ Libraries well suited for embedded development. Continue reading “Discover your C++ project internals: POCO case study”

5 Common reasons of using namespaces in C++ projects

Namespaces were introduced to the C++ Standard in 1995 and usually they are defined like this:

A namespace defines a new scope. They provide a way to avoid name collisions.

Namespaces in C++ are most often used to avoid naming collisions. Although namespaces are used extensively in recent C++ code, most older code does not use this facility. 

After exploring the source code of many C++ projects, here are some common reasons of using the namespaces in these projects. Continue reading “5 Common reasons of using namespaces in C++ projects”

Detect the obfuscated names in a C/C++ project

How many times do you already discover a code like this:

obfuscatednames

Maybe in some cases it’s not a big issue to have such code. But if this coding habit is used many times by the developers, it will cost a lot to the company. Each new comer who needs to debug or add a new feature will spent a lot of time to understand the existing codebase.

How to detect these obfuscated names to refactor them? Continue reading “Detect the obfuscated names in a C/C++ project”

Good coder should hate to code a lot.

Coding is fun for many developers and as coder you are guaranteed to not be bored, each year many new languages, technologies, frameworks and libraries emerge.

The coders are the central piece of a project, their contribution are very important and having good coders is a big guarantee to make a project a success.

But who can be qualified as a good coder, the one who code a lot in a reduced time? Continue reading “Good coder should hate to code a lot.”

Managing Technical Debt with Agile Algorithm

Form wikipedia we can discover a brief explanation about the technical debt:

Technical debt (also known as design debt[1] or code debt) is “a concept in programming that reflects the extra development work that arises when code that is easy to implement in the short run is used instead of applying the best overall solution”.[2]

Technical debt can be compared to monetary debt.[3] If technical debt is not repaid, it can accumulate ‘interest’, making it harder to implement changes later on. Unaddressed technical debt increases software entropy. Technical debt is not necessarily a bad thing, and sometimes (e.g., as a proof-of-concept) technical debt is required to move projects forward. On the other hand, some experts claim that the “technical debt” metaphor tends to minimize the impact, which results in insufficient prioritization of the necessary work to correct it.[4][5]

Continue reading “Managing Technical Debt with Agile Algorithm”

Learn basic “C” coding rules from open source projects

Every project has its own style guide: a set of conventions about how to write code for that project. Some managers choose a basic coding rules, others prefer very advanced ones and for many projects there is no coding rules, and each developer uses his own style.

It is much easier to understand a large codebase when all the source code is in a consistent style.

Many resources exist talking about the better coding rules to adopt, we can learn good coding rules from:

  • Reading a book or a magazine.
  • Web sites.
  • From a collegue.
  • Doing a training.

Another more interesting approach is to study a known and mature open source project to discover how developers implement their source code. In the “C” World, the Linux kernel could be a good candidate.

For the beginner or even the intermediate C developers, the Linux kernel is maybe not easy to go inside, however the goal is not necessarily to contribute to its source code but to explore how it’s implemented.

Let’ take as example a function implementation from the Linux source code

linux11

The code looks very clean, indeed the function

  • Has only few lines of code.
  • The signature is well defined.
  • It’s well commented.
  • It’s well indented.
  • The variable names are very clear.

The same  function could be implemented by another developer like this

linux12

The coding style has a big impact in the source code readability, investing some hours to train developers, and doing periodically a code review is always good to make the code easy to maintain and evolve.

Let’s go inside the linux kernel source code using CppDepend and discover some basic coding rules adopted by their developers.

Modularity

Modularity is a software design technique that increases the extent to which software is composed from separate parts , you can manage and maintain modular code easily.

For procedural language like C where no logical artifacts like namespace, component or class does not exist, we can modularize by using directories and files.

Here are some possible scenarios :

  • Put all the souce files in one directory
  • Isolate files related to a module or a sub module  into a specific directory.

In case of the Linux kernel, directories and sub directories are used to modularize the kernel source code.

linux15

Encapsulation

Encapsulation  is the hiding of functions and data which are internal to an implementation.  In C, encapsulation is performed by using the key word static . These entities are called file-scope functions and variables.

Let’s search for all static functions by executing the following CQLinq query

linux17

We can use the Metric view to have a good idea how many functions are concerned. In the Metric View, the code base is represented through a Treemap. Treemapping is a method for displaying tree-structured data by using nested rectangles. The tree structure used in a CppDepend treemap is the usual code hierarchy:

  • Projects contains directories.
  • Directories contains files.
  • Files contains struects, functions and variables.

The treemap view provides a useful way to represent the result of a CQLinq request, so we can visually see the types concerned by the request.

linux2

As we can observe many functions are declared as static.

Let’s search now for the static fields:

linux3

The same remark as functions, many variables are declared as static.

In the Linux kernel source code the encapsulation is used whenever the functions and variables must be private to the file scope.

Use structs to store your data model

In C programing the functions uses variables to acheive their treatments, theses variables could be:

  • Static variables.
  • Global variables.
  • Local variables
  • Variables from structs.

Each project has it’s data model which could be used by many source files, using global variables is a solution but not the good one, using structs to group data is more recommended.

Let’s search for global variables with a primitive type:

linux4

only very few variables are concerned, and maybe we can group some of them into structs, like (elfcorehdr_addr and elfcorehdr_size) or (pm_freezing and pm_nosig_freezing).

Let function be short and sweet

Here’s from the linux coding style web page, an advice about the length of functions:

Functions should be short and sweet, and do just one thing.  They should
fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
as we all know), and do one thing and do that well.

The maximum length of a function is inversely proportional to the
complexity and indentation level of that function.  So, if you have a
conceptually simple function that is just one long (but simple)
case-statement, where you have to do lots of small things for a lot of
different cases, it's OK to have a longer function.

 Let’s search for functions where the number of lines of code is more than 30

linux14

Only few methods has more than 30 lines of code.

Function Number of parameters

Functions where NbParameters > 8 might be painful to call  and might degrade performance. Another alternative is to provide  a structure dedicated to handle arguments passing.

linux7

only 2 methods has more than 8 parameters.

Number of local variables

Methods where NbVariables is higher than 8 are hard to understand and maintain. Methods where NbVariables is higher than 15 are extremely complex and should be split in smaller methods (except if they are automatically generated by a tool).

linux9

only 5 functions has more than 15 local variables.

Avoid defining complex functions

Many metrics exist to detect complex functions, NBLinesOfCode,Number of parameters and number of local variables are the basic ones.

There are other interesting metrics to detect complex functions:

  • Cyclomatic complexity is a popular procedural software metric equal to the number of decisions that can be taken in a procedure.
  • Nesting Depth is a metric defined on methods that is relative to the maximum depth of the more nested scope in a method body.
  • Max Nested loop is equals the maximum level of loop nesting in a function.

The max value tolerated for these metrics depends more on the team choices, there’s no standard values.

Let’s search for functions candidate to be refactored:

linux8

only very few functions could be considered as complex.

Naming convention

There’s no standard for the naming convention, each project managers could choose what they think it’s better, however what’s very important is to respect the chosen convention to have an homegenous naming.

For example in case of Linux, the structs must began with a lower case, and we can check if it’s true for the whole kernel source code, let’s execute the following query:

linux5

 

Only 4 structs began with “_” instead of a lower case letter.

 Indentation

The indentation is very useful to make the code easy to read, here’s from the linux coding style web page  the motivations behind the indentation:

Rationale: The whole idea behind indentation is to clearly define where
a block of control starts and ends.  Especially when you've been looking
at your screen for 20 straight hours, you'll find it a lot easier to see
how the indentation works if you have large indentations.

Now, some people will claim that having 8-character indentations makes
the code move too far to the right, and makes it hard to read on a
80-character terminal screen.  The answer to that is that if you need
more than 3 levels of indentation, you're screwed anyway, and should fix
your program.

 Conclusion

Exploring some known open source projects is always good to elevate your programming skills, no need to download and build the project, you can just discover the code from GitHub for example.

 

Learn from Folly source code the new C++11 features.

Six years ago Facebook released their C++ library named Folly , it’s a large collection of reusable C++ library components that internally at Facebook are used extensively.

But many mature C++ open source libraries exist, why introduce another one ? Here’s the motivations from their website behind  its utility:

Folly (acronymed loosely after Facebook Open Source Library) is a library of C++11 components designed with practicality and efficiency in mind. It complements (as opposed to competing against) offerings such as Boost and of course std. In fact, we embark on defining our own component only when something we need is either not available, or does not meet the needed performance profile.

Continue reading “Learn from Folly source code the new C++11 features.”

Optimizing Memory Usage: Insights from Doxygen

When the processes running on your machine attempt to allocate more memory than your system has available, the kernel begins to swap memory pages to and from the disk. This is done in order to free up sufficient physical memory to meet the RAM allocation requirements of the requestor.

Excessive use of swapping is called thrashing and is undesirable because it lowers overall system performance, mainly because hard drives are far slower than RAM.
Continue reading “Optimizing Memory Usage: Insights from Doxygen”

Doom3 is the proof that “keep it simple” works.

If you search on the web for the best C++ source code. The Doom3 source code is mentioned many times, with testimonials  like this one.

I spent a bit of time going through the Doom3 source code. It’s probably the cleanest and nicest looking code I’ve ever seen.

Doom 3 is a video game developed by id Software and published by Activision.The game was a  commercial success for id Software; with more than 3.5 million copies of the game were sold. Continue reading “Doom3 is the proof that “keep it simple” works.”