I know some languages (C comes to mind) its best to declare as few variables as possible. In a language with memory management like C# is it better practice to continue with this philosophy or should I be declaring unnecessary variables if it improvese readability?
Ex: Declaring unnecessary database and server variables
public static DataTable CreateMonthlyPerformanceSummaryTable(int perfSumId, DateTime date, bool startDate)
{
DataTable monthlyPerformanceTable = new DataTable();
string dataBase = PerformanceDBDataObject.DBName;
string server = PerformanceDBDataObject.Server;
using (SqlConnection conn = SQL.ConnectToDB(dataBase, server, 30))
{
if (startDate == true)
{
SelectPerformanceSummaryUsingStartDate(date, monthlyPerformanceTable, conn);
}
else
{
SelectPerformanceSummaryUsingEndDate(date, monthlyPerformanceTable, conn);
}
}
return monthlyPerformanceTable;
}
Or: Simply plugging in Values into ConnectToDB
public static DataTable CreateMonthlyPerformanceSummaryTable(int perfSumId, DateTime date, bool startDate)
{
DataTable monthlyPerformanceTable = new DataTable();
using (SqlConnection conn = SQL.ConnectToDB(PerformanceDBDataObject.DBName, PerformanceDBDataObject.Server, 30))
{
if (startDate == true)
{
SelectPerformanceSummaryUsingStartDate(date, monthlyPerformanceTable, conn);
}
else
{
SelectPerformanceSummaryUsingEndDate(date, monthlyPerformanceTable, conn);
}
}
return monthlyPerformanceTable;
}
Personally I think the first option is better because the shorter line length allows for improved readability, but I am wondering if this is bad practice.
if (startDate)
instead ofif (startDate == true)
:)startDate
is a terrible name for a Boolean variable. If I randomly sawstartDate
somewhere in a block of code and did not see the type declaration, Boolean would definitely not be the type I'd infer.