Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Initialize method used for and do I really need one?

I am a bit confused about what the Initialize method is typically used for in a 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 a convention that is normally used?

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

like image 456
Samantha J T Star Avatar asked Dec 13 '11 11:12

Samantha J T Star


People also ask

When do we need to initialize an object?

So in most cases an Initialize method shouldn't be required. In cases where initialization involves more than putting the object into a usable state (e.g., when heavy work needs to be performed or virtual members or external need to be called), then an Initialize method is a good idea. Well put.

Why would you want a separate Initialize method?

Sometimes you have something more complicated though: Sometimes you will want a separate Initialize method because you want to call it at a separate time from construction. Sometimes you want one because you are writing multiple constructors, and you want to share some of the implementation between them.

Should a constructor have an Initialize method?

A constructor should only initialize an object, not perform heavy work. A constructor should not directly or indirectly call virtual members or external code. So in most cases an Initialize method shouldn't be required.

Can we create another method to initialize a class?

We could create another initialize method ( Java lets us create several methods with identical names) to let the programmer do this. Here's how our class would look:"


2 Answers

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

You can put it all in the constructor. In this simple case, you should. Constructors are for initializing your object.

Sometimes you have something more complicated though:

  • Sometimes you will want a separate Initialize method because you want to call it at a separate time from construction.
  • Sometimes you want one because you are writing multiple constructors, and you want to share some of the implementation between them.
  • Sometimes your initialization is complicated, and you want to give the pieces of it good names so you know what your code is doing. So you break those pieces into separate methods.

None of these are the case for this code, so I'd just skip it and throw the code in the constructor.

Is there a convention that is normally used?

No. People do whatever is the easiest to read and understand, whatever requires the least extra code to be written, and whatever causes the least duplication of code.

However, if you're making the Initialize method public, and not calling it from the constructor, I highly recommend you call it Initialize. Design it to only be called once.

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

No. You never need to use this to access class members unless you have another local variable with the same name. Here's such a case:

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;
    }
}
like image 194
Merlyn Morgan-Graham Avatar answered Nov 09 '22 11:11

Merlyn Morgan-Graham


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

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

In your specific example it appears to have little value.

like image 44
Oded Avatar answered Nov 09 '22 11:11

Oded