LWN.net Logo

What is Science?

What is Science?

Posted Jun 30, 2012 1:41 UTC (Sat) by Cyberax (✭ supporter ✭, #52523)
In reply to: What is Science? by Wol
Parent article: Why learn C? (O'Reilly Radar)

>On the other hand, if it takes 10 lines of C++ or 100 lines of C ...
Usually it's the other way around..


(Log in to post comments)

What is Science?

Posted Jun 30, 2012 8:15 UTC (Sat) by HelloWorld (guest, #56129) [Link]

That can't possibly be true. Everything that can be done in C can be done in C++, and more.

What is Science?

Posted Jun 30, 2012 8:27 UTC (Sat) by Cyberax (✭ supporter ✭, #52523) [Link]

I'm speaking about rewrites in "C++ style", not simple "recompile with g++" rewrites.

They do tend to shrink amount of lines of code quite significantly.

What is Science?

Posted Jun 30, 2012 8:39 UTC (Sat) by HelloWorld (guest, #56129) [Link]

You're contradicting yourself. Wol said "if it takes 10 lines of C++ or 100 lines of C", you said "it's the other way around..", and now you're saying that rewrites in C++ style tend to shrink the amount of code.

What is Science?

Posted Jun 30, 2012 22:00 UTC (Sat) by Cyberax (✭ supporter ✭, #52523) [Link]

Yes, rewrites in modern C++ tend to shrink code significantly compared to plain C. There's no contradiction.

What is Science?

Posted Jul 1, 2012 10:17 UTC (Sun) by HelloWorld (guest, #56129) [Link]

Uh, yes there is, unless my english parser is utterly broken, but I don't think so.

What is Science?

Posted Jun 30, 2012 8:28 UTC (Sat) by khim (subscriber, #9252) [Link]

This depends on your design very much. If you've dreamed up nice buzzword-compliant architecture of your project with classes, delegates and other such things then usually C++ code is smaller. If you instead will think about the task on hand and remove all the useless abstractions then often the end result will be less buzzword-compliant yet much smaller.

Of course you can write similar code in C++, but it'll not be "ideomatic C++".

What is Science?

Posted Jun 30, 2012 8:44 UTC (Sat) by Cyberax (✭ supporter ✭, #52523) [Link]

Uhm. Why classes are 'buzzword'? They are, like, 30 years old buzzword. In fact, even in languages like Java/C# architects now try to separate data from functionality (Fowler calls that 'anemic domain model'). And I don't understand that bit about 'useless abstractions'. For example, this kind of code:
std::function<void(int,int)> point_callback;

struct dist_summator
{
   Double dist;
   void add_point(double x, double y, double z)
   {
      dist+=sqrt(x*x+y*y+z*z);
   }
};

dist_summator sum;
point_callback=boost::bind(&dist_summator::add_point, &sum, _1, _2, 123.0d);
...
//Get points
...
//Use sum.dist
Is worse than something like this:
(void)(void*, int, int) point_callback;
void * point_callback_ctx;

typedef struct _dist_summator_with_fixed_z_ctx
{
   double sum;
   double fixed_z;
} dist_summator_with_fixed_z_ctx;
void sum_x_y(void *ctx_ptr, int x, int y)
{
   dist_summator_with_fixed_z_ctx *ctx=(dist_summator_with_fixed_z_ctx*)ctx;
   ctx->sum+=sqrt(x*x+y*y+ctx->z*ctx->z); //Whoopsie, forgot about ints here!   
}

dist_summator_with_fixed_z_ctx ctx;
point_callback = &sum_x_y;
point_callback_ctx = &ctx;
//Get points
...
//Use ctx.sum

What is Science?

Posted Jul 1, 2012 5:50 UTC (Sun) by wahern (subscriber, #37304) [Link]

Both of those examples are really ugly. Arguing that one dog is uglier than the next is kind of pointless.

What is Science?

Posted Jul 1, 2012 15:16 UTC (Sun) by hummassa (subscriber, #307) [Link]

Otoh they are close-to-what-I-have-found-IRL many, many times in the last 20 years.

What is Science?

Posted Jul 9, 2012 5:27 UTC (Mon) by daniel (subscriber, #3181) [Link]

And the C example is actually prettier than plenty of kernel code that does similar things.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds