Category Archives: Software

Refactor-able code

I have been thinking lately what is the best way to Stop over-engineering and still not fall victim of duplicate code (or even worse – very similar code).

At first glance, the approach of Test-first programming and refactoring seems to be the perfect remedy to over-engineering. What it basically says is:

  • Keep the code as simple as possible
  • If you are about to use duplicate or similar code – re-factor at all costs

This guarantees you that the code will be right-engineered. Not over-engineered (unnecessary complexity), nor under-engineered (duplicate, similar code all over).

The problem with that approach is that it does not work. It does not work because the “at all costs” rule above is frequently overruled by non-engineering reasons. A typical example will be when you decide not to re-factor to fit in the latest feature since you are too close to a release and you don’t want to introduce new bugs or void the testing already done.

Then you fall into the “I will patch it now for the release and re-factor later” down-spiral. So if you are stubborn on the “at all costs” rule, you will not be able to make your releases, and if you are not you will end up with an under-engineered code-base.

The root of the problem is that refactoring takes time and brings instability to the system. And to me there is no reason for that. We should be able to write code that is not complex at that moment, but is refactor-able (or refactor-friendly). Such refactor-able code will be easy and clear to refactor if necessary, but without doing it in advance. We will save time from the development (as compared to over-engineering it), but we will still be able to follow the “at all costs” rule, since refactoring will be easy.

My goal is to come up with a definition and how-to for refactor-able code. I am posting this much more as a basis of discussion and ideas, rather than as something carved in stone.

Definition: refactor-able code – code that is easy and risk-free to refactor
How to do it:
There is just one simple rule – Over-engineer in your head, do it simple. What I mean is to keep in your head a vision of how would you over-engineer it if you had all the time in the world. Then write the code simple, preparing it to match the vision in your head.
Since this is a really vague how-to, I will try to list cases in which you can substitute the urge to over-engineer something with much simpler preparations for that. Following is a list of examples

If I was over-engineering I would… Instead I will…
Split this into two modules and use callbacks to communicate Keep the two modules together, but keep the would-be-callbacks as separate functions.
Make a base class for the generic part and inherit it for this special case Do one class, keeping the “generic” methods together and don’t allow a lot of special-case code to go into the generic methods
Share

Repair dynamic disks that go foreign

How to repair dynamic disks that go foreign or will not reactivate

Sometimes when frequently switching dynamic disks among machines, moving parts of the disks of a group amongst machines, or doing something not very simple sometimes the LDM (Logical Disk Manager) will break and some or all of the disks will go foreign or missing. The LDM is the table that contains information about all disks layout – partitions, stripes, etc.

In many cases you will be able to get to your volumes using “Import foreign disks” or “Reactivate disk“. Sadly sometimes the Windows Logical Disk Manager goes berserk and fails with “Disk group has no configuration copies” or some other cryptic error.

Consider you have done everything you thought of, and you don’t have a backup of your LDM created with Tiger Technology‘s Dynamic Disk Optimizer tool (which would have saved you). Then there is the last measure – re-building your dynamic disk configuration manually. The rest of this post will explain how do do it.

I assume you:

  • Remember by hard the layout – partition sizes, striping, spanning, etc. If you are not 100% certain do not proceed – you will DESTROY your data.
  • The you are at least a developer or sys admin. I will be talking in disk sectors, hex, cylinders and complex abbreviations like NTFS, NTLDR.
  • You are familiar with working with a disk Hex editor (like WinHex). If you don’t feel crafty with this – do not read further – ask for help
  • Understand this is just information I publish. Should you decide to rebuild your dynamic disks following this advise, and you lose your data as a result, the responsibility is yours.

Lets use a stripe of two disks as an example. It won’t import. It looks like this in disk management:

foreign.jpg

Step 1: First we have to backup the first 4096 bytes of your NTFS. Use a hex editor (like WinHex) to find the NTFS (it should be on disk 1). Depending on weather the disk is MBR, GPT, and how you partitioned the NTFS(es) might be in different places. Look for the string NTFS with the hex editor, and when you find it – check if it matches this picture:

backup.jpg

Then from the start of the NTFS sector, write 4096 bytes to a file somewhere. This is the important info to rebuild the volume. If you don’t successfully backup that, and continue – the data is LOST. Also remember disk number and offset where you took the backups from.

You have to do this step for each volume (not disk). You should end up with as many NTFS backup files as many volumes you had. If you had partitions in the middle of the disk – it might be tricky to find all NTFS headers.
Step 2: Convert the disks to basic
convert.jpg

Step 3: Convert back to dynamic

convertdyn.jpg

Step 4: Create your volumes again as they were. EXACTLY the same. When Windows ask to format the new volume DO NOT ALLOW.

format.jpg

Step 5: Write back all the NTFS headers you backed up in Step 1 (you remembered disk offsets and numbers as I told you right? If not – search for the NTLDR signature as screen on the WinHex screen shot above – it is on the next sector as you can see from the screen shot, and it will be there).

Step 6: Try to mount/access the volume. You should see your data :)

Share

Using OpenVPN on Windows Vista

Well, thanks to the long holidays I finally had time to give the Vista release a spin. For the purpose I used my company laptop – a Lifebook T4210 tablet PC. Since i was tempted to switch to Vista completely and wipe out the old XP, I had to prove that all the tools I need for my job will work. One of the most important was OpenVPN, since this is what we use to access our network.

The straightforward approach (simply installing and running it) did not work, since OpenVPN was failing to adjust my routing table. It uses an API (IPAPI) that does not work, even if I “Run as Administrator” it. Fortunately OpenVPN has an option to set routes by executing route.exe (route-method exe), which does the trick. Also, OpenVPN should be run as Administrator, which ruled out the option of using its own right-click menu “Start OpenVPN on this config file”. What i found works best is:

  1. Add to your .ovpn file the following two lines:
    route-method exe
    route-delay 2
  2. Create a one-line .BAT file to run openvpn on your .ovpn config file like this
    “C:\Program Files\OpenVPN\bin\openvpn.exe” [PATH_TO_OVPN_FILE]
  3. To start the VPN, right click on the bat file and choose “Run as Administrator

Enjoy.

Share

Get a hard disk serial number

I have been planning to write on that topic since October, but never got the time since I was pretty busy with the releases of MetaSAN and stoneGate. Now during the holidays I finally have time to post.

The issue that I came upon was (at the beginning) looking quite simple – get the serial number of a locally attached hard disk. My first approach was to simply use IOCTL_STORAGE_QUERY_PROPERTY and hope that Microsoft did all the work for me. Unfortunately this was behaving strange. On half the hard disks in our lab it would succeed and return the correct disk serial, on others it will fail (SerialNumberOffset being zero).

Ok, I think to myself, someone must have had the same problem – lets ask google. Soon I figured that 90% of my searches were ending into a commercially sold DLL named GetDiskSerial. Someone had solved the problem, and was selling the solution. By that time I had enough budget for a $19 toolkit, but could not accept that something I thought should be a simple API/IOCTL call should cost money at all. I downloaded an eval, it worked fine on all the machines I had at my disposal.

I did not give up, and after some more hours I found a utility called diskid32 and downloaded it. It did not work, but I found that the author published the source here. I extracted the part that was doing the IDENTIFY, compiled it and it worked fine for me. I did not figure out why their executable failed.

Anyway, here is a free alternative to the GetDiskSerial DLL – getdiskserial.zip.

The source has two functions with identical interfaces – GetDiskSerial1 and GetDiskSerial2. The first is using the MS IOCTL, the second – the method by Lynn McGuire (diskid32). You can easily make a super-function of the two, to always get the disk serial.

I am not claiming the source is my creation – I just wanted to share my experience and give you the result I came up with – simply combining the two approaches and making the interfaces/results match (the diskid32 returns the serial as text, while the MS IOCTL returns it as a string containing the hexadecimal values of the ASCII code of the disk serial).

I still think that the commercial DLL has some benefits – it has versions for various languages and development platforms and probably works with a wider variety of disks. I have not yet found a disk that neither IOCTL_STORAGE_QUERY_PROPERTY nor diskid32 will work on, but if you find one – drop me a line please.

Share

IE 7 RC1 vs Firefox 2.0 RC1 comparison

I spent some time this week to take a look at the release candidates of the two browsers. I am a great MS supporter, but on the other hand I prefer Firefox for browsing so far, so I was (I hope) not biased at all.

I started with IE7, download and install – no problems. Started and went on to do my usual browsing with it. Soon I felt kinda sluggish browsing with it. Started Firefox 1.5 along and started visiting the same pages. Firefox was times faster in re-drawing the screen and in smoothness of the scroll with the mouse wheel. I can’t stress this enough – Firefox faster redrawing makes browsing with IE feel like you are using a 600Mhz PC with 128Mb of RAM. I noticed that browsing the same pages from startup, IE had eaten up approximately 120% the memory Firefox needed.

Since I am a big fan of mouse gestures and that was the reason so far to use Firefox, I immediately checked if there us such an add-on for IE 7, and I quickly found one (hey, a point for IE, it has gestures at last) and installed it. And then came the frustration!!! As a long-time mouse gesture user in both Opera and Firefox I can surely say that I have never seen so poor implementation. It might be a result of the overall sluggishness of the browser, but the way I am doing gestures in IE I got about 40% correct gesture and 60% … the right click menu… WTF? For IE to recognize my gesture I had to do it very slow and distinct, while Firefox and Opera can recognize a gesture performed in just a few pixels on the screen. I am sure that the usability testing spent on the gestures was close to zero.

Quick tabs – that is a new, very nice feature in IE. Did I forget to mention IE has tabbed browsing now? I probably did, since it is a feature all other browsers have had for years now. Ok, so it has tabs, and has a feature that will tile all your tabs in a since screen allowing you to quickly see all the opened pages rendered, and you can switch to any tab by clicking on it. A really nice way to switch between tabs – love it!

Summary for IE7: Quick tabs are good, performance is awful, mouse gestures are awful, crashes frequently (when opening new tabs quickly 90% of the times it will crash)

Then I continued to install Firefox 2.0 RC1. Since I was frustrated with IE performance I tired this first – and the new Firefox is as fast and consumes as much memory as the 1.5 version. Everything works smooth and from the new features there is one that I instantly fell in love with – spell checking fields in forms. Before I used to write my blog posts in Word (just for the sake of spell checking) and then paste them in the blog. Right now, I type this post directly in the web-based editor, and my spelling mistakes are underlined in red. Sweet!

Summary for Firefox: Fast and light as always, spell checking is a great new feature!

That’s it – the verdict is more then obvious (and as a MS fan – I don’t like to admit it)
- Firefox is now even further ahead of IE

Share

New dotbot version and WoW thoughts

New dotbot version and World of Warcraft thoughts

Released a new version of dotbot two days ago. I had quit playing World of Warcraft for almost six months now, but some people on the Curse Gaming forums that use the addon kept asking me about bugfixes and recent patches support.

To make the release I used a friend of mine’s account, and logging in made me again think of re-activating my account. I strolled around Ironforge and the surrounding area, and this reminded me again how great this game is, how it creates a unique feeling of the world around and how much attention was paid to every small aspect of it.

Still my account will remain frozen, until Blizzard does something to attract back players that don’t have the time to do 40ppl raids and people who want to progress through PVP combat. Sure, there are honor and battleground rewards, but these are not comparable at all with what you can get if you do mindless raids of the same dungeon over and over again, re-doing the same fights over and over until your uber item drops. This is also the reason why battlegrounds are not fun for casual players anymore – it simply is not fun to fight anyone equipped times better then you, who got his equipment not by any skill, but just by investing hundreds of hours re-doing the same dungeon runs over and over.

I cannot blame Blizzard for doing this of course – they are simply trying to please the larger customer base. This however alienates the casual players from the game totally. Many of my colleagues that tried to play casual, since they don’t have the time to play 5-6 hours each day, have also quit.

Share

Wexelblat’s Scheduling Algorithm

Perfect way to approach planning for new products or features. Funny and very true!

Wexelblat’s Scheduling Algorithm:

Choose two:

  • Good
  • Fast
  • Cheap
Share

MSI setup requires .NET 1.1.4322

“Setup requires .net framework version 1.1.4322″ with version 2.0 installed

Lost almost half a day trying to install a MSI package that complained “setup requires .net framework version 1.1.4322″, and I had version 2.0 installed. I found the solution in the blog of Hannes Pavelka, which turned out to be one interesting development blog. You can see the solution of the problem here.

Share

C preprocessor power

I was discussing with a friend the popular chalange to write a program that prints its own source. In C of course. The shortest one we found was:

main(){char*s=”main(){char*s=%c%s%c;printf(s,34,s,34);}”;printf(s,34,s,34);}

What I did not like about this one is that it relied on the knowledge that the ASCII code of the quote is 34. I wanted to find a solution that is still short, but does not have that. Preprocessor to the rescue:

#define _(__) char*a=#__;__
_(main() {printf(“#define _(__) char*a=#__;__\n_(%s)”,a);})

Share

Visual C++ Decorated Names Syntax

Ever wondered how Visual C++ encodes parameter info in the so-called decorated names?
This knowledge is close to useless, and the only time I needed it was when I tried to generate a DLL that needs to completely mimic another DLL’s exports. I wanted to re-create the original CPP & H file for a DLL that I had only the exports for. So I started looking on the Internet for info how decorated names are created. MSDN has just a little paragraph about decorated names stating:

A decorated name for a C++ function contains the following information:
The function name.
The class that the function is a member of, if it is a member function. This may include the class that encloses the function’s class, and so on.
The namespace the function belongs to (if it is part of a namespace).
The types of the function’s parameters.
The calling convention.
The return type of the function.

I found that info insufficient, so I decided to figure out the format by declaring functions and looking at the decorated name that the compiler generates. The conclusions I have made are for sure not complete, I just only went that far so that I can re-create the declaration of each of the functions in that DLL. But the result IMHO is usable and even if I have missed a type it can be easily added.

Here is what I have figured out about the format:

  1. C++ decorated names start with a ‘?’ sign.
  2. The name and namespace/class follows in format name@class@@
  3. Visibility specifier follows:
  4. A – private
    I – protected
    Q – public

  5. Unknown character follows (always A)
  6. Definition of the parameters (see below)
  7. The decorated name ends with “@Z”

Ok, so number 5 of the above list is pretty general :). Let’s be a little more specific :).
Types can start with an arbitrary number of the following modifiers:

PA: *
AA: &
PB: const *
QA: *const
AB: const &

The type signature follows. Here is the table:

E this pointer
P6 Function pointer (complete function declaration follows)
D char
X void
F short
J long
H int
M float
N double
O long double
E unsigned char
G unsigned short
I unsigned int
K unsigned long
_J __int64
_F __int16
_H __int32
_D __int8
? Type name follow terminated by @@
U User type. If it starts with a digit – index of user type. If it starts with a letter – new user type. Consider the following example – the function void func(MyType *a, MyType b) has decorated name of ?func@@YAXPAUMyType@@U1@@Z
V Similar to U. Not sure what it is about. Appears when the user type is the class that the method is member of.

Here is a class that will parse a decorated name, and print its declaration :) Have fun hooking C++ DLLs :)

CPPFunc.c
CPPFunc.h

Share