Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #8805 > unrolled thread

Many forward reference in the code. Is it a ugly design?

Started byKaiWen <wenkai1987@gmail.com>
First post2011-08-13 20:33 -0700
Last post2011-08-14 00:10 -0700
Articles 11 — 6 participants

Back to article view | Back to comp.lang.c++


Contents

  Many forward reference in the code. Is it a ugly design? KaiWen <wenkai1987@gmail.com> - 2011-08-13 20:33 -0700
    Re: Many forward reference in the code. Is it a ugly design? Lynn McGuire <lmc@winsim.com> - 2011-08-13 22:42 -0500
      Re: Many forward reference in the code. Is it a ugly design? KaiWen <wenkai1987@gmail.com> - 2011-08-13 21:27 -0700
        Re: Many forward reference in the code. Is it a ugly design? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-08-14 05:54 +0000
          Re: Many forward reference in the code. Is it a ugly design? KaiWen <wenkai1987@gmail.com> - 2011-08-13 23:56 -0700
      Re: Many forward reference in the code. Is it a ugly design? "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> - 2011-08-14 07:27 +0200
        Re: Many forward reference in the code. Is it a ugly design? KaiWen <wenkai1987@gmail.com> - 2011-08-13 23:48 -0700
          Re: Many forward reference in the code. Is it a ugly design? Nobody <nobody@nowhere.com> - 2011-08-14 10:32 +0100
            Re: Many forward reference in the code. Is it a ugly design? KaiWen <wenkai1987@gmail.com> - 2011-08-14 05:13 -0700
          Re: Many forward reference in the code. Is it a ugly design? Paul N <gw7rib@aol.com> - 2011-08-15 13:15 -0700
        Re: Many forward reference in the code. Is it a ugly design? KaiWen <wenkai1987@gmail.com> - 2011-08-14 00:10 -0700

#8805 — Many forward reference in the code. Is it a ugly design?

FromKaiWen <wenkai1987@gmail.com>
Date2011-08-13 20:33 -0700
SubjectMany forward reference in the code. Is it a ugly design?
Message-ID<74a4b057-7063-4944-8cb9-b6bc7559abd5@g5g2000prn.googlegroups.com>
Many forward reference in the code. Is it a ugly design?

[toc] | [next] | [standalone]


#8806

FromLynn McGuire <lmc@winsim.com>
Date2011-08-13 22:42 -0500
Message-ID<j27g73$6f3$1@dont-email.me>
In reply to#8805
On 8/13/2011 10:33 PM, KaiWen wrote:
> Many forward reference in the code. Is it a ugly design?

If it is then so is mine.  I've got about 30
classes forward referenced.  Can't get a clean
compile without doing that.  This is a user
interface program with about 700 classes.

Lynn

[toc] | [prev] | [next] | [standalone]


#8809

FromKaiWen <wenkai1987@gmail.com>
Date2011-08-13 21:27 -0700
Message-ID<b30a5b97-37d1-4af4-bda7-5a0e10d80d10@m6g2000prh.googlegroups.com>
In reply to#8806
On 8月14日, 上午11时42分, Lynn McGuire <l...@winsim.com> wrote:
> On 8/13/2011 10:33 PM, KaiWen wrote:
>
> > Many forward reference in the code. Is it a ugly design?
>
> If it is then so is mine.  I've got about 30
> classes forward referenced.  Can't get a clean
> compile without doing that.  This is a user
> interface program with about 700 classes.
>
> Lynn

I think a class reference many classes, it mean it know too much.

[toc] | [prev] | [next] | [standalone]


#8812

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2011-08-14 05:54 +0000
Message-ID<slrnj4eonr.gtj.grahn+nntp@frailea.sa.invalid>
In reply to#8809
On Sun, 2011-08-14, KaiWen wrote:
> On 8??14??, ????11??42??, Lynn McGuire <l...@winsim.com> wrote:
>> On 8/13/2011 10:33 PM, KaiWen wrote:
>>
>> > Many forward reference in the code. Is it a ugly design?

It's called a "forward declaration", by the way.

>> If it is then so is mine.  I've got about 30
>> classes forward referenced.  Can't get a clean
>> compile without doing that.  This is a user
>> interface program with about 700 classes.
>>
>> Lynn
>
> I think a class reference many classes, it mean it know too much.

But the number of forward declarations is not a good measure of that.

Most of my forward declarations aren't there to break circular
dependencies -- they are there to cut down on #including. I can reduce
the number of forward declarations by including the headers with the
real declarations, but that would make the code /worse/.

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [next] | [standalone]


#8824

FromKaiWen <wenkai1987@gmail.com>
Date2011-08-13 23:56 -0700
Message-ID<578caa61-c2d9-4201-a8ee-3f9cfdbc72d8@y19g2000pre.googlegroups.com>
In reply to#8812
On 8月14日, 下午1时54分, Jorgen Grahn <grahn+n...@snipabacken.se> wrote:

> It's called a "forward declaration", by the way.

Thanks. ;)

[toc] | [prev] | [next] | [standalone]


#8810

From"Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com>
Date2011-08-14 07:27 +0200
Message-ID<j27mbe$vhc$1@dont-email.me>
In reply to#8806
On 14.08.2011 05:42, Lynn McGuire wrote:
> On 8/13/2011 10:33 PM, KaiWen wrote:
>> Many forward reference in the code. Is it a ugly design?
>
> If it is then so is mine. I've got about 30
> classes forward referenced. Can't get a clean
> compile without doing that. This is a user
> interface program with about 700 classes.

A large number of forward references need not be a sign of bad design. 
For example, for a sufficiently large system build time can be an 
important criterion. Trading some idealism for shorter build time can 
make sense.

However, a large number of circular references indicate that things 
could be simplified a lot. Whether it's worth the time to do it, that's 
another question.

So, I think, the OP's question has too little information to say 
anything (we don't know the reason for the forward references), and your 
reply has too little information (we don't know the cost of working out 
the circular references, or where they came from).

But as a general rule, it's a good idea to try to avoid circular type 
dependencies.

Circular dependencies make things complicated both for physical 
packaging of the code, and for understanding the constraints on data 
structures etc., and for automating things via smart-pointers.


Cheers,

- Alf

[toc] | [prev] | [next] | [standalone]


#8823

FromKaiWen <wenkai1987@gmail.com>
Date2011-08-13 23:48 -0700
Message-ID<4f2a10a2-f3a8-4900-b7cd-784a84a655cc@v9g2000pri.googlegroups.com>
In reply to#8810
On 8月14日, 下午1时27分, "Alf P. Steinbach"
<alf.p.steinbach:use...@gmail.com> wrote:
> On 14.08.2011 05:42, Lynn McGuire wrote:
>

> So, I think, the OP's question has too little information to say
> anything (we don't know the reason for the forward references)

Sorry, my english is not good. I'm a new hand.
I use forward references to break circular dependencies.
It's just a smell when I wrote my class. I felt it's not good when
there are too many forward references.
As you said, I need a general rule. Is there any way (or pattern, if
there is, too happy) to avoid this.

[toc] | [prev] | [next] | [standalone]


#8834

FromNobody <nobody@nowhere.com>
Date2011-08-14 10:32 +0100
Message-ID<pan.2011.08.14.09.32.03.882000@nowhere.com>
In reply to#8823
On Sat, 13 Aug 2011 23:48:44 -0700, KaiWen wrote:

> Sorry, my english is not good. I'm a new hand. I use forward references to
> break circular dependencies. It's just a smell when I wrote my class. I
> felt it's not good when there are too many forward references. As you
> said, I need a general rule. Is there any way (or pattern, if there is,
> too happy) to avoid this.

Too many circular dependencies tends to indicate a lack of modularity,
every component needing to know the details of every other component.

One way to eliminate such dependencies is by splitting a class into an
interface (with no member variables and only pure virtual methods) and an
implementation (which derives from the interface).

E.g. rather than A having a reference to B and B having a reference to A,
A has a reference to X, B is derived from X, B has a reference to A. These
can be defined in the order X, A, B, with no circularity.

[toc] | [prev] | [next] | [standalone]


#8838

FromKaiWen <wenkai1987@gmail.com>
Date2011-08-14 05:13 -0700
Message-ID<188b2e2a-ae64-4276-b141-0ba4cbd26d37@t30g2000prm.googlegroups.com>
In reply to#8834
On 8月14日, 下午5时32分, Nobody <nob...@nowhere.com> wrote:
> On Sat, 13 Aug 2011 23:48:44 -0700, KaiWen wrote:
> > Sorry, my english is not good. I'm a new hand. I use forward references to
> > break circular dependencies. It's just a smell when I wrote my class. I
> > felt it's not good when there are too many forward references. As you
> > said, I need a general rule. Is there any way (or pattern, if there is,
> > too happy) to avoid this.
>
> Too many circular dependencies tends to indicate a lack of modularity,
> every component needing to know the details of every other component.
>
> One way to eliminate such dependencies is by splitting a class into an
> interface (with no member variables and only pure virtual methods) and an
> implementation (which derives from the interface).
>
> E.g. rather than A having a reference to B and B having a reference to A,
> A has a reference to X, B is derived from X, B has a reference to A. These
> can be defined in the order X, A, B, with no circularity.

Hum, a good idea.
I found another way to do it.
eg:
If A have a reference to B, and B have a reference to A.
In A::do_something() will call
B::do_something(), then let A have a function object, which is a
wrapper
of B::do_something(). (The function could be boost::function or
something other).

Advantage:
Firstly, the function object in A could reference to
B::do_some_thing() or a
globe function or some_other_object::do_something(), and can be
replaced at
runtime.
Secondly, if A only call B::do_something(), A has a reference of B
(know all
of the interface of B) is unnecessary, and a forward declaration is
unnecessary too.

[toc] | [prev] | [next] | [standalone]


#8875

FromPaul N <gw7rib@aol.com>
Date2011-08-15 13:15 -0700
Message-ID<44432be6-7148-4bfa-8d6b-8e18721b4493@e7g2000vbw.googlegroups.com>
In reply to#8823
On Aug 14, 7:48 am, KaiWen <wenkai1...@gmail.com> wrote:
> I use forward references to break circular dependencies.
> It's just a smell when I wrote my class. I felt it's not good when
> there are too many forward references.
> As you said, I need a general rule. Is there any way (or pattern, if
> there is, too happy) to avoid this.

Is it possible to use "layers" where each class only deals with
classes in the next layer down? Or more generally, to decide for each
class how "basic" or "high-level" it is, and only have it call classes
that are more fundamental?

[toc] | [prev] | [next] | [standalone]


#8826

FromKaiWen <wenkai1987@gmail.com>
Date2011-08-14 00:10 -0700
Message-ID<06798d4d-40fc-4be8-bbdf-aa1a45793151@l11g2000prh.googlegroups.com>
In reply to#8810
On 8月14日, 下午1时27分, "Alf P. Steinbach"
<alf.p.steinbach:use...@gmail.com> wrote:
> On 14.08.2011 05:42, Lynn McGuire wrote:
>
> > On 8/13/2011 10:33 PM, KaiWen wrote:
> >> Many forward reference in the code. Is it a ugly design?
>
> > If it is then so is mine. I've got about 30
> > classes forward referenced. Can't get a clean
> > compile without doing that. This is a user
> > interface program with about 700 classes.
>
> A large number of forward references need not be a sign of bad design.
> For example, for a sufficiently large system build time can be an
> important criterion. Trading some idealism for shorter build time can
> make sense.
>
> However, a large number of circular references indicate that things
> could be simplified a lot. Whether it's worth the time to do it, that's
> another question.
>
> So, I think, the OP's question has too little information to say
> anything (we don't know the reason for the forward references), and your
> reply has too little information (we don't know the cost of working out
> the circular references, or where they came from).
>
> But as a general rule, it's a good idea to try to avoid circular type
> dependencies.
>
> Circular dependencies make things complicated both for physical
> packaging of the code, and for understanding the constraints on data
> structures etc., and for automating things via smart-pointers.
>
> Cheers,
>
> - Alf

Sorry, It's "forward declaration", I fell so awkward.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web