scitools.com

← Metric catalog

Classes is one of the source-code metrics Understand computes across 17+ languages — browse them in the GUI, export and track them, or script them with the Python API.

Classes

API ID: CountDeclClass
Languages: Basic, C#, C++, Java, Pascal, Python, Web
Targets: Architectures, Files, Project

Number of classes.

Exactly what qualifies as a class for this count depends on the language and, for C++, the parse mode.

For example:


// class_macro.h
#define DECLARE_TAG_CLASS(ClassName) \
  class ClassName { \
  public: \
    int tag = 0; \
  };

// classes.cpp
#include "class_macro.h"

struct Point {
  int x;
  int y;
};

union Value {
  int asInt;
  float asFloat;
};

class Shape {
public:
  virtual double area() const = 0;
};

enum class Color { Red, Green, Blue };

DECLARE_TAG_CLASS(MacroClass)

classes.cpp declares five class-like types: the struct Point, the union Value, the class Shape, the scoped enum Color, and MacroClass — generated by the DECLARE_TAG_CLASS macro, which still counts since the preprocessor expands it into a real class declaration before either parser ever sees it. Despite the name, C++'s strict (Clang-based) parser counts every one of these class-like declarations — structs and unions included, not just class — giving CountDeclClass = 5 for this file.

C++'s fuzzy parsing mode (the legacy parser used when Clang can't process a file) is pickier: it only recognizes an actual class, not struct or union. So fuzzy parsing gives CountDeclClass = 3 for the same file — Shape, MacroClass, and (as a quirk of its simpler keyword-based parsing) Color, which gets misidentified as a class rather than a scoped enum. Point and Value aren't counted.

See Functions for the equivalent count of function declarations.

Targets By Language: Configuration: