I saw a lot of code with the following structure:
public void Blah()
{
int a = 0;
string b = "";
DateTime c = DateTime.MinValue;
bool d = false;
// ...More initializations with dummy values
// Overwrite the values in a, b, c, d, e.g. a = ReturnInt();
// Do calculations, reading the values from a, b, c, d, like DoCalculations(a);
}
In general I much prefer something like:
public void Blah()
{
int a = GetInt();
string b = GetString();
DateTime c = GetDateTime();
bool d = GetBool();
// Do calculations, reading the values from a, b, c, d, like DoCalculations(a);
}
Is that really necessary and will there any performance hits due to the extra initializations?
Variables should be initialized before use only if that use is a
readif you are going to overwrite it immediately after initialization, it is better to follow the 2nd style you show, mainly for readability purposes, I expect performance hit (from style #1) if any would be typically minimal.