Pages

Tuesday, 22 November 2011

5 Steps To Effective And Efficient Comments

Recently I visited the dentist for a regular trip to the hygienist. My previous hygienist had moved away and so I had a new one. As part of the visit, my new hygienist said something that got me thinking: "I read your notes earlier and from that...". Exactly what followed is unimportant other than to take home the point that my new hygienist already had an idea of my dental history before they even met me.

Those few words struck a chord with me about how important your comments can be in your code. And whilst the example I give describes when someone else is tackling something you've coded, can you honestly remember every line of code you've written? I know I cant and I can think of at least one occasion where I've revisited a piece of code I wrote, had insufficient comments (that would be none) and could not understand why I had done something in a particular way.

So how do we go about ensuring we give good details about the code without wasting time, effort and the concentration of the reader?

Tuesday, 8 November 2011

T-SQL Tuesday #024 - Calculating Easter with a CLR Function

T-SQL Tuesday for November 2011 is about Prox n Funx (Procedures and Functions) and is hosted by Brad Schulz here: http://bradsruminations.blogspot.com/2011/10/invitation-for-t-sql-tuesday-024-prox-n.html

This is my first T-SQL Tuesday post (be gentle) and I thought I'd share a function I have recently converted from T-SQL into a CLR based function as it fits with this months topic.  I wont be going into what the CLR is or how you go about using it in SQL Server as there are plenty of posts and articles on the Internet already that explain it far better than I ever could.

A little while back I created a blog post on calculating the date of Easter when given a particular year, that post is here: http://blog.sqlconcepts.co.nz/2011/08/i-like-chocolate-eggswhen-is-it-easter.html

The function works fine from a logical point of view, however, due to the type of calculations going on it doesn't perform that well.  If you use to populate a date dimension (or other static list of dates) its more than adequate for the job but if you are calling it frequently you'll want something with less overhead and more speed.

For that reason I set about turning it into a C# CLR function.  Another reason for doing this was to get me back into the CLR and C# coding as I've only ever done a few pieces here and there.  I need the practice!

Monday, 7 November 2011

Delete files by age using FORFILES

Ever needed to tidy up some old backups? Perhaps wanted to delete a bunch of files by age?

If you're using standard SQL tools for local backups and old file tidy ups you can get into the situation I just faced when the files are not tidied up in time. Your disk fills up, your backup and cleanup task starts failing and the world comes crashing down.

Sure you can manually delete old files, but when there's lots of files in lots of directories and no PowerShell you'll want something to help you. Enter FORFILES.EXE

I ran the following to tidy up files older than 2 days in the backup directory:

forfiles /m *.* /s /d -2 /c "cmd /c del @path"

That command will delete (/c "cmd /c del @path") all files (/m *.*) older than 2 days (/d -2) in the current folder and all its sub-directories (/s).

Update (2012-02-07)

I recommend you check out PowerShell for all your command line needs. I blogged about a PowerShell way to do this here: http://blog.sqlconcepts.co.nz/2012/02/deleting-files-by-age-with-powershell.html