0

My IDE constantly says me to do this. I vaguely remember I've read in some C++ paper that has something to do with exceptions in constructors.

Java example:

    void foo(){
        String result = "";
        int i = 5; // should this ever be a problem ?
    }

For both lines IDE suggests to separate.

Additional questions

1. is there difference if I create by value or by reference.
E.g. in C++ class A{}; ... A a = ...; vs A* a = new...;

2. my guess it's never a problem with basic types such as int or float. Are POD's the same ?

Alexander Malakhov
  • 3,383
  • 2
  • 33
  • 58
  • You have tagged your question language-agnostic, but it is indeed very language-specific. Please make up your mind. – Björn Pollex Dec 14 '11 at 08:31
  • @Björn: that's because I'm not sure what the problem is. I thought it's similar in Java and C# (and probably other managed langs), and in C++, D, Delphi (and probably other, where manual management is possible). So decided to restrict it to langs with constructors. Do you think I should retag it with those 5 tags ? – Alexander Malakhov Dec 14 '11 at 08:51
  • I think as it stands, the scope of the question is too big. The answer would require a detailed explanation for each of the languages, as they are all very different in how they handle exceptions and object-construction/-initialization. – Björn Pollex Dec 14 '11 at 08:55
  • @Björn: Anyway, I want to know this at least about Delphi, Java and C++. Even if it's gonna be several answers, it's ok. So, I will keep things as they are for some time and see where it goes. – Alexander Malakhov Dec 14 '11 at 09:20

1 Answers1

0

This answer deals with C++, behaviors in other languages might be different

You actually should initialize your variables when you declare them.

For POD types, this is just to so you don't forget to initialize them and thus use uninitialized data.

For user types, this is done for optimization:

A other;

//this will first construct a, and then assign a new value to a
//the initial construction is useless and takes up processing time
A a;
a = other;

//b is only initialized once
A b = other;

1) That's not creating a variable by value or reference. It's creating it on the stack or on the heap. There are differences which are explained in a lot of posts here on SO.

2) It's not a problem, it's more of a habbit for POD's. The main reason why initialization is done in the same line as the declaration was explained in the first lines of the answer.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • If you want to know how this relates to exception-safety, read about [RAII](http://stackoverflow.com/questions/395123/raii-and-smart-pointers-in-c). – Björn Pollex Dec 14 '11 at 08:36
  • IIUC, your `"2) It's not a problem, it's more of a habbit for POD's"` is not correct. C++ will call (auto-generated) default c'tor anyway, i.e. it has nothing to do with habits. (I'm reviewing my non-accepted Q's, please don't take it as nit-picking) – Alexander Malakhov Apr 10 '20 at 22:16