Saturday, December 12, 2009

Weekly Source Code: C# DateTime Developer Pitfall

Base upon my previous blog post that every language has some pitfalls I had a production issue and saw one of the pitfalls that C# have with the DateTime class which is part of the .Net framework.

I renamed this title since its more a pitfall of the developer not understanding the proper use of the DateTime class. The all point is that your telling the constructor to create a date, you don't tell it to work with increments.

In the next code you want to get the 1ste date of the following month:

DateTime currentDate = new DateTime(2009, 11, 05);
DateTime nextDate = new DateTime(currentDate.Year, currentDate.AddMonths(1).Month, 1);
Console.WriteLine("nextDate: " + nextDate.ToString("dd/MM/yyyy"));

So the answer is 01/12/2009 which is correct but what if you do the following:

DateTime currentDate = new DateTime(2009, 12, 05);
DateTime nextDate = new DateTime(currentDate.Year, currentDate.AddMonths(1).Month, 1);
Console.WriteLine("nextDate: " + nextDate.ToString("dd/MM/yyyy"));

Code is exactly the same, I just change the month to December since you want to get the day for the 1ste of Jan 2010 but the end result is: 01/01/2009. Ouch!!

Yes the DateTime object doesn't increment the year (2009 -> 2010).

To fix:

DateTime currentDate = new DateTime(2009, 12, 05);
DateTime nextDate = new DateTime(currentDate.Year, currentDate.Month, 1);
nextDate = nextDate.AddMonths(1);
Console.WriteLine("nextDate: " + nextDate.ToString("dd/MM/yyyy"));

You see that you can't increment in a DateTime.

Friday, December 4, 2009

For The Love Of C++

Alot of programmers that use more modern programming langauges (C#, VB.Net, Java, Python, Delphi etc) dish C++ by saying what bad language it is, how its the worse thing ever created etc.. But they don't know it (have you ever code any C++? have you ever even see any C++ code?) Don't dish something that you don't know...

Languages like C#/Java is very abstract, they hide things from a programmer like memory management that get managed by a garbage collector etc Where in C++ memory management get done by yourself via pointers (yes pointers are very difficult!) C++ have pitfalls, very bad once aswell yes but they can be privented by putting rules in place, use 3rd party libraries like Boost that support smart pointers for memory management (for this artical am not going to explain what smart pointers are) have code reviews etc. A programmer can write crap in any language, in C++ its just more easy for this you need serious programmers that take there job seriously that are willing to learn the C++ pitfalls and how to deal with them, are willing to read books like Effective C++.

C++ is powerful it teach you how to come close to understanding how the machine works, you learn new things / new ways of doing things all the time... its fun! Think abit beyond... The OS that you use (MS Window/Linux), your mouse driver, your Webcam, your iPod, your iPhone, your IM program like Skype etc are all written in C++ or some dialect of it (like Objective C)... the C# / VB.Net etc geeks can also stop moaning since do you guys know? the compiler for your language is written in C / C++.

So why does C++ have so many pitfalls?

To start, every language has some form of pitfalls... am studying for my Java Programmer Sun Certification and let me tell you, even Java got alot of pitfalls. C# aswell! So don't think C++ is the only language that you can write crap in, you can write crap in any language and any language can make you scream from frustration when you struggle with some weird issue that is actually a language pitfall.

C++ have alot of pitfalls, full books are written for them like Effective C++ (btw you also get Effective Java) get see the reason why we need to go back into history... (time Machine please!)

C++ was called "C for Classes" where Bjarne Stroustrup want to add object oriented programming (OOP) features to the powerful C programming language from his experience coding in Smalltalk, Simula etc. (interesting artical here) it was criptic way of adding classes but it worked and soon the name was given "C++" along with more languages features added over time... like meta programming, generics in the form of templates, run-time libaries like STL etc To keep C++ standard a standard committee formed, standards are there for a reason since it protect us my dear fellow programmers from one compiler vendor adding some language feature that isn't part of a standard meaning that when you ever wanted to move your code over to that compiler you will have issues - might not be at compile time but at run-time damaging your customer.

When adding a new langauge feature you add more complexity ontop of more complexity and you must keep backward compatiability! Why backward compatiability? Example: Python 3x isn't compatiable with Python 2x so how many developers ported there code over? Answer: Almost none, reason being that a programmer just don't have the time or resources to do a port, you can't tell the your boss/management to wait for a year for you to rewrite all the software (that can be millions lines of code) since business must go on... new products release, new customers, new features in the application etc. In the ISP world its a good example. So for C++ every new feature needs to be compatiable in different context together with other language features... its a difficult process, committee's fight about things for years... the new C++ 0x standard has been going on for over a decade now. So "0x" since you don't know when it will be release, it can be in 2020! that is why C++ has some many pitfalls and is so complex because of 30 years of history... languages like C# will go the same route or are already going the same route. The C++ standard committee just can't do what the Python guys did by "cleaning up C++" since millions of lines of code all over the world will break.

So to end this story... this the truth ladies + gentelman, don't dish something that you don't know the history about or ever even used before.