Saturday, February 21, 2009

C# vs. C++

Today I’m going to bring the programming monsters together, compare its environment features and calculate their bout points. It’s impossible to cover all in one post, so here I’m going to talk about something that make up development philosophy, peculiar attributes in contrast to each other.

C++ strengths


1) friend


Friend declaration allows one class to get access to non-public members of another class. On the first look this feature is redundant and means nothing but object-oriented principles violation. However, I can figure four reasons where it’s important to have friends.
  • Non-public constructors/destructors. This case covers different creational patterns.

  • Non-public methods which however are not intended for internal class usage. This technique allows usage of non-public class interface only by special class, group of classes or proxies.

  • Lack of direct access to class data for some operation that a class doesn’t respond itself. The best example here is (de)serialization. Moreover, you can see that C# XmlSerializer undermines data encapsulation process completely.

  • Highly coupled by design classes as an alternative to nested classes.

  • The alternative to c++ friend is c# internal keyword, which is the part of friend assembly technique. However, I can’t say that it’s equivalent.

    2) const


    In c++ there are two intrinsic const goals, which were omitted in c#.
  • Passing arguments with const keyword to methods prevents its further modification in the methods.

  • Marking class methods with const keyword creates an assurance that class data are read only in the methods.

  • Of course, these techniques require additional workload, but it’s very powerful feature. Often a const keyword says more than enclosed method documentation.

    3) Memory model


    My pretence to c# is the excessive memory simplification. It becomes a problem when you need to pass structure (value-type) by reference, and vice versa, to pass class (reference-type) by value. The first case was solved by adding ref/out keywords (except the situation with a method’s returning value by reference – it’s impossible because a value lifetime strictly depends on a method’s scope. But there is another example where we need to return this structure by reference – it’s also impossible). Excepting previous remark, things look normal here. A value by default is an optimal approach and when you really need a reference, ref/out keywords appear.
    More complicated situation we have with the second case. When you need to pass an object by value, you should clone it. ICloneable interface is intended for it. However, you can’t see any method’s declaration signs that an object is passed by value. It’s potential pitfall. Another interesting thing is that you can use ref/out keywords with reference types. It’s a rough equivalent to c++ reference to pointer. Next c# code snippet demonstrates it:

    public class A
    {
    }

    class Program
    {
    static void Method(A a)
    {
    a = new A();
    }
    static void Method(ref A a)
    {
    a = new A();
    }
    static void Main()
    {
    A a = null;
    Method(a);
    Console.WriteLine(a == null);//True
    Method(ref a);
    Console.WriteLine(a == null);//False
    }
    }

    In c++ the concepts of pointer and reference with the operators arrow, dot, dereference and address-of solve all possible tasks in the generalized manner without any ad hoc solutions.
    Another problem is the garbage collector. Yes, it frees you from memory managing, but however it also frees you from thinking about memory usage at all. new keyword in c++ is not a simple operator to create an object, it creates a heap object and the purpose of this creation is an obvious intention to keep up object’s lifetime.
    Of course I cannot but mention about famous c++ memory leaks. Well, they are not such horrible as people say. Dangling pointers that don’t leave your code are solved easy. Problems can be with third-party APIs and foreign code where a solution becomes harder. However, I can’t drop this c++ point due to this reason.

    4) header/source organization


    Another pretty c++ thing is headers. I don’t mean a compilation/linkage model (here c++ is much more difficult than c#), but mean header as contents of a module and header + source as a complete module. Abstraction and modularity are the key design concepts and c++ makes the cut, c# approach that uses only source files and one source file is class declaration & definition doesn’t fascinate me.

    5) cross-platform feature


    In case when a project or its part can be potentially cross-platform or uses Linux, Mac as a primary environment, or uses third-party cross-platform libraries already written in c++, c++ becomes a good choice to get native cross-platform code.

    C# strengths.


    1) interface


    Interface is the amazing thing that c# gives. I don’t mean the technical approach (by the way I think c++ with multiple inheritance is better here because it can include not only a behavior but private data also, which forms a completed reusable block), but mean the ideological approach – how framework classes reuses interfaces and how it increases abstraction on the whole. Learning an API becomes easier. Ideally, you learn a class description and interfaces that a class implements. The number of interfaces is much more less than classes in an API. The difference between inheritance and interface is that inheritance considerably helps to reuse code but unfortunately doesn’t help to create good abstraction. Interface, which is the evolved form of abstract class, makes a contract and each class that implements it must adhere to its contract. Moreover, interfaces go through class hierarchies.

    2) Environment benefits.


  • .NET Framework code style naming convention. To tell the truth I’ve been using it everywhere.

  • Visual Studio maybe the best development environment even for cross-platform based c++ projects. But some features like code auto-formatting (ctrl-K-D), Refactor menu and so on belong to c# only.

  • Faster, easier and more careful compilation.


  • 3) ASP + ADO


    This collaboration forms the complete platform for web portals or any web server based solutions. It’s the stock phrase, which embodies web/data programming based on MS Windows platform + MS SQL Server.

    4) WPF


    GUI programming evolves very well. Since .NET 3.0 it aims to any desktop/web environment in generalized manner including animation, 2D/3D graphics, multimedia features.

    5) WCF


    Service-oriented features also evolve. Since .NET 3.0 Web Services and Remoting are generalized into one package.

    Conclusion.


    So for today what’s the reason to choose between C# and C++? If we talk about IT-related projects c# becomes the obvious choice. If we talk about programming projects we should look into C++ points 5 and also 3 and C# points from 3 through 5. Depending on project’s complexity both can be the choice. However the most important matter here is performance and machine resources cost. Actually I haven’t mentioned about c# managed environment vs. c++ native. It’s another discussion and it must be uncovered later.
    Another programming niche is maintaining existing projects and here c++ is on demand. Migrating stuff doesn’t help.