Googles appar
Huvudmeny

Post a Comment On: cbloom rants

"09-26-09 - Habits - Code Inlining"

6 Comments -

1 – 6 of 6
Blogger   said...

my personal favorite is the local function. Keeps code flow reasonable, but doesn't duplicate code everywhere all the time.

void foo() {
struct local_t {
static void bar() {
}
};
local_t::bar();
local_t::bar();
local_t::bar();
}

its exceptionally convenient when you want to qsort or std::sort. No more hunting for the correct sort function, just put it inline.

September 26, 2009 at 11:32 AM

Blogger cbloom said...

Yeah local functions are pretty gross in C++99, but they will be neat in C++0x.

September 26, 2009 at 12:09 PM

Blogger cbloom said...

Actually I really don't like the local function. It's basically the worst of both worlds. It makes the code flow nonlinear, and it also fails to make a robust utility function that's easily shared and reused. The fact that you can't use it in templates makes it basically useless in C++9x.

Also it's another conscious choice which means another possibility for mistakes. If there's a valid complaint about C++ it's that it gives the practitioner too many difficult choices in how to write code. Most modern variants like Java and C# improve C++ by greatly reducing the ways you can do things.

September 26, 2009 at 1:27 PM

Blogger cbloom said...

In other new, the way Blogger loads the page, auto - logs you in, then resets the page is really fucking killing me. Fix your shit Google. I keep typing things in the comment box then having them wiped out when the page finally finishes loading and decides to reset and hammer on my progress.

I've written about this bug before, but it's much worse now at my house because my internet is so fucking absurdly slow.

September 26, 2009 at 1:29 PM

Blogger   said...

yeah, I'm really looking forward to lambda functions in the new standard. local functions make some things nice, but the certainly have their drawbacks. One large one is that they are hard to debug as the debugger messes up quite frequently in them. In general I'm a big fan of linear code. None of that function fan-in fan-out stuff just because. If it makes sense to do it that way, then do it that way. Otherwise keep it simple.

September 26, 2009 at 10:57 PM

Blogger   said...

btw, the linear code argument is not a new one. Its been around since the inception of high level programming languages. I can't find the article at the moment, but there have been studies done, and people aren't very good at understanding code that isn't linear. This is taken to extremes in other industries (like the aviation industry) where people's lives are on the line. They don't like to have any branches in their code flow at all if they can help it. One bug after all can cost them huge amounts in court, as well as its a stain on their conscious. However, this leads people to places they don't like to be.
Linear code -> inlining code -> less code reuse -> why are we using c++?
People are sometimes a little over passionate when it comes to their language and coding style of choice. I try to have no opinions myself and keep an open mind. Afterall, in some places I'm sure C++ makes excellent use, however in others it probably hurts more than it helps. All to often I find people using virtual functions for example when there is absolutely no need for it. Just because they might at some point in time in a mythical future. Maybe I should write a blog entry. This comment is getting a little long.

September 26, 2009 at 11:08 PM

You can use some HTML tags, such as <b>, <i>, <a>

This blog does not allow anonymous comments.

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.