Skip to content

Firmware Quality, Reduce Bug Opportunity by Reducing/Eliminating "extern". Suggest Using Classes #283

Description

@ForrestErickson

Describe the bug

Global can be the source of coding problems. See next comment below "Summary: Why globals are bad"

A clear and concise description of what the bug is.
Short Summary of the expected behavior:
Lee asked an AI the following:

Q. in general in programming C++ can you avoid using extern declaration by creating classes...

The AI Answer:

In C++, the extern keyword is used to declare a variable or function that is defined in another translation unit (a .cpp file). This is primarily used for global variables and non-member functions to establish their linkage across different source files.

While you cannot directly use extern with class definitions themselves, creating classes can significantly reduce or eliminate the need for extern declarations, especially for managing data and functionality across multiple files.

Here's how classes achieve this:
Encapsulation of Data: Instead of using extern to declare global variables that are accessed directly from multiple files, you can encapsulate that data within a class. Instances of this class can then be created and passed between functions or accessed through well-defined interfaces (member functions).
Member Functions: Class member functions operate on the class's data. If you have functions that need to interact with shared data, making them member functions of a class that holds that data allows you to manage access without resorting to global functions and extern declarations.
Singletons (with caution): For truly global, single-instance data or resources, you can implement the Singleton design pattern using a class. This provides a controlled, single point of access to the shared resource, eliminating the need for extern declarations for that resource. However, Singletons should be used sparingly as they can introduce tight coupling and make testing more difficult.
Passing Objects: Instead of relying on extern variables, you can pass instances of your classes (containing the relevant data and functions) as arguments to other functions or store them as member variables in other classes. This promotes better organization and reduces reliance on global state.
In essence, by structuring your code with classes and objects, you move from a global, file-based sharing model (where extern is often needed) to an object-oriented, instance-based model, where data and functionality are encapsulated and managed within objects. This generally leads to more maintainable, modular, and less error-prone code.

AI responses may include mistakes.

Exposure to use of "extern"

Instances of "extern" in the code as of 20251102

Image

Metadata

Metadata

Labels

Spiketime-boxed research to answer a question, reduce uncertainty, or evaluate a technical approach.

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions