📜 ⬆️ ⬇️

Clean C ++


Let's get acquainted.
I am Seryoga. (It's not me on the photo). I work at Intel. Together with colleagues I write GPA . I've been programming for about 20 years now. Well, if you count it from school. Recently, a lot of different thoughts have accumulated that I want to share with someone. Tell someone about what is good and what is bad. You can tell and emptiness (so even calmer, no one distracts and does not hesitate with His very Right opinion), but it is not very effective. Therefore, I will merge my thoughts here. Suddenly, someone will come in handy ...

As an introduction
Many believe that there are two related languages ​​- C and C ++. At the same time, C ++ is supposedly the same C, with only two pluses, i.e. OOP. This is a very common misconception. "In fact, everything is not so." C and C ++ are completely different languages ​​that have practically nothing in common. However, historically, C ++ is syntactically compatible with C, i.e. can compile programs written in C. Because of this feature, there is no clear distinction between languages ​​and there is a lot of code written in a terrible mixture of these languages. Moreover, the proportion of this mixture can vary even within the framework of one program for one author. We will call such creativity language "C with classes." Sometimes the use of "C with classes" can be a conscious choice, dictated by circumstances and a task to be solved. Unfortunately, most often this code simply demonstrates the huge difference between ambitions and the real abilities of its author. If you are still at the very beginning of your journey, it is better to take something more stringent, for example, pure C or Pascal. And then come here when it becomes crowded in these languages.

By the way about the circumstances and tasks. Who do you think is a “real programmer”? No, not a hacker. And not the one who knows about ALL programming languages. And not even the one who is able to write the most "cool" program. The programmer is the one who can solve the set algorithmic problem in the stated period with a given level of quality . Re-read this definition several times slowly, pondering every word. Try to understand such a simple thing that there is no task, time and quality of programming! It can be creativity, learning, experiment, research, anything else, but not programming. Because the result of programming is a program that serves people, which, from the moment of its creation, day after day, repeatedly solves its task for them. Even Wikipedia in the definition of the term programmer 3 times mentions the word “task”! Understand that no one needs an ideal program written over an infinitely long time. People will be more willing to use at least something that solves their problems here and now, well, in a pinch, tomorrow. Also, few people will use the foul-smelling hand-made article, slapped somehow, when there are already many more simple and pleasant solutions to their problems around. Virtually no one will use the "funny trinket" for a long time. A variety of animals running around the screen, heaped special effects animation windows and menus, the most correct operating systems, etc. - all this junk is doomed to create a wow effect in the younger generation, and then with honor go into oblivion. I can tell you about quality assurance. Explain something about timing. But the task will have to look for yourself.
')
I think it's time to talk for C ++. I hope you have already read the thick and clever books about this language, wrote and compiled your first program? If not yet - go read and write, talk, when you understand at least the syntax of the language. If you are still there, then you probably already know - this language supports OOP at the syntax level. However, you should understand for yourself one important thing, so as not to slip into the "C with classes." In C ++, everything is a class. I'll try on the fingers:
void foo();  
...  
foo();

C. . C++, :
class Foo  
{  
public:  
    static void foo();  
};  
...  
Foo::foo(); 

:
class foo  
{  
public:  
    foo();  
};  
...  
foo(); 

:
class Foo  
{  
public:  
    void operator()();  
};  
...  
Foo foo;  
foo(); 

, :
namespace Foo  
{  
    void foo();  
}  
...  
Foo::foo(); 

( , ) class. , class — , . , . , «» — .
class IFoo  
{  
public:  
    virtual void foo() = 0;  
}; 

«», - . , - . , . «» .
:
template <typename TypeA> class DoIt
{
private:
    TypeA m_it;
public:
    template<typename TypeB> DoIt(TypeB it) : m_it(it) {}
    operator TypeA() {return m_it;}
};
...
TypeC c;  
TypeD d = DoIt< TypeD >(c); 

? , , . « » .

: — , — . — . C++ . . . « »
#include <iostream>  
int main(void)  
{  
    std::cout << " !!!\n";  
    std::cout << " \n";  
    return 0;  
} 

C++:
#include <iostream>  
class App  
{
public:
    int Run()  
    {  
        std::cout << " !!!\n";  
        return 0;  
    }  
    ~App()  
    {  
        std::cout << " .\n";  
    }  
};

int main(void)  
{  
    try
    {
        App().Run();
    }
    catch(...)
    {
        throw;
    }
    return 0;
} 

main . . , :
#include <iostream>  
#include <stdexcept>  
class App  
{
public:
    int Run()  
    {  
        std::cout << " !!!\n";  
        throw std::runtime_error("-     .");  
        return 0;  
    }  
    ~App()  
    {  
        std::cout << " .\n";  
    }  
};

int main(void)  
{
    try
    {
        App().Run();
    }
    catch(...)
    {
        throw;
    }
    return 0;
} 

, - . , , . , . «» C++. , try… catch — . .

, .

Source: https://habr.com/ru/post/156863/


All Articles