Friday, 13 April 2007

Visual C++: A bright future

Many of you out there that use Visual C++ on a day to day basis may have started to become dismayed by the way it seems to be lagging behind the likes of Visual Basic and Visual C#. Well people there is some good news. I was recently watching an interview with the Visual C++ team on channel 9, and it seems that they have seen the error of there ways and are working hard on pulling Visual C++ up to the standard of the rest of the Visual Studio suite.

Now for the bad news. Unfortunately not all of these cool new features will be ready in time for the next release of Visual Studio (Orcas), however we will be getting an updated version of MFC that will have all the common controls updated so they work correctly with themes etc, this in itself will be nice to have.

By the sounds of it the coolest features are going to take a while due to the fact that the Visual C++ compiler front end is still based around a 20 year old piece of software. It looks like in the the next version of Visual C++ they will have re-written a huge amount of code to give the compiler front end more flexibility to allow it to keep up with the likes of Visual C#.

Another good point I noticed was they are now starting to ease back a little from .NET as they have noticed that most of their customers have massive code bases, ranging in the millions of lines of code and expecting them to move over to .NET would be impossible. Instead they are going to introduce better interop libraries to allow for example .NET WPF front ends to better interact with native C++ back ends.

It'll be interesting to see what new tools we end up with. It's just a shame it's going to take so long.

Tuesday, 20 February 2007

WPF: It's going to change everything

Today I watched this Microsoft developers launch event web cast, and I was well and truly amazed by what I saw. The web cast at its core is basically about the new things that will be possible using such things as Vista and the new Visual Studio integration with office 2007. This was all the usual Microsoft evangelism, however towards the end of the presentation they bring on a group of developers from The London Underground, and at this point things start to really heat up.

They start off by showing some of the features of Excel 2007 that allow them to pull data straight from a web service and manipulate it in the usual way in which you can in Excel. While this is nothing amazing it does show the power of web services when they are combined with analytical tools.

The real fun starts when they bring out their latest WPF based app. This thing is amazing, for a start it's running completely within a web browser and the responsiveness of it is instant. Fair enough the data source may be on the same machine but this thing is super smooth and extremely heavy on the graphics, in fact there are even full 3D sections.

It's just the way that they managed to combine so many different web service data streams and collate them together to give virtually every piece of information you could want to know about the underground at any given time. Not only this, but the use of WPF has allowed them to make a highly flexible interface that allows the data to be viewed in as many ways as you would want.

It's this kind of presentation that gets me excited about development again. The possibilities are mind blowing. I predict that in the next 6 months or so the web is going to become a very different place.

If you want to skip straight to the demo then just click on the fourth slide from the end of the presentation and you'll be in about the right place.

Monday, 19 February 2007

Increase Adsense Revenue Legitimately

AdSense is one of the most popular CPC advertising networks on the planet. It is context based meaning you do not choose the genre of adverts displayed... Google does!

When you have a Google advert on your page Google tries to best match its list of advertisements to what it thinks your page content is about. It uses an eBay style auction system internally to decide what ads to place on your site. It tries to get the best value for publishers and advertisers. It is a complex system that really is the subject for another article.

As we all know certain markets, keywords etc are more competitive than others. For example loans and finance adverts normally reap MUCH higher payments per click than say football adverts. Simply because there are more people trying to advertise loans and finance options to you. Note there are other reasons, but again are out of the scope of this article.

So you may find that your site may get thousands of visitors, but you end up only getting a few cents / pence per click. This may be because your site is targeting the wrong keywords.

Google AdWords Keyword Tool
You can use the Google AdWords Keyword Tool to not only find other related keywords, but to measure the estimated cost to the advertiser for that keyword. This will allow you to, by simply tweaking a few phrases in your content, title and meta tags, to significantly increase your CPC.

Made for AdSense Websites
Another reason for getting low CPC is made for AdSense websites, certain websites target a broad range of keywords with a very low CPC in order to get you on their site. They then have much higher paying adverts on their site. So they pay you 4 cents to get a visitor on their site, then they get paid $1 for them to leave. Good deal for them, bad deal for you!

There is another great little site out there called adsblacklist that basically generates a list of well known made for AdSense websites which you can place in your competitive sites filter option within AdSense making sure they do not get displayed.

Friday, 16 February 2007

Help fight SPAM using SPF records

I've been using a certain domain now for years for my email and I recently decided to move it to googles new apps for domains. Basically if you haven't already seen it, google are now offering a service where you can get customised gmail accounts and calenders along with a few other bits and bobs for your own domains.

To use the service you need to have a domain provider that allows you to control the DNS settings of your domain. For this I choose to move my domains over to 123-reg.co.uk.

After moving my email over to google I noticed that my catchall address was starting to get a lot of spam. After a further investigation it turned out that someone was sending spam messages and were using an email address on my domain as the reply address.

Obviously I don't want my domain getting onto any spam black lists, so I decided to see if there was anything I could do about it. The initial information I found was not good news, because of the way current email protocols work, it is perfectly valid for someone to put anything they like as the reply address in an email.

Slightly dismayed I carried on looking around to see if there was a way of at least cutting down some of the spam, and that's when I came across Sender Policy Framework (SPF). SPF is an open standard that attempts to stop the type of spam I was receiving. The way it works is you create a SPF record and put it in a TXT record in the DNS settings of your domain. This information dictates where email from your domain is allowed to come from.

Basically SPF can be used by email servers to check to see if the email it just got was sent from a valid source. If it was then all is fine, where as if it isn't the message is dumped.

The SPF protocol is very flexible and offers a huge range of options when it comes to filtering the mail. After setting up my SPF record my spam has gone from around 400 messages a day to around 20, and is still falling.

The only disadvantage I can see with SPF at the moment is the fact that not all email servers on the net use it. Also it only stops messages that make it look like your domain is spamming people. This in itself is a good thing though as it's likely to keep you off spam black lists.

If you are using google apps for domains already then there is a section in their help that tells you what you need to set your SPF record to. Otherwise check out the main SPF web site and see if you too can help keep spam at bay.

Monday, 12 February 2007

Top 5 Optimisation Techniques

This post is aimed at Engineers who, along the way, forgot how to effectively optimise. I am not claiming to be the oracle of optimisation but after working with several different platforms, including CE [argh], I have picked up a number of quick and easy tips to help speed up your program. Most of the optimisation techniques I talk about aren't language specific so you can apply them to any project your are working on.

So anyway here is my top 5 methods to optimise code in any language:

1. Remove unnecessary code.
If the code isn't needed, then lose it. Use only the code you need to use to get the job done. If you have re-written a function or class then make sure you remove all the old code fully as it will just slow your program down.

2. Move code out of loops
Another big drain on CPU is having code within tight loops. By tight I mean loops with many iterations. This can include everything from variable declarations to pre-processing values outside the loop. Every little can help especially when speed is essential. Remember to also look at function calls within tight loops. The function call itself can have a slight overhead, but the main importance is to make sure the function itself isn't bloated. For more information regarding tight loop optimisation check out our post on Duff's Device

3. Pass objects by reference rather than value
Passing around objects can be very costly as they are copied every time another function or class uses them. To get around this pass objects by reference or pointer where possible and the memory copied will be reduced to the size of your pointer [4-8bytes].

4. Pre Allocate memory
Memory allocation, and manipulation, is one of the slowest operations you can do as a programmer. To avoid unnecessary hold ups try allocate memory before you need it. It is also advisable to block allocate memory rather than allocate little bits at a time as it is more efficient in both allocation and subsequent referencing.

An example of this would be to use an array rather than a list [standard library]. A list allocates memory as and when it needs it so the memory allocated for a particular object in the list is not guaranteed to be adjacent to the previous object. So pre allocate your array [maybe at start up] so you have no slowdowns later on.

5. Minimise and optimise disk access
Another hog on you application is disk access. Hard disks, or flash drives are a LOT slower than manipulating data in memory so care should be taken when writing out files. You will find that writing a bytes at a time is slower than just flushing through a lot of data. This depends on the disk medium you are writing to but generally writing out in bigger chunks [e.h 64k] is much more efficient. It is best to experiment on your particular platform when writing out data to avoid other bottle necks [write buffers for one can slow you down].

So there you go, 5 little tips that you already knew but probably forgot. Bear these in mind next time you are wondering why your next masterpiece is running a little sluggish.

Thursday, 8 February 2007

How to use the const keyword to make your C++ better

As the title suggests, I firmly believe that the correct usage of const can make many aspects of your C++ program, and indeed programming style, "better". By "better" I mean smaller executables, more robust, faster, safer, sexy, OK so I lied about it being sexy but the rest is true.

Const is a keyword that many C++ programmers don't use, they either forget to use it, don't know how, or they think its "easier" to program without it. Const is there for a reason and using it will bring many advantages to your programs, from design through to development and debugging.

It is quite hard to define what const is. This is because it is a contextual keyword whose meaning changes depending on where it is used in your code. In general terms it means that the object it refers to cannot be modified after its original creation.

So why would anyone want to use const at all? The simplest case of const is where you have a method that returns a pointer to a const object. In this use it will ensure that no one [or class] can modify the object that was returned. This re-enforces good design and can also have positive effects on your code.

OK so how do you use const? As I mentioned earlier it can be a little tricky to understand how const works in all situations, especially if your new to the keyword. Here are a few examples of how and when to use const, and what the desired effect is:

//#define MAX_SIZE 100
const int MAX_SIZE = 100;


Here I use a const int to replace a #define. You should generally try to use const objects rather than #defines for one very good reason. #define does not use type checking, const [object] does, which immediately improves your ability to debug code as the compiler will flag up any type issues at compile time.

MyObject *MyClass::GetMyObject() const
{
return &m_MyObject;
}

One of the main uses of const is its use within function declarations. The following code snippet shows how you can append const to a function declaration to show that the function does not change anything [outside of its own scope]. The benefit of this is that the compiler can create leaner, more efficient, code for that function as well as those calling the function since it knows it will never modify any member variables or objects outside of its own scope. Member functions that are declared const also have the added feature of being the only functions that can be called from a const object. This makes sure that a const object can not modify its contents.

void AnotherClass::Foo(const MyClass &obj)
{
obj.GetMyObject();
}


Above shows another method of using the const keyword, this time as a function argument. Because obj is a const reference to a MyClass object it means that only const member functions of MyClass can be called. If obj had been a pointer then the same would apply.


void AnotherClass::Foo(const MyClass *obj)
{
obj->GetMyObject();
}

If you want to use pointers and you want the pointer itself to be const I.E you can not modify what the pointer points to then you would need to move the const as below.

void AnotherClass::Foo(MyClass* const obj)
{
obj->SetSomething(10);
}


This means that you can call non const member functions of obj but you can't change what it points to. If you want it so you can only call const member funtions and you can't change what the pointer points to then you would need the following

void AnotherClass::Foo(const MyClass* const obj)
{
obj->GetMyObject();
}


This is essentially the same as a const reference and if this is what you require a const reference should be used instead of a pointer. (To find out more about references see Know thy language c++ references).

All of the above also applies to return values from functions. Returning a const object means that it can only be allocated to another const object when it is initialised.....

const MyClass &test = someobject.GiveMeConstObject();

or a copy of it can be made if it is allocated to a variable on the stack

MyClass test = someobject.GiveMeConstObject();


Well that's about it for the use of the const keyword. Remember that const doesn't just tell the compiler what is happening in your code it also tells other people using your code what happens. In the real world the use of const is a must, it provides the compiler with a way of flagging to the user when something is wrong with the code and it allows the developer to protect data.