Coding style in .NET: whether to refactor into new method or not?

Posted by Dione on Stack Overflow See other posts from Stack Overflow or by Dione
Published on 2010-05-31T07:28:19Z Indexed on 2010/05/31 7:32 UTC
Read the original article Hit count: 168

Filed under:
|
|
|

Hi

As you aware, in .NET code-behind style, we already use a lot of function to accommodate those _Click function, _SelectedIndexChanged function etc etc. In our team there are some developer that make a function in the middle of .NET function, for example:

public void Button_Click(object sender, EventArgs e)
{
    some logic here..
    some logic there..

    DoSomething();
    DoSomethingThere();

    another logic here..

    DoOtherSomething();
}

private void DoSomething()
{
}

private void DoSomethingThere()
{
}

private void DoOtherSomething()
{
}

public void DropDown_SelectedIndexChanged()
{
}

public void OtherButton_Click()
{
}

and the function listed above is only used once in that function and not used anywhere else in the page, or called from other part of the solution.

They said it make the code more tidier by grouping them and extract them into additional sub-function. I can understand if the sub-function is use over and over again in the code, but if it is only use once, then I think it is not really a good idea to extract them into sub-function, as the code getting bigger and bigger, when you look into the page and trying to understand the logic or to debug by skimming through line by line, it will make you confused by jumping from main function to the sub-function then to main function and to sub-function again.

I know this kind of grouping by method is better when you writing old ASP or Cold fusion style, but I am not sure if this kind of style is better for .NET or not.

Question is: which is better when you developing .NET, is grouping similar logic into a sub-method better (although they only use once), or just put them together inside main function and add //explanation here on the start of the logic is better?

Hope my question is clear enough.

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET