Search

Sonntag, 16. April 2017

Why I personally don’t like to use C++ in business projects

Why I personally don’t like to use C++ in business projects:
I decided to write two  articles, because one day i was asked by one of my clients why i refuse to work on projects where C++ mixed with Java, second reason I want to capture my thought pattern after reading book  Bjarne Stroustrup Programming principles and practice using C++ (1634 Pages book!).
Before you read further, please notice this is my personal subjective opinion, I am Oracle certified Java developer after all, so consider this as last warning. This article consist technical stuff only, read my previous blog about business side.
Now, I will give there some good points about why I prefer to work with Java rather then with C++:
First: C++ has a huge inheritance problem, so called deadly diamond problem:
So we have 3 Classes Father, Mother , Son, Daughter, Son and Daughter both inherit the constructor of Father and Mother , personally I find very nasty the way of class declaration in separate header files, C++ separates declarations (header files) and definitions (in cpp files) (not my taste!) 

Look at this “hell”:

//contents of Daughter.h
#ifndef DAUGHTER_H
#define DAUGHTER_H
#include "Mother.h"
#include "Father.h"
class Daughter : public Father, public Mother
{
    public:
        //destructor
        ~Daughter();
};
#endif

//contents of Daughter.cpp
#include <iostream>
#include <string>
#include "std_lib_facilities.h"
#include "Mother.h"
#include "Father.h"
#include "Daughter.h"

Daughter::~Daughter(){
                cout << "goddbye from daughter!!!";
}

//contents of Son.h
#ifndef SON_H
#define SON_H
#include "Mother.h"
#include "Father.h"
class Son: public Father, public Mother
{
    public:
        //destructor
                              ~Son();
};


//contents of Son.cpp
#include "std_lib_facilities.h"
#include "Mother.h"
#include "Father.h"
#include "Son.h"

Son::~Son(){
                cout << "Goodbye from son!";
}

//contents of Mother.h 
#ifndef MOTHER_H
#define MOTHER_H

class Mother
{
public:
               Mother();
               //destructor
               ~Mother();
};


//contents of Mother.cpp
#include "std_lib_facilities.h"
#include "Mother.h"
Mother::Mother() {         cout << "Hello from Mother!!!";
}
Mother::~Mother(){
               cout << "Goodbye from father!!! ";
};

//contents of Father.h
#ifndef FATHER_H
#define FATHER_H
class Father
{
    public:
                              Father();
                              //destructor
                              ~Father();
};

//contents of Father.cpp
#include "std_lib_facilities.h"
#include "Father.h"

Father::Father(){
               cout << "Hello from Father!!!";
}
Father::~Father(){
               cout << "Goodbye from father!!! ";


}

//contents of TESTOOP.cpp
#include "std_lib_facilities.h"
#include "Father.h"
#include "Mother.h"
#include "Son.h"
#include "Daughter.h"

int main()
{
               cout << "Call to Father -> ";
    Father fatherObj;
               cout << endl;
               cout << "Call to Mother -> ";
               Mother motherObj;
               cout << endl;
               cout << "Son inhert: ";
               Son sonObj;
               cout << endl;
               cout << "Daughter inhert: ";
               Daughter daughterObj;
               getchar();
               return 0;
}
Second: C++ allows to use pointers (pointer madness) see the following example:
In this example you can read what the code doing , but imagine in the real world where you have thousand or more variables and each of this you can directly access via pointer, this is pure madness to work with such code!!
#include "std_lib_facilities.h"

int main()
{
       int number1 = 220;

       int* pointerToNumber;
       int* pointerToAPointer;
       int* pointerToAPointerToAPointer;

      
       pointerToNumber = &number1; //<-- this pointer hold the address of number1
       pointerToAPointer = pointerToNumber; // <-- pointerToAPointer hold the address of pointerToNumber!!!
       pointerToAPointerToAPointer = pointerToAPointer; //// <--pointerToAPointerToAPointer hold the address of pointerToNumber!!!

       cout << "Value of a pointerToNumber "<< *pointerToNumber << " address " << pointerToNumber;
       cout << "\nValue of a pointerToAPointer "<< *pointerToAPointer << " address " << pointerToAPointer ;
       cout << "\nValue of a pointerToAPointerToAPointer " << *pointerToAPointerToAPointer << " address " << pointerToAPointerToAPointer;
      
       getchar();
      
       return 0;
}


Third: C++ provides direct access to memory (random memory corruption!!!) here is example:
Access a memory which has nothing to do with your application, this is just unsafe in modern world where your application must be fast and safe and work in different environments!!!
#include "std_lib_facilities.h"
#include<stdio.h>

int main()
{
       int *p=(int *)0x00402000;  // acces to memory!!!!!
    printf("%d",*p);

    printf("%p", p);  // what have we here


       getchar();
      
       return 0;
}
  
Forth: C++ has problem with type checking == weakly typed system here is example
int main()
{
    int a = 125;
    bool problemState;
    problemState = (bool)a;

       cout << "Result "<< problemState;

       getchar();
      
       return 0;
}

In my opinion this very , very bad, because you lose value integrity.

Five: C++ allows memory leaks, this is especially nasty, I saw some projects, people actually did overworked in such cases! Here is simple example:
#include "std_lib_facilities.h"


int main()
{
     // This works!
    int * pointerToInt = new int;
    delete pointerToInt;

    // Memory leak
    int * q = new int;
    // no delete

       getchar();
      
       return 0;
}

Six: C++ have some standardization issues , even  the author of C++ Bjarne Stroustrup have spoken about it!


C++ by nature is very platform dependent (Except if you can do all your work in ANSI C++!!!), so don’t tell me that C++ can be easily ported to any platform, it’s not true (especially if you use libraries from Microsoft Environment but your requirements tell you to port application to Linux )!!!

Where and how to use C++?
I think C++ it’s like a weapon with high damage and precision rate, if you need to take one aim exactly as you imagine then go for it, but if you need general purpose language consider Java a quick and fast way to achieve the goal with safe net of unit tests. Java already provides large number of useful frameworks.
Summary: as you can see C++ is not easy to understand and to master, there are pitfalls which just wait for you to fall in, if you decide to use this language in your business,  then expect big and bumpy ride to your initial goal, but don’t take me wrong, I think C++ is great language but it has very special usage domain and can be very good for solving some special problems. My last advise would be at any time aware what you doing and write lots of unit test, don’t give up to the pressure which some management “give you”, you are responsible for your code and as professional you should care what you writing as final product!


7 Kommentare:

  1. This basically looks like a post from a Java Developer who discovered C++ and now is surprised that it's not like Java. Congratulations.

    Header files result from the idea that definition and implementation should be separated. There are still people who believe this in other languages and write an interface for every class.

    The Diamond Problem is not the multiple inheritance itself (which obviously you didn't mention by name) but the inheritance of already inherited stuff like Grandson inherits from Father and Son, but father/son is always a very bad example for inheritance. But there are situations where you need multiple inheritance and in Java you have to work around it with interfaces and nested instances.

    I don't get the pointer example. If you don't want a pointer to a pointer which points to a pointer, then don't do it.

    Direct access to memory might be a problem regarding stability but is a huge advantage regarding performance because it's much cheaper than a Garbage Collector. And it's needed when you don't think of personal computers but embedded devices or debit cards. Second of all, it's needed in security: Sometimes you have encrypted information that needs to be decrypted. You cannot prevent the now decrypted secret information to appear plaintext in the memory, but you can reduce the attack vector by removing it from the memory as soon as you don't need it anymore. But not in Java, there you can only tell the Garbage Collector that it's garbage and hope that it will soon clean up. C# introduced the unsafe block for that.

    Point five: Memory leaks is not "allowed" in C++, it's a consequence of the self memory managament. One could also say "Java allows SQL injection", but that's not quite right.

    C++ is also vendor independent while Java - I just Oracle - is not. And with more frameworks comes more platform dependence.

    AntwortenLöschen
  2. First thanks for interesting comment, let me make it clear.
    1. I stated that this article my subjective opinion
    2. I know C++ and have some experience with this language in business world.
    3. You did acknowledge that C++ having the deadly diamond problem or not?
    4. Pointer example show that C++ allow such complicated and very difficult to understand connection.
    5. Some JVM can convert byte code direct to platform dependent code so performance actually in Java is not an issue.
    6. In Java you can write to disk the encrypted data, so you argument is pointless, Java static and dynamic type safe language.
    7. Memory leaks are possible in C++ or not? I saw many c++ applications which done so.
    8. The ANSI C++ is vendor independent, other C++ versions are Vendor dependent, especially if you working with some Microsoft only frameworks. Oracle own Java but my personal experience, says that they actually listen to the community and there a big open community which communicate problems to Oracle and Oracle listen to it. And what about Microsoft and GNU Community do they get along with C++ Standard, according to Bjarne Stroustrup there is a problem, they talking very long about C++ language rules.
    9. And what about learning curve in C++ vs Java? How much years or month you need to take to master one of those languages, i think in Java you will write faster and better programms , rather as in C++.
    10. As i said early C++ has his advantages and in general it is all about your skills, if you mastered this language indeed you can write great software , but in modern world there is too much pressure to be master of all of IT domain, so think about it.

    AntwortenLöschen
  3. 3. Yes, just like Java has the SQL injection problem. Just don't run into the problem...
    5. But you loose the platform dependency, a huge pro-argument for Java. And this still does not work in the embedded domain. If you look at Java Card for example, it's has no longer much in common with Java.
    6. I think you don't get my point. You still have the unencrypted data in the memory and cannot get rid of it. And being a static and dynamic type safe language has absolute nothing to do with data protection.
    7. Yes, they are possible. Just like SQL injection is possible in Java. But it's not a sign of a poorly designed language, it's just a consequence of the ability to control the memory (or the database).
    9. I think in Java you can write faster, but this doesn't mean your quality of code is higher or your program will be faster. And I think learning faster has its pros and cons. Using Hibernate will not teach you SQL. Just like using a garbage collector will not teach you memory management. Learning to code C++ takes longer but you also gain much more knowledge about what the system is doing. Your "Senior Developer criterias"* say that you should know about memory management and even CPU registers, so shoudn't everyone learn C++?
    10. To master all of IT domain includes C++ and there are numerous scenarios where you should favour C++ over Java.

    *which are nonsense in my opinion, but that's another comment for another post

    AntwortenLöschen
  4. Good statements:
    3.SQL injection problem can happen in any language, it is obvious task which a programmer should be able to handle.
    5. Embedded domain is a very special case, read my blog where i clearly stated that Java is general purpose language first. Embedded domain actually is an appropriate area where C++ can be beneficial , or maybe even C like for example automotive and aeromotive.
    6. I didn't worked with java encryption frameworks, so i can only believe what you saying.
    9. Sure the better you know the domain , the better you can write algorithms to solve a problem, but in any case as freelance java developer most time i working with following problems:
    a) understand business requirements
    b) writing unit tests to cover code
    c) a lot of meetings with QA and software architects and other business analysts.
    d) negotiating a contract (in Germany such thing take a lot of time, too much regulations not cool)
    In the last 2 years i can't recall that somebody from senior level developers group actually was thinking and talking about memory optimization, but sure in Java it can be a problem especially if you use collections and generics in a wrong way.
    Personally i would spent more time for learning cpu registers and memory optimization, but only if it would be in the interest of my customers and with appropriate funding for the research.
    By the way the criteria of a senior developer are not my, probably your translator got it wrong, it was a quote by one of my friend who is a senior developer in one of the fortune 400 company based here in Germany.
    I think i am tangled and burned out with C++ , because i worked in legacy projects too much , i can recall days and nights where i look at code and was afraid to touch it, especially when the code wasn't covered by the unit tests , there is some huge unspoken respect to people who can program in c++, but for me currently java or c# languages where i more successful.

    AntwortenLöschen
  5. I have some questions about your examples:
    As meyercsprofi already mentioned in his comments you don't show the diamond problem in your first example, because father and mother don't share a common base class.
    Also, you have never created any classes, so your example program does nothing! It wouldn't be surprising if the compileroptimation will completely remove everything from your main () Function except the outputs.

    In your second example pointerToNumber, pointerToAPointer and pointerToAPointerToAPointer are exactly the because the initialisation is wrong.

    AntwortenLöschen
  6. Thanks for good comment, what about Son and Daughter , each of those classes inherit Father, Mother constructor and all they public methods. Haha gotcha, i made this example as simple as possible if Son get both constructors from Father and Mother at the same time this is in my opinion really, really bad.
    About pointer in C++ this code works, don't argue about this, just deal that c++ is too powerful and if you use pointers you should be aware of consequences.

    AntwortenLöschen
  7. For people who think that i don't understand the deadly diamond problem
    https://discussatconcepts.wordpress.com/2015/02/06/deadly-diamond-of-death/

    AntwortenLöschen

All comments are pre moderated be polite and respectful!

Blog-Archiv

Blog readers favorites