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

Discussion in 'C++' started by KaiWen, Aug 14, 2011.

  1. KaiWen

    KaiWen Guest

    Many forward reference in the code. Is it a ugly design?
     
    KaiWen, Aug 14, 2011
    #1
    1. Advertising

  2. KaiWen

    Lynn McGuire Guest

    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
     
    Lynn McGuire, Aug 14, 2011
    #2
    1. Advertising

  3. KaiWen

    KaiWen Guest

    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.
     
    KaiWen, Aug 14, 2011
    #3
  4. 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
     
    Alf P. Steinbach, Aug 14, 2011
    #4
  5. KaiWen

    Jorgen Grahn Guest

    On Sun, 2011-08-14, KaiWen wrote:
    > On 8??14??, ????11??42??, Lynn McGuire <> 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 .
     
    Jorgen Grahn, Aug 14, 2011
    #5
  6. KaiWen

    KaiWen Guest

    On 8ÔÂ14ÈÕ, ÏÂÎç1ʱ27·Ö, "Alf P. Steinbach"
    <alf.p.steinbach:> 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.
     
    KaiWen, Aug 14, 2011
    #6
  7. KaiWen

    KaiWen Guest

    On 8ÔÂ14ÈÕ, ÏÂÎç1ʱ54·Ö, Jorgen Grahn <> wrote:

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


    Thanks. ;)
     
    KaiWen, Aug 14, 2011
    #7
  8. KaiWen

    KaiWen Guest

    On 8ÔÂ14ÈÕ, ÏÂÎç1ʱ27·Ö, "Alf P. Steinbach"
    <alf.p.steinbach:> 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.
     
    KaiWen, Aug 14, 2011
    #8
  9. KaiWen

    Nobody Guest

    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.
     
    Nobody, Aug 14, 2011
    #9
  10. KaiWen

    KaiWen Guest

    On 8月14æ—¥, 下åˆ5æ—¶32分, Nobody <> 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 referencesto
    > > 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.
     
    KaiWen, Aug 14, 2011
    #10
  11. KaiWen

    Paul N Guest

    On Aug 14, 7:48 am, KaiWen <> 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?
     
    Paul N, Aug 15, 2011
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    1
    Views:
    615
    Jonathan Turkanis
    Feb 15, 2004
  2. Stuart D. Gathman

    Help beautify ugly heuristic code

    Stuart D. Gathman, Dec 8, 2004, in forum: Python
    Replies:
    14
    Views:
    663
    Stuart D. Gathman
    Jul 25, 2006
  3. Replies:
    10
    Views:
    491
    Default User
    Jul 14, 2006
  4. Replies:
    10
    Views:
    605
    Default User
    Jul 14, 2006
  5. Jim Langston

    Ugly C looking code

    Jim Langston, Aug 26, 2006, in forum: C++
    Replies:
    5
    Views:
    414
    Ian Collins
    Aug 26, 2006
Loading...

Share This Page