Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8292351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:26:52+00:00 2026-06-08T13:26:52+00:00

This is definitely subjective, but I’d like to try to avoid it becoming argumentative.

  • 0

This is definitely subjective, but I’d like to try to avoid it
becoming argumentative. I think it could be an interesting question if
people treat it appropriately.

In my several recent projects I used to implement architectures where long delegation chains are a common thing.

Dual delegation chains can be encountered very often:

bool Exists = Env->FileSystem->FileExists( "foo.txt" );

And triple delegation is not rare at all:

Env->Renderer->GetCanvas()->TextStr( ... );

Delegation chains of higher order exist but are really scarce.

In above mentioned examples no NULL run-time checks are performed since the objects used are always there and are vital to the functioning of the program and
explicitly constructed when execution starts. Basically I used to split a delegation chain in these cases:

1) I reuse the object obtained through a delegation chain:

{ // make C invisible to the parent scope
   clCanvas* C = Env->Renderer->GetCanvas();
   C->TextStr( ... );
   C->TextStr( ... );
   C->TextStr( ... );
}

2) An intermediate object somewhere in the middle of the delegation chain should be checked for NULL before usage. Eg.

clCanvas* C = Env->Renderer->GetCanvas();

if ( C ) C->TextStr( ... );

I used to fight the case (2) by providing proxy objects so that a method can be invoked on non-NULL object leading to an empty result.

My questions are:

  1. Is either of cases (1) or (2) a pattern or an antipattern?
  2. Is there a better way to deal with long delegation chains in C++?

Here are some pros and cons I considered while making my choice:

Pros:

  • it is very descriptive: it is clear out of 1 line of code where did the object came from
  • long delegation chains look nice

Cons:

  • interactive debugging is labored since it is hard to inspect more than one temporary object in the delegation chain

I would like to know other pros and cons of the long delegation chains. Please, present your reasoning and vote based on how well-argued opinion is and not how well you agree with it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:26:55+00:00Added an answer on June 8, 2026 at 1:26 pm

    I wouldn’t go so far to call either an anti-pattern. However, the first has the disadvantage that your variable C is visible even after it’s logically relevant (too gratuitous scoping).

    You can get around this by using this syntax:

    if (clCanvas* C = Env->Renderer->GetCanvas()) {
      C->TextStr( ... );
      /* some more things with C */
    }
    

    This is allowed in C++ (while it’s not in C) and allows you to keep proper scope (C is scoped as if it were inside the conditional’s block) and check for NULL.

    Asserting that something is not NULL is by all means better than getting killed by a SegFault. So I wouldn’t recommend simply skipping these checks, unless you’re a 100% sure that that pointer can never ever be NULL.


    Additionally, you could encapsulate your checks in an extra free function, if you feel particularly dandy:

    template <typename T>
    T notNULL(T value) {
      assert(value);
      return value;
    }
    
    // e.g.
    notNULL(notNULL(Env)->Renderer->GetCanvas())->TextStr();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am aware that this could be seen as subjective but this is definitely
Okay this is definitely a n00b question but here goes. The way I understand
This isn't a code question for once, but it definitely has me confused. Basically,
This might be a subjective question, but I'll give it a go. There are
This is probably something silly I'm missing but I'm definitely lost. I'm using .NET
Okay this is definitley an easy question and a stupid one but since I
this is my first question in here, and I would like to ask if
This question probably has no definite answer, so if it's considered subjective and, let's
Sorry for the rather subjective question, but I was hoping to get an opinion
I know the question is very subjective. But I cannot form the question in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.