principlecsharpMinor
What is the advantage/disadvantage of using default initializer for private fields?
Viewed 0 times
thewhatfieldsdisadvantageprivatedefaultusingforadvantageinitializer
Problem
Let's compare the following code:
1)
2)
What do you prefer and what are the advantages/disadvantages of these solutions? To me I prefer the first one because its obvious seeing constructor what is getting intialized, however the second can be covering multiple constructors better.
Edit:
I am pasting actual real code to make the question valid, adding some description: The class handles execution of powershell, there are some dependencies of class injected to constructor, and some fields initialized locally (syncRoot is used for thread-safety, logger is log4net for logging).
```
///
/// VCenter handler which creates the tasks and execute them
/// requirement: PowerCLI must be installed
///
public class VCenterPowerCLI : IDisposable
{
/// log4net logger
private readonly ILog logger = LogManager.GetLogger(typeof(VCenterPowerCLI));
/// factory of powershell runtimes
private readonly IPowershellRuntimeFactory powershellRuntimeFactory;
/// vcenter handler configuration
private readonly VCenterConfiguration configuration;
/// Thread-safe sync
private object syncRoot = new object();
///
/// Initializes a new instance of the VCenterPowerCLI class.
///
/// vcenter handler configuration
/// factory of powershell runtimes
public VCenterPowerCLI(
VCenterConfiguration configuration,
IPowershellRuntimeFactory powershellRuntimeFactory)
{
this.configuration = configuration;
this.powershellRuntimeFactory = powershellRuntimeFactory;
}
///
/// Immediate task - takes snapshot under the machine
///
/// machine id
/// snapshot name
public void TakeSnapshot(string machineId, string snapshotNam
1)
public class MyClass
{
private object syncRoot;
public MyClass()
{
this.syncRoot = new object();
}
...
}2)
public class MyClass
{
private object syncRoot = new object();
public MyClass()
{
}
...
}What do you prefer and what are the advantages/disadvantages of these solutions? To me I prefer the first one because its obvious seeing constructor what is getting intialized, however the second can be covering multiple constructors better.
Edit:
I am pasting actual real code to make the question valid, adding some description: The class handles execution of powershell, there are some dependencies of class injected to constructor, and some fields initialized locally (syncRoot is used for thread-safety, logger is log4net for logging).
```
///
/// VCenter handler which creates the tasks and execute them
/// requirement: PowerCLI must be installed
///
public class VCenterPowerCLI : IDisposable
{
/// log4net logger
private readonly ILog logger = LogManager.GetLogger(typeof(VCenterPowerCLI));
/// factory of powershell runtimes
private readonly IPowershellRuntimeFactory powershellRuntimeFactory;
/// vcenter handler configuration
private readonly VCenterConfiguration configuration;
/// Thread-safe sync
private object syncRoot = new object();
///
/// Initializes a new instance of the VCenterPowerCLI class.
///
/// vcenter handler configuration
/// factory of powershell runtimes
public VCenterPowerCLI(
VCenterConfiguration configuration,
IPowershellRuntimeFactory powershellRuntimeFactory)
{
this.configuration = configuration;
this.powershellRuntimeFactory = powershellRuntimeFactory;
}
///
/// Immediate task - takes snapshot under the machine
///
/// machine id
/// snapshot name
public void TakeSnapshot(string machineId, string snapshotNam
Solution
There are no differences between your two options. There are a few remarks we can make about though:
Consistency
Keep everything consistent. There are a few reasons to change it up (for example: you can do inline initialization all the time and only initialize those in the constructor that take a constructor argument), but that's because these two situations have a significant difference.
If there is no difference (for example: initializing a field to its default value) then you have to be consistent.
Multiple constructors
If you initialize your fields in your constructor and you add a second one, you might forget to chain them.
All things considered, I believe inline initializing to be better. There is no functional difference but it doesn't have the possibility of the constructor chaining overlook.
Aside from that, it also keeps the declaration and the initialization together which improves readability.
Comments
When I look at a variable called
Comments like this are a lot more important:
It is hard to deduct that PowerCLI must be installed if you omit it, so that's actually adding valuable information.
Consistency
Keep everything consistent. There are a few reasons to change it up (for example: you can do inline initialization all the time and only initialize those in the constructor that take a constructor argument), but that's because these two situations have a significant difference.
If there is no difference (for example: initializing a field to its default value) then you have to be consistent.
Multiple constructors
If you initialize your fields in your constructor and you add a second one, you might forget to chain them.
All things considered, I believe inline initializing to be better. There is no functional difference but it doesn't have the possibility of the constructor chaining overlook.
Aside from that, it also keeps the declaration and the initialization together which improves readability.
Comments
When I look at a variable called
IPowershellRuntimeFactory, a comment that says factory of powershell runtimes doesn't add any value.Comments like this are a lot more important:
/// VCenter handler which creates the tasks and execute them
/// requirement: PowerCLI must be installedIt is hard to deduct that PowerCLI must be installed if you omit it, so that's actually adding valuable information.
Code Snippets
/// VCenter handler which creates the tasks and execute them
/// requirement: PowerCLI must be installedContext
StackExchange Code Review Q#46918, answer score: 8
Revisions (0)
No revisions yet.