Thank you C++ for … exceptions

10/12/2012

Return value is not the best error handling mechanism

C

This example closely resembles the code I’ve seen in Windows CE drivers.

BOOL doStuff() {
    void* mem_buff = NULL;
    PHANDLE res_1_handle = NULL;
    PHANDLE res_2_handle = NULL;
    BOOL result = FALSE;

    if (mem_buff = malloc(WORK_BUFFER_SIZE)) {
        goto error;
    }

    if (RES_ERR == acqRes1(res_1_handle)) {
        goto error;
    }

    if (RES_ERR == acqRes2(res_2_handle)) {
        goto error;
    }

    if (compute(res_1_handle, res_2_handle, mem_buff)) {
        result = TRUE;
    }

    error:
    if (res_2_handle != NULL) {
        relRes2(res_2_handle);
        res_2_handle = NULL;
    }

    if (res_1_handle != NULL) {
        relRes1(res_1_handle);
        res_1_handle = NULL;
    }

    if (mem_buff != NULL) {
        free(mem_buff);
        mem_buff = NULL;
    }

    return result;
}

What’s not visible above is the amount of repeated error handling logic deep in the bowels of relRes[12]() and acqRes[12]().

C++11

As you may guess the resource classes can be done much better by the usage of templates so that there is much less code repetition. As we try to stick with the C code, C++ can’t shine that well.

class Resource1 {
public:
    Resource1() : m_handle(NULL) {
        if (RES_ERR == acqRes1(m_handle)) {
            throw;
        }
    }

    ~Resource1() {
        relRes1(m_handle);
        m_handle = NULL;
    }

    PHANDLE get() {
        return m_handle;
    }

private:
    PHANDLE m_handle;
};

class Resource2 {
public:
    Resource2() : m_handle(NULL) {
        if (RES_ERR == acqRes2(m_handle)) {
            throw;
        }
    }

    ~Resource2() {
        relRes2(m_handle);
        m_handle = NULL;
    }

    PHANDLE get() {
        return m_handle;
    }

private:
    PHANDLE m_handle;
};

bool doStuff() {
    try {
        Resource1 res1;
        Resource2 res2;
        std::unique_ptr<void> buffer(
            (void*)new char[WORK_BUFFER_SIZE]
        );

        return compute(res1.get(),
                       res2.get(),
                       buffer.get());
    }
    catch (...)
    {
        return false;
    }
}

You can choose your winner now.

Advertisements

Thank you C++ for … strongly typed enumerations

02/12/2012

enum needs a scope

“Enumerations are scope-leeches – they infect the outer scope.”

C++03

class Foo {
	enum Single {
		ONE = 1, TWO = 2, THREE = 3
	};

	enum Double {
		ONE = 2, TWO = 4, THREE = 6
	};
};

Result

error: declaration of ‘ONE’ conflicts with previous declaration ‘Foo::Bar Foo::ONE

error: declaration of ‘TWO’ conflicts with previous declaration ‘Foo::Bar Foo::TWO

error: declaration of ‘THREE’ conflicts with previous declaration ‘Foo::Bar Foo::THREE

Solution #1: Prefix

class Foo {
public:
	enum Single {
		Single_ONE = 1, Single_TWO = 2, Single_THREE = 3
	};

	enum Double {
		Double_ONE = 2, Double_TWO = 4, Double_THREE = 6
	};
};

int main() {
	// Prints: 1
	std::cout << Foo::Single_ONE << std::endl;
	// Prints: 4
	std::cout << Foo::Double_TWO << std::endl;

Solution #2: Wrap

class Foo {
	struct Single {
		enum Enum {
			ONE = 1, TWO = 2, THREE = 3
		};
	};

	struct Double {
		enum Enum {
			ONE = 2, TWO = 4, THREE = 6
		};
	};
};

int main() {
	// New way of declaring the above
	// enums (note the required Enum)
	Foo::Single::Enum s;
	Foo::Double::Enum d;

	// Prints 1
	std::cout << Foo::Single::ONE << std::endl;
	// Prints 4
	std::cout << Foo::Double::TWO << std::endl;
}

C++11

class Foo {
public:
	enum class Single {
		ONE = 1, TWO = 2, THREE = 3
	};

	enum class Double {
		ONE = 2, TWO = 4, THREE = 6
	};
};

int main() {
	Foo::Single s; // Typical way to use
	Foo::Double d; // the enumerations

	// The reason to cast to int will be discussed later.
	// Prints: 1
	std::cout << (int)Foo::Single::ONE << std::endl;
	// Prints: 4
	std::cout << (int)Foo::Double::TWO << std::endl;
}

enum‘s double life as an int

“Enumerations are implicitly convertible between each other, as they are ints in nature.”

C++03

// Ordered by their strength
enum Unit {
	LIGHT_INFANTRY,
	HEAVY_INFANTRY,
	COMMANDO
};

// Ordered by range
enum UnitRange {
	SMALL,
	MEDIUM,
	LARGE
};

/// @return Biggest unit range
UnitRange GetBest() {
	return LARGE;
}

Unit GetUnitToBuild() {
	// Poor programmer assumed GetBest() will
	// return the best unit the AI could build.
	if (HEAVY_INFANTRY >= GetBest()) {
		return HEAVY_INFANTRY;
	} else {
		// Will always choose this path.
		return LIGHT_INFANTRY;
	}
}

C++11

// Ordered by their strength
enum class Unit {
	LIGHT_INFANTRY,
	HEAVY_INFANTRY,
	COMMANDO
};

// Ordered by range
enum class UnitRange {
	SMALL,
	MEDIUM,
	LARGE
};

/// @return Biggest unit range
UnitRange GetBest() {
	return UnitRange::LARGE;
}

Unit GetUnitToBuild() {
	if (Unit::HEAVY_INFANTRY >= GetBest()) {
		return Unit::HEAVY_INFANTRY;
	} else {
		return Unit::LIGHT_INFANTRY;
	}
}

Result

error: no match for ‘operator>=’ in ‘(Unit)1 >= GetBest()

enum are of some type

“Enumerations are internally ints, even if they don’t have to be.”

C++03

enum Recipent {
	ALL,
	SERVER,
	CLIENT,
	NODE
};

enum Response {
	NONE,
	IMMEDIATE,
	DELAYED,
	PERIODIC
};

// Size of our packet is 24B.
struct Packet {
	unsigned char data[16];
	Recipent reci;
	Response resp;
};

C++11

enum Recipent : unsigned char {
	ALL,
	SERVER,
	CLIENT,
	NODE
};

enum Response : unsigned char {
	NONE,
	IMMEDIATE,
	DELAYED,
	PERIODIC
};

// Now the size is only 18B.
struct Packet {
	unsigned char data[16];
	Recipent reci;
	Response resp;
};

Comment

While the bit fields provide even better ‘compression’ they are not so easy to use and require careful casting from/to enum.

Thank you C++ for … initializer list

25/11/2012

Most vexing parse

“Everything that resembles a function is a function.”

C++03

struct Bar {
};

struct Foo {
	Foo() : x(0) {}
	Foo(const Bar&) : x(1) {}
	Foo(const Bar&, const Bar&) : x(2) {}

	int x;
};

int main() {
	Foo a();
	Foo b(Bar());
	Foo c(Bar(), Bar());

	std::cout << a.x << std::endl;
	std::cout << b.x << std::endl;
	std::cout << c.x << std::endl;
}

Result

Line 17: request for member ‘x’ in ‘a’, which is of non-class type ‘Foo()’

Line 18: request for member ‘x’ in ‘b’, which is of non-class type ‘Foo(Bar (*)())’

Line 19: request for member ‘x’ in ‘c’, which is of non-class type ‘Foo(Bar (*)(), Bar (*)())’

C++11

int main() {
	Foo a{}; // or just: Foo a;
	Foo b{Bar()};
	Foo c{Bar(), Bar()};

	std::cout << a.x << std::endl;
	std::cout << b.x << std::endl;
	std::cout << c.x << std::endl;
}

Result

0

1

2

Container initialization

“It’s not easy to initialize a container, bar some fancy comma operator tricks.”

C++03

struct Foo {
	Foo() : _container() {
		_container.push_back(1);
		_container.push_back(2);
		_container.push_back(3);
		_container.push_back(4);
		_container.push_back(5);
		_container.push_back(6);
		_container.push_back(7);
	}

	// Alternative
	Foo(int) : _container(helper()) {
	}

	static std::vector<int> helper() {
		std::vector<int> result;
		result.push_back(1);
		result.push_back(2);
		result.push_back(3);
		result.push_back(4);
		result.push_back(5);
		result.push_back(6);
		result.push_back(7);
		return result;
	}

	std::vector<int> _container;
};

C++11

struct Foo {
	Foo() : _container{ 1, 2, 3, 4, 5, 6, 7 } {
	}

	std::vector<int> _container;
};

Brief constructors

You (almost) always have to specify the constructor name.

C++03

struct YouDontWantToSpellThis {
	YouDontWantToSpellThis(int a, int b) {
		;
	}
};

std::vector<YouDontWantToSpellThis> container;

container.push_back(YouDontWantToSpellThis(1, 2));

C++11

container.push_back({1, 2});

ASP.NET dynamic controls & one gotcha you might have missed

24/11/2012

One of the most unintuitive and bristling with gotchas topic regarding ASP.NET site development is the creation of dynamic controls. Dynamic controls are these kinds of controls which are not static on the page. In other words, one page can contain different number and types of controls, depending on its state.

There are several rules to keep in mind while creating such controls:

  • Due to stateless nature of websites controls have to be recreated every postback/page request. Creating them only once will result in empty page after postback.
  • When recreating the controls you have to keep in mind to create the same ones that you have created when rendering last client request. Failure in doing so will cost you a nice exception of form “Invalid View State“.
  • Recreation of dynamically rendered controls should be done in LoadViewState() which you should override.
  • Creation of new set of controls requires deletion of old ones and creation of new set in Load() method during which you will probably consume various events telling you to change the state of the page.

There is however one gotcha that is not mentioned very often, or not emphasized enough. Whenever you are recreating your dynamic controls remember to assign the same IDs to the controls in order the events to work. If you are sure that you done all correctly (regarding dynamic controls) and for some reason the events are not handled, then you better check the IDs.

FileInfo Exists lies! Or not?

20/11/2012

I’ve recently wrote a code that basically looks like that:

FileInfo GetFile()
{
    var info = new FileInfo(path_to_not_yet_created_file);
    
    var someObj = new SomeObj();

    // ... Logic gathering/preparing various pieces of data ...

    someObj.SaveToFile(info);

    return info;
}

void foo()
{
    FileInfo file = GetFile();
    
    // Defensive programming ftw!
    Debug.Assert(file.Exists);
    
    // ... More code ...
}

Seems reasonable.

  1. Create FileInfo to some file to which you gonna save your result.
  2. Do some computations.
  3. Write the result to a new file pointed by FileInfo.
  4. Validate that the file was created using Exists property.

But it doesn’t work. Exists always returns false. The file is definitely there, process have adequate access rights, the path matches … even your dog can see the file. Madness!

After playing around for some time I learned one simple truth: FileInfo isn’t automatically refreshed and won’t reflect the current state of the file when querying its state. In Debug.Assert(file.Exists) Exists will always return false as at the point of creation (var info = new FileInfo(path_to_not_yet_created_file)) the file wasn’t existing.

I will stick with plain ol’ string for now, and remember not to use FileInfo so much. Hope that saves you some time.

No preparation for tests = easiest way of project failure

15/11/2012

Not long ago my friend cursed his P2P-like program as it seemed to be an endless well of bugs (multiple clients synchronization issues). I’ve talked with him, and this is basically how the talk went:

Me: So why didn’t you test (i.e. automated unit tests) it step by step, module by module with different number of stations?
Him: It’s easy to say for you. It will be very hard to test the network stuff.
Me: Aren’t there any tools to facilitate such tests?
Him: There are, but they are quite expensive and still require a lot of work.
Me: But why are you so inclined on testing the protocol (TCP/IP)? You should rather test the logic.
Him: Logic is dependent on the protocol so how can I do it?
Me: Don’t you have good interface/implementation separation?
Him: … nope.

As you may guess he continued with some manual testing until he found the causes of failures, at least most of them.

Basing on this story I understood why so many teams still don’t test their software enough. It’s because test-unfriendly architecture requires a ton of resources to test. Any by testing I don’t mean some rough manual tests to find if the code builds and doesn’t throw an exception in the first 5 … make it 3 … seconds! I mean automated unit tests run every day, every hour, every commit, all the time.

First, read the all popular Hevery’s guide on writing testable code. Remember: testing is easy, writing testable code is hard.

Second: take a look at the following diagram:

This is my personal ranking of stuff which is hard/easy to test/work with. The lower the element that you are interested to test is, the harder it will be to do it.

In other words, it’s very hard to test thread schedulers. You will only be able to create random tests that will basically play around with various thread counts, affinities and Yields. This will at least give you some security against pesky concurrent issues. On the other hand, if your application deals a lot with files, you will probably not have many problems with it. You will just create a set of test files, and then pump it through your logic.

Let’s say you identified that dealing with web communication will be a considerable part of your application. So what to do now?

  1. Treat the communication protocol as the biggest national secret that no class should be aware of.
  2. Describe the functionality of your code via abstracted interfaces (top-down).
  3. Verify (prototype) that you can create tests basing on the abstracted interface.
  4. Verify (prototype) that low-level communication protocol can be operated via interface (bottom-up).
  5. Make necessary fixes keeping the interface as abstracted as possible.

It’s obligatory that you prepare tests for your application, and I strongly recommend that you treat testability as one of architectural priorities. Ask yourself: “How will I test it?“, and have a solid answer on that.

Bash command works, but not when in script!

03/11/2012

As I have a Ubuntu One account (probably everyone has cloud account on some server) I decided to make some backups of data stored there. I’ve tested the following string in my console and everything worked:

tar cf - /home/me/Ubuntu\ One/foo/ | 7z -si ...
tar cf - "/home/me/Ubuntu One/foo/" | 7z -si ... # Alternative solution

Every smart cookie knows that for backups we use cron + scripts. Awesome, so I changed it to following script:

readonly bak_src='/home/me/Ubuntu One/foo/'
tar cf - $bak_src | 7z -si ...

Result?

tar: /home/me/Ubuntu\\: Cannot stat: No such file or directory
tar: One/foo/: Cannot stat: No such file or directory

Ok, that’s simple (space is wrongly handled), let’s do it this way:

# 'Ubuntu\ One' version won't work as the in the final string
# the '\' will be changed to '\\'.
readonly bak_src='/home/me/Ubuntu One/foo/'
tar cf - "$bak_src" | 7z -si ...</pre>

Great, that works. But in fact the string I ended up with was as follows

readonly bak_src='/home/me/Ubuntu\ One/foo/'
readonly cmd="tar cf - ""$bak_src"" | 7z -si ..."
echo $cmd # Returns: tar cf - /home/me/Ubuntu\ One/foo/ ...
$cmd

And … a failure. The old Cannot stat: No such file or directory returns. But why? This doesn’t make any sense; the echoed string is correct (I’ve checked it in the console) but it somehow doesn’t work in a script.

Reason? While the string looks virtually the same, bash doesn’t evaluate the bak_src and treats what is given as two separate command arguments divided by a space. To deal with it, we have to use the eval which forces bash to reevaluate the whole string so that the tokens can be selected correctly.

readonly bak_src='/home/me/Ubuntu\ One/foo/'
readonly cmd="tar cf - ""$bak_src"" | 7z -si ..."
eval $cmd

If you want to read more on this (especially if you didn’t get it from my explanation), read the Waldmann’s guide.

Happy bash hacking!

No translation to SQL

26/10/2012

When you start to like LINQ (to SQL) very much, someday you may find yourself in a nasty situation when “LINQ cannot translate method to SQL” during … runtime. That’s sad but quite logical: LINQ maps the logic to equivalent SQL statement. If there is no equivalence, it won’t work. In other words:

var foo = my_context.my_table.Where(val => SuperComplexMethod(val));

won’t work.

The usual answer is to use Expression<Func> but it will only help if it is possible to do (like converting Math.Pow()).

Another option is to restructure your function and let it use the supported logic.

Sometimes, however, you are to lazy to do all of that just to call some arbitrary function. The solution? Just use the AsEnumerable(). It basically tells the LINQ: “Ok, you can’t do what I want using the underlying database, so leave it to me”. There might be some performance issues due to not being able to use the power of the database, but many time this is the easiest way to go.

var foo = my_context.my_table.AsEnumerable().Where(val => SuperComplexMethod(val));

ASP.NET FileUpload doesn’t work

08/10/2012

So you added a FileUpload control in order to receive files from your clients. The problem? HasFile() returns false every time you send a file.

Reason of this problem is described in HTML specification. Basically, if the forms sent by your page will contain file data, multipart/form-data should be used.

To use it in ASP.NET we just need to specify following line in our code:

Page.Form.EncType = "multipart/form-data"

I am not 100% sure but I haven’t heard about a rule that forces you to use the above code in load, init, prerender or any other place. If you know more about this, feel free to write a comment.

Mutex-free C# application deadlock

01/10/2012

We’ve all heard about & encountered deadlocks. Nasty buggers they are, won’t you agree? After several long-night fights against them you should be well equipped to deal with them. You remember not to call external (delegate) code in synchronized context, you carefully add various logs to monitor behavior of mutexes, but to your surprise there is still an observable deadlock!

First, let’s make sure that we deal with a deadlock, not some funky system behavior, external library bug or endless loop. To test threads behavior you can basically do two things: change their number or add Thread.Yield(). We will focus on the first option.

In order to change the number of actively running threads we use the ThreadPool method. ThreadPool is the default way of using threads so I can safely assume that you will directly or indirectly (Tasks) use them.

We changed the number of threads, and we can observe that indeed the deadlock appears so we eagerly start debugging our poor parallel program, but the problem is not there.

What you should always check when using thread pools is whether the synchronization logic has taken all available threads. Take a look at the following example (assume the ThreadPool can run 10 threads concurrently).

The problem should be visible now.

  1. #10 waits for empty slot.
  2. Old threads won’t finish as they are long-running.
  3. Short-lived thread #9 (or a chain of inter-dependent threads) is waiting on #10
  4. #10 starves to death locking whole application.

You might not encounter this problem every day, but be careful when:

  • you frequently use BeginInvoke() in your code,
  • you port your application to older system on which the default number of threads is lower than you assumed.

While thread pools provide us with efficient creation of threads, they bring their own issues. Remember to enable sufficiently high number of max threads in pool, or reduce the number of threads that have to be run concurrently, in order to avoid synchronization starvation.