I'm not a professional programmer, and everything I know about programming I've taught myself / read somewhere on the internet or in a book.
Rightly or wrongly, I'm currently of the belief that I should always try to make my code as robust as possible, even if this means adding in redundant code / checks that I know won't be of any use right now, but they might be x amount of years down the line.
For example, I'm currently working on a mobile application that has this piece of code:
public static CalendarRow AssignAppointmentToRow(Appointment app, List<CalendarRow> rows)
{
//1. Is rows equal to null? - This will be the case if this is the first appointment.
if (rows == null) {
rows = new List<CalendarRow> ();
}
//2. Is rows empty? - This will be the case if this is the first appointment / some other unknown reason.
if(rows.Count == 0)
{
rows.Add (new CalendarRow (0));
rows [0].Appointments.Add (app);
}
//blah...
}
Looking specifically at section two, I know that if section one is true, then section two will also be true. I can't think of any reason as to why section one would be false and section two evaluate true, which makes the second if statement redundant.
However, there may be a case in the future where this second if statement is actually needed, and for a known reason.
Some people may look at this initially and think I'm programming with the future in-mind, which is obviously a good thing. But I know of a few instances where this kind of code has "hidden" bugs from me. Meaning it's taken me even longer to figure out why function xyz is doing abc when it should actually be doing def.
On the other hand, there's also been numerous instances where this kind of code has made it much, much easier to enhance the code with new behaviours, because I don't have to go back and ensure that all the relevant checks are in place.
Are there any general rule-of-thumb guidelines for this kind of code?
(I'd also be interested to hear if this would be considered good or bad practise?)
N.B: This could be considered similar to this question, however unlike that question, I'd like an answer assuming there are no deadlines.
TLDR: Should I go so far as to adding redundant code in order to make it potentially more robust in the future?
if(rows.Count == 0)will never happen, then you could raise an exception when it happens - and check then why your assumption became wrong. – knut 10 hours ago