DEV Community

Fahad Ali Khan
Fahad Ali Khan

Posted on

Improving Documentation in Theoretica

In my latest contribution to the open-source library Theoretica, I tackled Issue #81 focused on refining and enhancing the inline documentation for two central components: mat (the matrix class) and vec (the vector class). This issue was raised to improve the readability and usability of these classes, which are essential in mathematical computations and commonly used across various areas in the library. Proper documentation is key to making complex code accessible to other developers and contributors.

Issue Summary

The main goal of the issue was to update and improve the existing documentation for mat and vec classes. This involved:

  1. Adding clear, concise explanations for each function in both classes.
  2. Using consistent language and structure throughout.
  3. Removing redundancies and improving clarity, especially in cases where documentation was minimal or missing.

For anyone unfamiliar with these classes, mat represents an N x K matrix with templated types, while vec represents an N-dimensional vector. Both classes support various mathematical operations, such as addition, subtraction, scalar multiplication, and vector transformations.

Preparation for the Fix

Before diving into the documentation, I took time to thoroughly understand the structure of the mat and vec classes. This was essential because the codebase for these classes is quite extensive and contains advanced template programming techniques. Each function’s purpose, inputs, and outputs needed to be understood to accurately document them.

One of the initial challenges was ensuring I had a local development environment set up to properly view, test, and verify changes. I cloned the repository, configured the project, and set up Doxygen to generate HTML documentation to visualize changes effectively.

Learning Curve

Working on this documentation required understanding some complex aspects of C++ template programming. Since the library uses advanced C++ techniques such as templates, inline functions, and recursive function calls for implementing mathematical operations, I reviewed the theory and usage of these constructs to confidently write documentation that would be both accurate and clear for other developers.

I also had to familiarize myself with Doxygen conventions, as these would be used to generate structured documentation for the library. Specifically, I focused on how to organize documentation comments effectively without using @brief for this issue, as I aimed for a more descriptive style in line with the existing documentation.

Implementing the Documentation

The documentation process involved updating each function’s comment to ensure it provided value to the reader. Here’s a breakdown of some key elements of the update:

mat Class Documentation

In the mat class, I documented functions that dealt with:

  1. Matrix Operations: Functions such as operator+, operator-, and operator* which handle addition, subtraction, and multiplication of matrices and scalars.
  2. Vector Transformation: Methods like transform and cross that apply matrix transformations to vectors.
  3. Iterator Support: Functions like begin and end that enable iteration over matrix elements.

For each function, I included a description of the operation, the expected input types, and the output. For example, the documentation for transform reads as follows:

/// Transforms a vector by applying this matrix to it. 
/// @param v The vector to transform, whose size must match the matrix column count.
/// @return A new vector resulting from the transformation.
/// Raises an error if the vector size does not match the column count of the matrix.
Enter fullscreen mode Exit fullscreen mode

vec Class Documentation

For the vec class, the focus was on:

  1. Basic Vector Operations: Addition, subtraction, and scalar multiplication functions.
  2. Dot and Cross Products: I documented the mathematical implications of these functions and how they are computed internally.
  3. Normalization: I clarified the purpose of the normalize and normalized functions and explained how they can be used to obtain unit vectors.

Here is an example of the normalize function’s documentation:

/// Normalizes the vector in place, adjusting it to have a magnitude of 1.
/// This changes the vector’s direction without altering its direction.
/// Throws an error if the vector’s magnitude is zero.
Enter fullscreen mode Exit fullscreen mode

Highlights of the Fix

The Pull Request #92 includes detailed comments on each function, designed to make Theoretica’s core classes more accessible to users. By structuring the comments consistently, I ensured that users would find it easier to understand both the functionality and usage of mat and vec.

An interesting part of this PR was adding documentation for recursive functions like make_vec, which constructs vectors from multiple elements by using variadic templates. I provided clear explanations for how each argument in the parameter pack is assigned to successive elements in the vector.

Challenges and Overcoming Obstacles

The main challenge was understanding the nuances of each mathematical operation within the mat and vec classes. Each function had subtle requirements and constraints, especially when dealing with template parameters and generic data types. I overcame this by diving into each function’s implementation, running sample cases, and referencing C++ template programming guides.

Additionally, understanding the project maintainer’s preferences around documentation style was crucial. For example, they preferred that @brief not be used, so I adapted the documentation style to fit the existing codebase conventions.

Interaction with Project Maintainers

Working with the project maintainers was smooth. They provided helpful feedback regarding documentation style preferences, which helped refine the PR. This feedback loop ensured that the final submission met the maintainers' standards.

Final Thoughts and Next Steps

Working on this documentation update gave me valuable insights into advanced C++ programming and the importance of clear documentation in open-source projects. As next steps, I’d like to contribute more to Theoretica’s documentation efforts, potentially expanding documentation coverage across other classes and modules.

You can view my contributions in Issue #81 and Pull Request #92. If you're interested in contributing to Theoretica, there are plenty of opportunities for improving documentation and testing across the codebase, so consider jumping in!

Top comments (0)