10/30/2008

10-30-08 C# - 1

C# is obviously a copy of Java, MS wanted complete control and didn't like the open standard (and Sun is a bunch of bitches so I don't really blame MS on this one). Anyway, there is a certain simplicity and cleanliness to both C# and Java, and I think it almost all comes from the fact that they force you to stop doing silly micro-optimizations and worry more about your higher level code.

Everything that C# does that makes it clean you could do in C++. Initialize every value. Use garbage collection. Make everything an allocated GC'ed object so that you can put it in containers. Make everything derive from Object so you can put them in super generic containers. Dynamic cast between objects & throw exceptions. I think all of that is quite sensible. It makes code clean and safe, easy to write and easy to use.

But the vast majority of C programmers resist those things. They insist on using things like objects on the stack or char [] buffers for strings for the sake of efficiency. They don't initialize values when they "know" they don't need to, for efficiency. Once in a while that's actually important, but most of the time it's a mistake.

The nice thing about C# is you no longer have those options - and best of all, you stop thinking about it ! Even when I'm writing C++ I find myself thinking "is this ok? is this fast enough? am I using too much overhead?". In something like C# you have no option so you just stop worrying about it and that frees up your mind to focus on getting things done.

3 comments:

Donbot said...

But is this (stopping to worry) really a good thing? I think it always pays off to think about (micro-) optimizations in game development (Especially if you develop a game on the Nintendo DS). For "normal" applications you can easily use garbage collection, CObject base classes and types that auto-initialize all fields in the constructor. That's what i really like about C++: You always have the option.

Greets
Julien

cbloom said...

Yeah, I agree with you 100% - I don't actually like C# and I'm not advocating it's use in game development.

I adore C++, I love that you can pretty smoothly get anything between C#-style high level objects and down to nearly ASM low level efficiency.

I'm just thinking about why it feels easier to use and more direct for translating desires into functioning code.

Brian said...

I'm a big fan of Java-like languages. Type safety eliminates a huge class of nasty errors and the class library is a big win for hacking quick prototypes.

Though I may be biased as a person mostly writes compilers for Java-like languages. About the only time I program in C, it is really grungy stuff like writing a copying garbage collector which invariably have nasty bugs.

I think game programming is one of the few examples of where Java/C# is a lose. The big problem is you guys have serious performance constraints and there is no good way to use special CPU instructions/get low-level hardware access to graphics from Java.

old rants