The Software Purist |

Aug/16

20

More Internet on Mars

Related

Some years ago, I published a post titled “The Internet on Mars”: http://www.softwarepurist.com/blog/index.php/the-internet-on-mars/

Another blogger created a post with a similar idea, along with a cute little video that goes over some of their ideas. I liked it and wanted to pass along: http://www.internetproviders.com/shortcut/internet-on-mars/ .

No tags

Jan/16

1

Happy New Year!

Just wanted to wish everyone a Happy New Year! Coming back with more new content very soon.

No tags

Dec/15

21

Binary Search Part II

A while back (read: years ago), I wrote this post: Software Purist – Binary Search. It wound up being an exercise in algorithm design, along with some interesting points I made about the 80/20 principle and testing. Without looking at what I had done previously, I decided to try to write binary search again. Here’s what I came up with. You should note that this example will use C++11. Here’s the code:

#include <array>
#include <cassert>

using namespace std;

template<class T, class... Tail>
auto make_array(T head, Tail... tail)
  -> std::array<T, 1 + sizeof...(Tail)>
{
	std::array<T, 1 + sizeof...(Tail)> a = { head, tail ... };
	return a;
}

template <class TContainer>
int binarySearch(const TContainer& container,
                 const typename TContainer::value_type& val)
{	
	auto oldBegin = -1;
	auto oldEnd = -1;
	auto begin = 0;
	auto end = container.size() - 1;

	while ((oldBegin != begin || oldEnd != end) && (begin <= end))
	{
		oldBegin = begin;
		oldEnd = end;

 		const auto half = (end - begin + 1) / 2 + begin;
		
		if (container[half] == val)
		{
			return half;
		}
		else if (container[half] < val)
		{
			begin = half + 1;
		}
		else if (container[half] > val)
		{
			end = half - 1;
		}
	}

	return -1;
}

int main()
{
	auto x = make_array(1, 2, 5, 9, 13, 18, 72, 1385);

	assert(binarySearch(x, 0) == -1);
	assert(binarySearch(x, 1) == 0);
	assert(binarySearch(x, 2) == 1);
	assert(binarySearch(x, 3) == -1);
	assert(binarySearch(x, 5) == 2);
	assert(binarySearch(x, 8) == -1);
	assert(binarySearch(x, 9) == 3);
	assert(binarySearch(x, 12) == -1);
	assert(binarySearch(x, 13) == 4);
	assert(binarySearch(x, 15) == -1);
	assert(binarySearch(x, 18) == 5);
	assert(binarySearch(x, 36) == -1);
	assert(binarySearch(x, 72) == 6);
	assert(binarySearch(x, 1000) == -1);
	assert(binarySearch(x, 1385) == 7);
	assert(binarySearch(x, numeric_limits<int>::min()) == -1);
	assert(binarySearch(x, numeric_limits<int>::max()) == -1);
}

It took me a few tries to make it work flawlessly on paper. However, I noticed my test cases were more robust. Obviously, the use of C++11 clouds the issue slightly, as well. I also noted I’m more willing to use int as a data-type now and more likely to use automatic-typing. I’m sure this is due to the influence of other programming languages, like C# and Python. In comparing to the previous, I also noticed better naming, using half rather than newIndex. But, the most striking thing was the fact that my solution was actually different than the previous solution. It encapsulated the same idea, and it’s similar, but not exact. Both solutions work. It’s interesting to see how one’s programming-style evolves ever so slightly (or significantly) over time. It would be hard to say one solution is better than the other.

Going back to the original thought, I still find it amazing that only 10% of programmers get this right. But, as I tried it out on paper, it definitely took me a few goes through my test cases to get it quite right. So, maybe it’s not so far fetched. I think what this is really saying is that 90% of programmers don’t put enough time into unit testing and their test cases. So, let’s discuss that a bit more.

My test cases in this example encompassed all of the following:

  • All values in the array
  • Values not in the array
  • Values not in the array between values that were in the array
  • Negative values
  • Very negative values (checking for underflow)
  • Very large values (checking for overflow)
  • Values less than the minimum value in the array
  • Values larger than the maximum value in the array
  • Zero

If you look at the above, that’s a pretty robust way to approach boundary testing and I encourage you to do the same (where feasible). All of the above can be sources of errors. Also note that sometimes a test case covers multiple conditions, which is great. Just make sure you have a sufficient amount of test cases, to be confident. And then, of course, automate. Normally, one would use a unit test framework, but I wanted to make the code simple enough to run on modern compilers with no additional libraries.

· · ·

Dec/15

14

First Post Using Dvorak Keyboard

Hello everyone. This is my first post using the Dvorak keyboard layout. If you’re not familiar with Dvorak, it’s an alternate layout which has a different order of keys for efficiency purposes.

The normal QWERTY keyboard layout was developed at a time when very limited typewriters were the norm. Typing too fast risked damaging the typewriter, so the keys were oriented so that the most used keys were all on the left side of the keyboard. This was done to ultimately limit typing speed.

What Dvorak does is reorient the keys so the most used keys are near the center and split at a much more reasonable ratio. This results in typing a lot more with the right hand (Most people are righties) and faster typing speeds, due to better distribution.

I’ve been trying it out this week and so far the results are positive. I normally type around 100 wpm. While my speed has been nowhere near that so far, it is steadily climbing every day. I can see myself topping that in a few weeks, as long as I keep at it.

As far as using Dvorak, you don’t need a special keyboard to use it. You can just choose a different layout in your operating system. On Windows, you can change this in control panel -> Keyboards and Languages -> Change keyboards. Then go to add, expand your language and choose the appropriate Dvorak layout.

One final useful tip is to keep the On-Screen Keyboard visible while you’re learning. On Windows, you can find this in All Programs -> Accessories -> Ease of Access -> On-Screen Keyboard.

No tags

Recently, one of my colleagues at a previous job mentioned to me something to the effect of, “I’m less of a versioning geek that I feel I’m supposed to be as a Software Developer”. This comment sparked an interesting discussion.

First, I’ll start with some background. My former colleague used to work with me at the gaming company I worked at and tends to work on small projects and with artists. For him: the main features of an SCM he finds useful are commit, check out and the ability to tag a set of code.

As you might expect, my perspective is quite a bit different. I’ve worked on some small projects, for sure. But, my specialty is larger systems working with teams that have been as large as 50 people or more. In these cases, obviously an SCM has to be quite a bit more. When we talked further, he talked about “a normal case” being that one person owns a particular file, so why make it so complicated? This brought up the fact that for large projects, someone owning a file is not a normal case and tends to be a bottleneck. This is why locking-style SCMs are rarely used at most companies I work for—the overhead is too immense and it requires the file owner to be available 24/7 and instantly responsive, which is certainly an unrealistic expectation.

The killer features for large teams are the ability to commit often without conflict and be able to merge at critical points smoothly and efficiently. Distributed SCMs like Mercurial and Git are prime examples of systems that handle this well. For developers that work on smaller projects, like my friend, the main reason use to SCMs is to be able to go back to an old revision. Of course, this is useful for everyone, especially when you’re trying to understand a system, but I still find the case of merging to happen far more frequently for me.

The other interesting point of this discussion was specific to Git, since I didn’t realize his complaint was specific to Git at the time. Git has a lot of commands, but only a small subset are actually useful to most users. Furthermore, the vast number of commands can make things complicated to non-technical people. In his company, game artists were also using Git as a versioning mechanism for their artwork. I would debate whether this is a good approach for asset management for a variety of reasons (maybe this can be a future post). It definitely seems very complex for an artist.

Overall, I found the discussion very interesting from the perspective of someone working on different-style project than I tend to work on. What are some of the interesting ways you use source control? Do you find patterns that tend to ease things more for large projects vs. small projects?

No tags

So, I ran into a subtle registration bug in the development of Compucator.com, so it became an interesting opportunity to talk about some of the technical details.

As you probably are aware, I’m very much against the typical copy-paste programming we often see during development. Most likely, if you need to copy-paste something, probably it should be refactored. That being said, sometimes it’s a useful tool. The problem with doing such is that it’s also an error-prone tool.

The situation is that I was adding fields to a table in the SQL database. It was a table related to user information, so there were fields, such as address, zip code, phone number, email, etc…

Some of the fields were meant to be marked, as UNIQUE. For example, you might argue that email should be unique to prevent duplicate registrations. Obviously, the account name must be unique. However, it seems when I was doing this I accidentally made a copy-paste error and applied the UNIQUE attribute to the zip code field.

This is not the actual code, but imagine it looks like this:


CREATE TABLE Details
(
...
name VARCHAR(100) UNIQUE NOT NULL,
...
zipCode VARCHAR(10) UNIQUE NOT NULL
...
);

The real issue here is that because the zipCode was set as UNIQUE, when two users registered with the same zip, the second one failed and was not able to register. Going through the logs, I realized this occurred once, so I had to fix it.

Theoretically, I should’ve caught this during my unit tests. Unfortunately, I missed having any test cases where there were duplicate zip codes which is how it made it all the way to production, so definitely lesson learned here.

Furthermore, I wanted to correct this programmatically, as there were multiple unique constraints on this table. I didn’t want to take any risks of deleting the wrong constraint, and this change needed to make it to my SQL update scripts so any other test databases received the same update. The code would normally look something like:

ALTER TABLE Details
DROP CONSTRAINT UQ_Details_zipCode;

However, SQL Server will automatically generate names for constraints that I don’t explicitly give names to. So, the names were things like UQ_Details_xxxxxxxxxxx, where the name was unfortunately not meaningful.

Unfortunately, this still proved to be some complicated SQL code, so it seems the easiest thing to do was something like the following:


DECLARE @command nvarchar(1024)
DECLARE @tableName nvarchar(256) = N'Details'
DECLARE @columnName nvarchar(256) = N'zipCode'

SELECT @command = 'ALTER TABLE ' + OBJECT_NAME(so.parent_obj) + ' DROP CONSTRAINT ' + so.name
FROM sysobjects so
WHERE OBJECT_NAME(so.parent_obj) = @tableName
AND so.xtype = 'UQ';

print @command;

--execute (@Command)

NB: I tend to print out the command to make sure it’s doing what I think before I execute it.

I then just added a few lines to the script to re-add the constraints for fields that were actually supposed to have them.

Perhaps there’s a better solution, but this seemed cleanest. Has anyone run into any similar issues? Feel free to share your experiences.

No tags

Nov/15

23

Re-launch

Hello all. I know it’s been a long hiatus. Other projects and interests got in the way, but I’m ready to pick the blog back up, dust it off.

So, the first thing you might wonder if what I’ve been up to. I’m pleased to announce the launch of my new site Compucator.com. It’s a Software training site that I have developed and just launched. The first course is up and is a complete course on the C++ STL library, focusing on STL containers. It has lessons and exercises. I intend to create a part II of this course if there is interest. I’m also open to suggestions on other courses, including C++ and otherwise.

As far as the site itself, I’m pleased to offer a $5 OFF discount code. If you use the code “Launch” at checkout, you’ll receive this discount. The code is valid until the end of the year.

Now, that being said, I’m happy to continue offering free content on this site and to continue blogging. I have a list of ideas for new blog posts. But, what are you guys interested in?

· ·

Jun/13

20

Agile Defined?

I accidentally came across this:

Agile (verb) – To work on something when you don’t understand the problem space, the market, the actual goal of the product or really any engineering at all.
— UrbanDictionary

I have to say, when Agile isn’t implemented right, I certainly feel this way. As the Most Interesting Man in the World might say:

I don't always do agile, but ...

No tags

Apr/13

17

Forgotten C++ Macros

The joys of exploring the depths of C++. So, I was looking at the assert macro and I completely forgot about one of the macros it makes use of. It seems we commonly make use of __FILE__, __LINE__, __FUNCTION__, etc… But I completely forgot about __BASE_FILE__. This actually gives you the compilation unit, rather than the file that got included. This is pretty helpful, actually. Here’s a reference for the standard C++ macros:

I think I may make use of __BASE_FILE__ more often.

No tags

Apr/13

2

C++11 Move Constructors

Here’s a good article describing how the new C++11 feature for move constructors works. I’m putting together a demo application that will turn into a blog post later.

http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html

I like this new feature as it seems it will be very valuable in avoiding some annoying cases where you would otherwise do unnecessary copying. Will be interesting to see how easy it is to implement this performance improvement in practice.

No tags

Older posts >>

Theme Design by devolux.nh2.me