Coding practice

Discussion in 'C Programming' started by shaanxxx, Dec 24, 2007.

  1. shaanxxx

    shaanxxx Guest

    I alway had a question in mind whether to have two function in our
    programme or one function with "if statement"
    for two task having some part common.


    I try to explain through example

    -> First implementation
    Node * GetNextFile(ListState state)
    {

    ... //common statements for file and directory processing
    ... // File specific statement

    }

    Node * GetNextDIR(ListState state)
    {

    ... //common statements for file and directory processing
    ... // Directory specific statements

    }


    -> Second implementation
    Node * GetNext(ListState state , NodeType type)
    {

    ... //common statements for file and directory processing

    if (type == FILE_TYPE)
    {
    ... // File specific statement
    }
    else
    {
    ... // Directory specific statements
    }

    }



    Let me know your thoughts on this

    I think if common processing statements are less , we can go first
    implementation .
     
    shaanxxx, Dec 24, 2007
    #1
    1. Advertising

  2. shaanxxx

    Richard Bos Guest

    shaanxxx <> wrote:

    > I alway had a question in mind whether to have two function in our
    > programme or one function with "if statement"
    > for two task having some part common.


    It Depends.

    HTH; HAND.

    Richard





    (No, really. It depends. Not just on the task at hand, but also on the
    system(s) you intend to use.)
     
    Richard Bos, Dec 24, 2007
    #2
    1. Advertising

  3. shaanxxx

    shaanxxx Guest

    On Dec 24, 4:07 pm, (Richard Bos) wrote:
    > shaanxxx <> wrote:
    > > I alway had a question in mind whether to have two function in our
    > > programme or one function with "if statement"
    > > for two task having some part common.

    >
    > It Depends.
    >
    > HTH; HAND.
    >
    > Richard
    >
    > (No, really. It depends. Not just on the task at hand, but also on the
    > system(s) you intend to use.)


    How it could be dependent on system ?
    (my thoughts are , first implementation will have more code because of
    code duplication. This duplication of code be avoided using by
    replacing with function. This will result into more CPU cost . Its
    like if i try to save memory , i lose on cpu cycle)
     
    shaanxxx, Dec 24, 2007
    #3
  4. shaanxxx

    Richard Bos Guest

    shaanxxx <> wrote:

    > On Dec 24, 4:07=A0pm, (Richard Bos) wrote:
    > > shaanxxx <> wrote:
    > > > I alway had a question in mind whether to have two function in our
    > > > programme or one function with "if statement"
    > > > for two task having some part common.

    > >
    > > It Depends.
    > >
    > > HTH; HAND.
    > >
    > > Richard
    > >
    > > (No, really. It depends. Not just on the task at hand, but also on the
    > > system(s) you intend to use.)

    >
    > How it could be dependent on system ?


    Well, take only your own example. On some systems it would make sense to
    treat a directory as a specialised kind of file; on others, it would
    not.

    Richard
     
    Richard Bos, Dec 24, 2007
    #4
  5. shaanxxx

    Thad Smith Guest

    shaanxxx wrote:
    > I alway had a question in mind whether to have two function in our
    > programme or one function with "if statement"
    > for two task having some part common.
    >
    >
    > I try to explain through example
    >
    > -> First implementation
    > Node * GetNextFile(ListState state)
    > {
    >
    > ... //common statements for file and directory processing
    > ... // File specific statement
    >
    > }
    >
    > Node * GetNextDIR(ListState state)
    > {
    >
    > ... //common statements for file and directory processing
    > ... // Directory specific statements
    >
    > }
    >
    >
    > -> Second implementation
    > Node * GetNext(ListState state , NodeType type)
    > {
    >
    > ... //common statements for file and directory processing
    >
    > if (type == FILE_TYPE)
    > {
    > ... // File specific statement
    > }
    > else
    > {
    > ... // Directory specific statements
    > }
    >
    > }
    >
    >
    >
    > Let me know your thoughts on this
    >
    > I think if common processing statements are less , we can go first
    > implementation .


    The question is probably more suited for comp.programming.

    The choices impact both the interface to the functions and their
    implementation. My choice, without special circumstances, would be to
    choose the one that has the most sensible interface, which I think would be
    the first version with two specific functions: the interfaces are simpler,
    with each performing a single function.

    After you make that choice, you have an implementation question of whether
    to combine the common statements into a separate function or copy them.
    Subscribers to the DRY (Don't Repeat Yourself) principle, including myself,
    would prefer to place the common statements in a separate function.

    --
    Thad
     
    Thad Smith, Dec 24, 2007
    #5
  6. shaanxxx

    Jack Klein Guest

    On Mon, 24 Dec 2007 04:09:32 -0800 (PST), shaanxxx
    <> wrote in comp.lang.c:

    > On Dec 24, 4:07 pm, (Richard Bos) wrote:
    > > shaanxxx <> wrote:
    > > > I alway had a question in mind whether to have two function in our
    > > > programme or one function with "if statement"
    > > > for two task having some part common.

    > >
    > > It Depends.
    > >
    > > HTH; HAND.
    > >
    > > Richard
    > >
    > > (No, really. It depends. Not just on the task at hand, but also on the
    > > system(s) you intend to use.)

    >
    > How it could be dependent on system ?
    > (my thoughts are , first implementation will have more code because of
    > code duplication. This duplication of code be avoided using by
    > replacing with function. This will result into more CPU cost . Its
    > like if i try to save memory , i lose on cpu cycle)


    Where are the profile results that prove that your program is too
    slow?

    Where are the memory requirements and statistics for your program as
    it is, proving that is uses too much memory?

    Where are the test results proving that your program is complete,
    correct, and meets all of its requirements?

    If you don't have all of these things, you are wasting your time
    thinking about memory versus execution speed tradeoffs.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Dec 24, 2007
    #6
  7. shaanxxx

    Jack Klein Guest

    On Mon, 24 Dec 2007 01:57:45 -0800 (PST), shaanxxx
    <> wrote in comp.lang.c:

    > I alway had a question in mind whether to have two function in our
    > programme or one function with "if statement"
    > for two task having some part common.
    >
    >
    > I try to explain through example
    >
    > -> First implementation
    > Node * GetNextFile(ListState state)
    > {
    >
    > ... //common statements for file and directory processing
    > ... // File specific statement
    >
    > }
    >
    > Node * GetNextDIR(ListState state)
    > {
    >
    > ... //common statements for file and directory processing
    > ... // Directory specific statements
    >
    > }
    >
    >
    > -> Second implementation
    > Node * GetNext(ListState state , NodeType type)
    > {
    >
    > ... //common statements for file and directory processing
    >
    > if (type == FILE_TYPE)
    > {
    > ... // File specific statement
    > }
    > else
    > {
    > ... // Directory specific statements
    > }
    >
    > }
    >
    >
    >
    > Let me know your thoughts on this


    Both of your approaches are wrong.

    > I think if common processing statements are less , we can go first
    > implementation .


    If you think that, do that. Get lost counting statements and lose
    sight of important things.


    T1 common(T2 param)
    {
    /* ... */
    }

    T1 type_one(T2 param)
    {
    /* ... */
    }

    T1 type_two(t2 param)
    {
    /* ... */
    }

    T1 some_func(t2 param, t3 other)
    {
    common(param);
    if (other == /* ... */ )
    {
    type_one(param);
    }
    else
    {
    type_two(param);
    }
    /* ... */
    }

    Search "refactoring".

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Dec 24, 2007
    #7
    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. John Smith

    VB.Net bug or Poor Coding Practice?

    John Smith, Aug 11, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    493
    John Smith
    Aug 14, 2005
  2. Roman Roelofsen

    coding conventions, PEP vs. practice

    Roman Roelofsen, Jan 5, 2005, in forum: Python
    Replies:
    3
    Views:
    299
    Terry Reedy
    Jan 5, 2005
  3. js
    Replies:
    6
    Views:
    385
    Mark Jeffcoat
    Nov 22, 2006
  4. Justin.Voelker

    Best Practice for page coding

    Justin.Voelker, Apr 2, 2007, in forum: HTML
    Replies:
    12
    Views:
    639
    Toby A Inkster
    Apr 4, 2007
  5. Savvoulidis Iordanis
    Replies:
    7
    Views:
    585
    Göran Andersson
    Jul 20, 2008
Loading...

Share This Page