Recently, I came across a LinkedIn post showing a C++ developer’s reaction to seeingfor the first time how array sorting is done in Python. Unsurprisingly, the simplicity of Python’s approach was shocking.
arr=[64,12,22,55,44]
bubble_sort(arr)
print("Sorted Array:",arr)
Such observations are not about the language itself but rather the abstractions provided by libraries and frameworks. We can achieve the same simplicity in C++ by abstracting the complexity using our own framework or an existing one.
And instead of using a 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 a code like this:
auto arr[] = { 64, 12,22,55,44 }; boost::sort(arr); boost::print(std::cout, arr);
And even if this facility does not exists in a known C++ library. In many projects, developers use 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., and have a code like this:
#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 and easily understandable code comparable to any modern programming language. This requires mastering the art of hiding complexity from developers by using appropriate libraries and a robust in-house technical framework. The team responsible for developing this technical layer plays a crucial role in making the code more readable and maintainable.
Conclusion
Technical framework details abstractions in C++ can significantly simplify the development process by providing high-level interfaces for common tasks. This reduces the need to understand the intricate details of the language or the underlying implementation, making it easier for developers to write efficient, readable, and maintainable code. By leveraging these abstractions, developers can focus more on solving the actual problems at hand rather than dealing with low-level details.