Blog: Articles on C++11 and Computational Finance (by Daniel J. Duffy)

Daniel Duffy

C++ author, trainer
One-liners in OOP
1. Multiple inheritance was not a good idea. C++ does not support interface keyword. The AI community used MI a lot in the 90s.

2. Not evertyhing is an object. Behavioural commonality is not structural commonality.This messes up class hierarchies.

3. Deep class hierarchies are usually incorrect and not maintainable, especially with MI. For CAD, all you need is depth 1 and make sure each class has 1 major responsibility. Combine with composition and Visitor pattern. Separation of Concerns is key!

4. "Tiny classes" are not classes; they are not even objects. Just think of many many Command classes. Just use lambda functions.

5. Virtual functions and multiple classes are not needed; instead, use a single class with one or more std::function instances.

6. (Sapir-Whorf) At the time. some developers used GOF patterns left, right and centre. What happened in many cases is life started with the (GOF) solution and work back to the problem. Like Newton-Raphson, convergence is not a given.

7. Classes are context-sensitive (aka unstable). Many classes live in the head of their creator who may have incomplete information about the problem domain.

8. Our group had 1-7 on our radar screens for some time. Especially for fixed-priced projects. For developers on an hourly rate things can be slightly different. Parkinson's Law and gold-plating might kick in.

9. Class hierarchies are neither sufficient nor necessary.
 

Daniel Duffy

C++ author, trainer
Here's a very simple runnable example to show how Concepts work.

C++20 Concepts 101:
// Test101Concepts.cpp
//
// Simplest example of a system. Context diagram consists only
// of Input and Output systems.
//
// We use C++20 Concepts to replace policy-based design
//
// Composition
//
//  V2: design using C++20 modules
//
// (C) Datasim Education BV 2015-2021
//

#include <string>
#include <iostream>

// Interface contract specification
template<typename T>
    concept IInput = requires (T x) { x.message(); };

template<typename T>
    concept IOutput = requires (T x, const std::string& s) { x.print(s); };

// I/O stuff
template <typename I, typename O> requires IInput<I> && IOutput<O>
    class SUD
{ // System under discussion, using composition

private:
    I i; O o;
public:
    SUD(const I& input, const O& output) : i(input), o(output) {}
    void run()
    { o.print(i.message());    }
};

// Instance Systems
class Input
{
public:

    std::string message() const
    {
        // Get data from hardware device
        return std::string("Good morning");
    }
};

class Output
{
public:

    void print(const std::string& s) const
    {
        std::cout << s << std::endl;
    }

};

int main()
{
    Input i; Output o;
    SUD<Input, Output> s(i,o);
    s.run();

    return 0;
}
BUMP
 

Daniel Duffy

C++ author, trainer

This status-quo hasn’t changed in 12 years. The maintenance burden on the Standard is near minimal, and we hope the Committee spends almost no time agreeing to remove this unused and unimplemented feature, despite its origins being well-intended and the target use-case still being relevant. Indeed, the current specification simply missed the mark, and will not be missed.

We therefore propose outright removal instead of deprecation because lack of implementation and usage makes deprecation moot.
 
Last edited:

Daniel Duffy

C++ author, trainer
I am writing a new book on parallel programming/paralllel design patterns + applications in C++23, C# and Python. The software tools and cheap multi-core processors make this endeavour feasible.

One new feature in C++20 is coroutines, which have many uses.




Having an 8-core machne and only using 1 core is a shame..
 
Last edited:

Daniel Duffy

C++ author, trainer
Coroutines for Design Patterns

C++:
# PAC.py
#
# Simple example of Presentation-Abstraction-Control pattern. More generally, coroutines are
# a better mechanism for event-driven behaviour than OOP design patterns (State, Observer, Mediator,
# Blackboard, Layers, MVC).

# EXX. take OOP patterns and cast them to a coroutine form.
# DD 202207-14

import asyncio

async def P1():
    print("P1")
    await asyncio.sleep(1)
    return 5.0

async def P2():
    print("P2")
    await asyncio.sleep(2)
    return 2.0

async def P3():
    print("P3")
    await asyncio.sleep(3)
    return 3.0


async def A():
    print("A")
    x1 = await P1()
    x2 = await P2()
    x3 = await P3()

    return x1 + x2 + x3
 
async def C1():
    # Run awaitable objects in the argument list sequence concurrently.
    y = await A()
    print("C1 ", y+y)
 
async def C2():
    # Run awaitable objects in the argument list sequence concurrently.
    y = await A()
    print("C2 ", y*y)

async def main():
    print("in main")
    await C1()
    await C2()

if __name__ == "__main__":
    import time
    s = time.perf_counter()
    asyncio.run(main())
    elapsed = time.perf_counter() - s
    print(f"\n{__file__} executed in {elapsed:0.2f} seconds.")
 

Daniel Duffy

C++ author, trainer
C++:
# PingPong.py
#
import asyncio

# Ping-pong/chess example, like a state machine
async def pong(c):
    print(c, end = "")
    await asyncio.sleep(1)
 

async def ping(c):
    print(c,end="")
    await asyncio.sleep(2)

async def ping2(c):
    await SubPing(c)
 
async def SubPing(c):
    print("\nSubstate " + c)
 
async def main():
    print("in main, this is a state machine")
    await ping("a")
    await pong("1")
    await ping("b")
    await pong("2")
    await ping("c")
    await pong("3")
    await ping2("D") # substates
 
  

if __name__ == "__main__":
    import time
    s = time.perf_counter()
    asyncio.run(main())

    elapsed = time.perf_counter() - s
    print(f"\n{__file__} executed in {elapsed:0.2f} seconds.")
 
I am writing a new book on parallel programming/paralllel design patterns + applications in C++23, C# and Python. The software tools and cheap multi-core processors make this endeavour feasible.

One new feature in C++20 is coroutines, which have many uses.




Having an 8-core machne and only using 1 core is a shame..
Dear Daniel J. Duffy

I have no experience in C++ programming. I see that your have several books on the subject and other related areas.
Which books of yours (books from other authors are also fine) do you recommend me buying? Notice that I do not mind buying several books from the start.

Also, it seems to me that books like "Introduction to C++ for Financial Engineers: An Object-Oriented Approach" are outdated—at least reviewers on Amazon.com complain about code which does not work "our of the box". Obviously, I do not want to learn from outdated material.

Best regards,
 
The 2006 C++ book is based on C++03/C++98 standard. C++ has been changed; the current version is C++20.

For beginners the Quantnet C++ course is optimal.

If you are (really) serious about C++ then it the best investment of a lifetime

Alright, alright thank you.
I would have bought the course in a heartbeat, but the price too steep for me right now.
I have taken a TA position, but even if I manage to save up the total wage, it would still not be enough, because the price of the course is actually higher than the total wage I will receive. Hopefully, I will find a way of saving up the required amount (If I'm convinced that the course is the right choice for me.)

Various thoughts/concerns/questions

- How many hours per day does one have to allocate to the course? I'm still a student—at a rigorous problem—so my time schedule is alright tight.

- At which point should I take the course? I have not learned about, e.g., the Black-Scholes model yet.

- Also, the video https://quantnet.com/media/01-01-the-c-environment.1/ is 6 years old. Is the content not outdated?

- I like teaching material structured in the form of a book. For me videos are not an efficient way of studying/learning (there probably a few are exceptions, i.e. a short video will be fine in some cases). As far as I can see, there is no way of speeding up the video https://quantnet.com/media/01-01-the-c-environment.1/.
 

Daniel Duffy

C++ author, trainer
Well no, it is not outdated, We do up to C++20.
The two QN C++ courses are state-of-art. They are used by many univesities.


There are zillions of C++ books out there, some better than others (most are outdated) . BTW you don't learn C++ by reading, but by doing.
There are onlne C++ courses but they lack the depth of QN, which BTW is focused on finance,

The price is an investment. I spent 30 years developing and improving it. So, you get 30 years for $1450.

BTW what would you like to do with C++?

@APalley
 
Last edited:
Alright, alright thank you.
I would have bought the course in a heartbeat, but the price too steep for me right now.
I have taken a TA position, but even if I manage to save up the total wage, it would still not be enough, because the price of the course is actually higher than the total wage I will receive. Hopefully, I will find a way of saving up the required amount (If I'm convinced that the course is the right choice for me.)

Various thoughts/concerns/questions

- How many hours per day does one have to allocate to the course? I'm still a student—at a rigorous problem—so my time schedule is alright tight.

- At which point should I take the course? I have not learned about, e.g., the Black-Scholes model yet.

- Also, the video https://quantnet.com/media/01-01-the-c-environment.1/ is 6 years old. Is the content not outdated?

- I like teaching material structured in the form of a book. For me videos are not an efficient way of studying/learning (there probably a few are exceptions, i.e. a short video will be fine in some cases). As far as I can see, there is no way of speeding up the video https://quantnet.com/media/01-01-the-c-environment.1/.
Well no, it is not outdated, We do up to C++20.
The two QN C++ courses are state-of-art. They are used by many univesities.


There are zillions of C++ books out there, some better than others (most are outdated) . BTW you don't learn C++ by reading, but by doing.
There are onlne C++ courses but they lack the depth of QN, which BTW is focused on finance,

The price is an investment. I spent 30 years developing and improving it. So, you get 30 years for $1450.

BTW what would you like to do with C++?

@APalley
In addition to the material, you will be getting commercially grounded, detailed guidance and feedback on all your code. As Dr. Duffy mentioned, this is far more important than reading books or watching videos. Writing good code is an art, which is only picked up through practice and guidance.
 
Well no, it is not outdated, We do up to C++20.
The two QN C++ courses are state-of-art. They are used by many univesities.


There are zillions of C++ books out there, some better than others (most are outdated) . BTW you don't learn C++ by reading, but by doing.
There are onlne C++ courses but they lack the depth of QN, which BTW is focused on finance,

The price is an investment. I spent 30 years developing and improving it. So, you get 30 years for $1450.

BTW what would you like to do with C++?

@APalley
Yes, of course one have to practice (doing) and not just read/watch videos.

Where I find an outline of the things which are taught in the course?

I will like to work in quantitative risk management; I was advised to learn C++.

To be honest, the only reason for me to take the course will be guidance.
 
In addition to the material, you will be getting commercially grounded, detailed guidance and feedback on all your code. As Dr. Duffy mentioned, this is far more important than reading books or watching videos. Writing good code is an art, which is only picked up through practice and guidance.
Yes, guidance and feedback—I also reached this conclusion (i.e. that guidance and feedback are the most valuable (for me) elements in the course) when I was thinking about the course yesterday.
 
Top