Coding practice

S

shaanxxx

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 .
 
R

Richard Bos

shaanxxx said:
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.)
 
S

shaanxxx

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)
 
R

Richard Bos

shaanxxx said:
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
 
T

Thad Smith

shaanxxx said:
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.
 
J

Jack Klein

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
 
J

Jack Klein

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top