New answers tagged

0

I have adapted my code in the original question to accommodate for the more functional approach suggested by @G-Sliepen in his answer. Also a timing subroutine was added here and to the old OOP code, in order to compare performances. Functional version of code implementing Newton-Cotes quadrature formulas: main.cpp: #include <iostream> #include <...


1

Prefer Composition to Inheritance Currently, you have objects that store and hide the interval you’re integrating over, the tolerance, and the method of integration. Each class has a different virtual method that you pass a number of intervals and a C-style pointer to the function to be integrated. The basic idea of a object defining a type of integration ...


11

Use an enum to give names to choices Consider using an enum, or even better an enum class, to enumerate the possible choices for the Newton-Cotes formulas: enum class Formula { MIDPOINT = 1, TRAPEZOIDAL = 2, SIMPSONS = 3, }; See below for how to use it. Don't use (virtual) classes unnecessarily There is no reason for class NewtonCotesFactory to ...


14

I wonder whether the #include "NewtonCotesFormulas.cpp" at the end of the NewtonCotesFormulas.h header file could be somehow avoided. I used it because without I'd get linking errors. Yea, that's simply wrong. You need to give both that CPP file and the main.cpp file to the compiler, so it knows that both are part of the program to build. Don’t ...


2

I think you have over-engineered the problem. You don't need neither the Factory nor the Factory<T> classes. public class Root { private readonly Dictionary<string, Type> ModelMapping; private readonly ConcurrentDictionary<Type, Model> Models = new(); private readonly JsonSerializerOptions Options = new() { ...


6

Using enums in Python The posted code doesn't take advantage of most of the benefits provides by enums in Python. The typical use case of enums is to make decisions depending on a given enum member. For example, in the context of the posted code, I can imagine a create_foo method which takes as parameter a value of Color type that is an enum, and callers ...


Top 50 recent answers are included