C++ 2 min read

Abstracting technical layer details in C++ to mitigate the language's learning curve.

Share this article
Abstracting technical layer details in C++ to mitigate the language's learning curve.

Recently, I came across a LinkedIn post showing a C++ developer's reaction to seeing, for the first time, how array sorting is done in Python. Unsurprisingly, the simplicity of Python's approach came as a shock.

arr=[64,12,22,55,44]
bubble_sort(arr)
print("Sorted Array:",arr)

Such observations are less about the language itself and more about the abstractions provided by libraries and frameworks. We can achieve similar simplicity in C++ by hiding complexity behind either our own framework or an existing one.

Instead of using code like this:

 std::vector<int> vec = {64, 12, 22, 55,44};
 std::sort(vec.begin(), vec.end());
 std::cout << "Sorted vector: ";
 for(const int& num : vec) {
        std::cout << num << " ";
  }
  std::cout << std::endl;

We can use an appropriate C++ library and have code like this:

auto arr[] = { 64, 12,22,55,44 }; 
boost::sort(arr);
boost::print(std::cout, arr);

Even if such a facility does not exist in a well-known C++ library, many projects rely on in-house libraries that abstract technical complexities, making tasks easier regardless of the programming language.

C++ is often considered complex because it does not provide out-of-the-box facilities for common tasks. For instance, when using the Standard Template Library (STL), developers must work directly with iterators, which can complicate the code due to the frequent use of .begin() and .end() functions.

#include <iostream>
#include <vector>
#include <algorithm> // for std::sort and std::merge
#include <iterator>  // for std::back_inserter

int main() {
    std::vector<int> vec1 = {1, 4, 7, 10};
    std::vector<int> vec2 = {2, 5, 8, 11};
    std::vector<int> vec3 = {3, 6, 9, 12};

    // Merging vec1 and vec2 into vecMerged
    std::vector<int> vecMerged;
    std::merge(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), std::back_inserter(vecMerged));

    // Merging vecMerged with vec3
    std::vector<int> vecFinal;
    std::merge(vecMerged.begin(), vecMerged.end(), vec3.begin(), vec3.end(), std::back_inserter(vecFinal));

    // Sorting vecFinal
    std::sort(vecFinal.begin(), vecFinal.end());

    // Printing the sorted merged vector
    std::cout << "Sorted merged vector: ";
    for (auto it = vecFinal.begin(); it != vecFinal.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

However, with C++'s capabilities, we can achieve clean, easy-to-understand code comparable to that of any modern programming language. Doing so requires mastering the art of hiding complexity from developers through appropriate libraries and a robust in-house technical framework. The team responsible for this technical layer plays a crucial role in keeping the code readable and maintainable.

Conclusion

Abstractions provided by technical frameworks in C++ can significantly simplify development by providing high-level interfaces for common tasks. This reduces the need to understand the intricate details of the language and its underlying implementation, making it easier for developers to write efficient, readable, and maintainable code. By leveraging these abstractions, developers can focus on solving the actual problems at hand rather than dealing with low-level details.

Share this article