Sign in to follow this  

Breaking up is hard to do

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I think it's generally a good idea to break up the code into smaller functions that each only do one thing - as many as conceptually practical. This of course makes reading and trying to understand a particular function a lot easier later on. However, a side effect of this is that the source files almost become cluttered up with a plethora of small functions, and some of the general view is lost. This must be why some languages allow defining sub-functions, right? I have tried a few ways to accommodate for this, like using namespaces or more files. More often than not a particular function is only called by one other function, so I've been thinking if it makes sense to somehow indicate that (on the naming of the function or so.) And also name private methods with a special prefix like many programmers like to do with private data. What's you view on this and how do you usually work this out?

Share this post


Link to post
Share on other sites
You can work around C++'s lack of sub-functions by defining a class inside of a function and then making static class member functions:


void Read()
{
struct Impl
{
static void DoReadInt()
{
// ...
}
};

Impl::DoReadInt();
}

Share this post


Link to post
Share on other sites
Quote:
Original post by antareus
You can work around C++'s lack of sub-functions by defining a class inside of a function and then making static class member functions:

*** Source Snippet Removed ***


Somehow, I don't think that's an improvement.

Share this post


Link to post
Share on other sites
I do. You can drastically reduce the scope of functions by using this technique and avoid cluttering up the outer class with the definitions. When combined with templates on the outer member function, it is very powerful. I can make overloads on the inner class's functions that call either CreateFileA or CreateFileW depending on the type of the string the user passed into the outer function (which is templated).

Nesting a class inside a function also makes it final. It is impossible to derive from outside of that function. Alas, inner classes cannot be templated.

I am curious to know why you do not think it is an improvement, however.

Share this post


Link to post
Share on other sites
That's a very interesting approach. So you usually do this. It clearly isolates the sub-functions as you said.

But it feels a bit indirect, and that makes me wonder why C++ doesn't support sub-functions. Is it because they want to promote object-oriented design in contrast to the "older" top-down approach?

Share this post


Link to post
Share on other sites
Are you only talking about languages that don't have the almighty lambda? Surely you are familiar with it. If not, then learn here.
Quote:
In using sum as in section 1.3.1, it seems terribly awkward to have to define trivial procedures such as pi-term and pi-next just so we can use them as arguments to our higher-order procedure. Rather than define pi-next and pi-term, it would be more convenient to have a way to directly specify ``the procedure that returns its input incremented by 4'' and ``the procedure that returns the reciprocal of its input times its input plus 2.'' We can do this by introducing the special form lambda, which creates procedures.

Quote:
Original post by antareus
I do. You can drastically reduce the scope of functions by using this technique and avoid cluttering up the outer class with the definitions. When combined with templates on the outer member function, it is very powerful. I can make overloads on the inner class's functions that call either CreateFileA or CreateFileW depending on the type of the string the user passed into the outer function (which is templated).

Nesting a class inside a function also makes it final. It is impossible to derive from outside of that function. Alas, inner classes cannot be templated.

I am curious to know why you do not think it is an improvement, however.
I agree that it can be an improvement but it isn't 'the done thing' and isn't a commonly published idiom so people reading your code will generally react as Promit has.

Share this post


Link to post
Share on other sites
Quote:
I agree that it can be an improvement but it isn't 'the done thing' and isn't a commonly published idiom so people reading your code will generally react as Promit has.

Tough. It is not rocket science, or even half as tricky as some template metaprogramming. I don't see why people would be averse to learning the ins and outs of the language.

Share this post


Link to post
Share on other sites
Quote:
Original post by antareus
Quote:
I agree that it can be an improvement but it isn't 'the done thing' and isn't a commonly published idiom so people reading your code will generally react as Promit has.

Tough. It is not rocket science, or even half as tricky as some template metaprogramming. I don't see why people would be averse to learning the ins and outs of the language.
Part of maintaining code is recognizing what the original author intended. Seeing an idiom that isn't used commonly can throw people for a loop (requiring multiple readings to figure out the intention). Even if it is technically sound and concise, the politics or maintainence make this a suspect idiom.

It depends on who you expect to follow you in the role of reading your code. Do you have a lot of code monkeys where you work -needing the most basic languages (as newspapers write in English at a third grade reading level) or very expert people who will immediately know what you mean (as technical journals meant only for graduates in a specific field). There are benefits to writing to target either group.

Write for the target audience.

[Edited by - flangazor on September 15, 2004 7:15:06 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by antareus
I am curious to know why you do not think it is an improvement, however.


I was not familiar with the actual reasons for doing such a thing, so I found it to be unnecessarily complex. Your explanation certainly helps to describe why you'd do something like that...although I must admit I'm still a bit wary, as I've never had a need for anything of the sort myself. And you just gained 17 points.

Share this post


Link to post
Share on other sites
Quote:
Original post by flangazor
It depends on who you expect to follow you in the role of reading your code. Do you have a lot of code monkeys where you work -needing the most basic languages (as newspapers write in English at a third grade reading level) or very expert people who will immediately know what you mean (as technical journals meant only for graduates in a specific field). There are benefits to writing to target either group.

Write for the target audience.

Oh definitely. You wouldn't see code monkeys using templates, for that matter, of course. Many people complain of not being able to get through Modern C++ Design.

More strange code:

class Base {}; // has some interesting virtual functions in it

Base* Foo()
{
class Bar : public Base {};

return new Bar();
}

Bar is completely hidden from client code, hence the finality of it if they are in separate compilation units. Very cool.

Share this post


Link to post
Share on other sites
Oops, for a moment there I thought I was going to be giving relationship advice (from your post's title) but it's the wrong forum for that. :P

My guess is that you're probably breaking your code up into seperate functions too often. Don't get me wrong, it's a good thing to do, but "too much of a good thing makes it bad."

What would be really cool would be something that gives you a histogram of the number of places a function is called from. Then you would be able to see some interesting things about people's coding sytles. Hmm, maybe that should be my next project...

Share this post


Link to post
Share on other sites
antareus, another issue with the static member function in a local struct is the lack of linkage, therefore preventing one of the most useful uses of it: stl function mappings (foreach, etc). One can use boost::lambda instead.

Which makes me wonder why no one mentioned boost::lambda before. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
Oops, for a moment there I thought I was going to be giving relationship advice (from your post's title) but it's the wrong forum for that. :P


Yes, I had hopes of it attracting more devoted programmers. [wink]

Quote:
What would be really cool would be something that gives you a histogram of the number of places a function is called from.


It would be an interesting measure of a programmer's ability to abstract, modularize and re-use code. What more aspects can be analysed? Maybe this could one day lead to a new branch of comparative literature studies. Are there sociographic or historical aspects of this variance? Maybe it could be used as a test for employment. What can really be told about a person from his code?


Share this post


Link to post
Share on other sites
Quote:
Original post by antareus
You can work around C++'s lack of sub-functions by defining a class inside of a function and then making static class member functions:

*** Source Snippet Removed ***


I've been thinking a bit about this approach.
As you pointed out, it's very powerful and expressive, especially together with templates. However, I'm not sure it's completely free from certain problems.


struct outer {
static int i;
struct inner {
void f() {
struct local {
void g() {
i = 5;
}
};
}
};
};





For example, it seems the C++ standard does not cover outer::i to be found by local class function g()?

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#191

Share this post


Link to post
Share on other sites

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this