Friday, March 31, 2006

(SQL) should I use #temp table or a @table variable?
I found this great article about all the advantages and disadvantages between #temp, ##temp, @temp, temp tables

definitely worth reading, or course is part of my delicious feed too
(SQL) How do I update only the top # rows from a table?

This can be specially handy when you are testing your data, but it can apply to many production cases as well, I've seen people invent so many ways to do this, but here's a simple way of doing it

set rowcount 100
update YourTable set SomeField = 'Something' where SomeField is null
set rowcount 0

it is very important that you run set rowcount 0 after finishing whatever you needed rowcount for, else any other query (select, updates, etc) that you run will only run for the first # records and it can really give you some headaches, so whenever you write

set rowcount #
write
set rowcount 0

immediatly after that, then write the query in the middle that requires to be ran with the limited rowcount
Favorite features in .NET 2.0

Edgar posted today about one of the new functions in .NET 2.0 to make your life easier, like using

aString.IsNullOrEmpty()
instead of
aString != null && aString != "";

it reminded me about this video from Kit George where he shows some of the new cool little functions that will help you a lot

in .NET 2.0 you can now use:

if (aString.Contains(otherString))
instead of:
if (aString.IndexOf(otherString)<>0)

or

foreach(string line in File.ReadAllLines("c:\theFilePath"))
Console.WriteLine(line)

instead of:
string line;
using (StreamReader sr = new StreamReader("c:\TheFilePath")) {
while ((line = sr.ReadLine()) != null)
Console.WriteLine(line);
}

check out the video to learn more about these and other features

you'll also find the link on my delicious feed under videos
my del.icio.us feed
in case you haven't (heard of, or) used this yet, it is definitely a must, for those of you that don't know that del.icio.us is, is a place where you store your favorites (links) and you can attach TAGS to them, it creates a page for you with your list of favorites and you can quickly filter for what you are looking for using the TAGS you assigned when you added the link

this has many advantages, how many times have you tried to synchronize a list of favorites between more than one computer, or maybe you are with someone else and you know you have a site on your favorites list that you need right now...
del.icio.us will do that for you, pretty simple concept, but it is really powerful

if I didn't do a good job describing what del.icio.us is, here's the definition from the de.icio.us site itself

"What is del.icio.us?
del.icio.us is a collection of favorites - yours and everyone else's. Use del.icio.us to:
- Keep links to your favorite articles, blogs, music, restaurant reviews, and more on del.icio.us and access them from any computer on the web.
- Share favorites with friends, family, and colleagues.
- Discover new things. Everything on del.icio.us is someone's favorite - they've already done the work of finding it. Explore and enjoy."

here's my del.icio.us feed, I'll keep updating that

check del.icio.us out, I'm sure you'll find it usefull

Saturday, March 25, 2006

I just got my (very late christmas present) Cingular 8125, it's sweet!

sent from my cingular 8125, this is my dog Kombo

Friday, March 24, 2006

"it won't affect anything"... on basic development rules: don't make assumptions

When you go to sleep you set the clock's alarm and you assume that it'll work and it'll wake you up in the morning, in some cases you'll setup two alarms assuming that if one doesn't work the other one will (I least I do that when I'm going to be traveling the next morning), when you are driving you drive through a green light assuming that no one is coming from the sides, because they have a red light, etc, etc, etc, at some point we assume something; we live by assumptions!

when we are programming is no different, many times we make assumptions, and quite often those assumptions translate into poorly performant software or users end up using your software in a way you didn't expect it to which translates into a bad experience because you didn't program the software to be used that way (you assumed a different use)

so it's all about assumptions, we assume that a connection will exist, that a file will be there, that a query will run...

do yourself a favor, think about the assumptions you make and try and remove them, this is FAR easier said than done, but just by being aware that you shouldn't make assumptions you will already be ahead of the game, I have been living (not only programming) by this concept for years and I still make assumptions that will *cost me* errors in the software I write, but it is definitely a lot less errors

a very huge assumptions that is made is
"it won't affect anything"

that's a big one, everyone's done it several times, so think back how many times you've said "it won't affect anything" and you ended up breaking the entire system, messing up the database, etc

sure, that's what testing is for, but first of all, if you introduced new bugs you end up wasting a lot more time that it was necessary and second, not everyone has a test team, and if you test your own software since you are pretty sure "it won't affect anything" you don't test your software that well

here's a list of common things that you SHOULD NOT assume
- your app can use all the RAM it needs
- your app can use all the DISK space it needs
- a file will exist
- you will have permissions to do X or Y (specially on web apps, accessing files, etc)
- your app will run under administrator privileges (this is a big one too)
- a connection will be present (TCP, HTTP, SQL, etc)
- a connection will not be dropped in the middle of things
- it won't affect anything

that's a small but pretty good list I think, start avoiding assumptions, I guarantee you will become a better developer that creates more reliable software faster

Thursday, March 23, 2006

on quick little tips: claim your computer memory (kinda manual garbage collector)
on my development machine at work I have Windows 2000 with 1GB of RAM, that used to be more than enough, but nowadays, programs are just getting more and more memory hungry

while you can leave your computer up without reboots for weeks and weeks, you'll notice the memory consuption to keep going up and up

here's a trick to claim some of that memory back so other programs can use it:
- restart a bunch of your windows services and stop (or even disable) the ones you're not using!
- restart (close and reopen) any web browsers that you are using
that's it...

now if you want more detail keep reading

to open the services console, click on Start, Run, type "services.msc" (without quotes of course!) hit enter
click on the "status" colum, so get all of the ones that are started all together

the first one is Automatic Updates, stopping this service will claim quite a bit of memory, if you are good are keeping an eye on updates, stop and disable this one and you'll have an extra 30MB of RAM for other things

here's a list of services that you can (most likely) safely restart without causing problems
- Machine Debug Manager
- Print Spooler
- Your antivirus, i.e. I have "Symantec Antivirus Client"
- Task Scheduler
- TCP/IP NetBIOS Helper Service
- VNC Server (in case you have VNC installed on your machine)
- IIS Admin Service (this will restart other services that depend on it)
- MSSQL* (there can be a number of these ones, depending on what version of MSSQL you have, you can restart them all)

other services you can restart, I haven't noticed much gain from restarting them though:
- Background Intelligent Transfer Service
- COM+ Event System
- Computer Browser

Services you can stop (or disable):
- IIS Admin Service (this will stop other services that depend on it)
- MSSQL* (if you have the 2005 version, you have a bunch of these ones, and they all take a lot of memory)

I usually stop IIS and MSSQL because I don't use them everyday, if you can't stop them because you always use them, at least restart them

if you don't use MSSQL or IIS, except in very few cases, you can even disable them, and then just reenable them whenever you need them

performing the above steps can claim back a bunch of RAM, I've seen 200 MB go back in my machine, but it can vary greatly

now, if you don't like doing that manually, you can always create a .BAT file to do all that for you, and then you could put a shortcut somewhere in your desktop or quick launch bar

to restart a service from a .BAT file you just enter something like:
net stop "Task Scheduler"
net start "Task Scheduler"

you do that, and you can claim a bunch of RAM every day with a single click

of course, nothing beats restarting the machine, but again, there might be services that you don't really use, so no need to have them enabled, and of course you can always use the RAM for other things
Tour Technet - Mexico
I don't know that I have any readers (at all?) in Mexico, but if anyone's reading and are interested, Microsoft Tour Technet is coming to Mexico from March 14th through March 30th to the following cities:

Queretaro, Guadalajara, Chihuahua, Juarez, Monterrey, Len, Ciudad de Mexico Zona sur, Coatzacoalcos, Ciudad de Mexico Zona centro, Puebla y Tijuana

you can find more information and register by going here:
http://msevents.microsoft.com/cui/eventdetail.aspx?culture=es-mx&eventid=1032292033

and you can find the agenda and more information here:
http://www.microsoft.com/spanish/msdn/mexico/eventos/

Tuesday, March 21, 2006

Internet Explorer beta 2 (3 really) is looking pretty good
I've been browsing today using the latest release of Internet Explorer, and so far is looking pretty good, they have fixed some bugs with fonts and CSS (sites that didn't work right in the previous version are working ok now), I like it
opening and closing tabs using the middle button is pretty cool, there are a bunch of other features, click here to go to the download page and try it your self, I like it a lot better than FireFox now

Friday, March 17, 2006

SQL Bug (or feature?) when using sub-queries

I have been using SQL sub-queries for quite a while and had never seen this until today, I was trying to delete records from a table that were found in another table

*luckily* I created a backup before deleting anything, the query to delete ended up deleting every single record from the first table, I knew that not all the records from one were in the other one, so I investigated a little bit, and was able to reproduce the behavior

try this:

CREATE TABLE [temp1] (
[Field1] [nvarchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Field2] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Field3] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

CREATE TABLE [temp2] (
[Field2] [nvarchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Field3] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Field4] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

--Execute both inserts a few times
insert into temp1
values ('v1', null, null)

insert into temp2
values ('v2', null, null)

--now run this query
select * from temp1
where field1 in (select field1 from temp2)

you get all the records from temp1

notice that the field field1 doesn't exist in temp2, but it does exist in temp1, if I try a field that doesn't exist in neither table I will get the "Invalid column name 'fieldx'." error

weird... I wonder if this is a bug/known bug... or a feature!

Monday, March 13, 2006

blaming it on something else, other than your application

this article about ASP.NET 2.0 Unhandled Exception Issues reminded me of people that always tries to blame it on something else when their application fails, not that I like how the exceptions are handled in ASP.NET 2.0, but I've heard programmers complain about the network's fault, that the third party DLL is not behaving properly, that the drivers are corrupt, even that Windows is *crashing* (imagine that! =oP)

in the case of the article it refers to unhandled exceptions killing your application, truth is, when problems arise a lot of developers will look at something else before looking into their code to see what's wrong

if you just made a change to your program and now is not working, 99.999 it was your fault!, just go look at the changes you made and fix it! instead of wasting time blaming it on something else

if an exception is killing your application and you are left with nothing but criptic messages about the error, it still is your fault, go and put proper exception handling, exception handling should be there from the time you first write the code, why leave it at the end? or why leave it until you are not able to determine the cause of the errors?
whenever you are about to write code that can potentially raise exceptions, always write this code first (adapt it to whatever language you're using)

try
catch
finally

and then write the code in between

in the catch:
handle exceptions, re-raise if needed, include any additional information that will help you track what caused this exception (line #, record #, whatever data you were dealing with, etc, it will make it a LOT easier to track if you include this information)

in the finally:
release any resources

for Delphi I like to use
try try
except
//*** handle exceptions
end finally
//***release any resources
end

C# also allows you to use a "using" statement
using (somevariable = new... blabla) {
}

which is basically an implicit
somevariable = new blabla
try
//***your code here
finally {
somevariable.Dispose();
}

so use it whenever possible

unhandled exceptions are a BIG cause for software failures, so handle your exceptions and stop blaming it on something else

Friday, March 03, 2006

on quick little tricks, how do I verify that all the lines of a file have the same length?

open the file in your favorite text editor (on second thought, use Notepad, Notepad2, WordPad or something like that, certain fonts, margins and other beautifiers might screw up the results), make sure Word Wrap is OFF, select all the text, then scroll to the far right side and you should see a perfect line as long as you are using a fixed-width font (a font that displays all characters with the same width)

update: to include the name of such fonts, thanks Mason

Thursday, March 02, 2006

Windows Vista helps you protect your kids

if you are a parent worried about what sites their children are visiting, who are they chatting with, etc, you will be glad to know about the new built-in features on Windows Vista, check out the article:

Family Safety on Windows Vista

what is amazing to me is the comments of people complaining about these features, of course everyone can have whatever opinion, but I think leaving your children open in the wild internet is just plain stupid