What is the Initialize method used for and is it really needed?

I'm a little confused by the fact that the Initialize method is usually used in the constructor.

Why can't I just put everything in the constructor and why does the sample below call the initialize method?

  private IAzureTable<Product> _productRepository; public ProductService(string dataSourceID) { Initialize(dataSourceID); } private void Initialize(string dataSourceID) { this._productRepository = StorageHelper.GetTable<Product>(dataSourceID); } 

Is there an agreement that is commonly used?

In this example, do I need the word this in the Initialize method?

+6
source share
5 answers

Why can't I just put everything in the constructor and why does the sample below call the initialize method?

You can put all this into the constructor. In this simple case you should. Constructors are designed to initialize your object.

Sometimes you have something more complicated:

  • Sometimes you will need a separate Initialize method, because you want to call it at a separate time from construction.
  • Sometimes you need one because you write several constructors and you want to share some implementation with them.
  • Sometimes your initialization is complicated, and you want to give bits of its good names so that you know what your code does. Thus, you break these parts into separate methods.

None of them apply to this code, so I just skipped it and threw the code into the constructor.

Is there an agreement that is commonly used?

Not. People do what is easiest to read and understand, regardless of what is required to write the least additional code, and which causes the least duplication of code.

However, if you make the Initialize method public and do not call it from the constructor, I highly recommend that you call it Initialize . Create it only once.

In this example, I need the word "this." in the Initialize method?

Not. You do not need to use this to access class members unless you have another local variable with the same name. Here is a case like this:

 public class Something { private string someValue; public class Something(string someValue) { // must use "this" to access the member variable, // because a local variable has the same name this.someValue = someValue; } } 
+9
source

What you posted is a private helper method, not a class.

It is simply used so that the constructor is not cluttered with initialization code and to give a good name for what is done.

In your specific example, this seems to have little meaning.

+4
source

First of all, the code looks readable if you highlight functionality in your own methods.

Secondly, sometimes in certain situations you would like to publish the initialization so that the developer can use it after calling the constructor, because you do not want the code to work in the constructor. This usually happens if you have a form and you want some initialization code to be executed before the form is displayed, etc.

+1
source

1: Using the initialization method can be doubled:

  • There can be many initialization actions that need to be broken down into manageable chunks (i.e., the constructor may continue to perform other actions).
  • It may be necessary for the class to reinitialize itself, in which case the initialization code must be in a method other than the constructor.

In your specific example, although it makes little sense.

2: In the case of this :

Again in your example this makes little sense. It can use indifference between implementations of properties or methods in the base class and the current class (i.e. you would use base. Instead of this. )

+1
source

There is no agreed standard, but “Initialization” is a well-known word that is instantly recognized by everyone who reads the code and tells me everything I need to know about the purpose of the method. It could be called Bob() , but it is much less useful!

Why can't I just put everything in the constructor?

You can, but breaking it down into a method helps make things more readable. When your constructor is a single line of code, it really is not needed.

In this example, I need the word "this." in the Initialize method?

In the code you sent, no, this is not necessary. Why is he there? Maybe it’s autogenerated by a tool, maybe just a convention or style that someone used, could be inserted from somewhere else where it was needed ...

+1
source

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


All Articles