Current area: HOME -> Blogs -> pheaven's Blog

The Official Programmer's Heaven Blog

The blog where the Programmer's Heaven team post stuff.
Very Quick Guide To DLinq: Part 3 (LIKE, IN, DATEDIFF)
Posted on Wednesday, April 30, 2008 at 6:50 AM
Much of the time, it's fairly straightforward to express an SQL query in Linq instead. However, there are some things that aren't quite so clear how to do. In this article, I'll take a look at some of those.

LIKE

There are a couple of ways to replace LIKE. If you just want to directly use LIKE, then you can do so using the SqlMethods class. To use this, you must add the following using declaration at the top of the file:
using System.Data.Linq.SqlClient;
Then we can use it like this:
var Result = from Book in DB.BookResources
             where SqlMethods.Like(Book.Title, "%java%")
             select Book;
This will create an SQL query that locates all books containing "java" (case-insensitively). Interestingly, you can also do the following:
var Result = from Book in DB.BookResources
             where Book.Title.Contains("java")
             select Book;


Read More
Tags: .NET, C#, DLinq

An Introduction to Mock Objects
Posted on Tuesday, April 15, 2008 at 2:26 PM
Writing automated tests is a tried and tested way to improve the quality of software. In the initial phase of development, tests help to verify that the code functions correctly. In Test Driven Development, tests are written before the code, so any knowledge about the ins and outs of the implementation won't influence the writing of the tests. After the initial development, as changes are made over time, a comprehensive test suite can quickly point out unintended changes in the behavior of the code, so the bugs can be fixed before the software is shipped. Importantly, the tests are automated, so they are very cheap to run in terms of time.

What do we want from a test suite?

There are a range of properties that are good to have in a test suite.
  • It often allows bugs to be pinpointed to a relatively small area of the code base, saving debugging time.
Read More

When Linq and ref Parameters Meet
Posted on Tuesday, April 01, 2008 at 7:17 AM
It's fair to say that I'm a pretty heavy user of Linq. You'll find uses of it scattered across my code, from the obvious (using DLinq to query a database) to the slightly more exotic (writing queries over collections obtained from classes in System.Reflection). Linq often allows you to express a problem very neatly, resulting in compact, readable code. It also factors out the application of operations and leaves you to worry about the operations themselves, likely decreasing bugs.

Fixing A Bug

Today I ran across some code that took a parameter, then used it in a Linq query. Omitting the clutter, it looked something like this:
public void Lookup(string URL, ref int ID, ref int Status)
{
    // ...stuff...
    var Result = from D in DB.Datas
                 where D.URL == URL
                 select D;
    // ...more stuff...
}


Read More
Tags: .NET, C# 3.0, Linq

Very Quick Guide To DLinq: Part 2
Posted on Tuesday, March 25, 2008 at 2:45 AM
In this part I'm going to take a look at inserts, updates and deletes using DLinq.

The Overall Model

First, to make changes to a database, you instantiate the data context object, just as we did before doing selections in the previous part.
var DB = new ResourcesDB();
You then do the required changes; we'll look at this part in just a moment. However, the inserts, updates and deletions are not performed immediately. They do not take place until you explicitly submit the changes:
DB.SubmitChanges();
This means that you can make many changes, then send them to the database all at once.

Inserting Data Into A Table

To make a single new entry to a database table, use the InsertOnSubmit method on the Table object. This takes a single parameter: an instance of the object that represents a row in the the table. You can use the object initializer syntax to write this very neatly.
DB.BookResources.InsertOnSubmit(new BookResource()
{
Read More
Tags: .NET, C#, DLinq

Very Quick Guide To DLinq: Part 1
Posted on Friday, March 14, 2008 at 9:10 AM
We have already written about using Linq, but didn't cover DLinq. This post takes a quick look at getting started with DLinq, which allows you to write Linq queries against databases. Once you're up to speed, you can take a database and be writing Linq queries against it in under five minutes!

In future parts in this series of posts, we'll look at doing database updates, writing more complex queries and debugging. In this one, we'll just look at doing some simple queries.

Creating The Linq to SQL Classes

The bad news is that before you can use DLinq, you have to create Linq to SQL classes to represent your database tables. The good news is that Visual Studio 2008 (aside from the Express Edition) can do all of the hard work for you in this.

Read More
Tags: .NET, C#, DLinq

Float, Double and Decimal: do you know the differences?
Posted on Tuesday, March 11, 2008 at 3:17 AM
There are a number of data types available in the .Net framework for storing numbers with fractional parts. They are each appropriate for different situations, and using the wrong one can lead to errors in calculations.

Thinking About Accuracy

In some applications, you require that calculations involving numbers with fractional parts are exact. Examples of this are in financial applications, where losing the odd cent here and there in a calculation is unacceptable. Customers expect their accounts to be completely accurate, not to mention the tax man.

In other applications, you don't care about exact results, but you are interested in having an answer that is correct to a certain number of significant digits. This is the case in experimental physics, for example. When you make a measurement of something, you can only measure it as accurately as your equipment will allow you. Therefore, you have an inherent error in that value already, which is going to cascade through any calculations you do with it. This means that while your computed answer may be 5.1826, you may know that the values this answer was calculated from were only accurate to three significant digits, and so the last two digits in this result (the 0.0026) don't matter. If there is a computational error in them, so be it - it's not going to hurt us.

Read More

Can a language make programming more fun?
Posted on Monday, March 03, 2008 at 7:38 AM
I've programmed in a range of languages. There are some that I really enjoy coding in, there are some I can put up with and there are some that frustrate and bore me. Having an interest in language design, I got somewhat curious about what it is that makes working with some languages more enjoyable than others.

A couple of questions came to me right away. First, there is probably a lot of personal preference here. What makes a language enjoyable for me to work in may well be what makes it frustrating to someone else, and vice versa. Second, languages are tools. I've said often enough that different languages are suited to different tasks, and you should pick the one that best fits the job at hand. How important a factor is whether one enjoys working in the language when trying to choose whether to use it in a project or not?

Personal Preference

Read More

Google Trends of Programming Languages
Posted on Tuesday, February 26, 2008 at 3:23 PM
Google Trends allows you to compare the popularity of various search terms. A friend pointed me at this, having put Java into it and noted a "dropping trend". I played around with it for a while, sticking in various language names, and came away with more questions than answers.

How to write stupid conclusions

The obvious but stupid way to write a blog post based upon Google trends would be to look at the graph for Java, look at the graph for Lua (a recent scripting language suited to embedding) and spurt out things like, "OMGWTFBBQ LOOK! Java is getting less popular and Lua is getting more popular!". Let's look more carefully at this, though.

Read More

Do try...catch blocks hurt runtime performance?
Posted on Tuesday, February 19, 2008 at 6:23 AM
Recently, a friend mentioned that someone had told him that writing a try...catch block in C# (or substitute some other .Net language here) resulted in a "huge penalty" in terms of performance compared to if you had not written it. That is, merely writing such a block actually hurt program performance, even if an exception was never thrown. He didn't believe this was true, and rightly so - it's completely wrong. This post is a tidied up version of my explanation.

Inside a .Net Assembly

A .Net assembly, if we ignore various headers, consists of three things:
  • Bytecode: a sequence of low-level instructions that specify the body of a method
  • Metadata: a set of tables, a little bit like a database, describing higher level constructs such as classes, methods, signatures and so forth
  • Heaps: places where string constants and other such things are stored
Read More
Tags: .NET, C#, CLR, Exception

C#.NET or VB.NET?
Posted on Monday, February 11, 2008 at 9:10 AM
We get a lot of emails from Programmer's Heaven users. Amongst the requests that we do people's homework, which we ignore, we also get some more interesting questions sent to us too. We figured that when we do, we may as well share our answers more widely, and also this gives other folks a chance to give their input too. Recently we got one from someone who had discovered us through the C# School e-book and asked, "please kindly advise which of the two, C# and VB .NET is better for developing commercial applications like Payroll".

The Similarities

Both languages run in the Common Language Runtime and allow you to have full access to the .Net Class Library. This means that pretty much whatever is in the .Net Class Library is equally available to both VB.NET and C#.NET. Both languages have a similar feature set too, providing strong support for Object Oriented Programming, which is good for developing database-backed applications, as your Payroll surely will be.

Read More
Tags: .NET, C#, VB.NET

What is REST, anyway?
Posted on Wednesday, February 06, 2008 at 11:09 AM
REST is short for Representational State Transfer. I'd place that into the category of names that makes more sense once you understand what REST is, but isn't quite so helpful in terms of explaining it. REST is often heard in the context of web services. When it was suggested as a topic for me to write about it was described to me as a web service but doing "the query string thing" to pass it parameters. That's not really what it's about, though.

Verbs and nouns

Verbs are doing words. Examples in natural language are "pay", "drink" and "shock". In most programming paradigms, we spend a lot of time defining how to do things, by writing methods, subroutines or functions. These are our programming equivalent of verbs.

Nouns represent entities. Examples in natural language are "money", "beer" and "mother". Whenever we have data in our program and we somehow identify it (for example, by name binding or variable declaration), these are just like nouns in natural language.

Read More
Tags: REST

Divvy up my list? Aye!
Posted on Thursday, January 31, 2008 at 4:28 AM
I wasn't born in Yorkshire, but I spent most of my childhood growing up there. Yorkshire, a large county in the north east of England, has its own collection of slang, not least including the word "aye", which it appears is appropriate in response to pretty much any situation. Another slang phrase is "divvy up". Basically, this just means to divide a bunch of things up somehow.

These days, I spend a lot of my time writing C#. If I'm dealing with a bunch of things in C#, I'll usually store them in a generic List collection, or some other collection. This got me thinking: how can I divvy up a List in C# into multiple Lists? And that's when I decided to implement the Divvy extension method.

What we're aiming for

Suppose we have a list of scores:
var Scores = new List<int>()
    { 87, 32, 45, 60, 91, 10, 58, 77, 66, 71 };


Read More

Higher Order Programming Is Easy!
Posted on Tuesday, January 22, 2008 at 7:09 AM
Higher Order Programming is one of those things that to many people sounds weird, magical, mysterious or just too hard for them to be able to do. It's not. If you have ever passed a function or method as a parameter to another function or method, then you have done higher order programming. If you have ever used a function pointer in C or a delegate in C# or some kind of callback mechanism, then you have done higher order programming.

Define it!

We are all used to writing code that takes parameters. We do it whenever we write subroutines, functions or methods. Normally these parameters are data. In higher order programming, one or more of these parameters is code rather than data. That is, you can pass code, functions and/or methods around, just as you can pass data around. That's all there is to it.

Show me an example

Let's look at C#, because the syntax will not be too unusual to programmers in many languages. Suppose we have a List of scores for a test.

Read More

Why automated testing matters
Posted on Thursday, January 17, 2008 at 2:55 AM
Test Driven Development is a development methodology that encourages you to write tests before writing the code that is being tested. Following it completely strictly means that you never make a functional change to the code (that is, adding a new feature or fixing a bug) until you have a failing test that implementing the feature or fixing the bug will cause to pass.

For those who are not used to writing tests, such a strict methodology can feel quite daunting, potentially unproductive and perhaps fun-spoiling. I can empathize with all three points of view, but at the same time it's worth trying almost everything at least once to see how it works out (though I'm still not so keen on trying a bungee jump).

In this article I'm going to look at the idea of automated testing more generally. I'll share a couple of my experiences of it, then I'll look at some thoughts for giving it a go in your own environment.

Why Automate Testing?

Read More

Sun acquires MySQL
Posted on Wednesday, January 16, 2008 at 8:27 AM
Sun have today announced their plans to acquire MySQL, the company behind the popular MySQL database server. This gives Sun a large stake in the database market. MySQL already has a range of high-profile clients, with some household names including Facebook, Yahoo!, BBC and Nokia.

MySQL is an Open Source and Free Software database, and members of the community will be somewhat reassured by Sun's background. They are the largest contributors of open source code, well known for Open Office and more recently for releasing Java under the GNU GPL. The MySQL VP of Community wrote a blog post to reassure the community about the acquisition.

Read More

What are the next generation of programmers learning?
Posted on Monday, January 14, 2008 at 7:22 AM
Recently I read an article asking, where are the software engineers of tomorrow? Written by a President and Vice-President of the company AdaCore, they lament the spread of Java as a first programming language taught on many Computer Science courses and a reduction in the formal and theoretical background that students are being given.

While I can't say I agree with every word they say (for example, they appear somewhat dismissive of dynamic languages), they certainly have a lot of good points. You certainly don't learn to be a good programmer, developer or architect by learning one or two programming languages. To do that you need to understand the whole stack of abstractions form top to bottom, be aware of many programming paradigms and understand (or at least be aware of the existence of) the theory that explains it.

Read More

Today's Topic: Perl's $_ Variable
Posted on Tuesday, January 08, 2008 at 5:30 AM
When we are speaking we often use words such as "it" to refer to the thing we are currently talking about. For example, "My computer has just had a PSU failure. It is on fire." You could say that "it" refers to the current topic, which we assigned in the previous sentence.

Anyone who's read many of my ramblings will know that one thing that interests me is the use of features of natural language in programming languages. What if we could express the idea of the current topic in a programming language, though?

Turns out that is exactly what the $_ variable in Perl is for. It is sometimes also known as the "default variable". If you have read many Perl scripts you will probably have come across things like this:
chomp;
s/\[b\](.*?)\[\/b\]/<b>$1<\/b>/;
print;
The question that people often ask on seeing this is - chomp what? Bind a substitution to what? Print what? A fairly substantial number of Perl built-ins, when invoked a parameter missing, will use the default variable $_ instead. You could re-write the above as:

Read More
Tags: Perl

Win a GPS sensor in our Alpha Five competition!
Posted on Friday, December 21, 2007 at 2:28 AM
Programmer's Heaven have teamed up with Alpha Software to give you the chance to win one of two Garmin GPS 18 sensors or one of ten copies of their award winning database application development software - Alpha Five!

The GPS sensor is a Garmin GPS 18. Plug it into a USB or serial port, install the included software and your laptop becomes a street navigator. Or take it with you when you travel and see what points of interest are nearby using its extensive points of interest database. We're sure you can think of plenty of other creative uses!

Entering is free and easy: just supply your contact details (these will, of course, not be disclosed to anyone, including Alpha Software) and answer a few questions about the Alpha Five product. So what are you waiting for?

http://www.programmersheaven.com/competition/alphafive/

IE8 Passing Acid 2 Test
Posted on Thursday, December 20, 2007 at 4:53 AM
The Internet Explorer team, currently working on Internet Explorer 8, have announced that they can now render the Acid2 test correctly.

The Acid 2 test is far from a complete standards compliance test, but more an indicator of a browser's level of CSS support. If anything, perhaps this is a sign that the IE team are getting serious about web standards. The Acid 2 test is already passed by Opera and Safari, and the current Firefox 3 BETA release also passes it. Since Firefox 3 will almost certainly be out before IE8, that means Microsoft are still the last of the major browsers to arrive at the Acid 2 party.

Read More

Subscribe

RSS Feed RSS Feed

By Tag

.NET Algorithm alpha five C# C# 3.0 DLinq Higher Order Programming Java Lambda Expressions Linq Linux MySQL Performance Perl Programming SSH Test Driven Develoment Testing Threading UNIX

By Month

April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
October 2007

Help

Check out the Blog FAQ.


Sponsored links

Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Villanova University Six Sigma & IT Certificate Programs
100% Online programs in Six Sigma, IS Security, CISSP Prep, Business Analysis, Proj. Mgmt. and more!
Virtual File System SDK
Create your own file systems in Windows and .NET applications
PureCM Software Configuration Management
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!


Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.