Jan 27 2012

Lambda Expressions in C#

Lambda Expressions are very powerful when it comes to filtering sets. There are built in methods such as Where() or FindAll() available for most collections in C#. These methods use Lambda Expressions for filtering. I have listed a few examples below.

Find evens in a list of positive integers


List numbers = Enumerable.Range(1, 100).ToList();
var evens = numbers.FindAll(n => n % 2 == 0);

Find primes in a list of positive integers


List numbers = Enumerable.Range(1, 100).ToList();
for (int i = 2; i < 8; i++) {
numbers = numbers.Where( n => (n > 1) && ( (n == i) || (n % i > 0) ) ).ToList();
}

Although, it is probably not necessary to explain the expression above; prime numbers cannot be devided by 2, 3, 5, 7 and not equal to 1. Why doesn’t  it matter that 4 and 6 are in the loop too? It is because 4 is a multiple of 2 and 6 is a multiple of 3. If a number is devisible by the multiple of a prime number then it must be devisible by the base prime too. Therefore anything that is devisible by 4 or 6 cannot be a prime.

Lambda Expressions can use Generic Delegates to ensure that the input, output is matching a given type. For instance, the expression below will return a compiler error because the inferred type is a double.

Func<double,double> giveMeDouble = (x) => x / 2;
int MyInt = giveMeDouble(1);

This short introduction is not even scratching the surface of what Expressions can do. If you want more information about Expressions check out ExpressionTrees on MSDN.

Related reading:

C++C++ Coding Standards : Rules, Guidelines, and Best Practices
Covering the facets of C++ programming, this book discusses: design and coding style, functions, operators, class design, inheritance, construction/destruction, copying, assignment, namespaces, modules, templates, genericity, exceptions, and STL containers and algorithms. Each standard is described concisely, with practical examples….

Jun 21 2011

Google Algorithm Changes

I have come across this eye opening article that sheds some light on the algorithm changes committed by Google over the years. It has encouraged me do a little research and think about the effects it might have on sites in the future. The article shows plausible evidence that Google is not reluctant to make fundamental changes to the way they are indexing documents on the web nor anxious about what this means to e-commerce companies in the world. Google has set a goal to serve their customers (the users) primarily with less regards to corporate entities whose role also is, or supposed to be, to serve them. The SEO community is divided about their methods, but we are all used to that being futile anyway…I mean having an opinion about it. Continue reading

Related reading:

Feb 2 2011

SQL Server – Char vs. Varchar

Beginner developers often get confused about which types should they use when they create tables or what difference it makes.

Well, there is a good reason for the existence of all types – or at least there was when they were invented. I will soon publish a new series of posts about SQL data types, however, in this post I will talk about the most commonly misunderstood types and what troubles they likely to cause.

I found string types to be the most troublesome, especially Char. First and foremost Char is a fixed length type, which means that the space for the length of the characters is reserved. Opposed to this VarChar (acronym for variable character) is a variable length type which could save a considerable amount of space in large databases in exchange for some speed*. Continue reading

Related reading:

Dec 16 2010

The importance of modeling

Why is it important?

Modeling is building, creating a representation of something – an object, a process or maybe a series of events. More often than not its main purpose is to create an opportunity to closely observe and examine the subject, to help the observer to a better understanding.

The sole purpose of software development is to solve problems. Even video games can be reduced to problems and their solutions that together ultimately led to the end product that so many of us enjoy. The first step towards successfully solving a problem is to study and thoroughly understand them.

How does this help us?

In IT we use UML (Unified Modeling Language) to help us with this task. There are various diagrams that can be made using UML. One of the simplest ones are called use case diagrams. As the name suggests, these charts are used to map out how the user should interact with the solution. Amongst the most common objects there are, of course, users (actors), systems, packages, etc. These objects can be assigned IDs and connected to one another, similarily to the better known everyday flow charts. Probably the most significant difference is that in case of use case models objects are really in depth objects containing details, requirements, sub-diagrams, etc. Continue reading

Related reading:

Dec 11 2010

Google Analytics Pain…

Let us admit that Google Analytics (GA) is brilliant and not to mention free before I go into the details of the problems that one have to face by implementing it.

First of all GA is JavaScript based.

I know this does not sound like a problem at first but let us not get too ahead of ourselves and think about it a for bit…
We all know at least two things about JavaScript and that it works on the client computer and that it is susceptible to errors in the code. What does this mean?

Since scripts run on the client computer, the users decides whether they want to allow or deny them in their browser. In fact, user statistics show that 5-10% of all users turn off scripting in their browsers. Although this number has been improving over the last few years I still think ignoring 5-10% of one’s customer base would be a mistake. – I can actually hear some people saying that “…this should not matter, because 90-95% is still a representative number and it should give you a very good idea about what your users are doing blah blah blah” and they might be right. However in my opinion that 5-10% represents a specific segment of your users – probably the more tech conscious ones who perhaps should be targeted in a different way precisely for that reason.

This is stretching out into the realms of marketing so let’s move on…

The second thing I mentioned is the bigger issue, I think. If you dare to do a little experiment and go for some free surfing on the web you will quickly realize what I am talking about. Most of you must have seen that annoying little exclamation mark in a bright yellow triangle at the bottom left corner of your browser windows. It is likely that some of you haven’t even noticed this while you were digging around the web, simply because all seemed to be fine… However, as always, not all is what it seems!

Let’s say someone who is new to the web world… a jockey for instance who decided to get off the saddle and do something more static, like stooping at the front of a computer screen and try to decipher HTML and JavaScript. Since our guy is a jock and not stupid, he probably spent a reasonable amount on a good domain name and even paid a bundle of cash to a semi-professional to build a half decent web site and implement Google Analytics.

So, now that the good deed is done our theoretical retired sportsman is free to start experimenting with the mighty code. – Well, this is also where the problems start. It takes only a typo to brake a js file then GA stops working right there and then.

I know that my example is a little extreme but in the real world when some company hires some developer whose job is to implement and maintain GA could face serious issues due to the same reasons. If our guy is not lucky he could end up having to work with a website that is using a mixture of coding languages and awkward methods spreading over a number of servers likely with no documentation what so ever. Just to make it worse the board could be full of people with project management, marketing etc. background whom actually have no clue about what has been happening with their web site for the past 5 years or so…

Hmmm…In this unfortunate situation our newbie employee will have to be really careful. The site could easily have hundreds, maybe thousands of pages and GA should be implemented on all of them. Not to worry, our lad is smart so he wraps the Google code into a library and run a global search to implement it on the pages. Within a few days the nice charts starting to appear as the marketeers log into their accounts and they are all happy campers. Job well done!

However, it is not the end of the story. In a few months time someone changes something in some js file and suddenly figures start dropping… The next day the marketing crew is banging on the table demanding an answer, and by one look at the Director’s sweaty forehead our chap feels urged to find one. – Where shall he start?

Well, there are quite a few different ways to find the problem but none of them is simple. Hence my reluctance to accept JavaScript as the best analytics method. Back in the oldies, we had tracking pixels and they worked just fine. Why Google got rid of it? – I don’t know.

In summary, my suggestion to anyone who is about to implement GA is that they should make a bulletproof plan for the implementation and try to explain to its future users what they should expect. In the plan, carefully consider the IT infrastructure and map out rewrites and redirects before jumping into the deep water. It is generally true that good planning saves you time and money…in this case it also does save you lots and lots of headache.

Related reading:

© George Berdal 2006-2010 All Rights Reserved


Valid HTML 4.01 Transitional