I have the following .net-page. I hope it is ok that it is only an example, describing my problem(s) pretty well.
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="textBox1"></asp:TextBox>
<asp:TextBox runat="server" ID="textBox2"></asp:TextBox>
<asp:Calendar runat="server" ID="calendar1"></asp:Calendar>
</div>
</form>
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
textBox1.Text = "Hello";
textBox2.Text = "User";
DateTime yesterday = DateTime.Now.Date.AddDays(-1);
calendar1.SelectedDate = yesterday;
}
}
}
The Page_Load-routine
fills two textboxes with some text and sets the selected date of a calendar control to yesterday.
My questions are:
- Do I need to or is it recommended to outsource any code into separate routines? I could f.ex. outsource the "calendar part" of the code into a routine
SetCalendar
, but is it best practice? (It is clear that I would outsource the code if needed on several places in the page class, but this is not the case here.) - Do you think the routine conforms with the Single Responsibility Principle? (A routine should have one and only one responsibility) Is the principle of any importance in a module of this type (event)?
- I read that a routine should be as generic as possible. If I had to outsource the "calendar part", would it be good to hand over the date as a parameter, even if I never set the calendar to any other date than yesterday?