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.


Wednesday, November 11, 2009

About SCJP Sun Certified Programmer For Java Exam

Questions from colleagues and readers of my blog, here is more information about the SCJP exam.

The SCJP exam is a theoretical exam with very tricky questions, it test your knowledge of the Java programming language which is the foundation of all future certifications like Java Developer, J2ME etc.

To study for the exam I suggest buying the SCJP Sun Certified Programmer For Java 6 Study Guide (R409.00) book and spend about a year studying, do mock exams etc until your sure you can pass the exam - its very difficult!

For help you can visit JavaRanch
Which is a website started by the authors of the SCJP book.

Phone Prometric Testing Centre on 0800 99 11 20 two weeks in advance, before writing the exams to book. Pricing can be checked with them.


Friday, November 6, 2009

Increase Visual Studio.Net 2008 Memory

For work I use Visual Studio.Net 2008 along with a lot of plugins like SQLPrompt etc also Team Foundation Server (TFS). Lately I've been getting out of memory exceptions when I try to checkin my code, after some searching I found that Visual Studio.Net only use max 2GB of RAM memory where my laptop has over 4GB of memory.

I found an article that explain how to hack Visual Studio.Net to increase its memory usage.

After trying it out it seams that its working! Hope someone else will find it helpful.

Monday, October 26, 2009

How Visual Studio.Net Upgrade Your Solution

Weekly source code for this week will be published a bit later

When opening an VS.Net 2005 solution in VS.Net 2008 to which version will the upgrade wizard try to upgrade your solution too? Answer: .Net framework 3.5

The upgrade wizard will always try to upgrade to the current .Net framework for that version of VS.Net for example:
  • VS.Net 2005: .Net Framework 2.0
  • VS.Net 2008: .Net Framework 3.5
  • VS.Net 2010: .Net Framework 4.0
You will be asked the question if you wish to upgrade but you can always say "No". If you say no then the solution will stay in the old version using the multi-target feature. if you wish to later upgrade (like making use of the new LINQ features in .Net framework 3.5) you can do so under the properties of the project.

Thursday, October 22, 2009

Follow-Up: Make Web Service Configurable

Base upon the comments of a reader on my last weekly source code posting is that there is an additional 2x ways to set the URL location of a Web Service:

string newUrl = "http://server/path/to.asmx";
MyService service = new MyService();
service.Url = newUrl;

Or in new C# 3.0 you can use object initializers:

string newUrl = "http://server/path/to.asmx";
MyService service = new MyService {Url = newUrl};

I still prefer using a constructor.

Wednesday, October 21, 2009

Tuesday, October 20, 2009

Weekly Source Code: Make Web Service Configurable

Sorry for the delay, been crazy at work.

In Visual Studio.Net you call web services like the old style ASP.Net web service (not new WCF) by adding a reference to the project using Add Web Reference option. In the pop up dialog you point it to the web service URL, Visual Studio.Net will then generate the required C#/VB.Net stub classes.

But its not to say that the web service will be at the same URL location expecially if you move between development - > QA -> production environments. How do you make the web service URL configurable?

The web service stub class Reference.cs (for C#) or Reference.vb (for VB.Net) has a URL property, you can always set this property in your code but its a nice way to rather do it when you call the constructor e.g.:

MyService service = new MyService(webserviceUrl);

You do this by adding the following constructor to the MyService class:

MyService(String theUrl)
{
Url = theUrl;
}

The downside (before Visual Studio.Net 2008) is that if you update the web reference (after adding an additional web method to the web service etc) the code get regenerated meaning your code will break and you will have to add the constructor again.

Fortunately Visual Studio.Net 2008 introduce partial classes which allow splitting definition of a class across multiple files.

The Reference.cs / Reference.vb class also is a partial class so you can add an additional partial class with the same name and same namespace:

public partial class MyService
{
MyService(String theUrl)
{
Url = theUrl;
}
}

Now you can update the web service reference without worrying about such problems.

So what is partial classes? Yes they allow you to split the definition of a class across multiple source files but why? In this sample it show that its for code generation mainly since you normally generate code then add your own additions (like a constructor in this case) but if you re-generate your custom code is lost. Partial classes (and later on I will introduce partial methods) resolve this problem. Why did MS add it? Look at LINQ to SQL which also generate code, now you can add custom logic like validation with partial methods without ever worrying that re-generation will break your logic.