Dec 31 2008

Refactor for C++ – addendum

Category: Programming, toolsMaciek Talaska @ 6:01 pm

A few days ago I have written a short note in which I have enclosed some of my thoughts about freely available Visual Studio 2005/2008 plugin for C++ developers (DevExpress Refactor for C++). I have pointed out that Refactor had some problems with simple refactorings in one of my projects. The simplified code snippet I have provided is not good enough to help understand the problem (the snippet seems to be too simple, and it is impossible to encounter the issue I was writing about). I have spent some of my spare time to find the code that caused problems and try to analyze it and provide a code snippet that illustrates the issue. The following code is very close to the one that I have checked to be error-prone:

// forward declaration    					//(1)
void functionEx( );    						//(2)
typedef void( *void_function_pointer)(void);			//(3)

class SimpleClass
{
private:
	void (*function_pointer)( );				//(4)
	void_function_pointer func_ptr;

	void SetCallBack( void (*function_pointer)(void) )
	{
		this->function_pointer = function_pointer;	//(5)
	}

public:
	void function( )
	{
		printf("\n");
	}

	void Prepare( )
	{
		SetCallBack( functionEx );
	}

	void Invoke( )
	{
		function_pointer();
	}

};

SimpleClass *externalObject;

void functionEx( )
{
	externalObject->function();				//(6)
}

int _tmain(int argc, _TCHAR* argv[])
{
	externalObject = new SimpleClass();
	externalObject->Prepare();				//(7)
	externalObject->Invoke();				//(8)

	return 0;
}

First three lines (marked with (1), (2) and (3)) are just simple forward declarations of global function and pointer to the function that takes no arguments and does not return anything (’procedure’ in some languages). Declared function pointer is used inside SimpleClass (4) and SetCallBack assigns argument value (function pointer) to variable inside SimpleClass (5). Global function ‘functionEx’ (6) does nothing more than calling ‘function’ from object of type ‘SimpleClass’. Where’s the problem here? It seems everything is simple, right? Right. But it is the simplified version of the code I have after I manually renamed function names. The code I was working on was not object-oriented at all – at the beginning. Let’s imagine, that the name of global function is not ‘functionEx’ but ‘function’. Code does not compile. Ok. Lets use Refactor C++ to rename the name of the global ‘function’ to anything that will be different than the name of the method inside SimpleClass. The problem is, Refactor tries to rename things that shouldn’t be renamed, and skip those, we would like to have renamed. Moving cursor to ‘Prepare’ function, selecting ‘function’ (remember, we do not have any ‘functionEx’ in our code!) makes something strange: refactor tries to rename ‘function’ but… it renames the name of the method inside SimpleClass instead of the global function. And now imagine, that the code is a bit more complex, that there are a couple of files inside the projects – headers and .cpp files… This ‘unwanted’ refactoring makes more harm than good (in this case). You may say, that it is because of the code, that looks the way it does… I do not think so. I would expect refactoring tool to be much more ‘intelligent’, but still it is better to have anything (even not the state-of-the-art refactoring tool) than to be forced to make all editing by hand.

I was very interested if the mentioned earlier Whole Tomato’s Visual Assist X will be intelligent enough to cope with renaming the ‘function’. Well, Visual Assist X provides its own mechanism for renaming (not so well integrated with Visual Studio editor as in case of Refactor for C++) and it is much easier to perform renaming, because Visual Assist X shows what actions it is going to perform (and you may uncheck those, you don’t want to happen). Visual Assist X by default wanted to rename the method inside SimpleClass – just like Refactor for C++. So it seems that there is no perfect solution, and one has to be very careful while refactoring anything that contains any non standard pieces of code. Just be aware that something like the issue I have described above may happen, and that’s it.

Happy coding in 2009 :)

Tags: , , , ,


Dec 25 2008

All developers are equal, but some are more equal…

Category: ProgrammingMaciek Talaska @ 12:48 am

Microsoft Visual C++ from Visual C# developer perspective…

After a while developing apps only using .NET (mainly C#) I felt a need to get a deep dive into C++ programming (DirectX). First impressions? It seems that VS is completely different (much harder) to use if you are C++ developer. There are many things that do not work as good as when using C#. First problem is that Intellisense for C++ is much poorer. You got to deal with .ncb files (sometimes the only solution is to delete .ncb file in your project and then… surprisingly IntelliSense gets back from grave).

Worth noting is that DevXpress (Microsoft partner) released another great plugin for Visual Studio without charging for it: Refactor for C++ (the plugin is mentioned also on MSDN site: http://msdn.microsoft.com/en-us/visualc/bb737896.aspx). It is definitely something you should give a try (and I bet you won’t stop using it). Don’t be fooled by installer – the latest version DOES work with Visual Studio 2008 (and not only 2005 as you could think from what is being displayed during installation). Having Refactor for C++ is much better than not having any opportunity to refactor code, but… there are many things that any Visual C# developer is used to, and think of as a ‘must be’ feature of Visual Studio IDE. The refactorings offered by DevExpress’ product are really simple, do not expect possibility to move function body between header / cpp file, extracting parts of a function to another one does not work as I expected, and even simple renaming caused me some problems. Let’s take under consideration following piece of code:

// forward declaration
void RenamedFunction( );

class SimpleClass
{
public:
    void function( )
    {
        printf("<SimpleClass/>n");
    }
};

SimpleClass *externalObject;

void RenamedFunction( )
{
    externalObject->function();
}

As you can see Function is a global function name, and also a name of the SimpleClass. I had similar situation in my code. I wanted to rename Function, and what happend? Well… Refactor renamed it, but… it also renamed the name of the method (SimpleClass->Function became SimpleClass->NewName). I have fixed function names in several places, and created a new project to test if it is safe to rename functions using Refactor for C++ but I couldn’t get the same behaviour – so maybe it really WAS my fault ;) Well… I have checked it in my project… and yes, it still renames the global function AND method inside SimpleClass… I wonder why this bug does not occur in the sample I have provided above… I’ll try to investigate this issue further on.

Most of the things I am missing in Visual Studio IDE itself and Refactor for C++ are available through great plugin by Whole Tomato Software – Visual Assist X. I am sure, you have heard of its biggest opponent – Resharper from JetBrains, so why should be interested in Visual Assist X? The answer is simple, if you never ever use any other language than C#/VB.NET – you have two options: Visual Assist X or Resharper – they both increase coding speed and make you being less afraid even if there are some complicated refactorings to be made. And if sometimes you use C++ as your development language (especially unmanaged)…Visual Assist X is the only option available.

The last thing that made me mad was… VS seems to ‘lost’ support for .hlsl / fx files (pixel/vertex/geometry shaders written in HLSL / Cg). It is kind of a strange, because I remember that it worked long, long time ago (around August 2005 – Visual Studio 2005 Beta with DirectX SDK from August 2005 as far as I can remember). Why doesn’t it work now? It is hard to say. I don’t even know if it’s an issue of VS2008/2005 or just something was broken inside DirectX SDK (I am currently using November 2008). It is funny how quick one became used to something that was a ‘wow’ feature not so long ago :) I missed syntax highlighting in effect / hlsl files so much, that I started looking for some alternative or plugin for VS or some .xsd file (with definition of shader key words) at least… And I found one – Intellishade.Net. It is great that someone gives for free something I thought Microsoft offers :)

After all this, I am really curious what has been said on one of the PDC 2008 sessions concerning new Visual Studio (I think the title of the session was: “VC10 is the new VC6″). I really hope that there will be enough encouragement from all C++ developers, so that MS creates a lot more convenient and powerful IDE for VC++ developers. There is still need for the good C++ IDE for Windows, and I think it is much behind Visual C#. C++ is not dead, and it seems that it won’t be for a long time.

Tags: , , , , , , ,


Dec 07 2008

Color customization in CodeRush XPress

Category: Programming, toolsMaciek Talaska @ 8:30 pm

A while ago DevExpress introduced a new, totally free product for developers using Visual Studio – CodeRush XPress. I found it quite helpful, so it became one of the plugins I am using during my day to day developer activities. However, I was looking for possibility to change default colors (I have black background in my VS environment, so not all the colors are easy to notice), and couldn’t find one. Recently, I have found the solution on DevExpress site. It’s a pity, that one must be aware of this magic key combination, instead of being able to configure plugin behaviour from VS options menu. Anyway, after pressing Ctrl+Shift+Alt+O (easy to remember, right? all the modifier keys and ‘O’ as in Options ;) you will see an option dialog, with dozen of things to change (or better: ‘personalize’):

CodeRush XPress' option dialog.

Tags: , , , ,


Sep 19 2008

Visual Studio & SVN integration

Category: Programming, toolsMaciek Talaska @ 2:29 pm

SVN is nowadays one of the most popular version control systems. There is a great tool for Windows called TortoiseSVN – it is very popular, easy to use and has a lot of features. But… it is not very convenient while developing. The need to switch from Visual Studio just to check-in, resolve conflicts… well it is definitely not what I want. I wanted to have a tool that integrates with Visual Studio just like Microsoft’s Visual Source Safe or TFS does. Quite a while ago I tried AnkhSVN, but it didn’t work as good as I wanted – it had a heavy impact on my Visual Studio (sometimes I had to wait very long to be able to do anything – because VS tended to freeze).

Few months ago I found out that many people praise VisualSVN. It is not a plugin (just like the old AnkhSVN) – it uses SCC interface, so it behaves just like VSS/TFS. Some people says that the only thing they dislike is that they feel it just a wrapper for TortoiseSVN (VisualSVN just launches TortoiseSVN’s dialogs for most of the operations). I think it is not a disadvantage – TortoiseSVN is a great client, so if there is a way to use not, and not be forced to switch from Visual Studio to Explorer / Total Commander / any other shell… that’s just great (for me). The only disadvantage of VisuaSVN is that one have to pay for it. In the past it was very cheap (19$) but after gaining popularity it became much more expensive – now it costs 50$ (but there is a way to get it for free – one just have to be active developer involved in open source project).

Well… I wanted to find a free alternative, and that’s how I found Garry Bodsworth’s special settings, that integrates TortoiseSVN with Visual Studio. It is free. It is fast (there is no impact on Visual Studio performance). It contains both: version for VS2008 and VS2005. Perfect? Well… almost ;) It is just a setting file, so ‘Pending Checkins’ won’t work, there is no mechanism showing new files or changed files in projects / solutions…

All those things that Garry’s settings lacks are provided by the latest, totally rewritten version of AnkhSVN! Now AnkhSVN works not as a plugin, but as SCC package – the integration is done the way it should be. The impact on Visual Studio performance is not noticeable. ‘Pending Checkins’ works just like with VSS / TFS, managing newly added, changed or deleted files in solutions / projects is much simpler – thanks to showing all the changes right in the solution view. Personally I think that TortoiseSVN’s dialogs are better ;) so at the moment I am using both – Garry’s settings and AnkhSVN. Garry’s settings allows me to use very well known by me mechanisms provided by TortoiseSVN, and AnkhSVN shows me all the changes I have made recently – it saves me a lot of time, and I am sure I won’t miss any file while doing heck-in.

Tags: , ,


Nov 25 2007

Getting started with Silverlight

Category: Programming, toolsMaciek Talaska @ 1:59 pm
Recently I have heard about INETA’s “Silverlight European Challenge” and I have thought that it is a great opportunity to learn this cool new technology from Microsoft. My first steps were to go to Silverlight’s site (community -> gallery) – just to install the runtime and watch some samples. Some of them were really amazing, so I was very excited, and started to install everything I thought I would need as a developer. I have installed:
After a couple of minutes, when everything I have listed above was installed I created first Silverlight project in Visual Studio 2008 Beta 2. It compiled at once with no errors or warnings. But when I wanted to run the application (you know, the simpliest “Hello world from Silverlight” thing) it didn’t work. I saw the “Get Microsoft Silverlight” instead of what I expected. I went back to the silverlight.net site, just to check if problem was with the application, or something was broken in my developer’s environment. Samples I was watching a few minutes earlier just stopped working. I decided not to spoil my working environment and to test Silverlight’s installation on virtual machine. I used images with Visual Studio 2008 Beta 2. At the very beginning I realized that I did not need to install Silverlight 1.0 SDK. The next thing I have discovered was that samples stopped working after installing VS 2008 Tools for Silverlight. Unfortunately it is impossible (at least very hard) to develop anything using Silverlight without those VS tools (VS Beta2 is not able to cope with Silverlight’s project type without this add-in). The last thing I tried was to install only Silverlight 1.1, and VS 2008 Tools for Silverlight 1.1. and… it worked!

It is really dissapointing that there is no information concerning what you should install to get everything work as it should. There is a topic on a forum on a Silverlight community site, but it describes installation issues with older version (July 2007) of Silverlight 1.1 Alpha package.

So… just to sum everything up, to have environment for Silverlight development configured properly all you need to do is install:

  • Silverlight 1.1
  • VS 2008 Tools for Silverlight

and that’s it! Nothing more! (you may also install SDK for Silverlight 1.0 and 1.1 – it should not spoil your environment).

Hope it helps… happy Silverlight-contest coding ;)

Tags: , ,