Wednesday, May 30, 2007

declaring variables inside a loop in c#

interesting thread here: http://www.thescripts.com/forum/thread505814.html

in the past (c++) it was best practice to avoid declaring variables inside a loop; in c#, both of these compile to the same IL:

for (int i = 0; i < 100; i++)
{
StringBuilder sb = new StringBuilder();
}

StringBuilder sb;

for (int i = 0; i < 100; i++)
{
sb = new StringBuilder();
}
and since the first example has the StringBuilder scoped to the for loop, you can’t accidentally use it after this code block executes. so: declare variables inside loops in c#.

Wednesday, May 16, 2007

devscovery 2007 5/9 - 5/11

this was a great conference run by Wintellect, a training/consulting company that works very closely with msft on product development. the speakers were Wintellect’s technical leads (jeff richter, john robbins, jeff prosise, etc.), who are the authors of some of the Microsoft Press books we read, and all were excellent. topics covered included some of what’s in store for us in the near future (wpf, wcf, silverlight) and some stuff we can start taking advantage of right now (asp.net ajax extensions, asynchronous programming, debugging). the c# 3.0 language enhancements are very interesting; lots of syntactic shortcuts to reduce what you have to type that end up making things look very javascript-looking.

wpf (part of .NET 3.0)

  • does not replace Winforms; for quick development of standard GUI, not-so-flashy desktop apps, Winforms will still have a place
  • page layout defined in XAML
    o easy to exploit graphics and animation compabilities
    o nearly every property of every object is animatable
    o binding allows the property of an object to change as the property of another object changes
  • similar structure to ASP.NET in that .xaml page containing markup has .xaml.cs code-behind page containing coding logic

BETA - silverlight (formerly WPF/E)

  • cross-platform browser plugin
  • "silverlight CLR" provides sandboxed capabilities (limited access to file system, networking, etc.)
  • allows you to write almost-identical xaml pages as wpf apps
  • v1.0 currently in beta
    o requires you to interact with xaml objects via javascript
    o v1.1 (currently in alpha) allows
    o allows you to interact with xaml object using C#
    o will likely displace Flash in Microsoft development shops

CTP - c# 3.0 (next version of the c# language)

  • in general, the language is becoming more functional looking (LISP-like), and most new constructs are easy-to-write shortcuts to the actual operations that are rewritten at compile-time
    § implicitly typed local variables - allow you to say:

    var x = new String("hi");

    instead of:

    string x = new string("hi");

    which is particularly useful when you are instantiating a complex type
  • extension methods - you can define a static method in a static class with new "this" keyword in parameters as follows:

    public static int StringToIntFunction(this string s) {...do something...}

    and then this function is available in instance-syntax to any String object, so you can say:

    string s = "hi";int i = s.StringToIntFunction();
  • anonymous types - compiler will generate a type name for you, with specified property accessors:

    var someEmployee = new { Name = "MyName", Title = "Developer" }

    and you can then call someEmployee.Name and someEmployee.Title as if you had created an Employee class and specified these properties
  • LINQ (Language INtegrated Queries) - new constructs allow for T-SQL-like syntax for IEnumerable interactions:int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };var lowNums = from n in numbers where n < products =" GetProductList();var" productinfos =" from" price =" p.UnitPrice};" href="http://download.microsoft.com/download/5/8/6/5868081c-68aa-40de-9a45-a3803d8134b8/csharp_3.0_specification.doc">http://download.microsoft.com/download/5/8/6/5868081c-68aa-40de-9a45-a3803d8134b8/csharp_3.0_specification.doc

asp.net ajax extensions - http://ajax.asp.net/

  • we can and should start using this NOW! if you see something that will speed development time or provide richer user experience, use it!
  • the asp.net ajax control toolkit contains lots of widgets that use the ajax extensions and is open-sourced
  • tag allows you to specify path the your webservice; this generates a javascript-callable wrapper for your webservice methods, so you get real nice webservice.methodName() syntax on webservice calls
  • JSON (JavaScript Object Notation), which is a much less-verbose SOAP alternative we should also be using already

threading and asynchronous programming model

  • as much as possible, use beginread()/endread() instead of read(), etc.
  • can create UI issues and is a bit more difficult model to program using (since everything is done with callbacks), but keeps threadpool lean and mean
  • use Interlocked class for thread-safe variable increment/decrement

debugging, unit testing, and perf tuning

  • VSTS provides almost all the necessary tools
  • UI automation testing in .NET 3.0 provides APIs for driving Winform and wpf apps
  • developers average 1 bug for every 10 lines of code; with unit testing and code-coverage averaging 80-90% coverage, ratio moves toward 1/1000
  • beware of finalizers (cause objects to be moved to GC finalizer queue, which is not cleaned until next GC run)

tools

  • BETA - Blend (formerly Expressions)
    o Flash-like IDE for creating xaml for animations
    o release versions due later this year
  • CTP – “Orcas” (next version of Visual Studio)
    o project types for Silverlight apps
    o intellisense for xaml and javascript
    o release versions due later this year
    o reflector – FREE - see c# code for .NET library
    o http://www.aisto.com/roeder/dotnet/
  • web development helper – FREE - firebug-like plugin for IE
    o http://projects.nikhilk.net/Projects/WebDevHelper.aspx
  • .net memory profiler - awesome tool to analyze .net memory usage
    o http://www.memprofiler.com/

Monday, May 14, 2007

cool way to use COALESCE to return comma-delimited list in one-to-many relationship

many times when you have a one-to-many relationship, you want to return a comma-delimited list of the “many” (for example, a list of all the subjects a tutor teaches). so typically your one-to-many join in your query would produce a result set such as {UserId, SubjectId}, and then you would parse this list in application code. a neat approach i saw an example of today is to use sql’s COALESCE function with a bit of string magic to get the same comma-delimited list directly from the database (COALESCE returns the first non-null expression among its arguments). so, if you wanted to return the list subjects that userId 1 teaches, you would have:

declare @SubjectList varchar(2048)

SELECT @SubjectList =
COALESCE(@SubjectList + ', ', '') + pg.GroupName

FROM TableGroups pg

SELECT IsNull(@SubjectList, '')


you can then put this logic in a UDF and call it from a select statement. for more details, see here: http://www.sqlteam.com/item.asp?ItemID=2368

Tuesday, May 8, 2007

coding standards

having standards makes it much easier to read code you didn’t write, so i do as much as i can to encourage adherence to the list here.

Definitions:
  • “UpperCamelCased” = capitalize first letter of each keyword (.NET style); do not use underscores)
  • “lowerCamelCased” = captialize first letter of each keyword except the first (java-style); do not user underscores
  • “Hungarian Notated” = prefix variable name with indicator of its type (i.e. string sMyString or strMyString)

C#: (in general, follow Microsoft .NET standards)

Functions

  • Public functions “UpperCamelCased”
  • Private functions “lowerCamelCased”
  • Parameters “UpperCamelCased”

Variables

  • Module-level variables begin “m_”
  • Local function variables either:
    o “lowerCamelCased”
    o Keywords separated with “_”
    o Prefixed with “tmp_”
    o “Hungarian Notated”

Enums

  • “UpperCamelCased”
  • Type name pluralized (i.e. “MyItems”, not “MyItem”)

Constants

  • “UPPER_CASE” (all capitalized; keywords separated with “_”)

For example:

public class MyClass
{

public const THIS_IS_MY_CONSTANT = 0;

public enum MyItems
{

None = 0
,SomeItem = 1
,AnotherItem = 2

}

private string m_MyVariable;

public void TellMeEverything(string Input1)
{

bool localVar = false;
bool local_var = false;
bool tmp_LocalVar = false;
bool bLocalVar = false;

}

private void tellYouNothing(string Input1)
{

}

}


Database Tables

  • Table names “UpperCamelCased_WithAnyExtras” (capitalize first letter of each key word and use a single “_” character as desired)
  • Column names “UpperCamelCased”
  • T-SQL keywords “UPPERCASE” (all capitalized)
  • T-SQL Datatypes “alllowercase” (do not capitalize any character)

For example:

CREATE TABLE Groups
(
GroupId int
,GroupName varchar(50)
)

Database Sprocs

  • Sproc names “UpperCamelCased”
  • Separate input parameters or returned columns with comma on line of subsequent parameter or column name
  • Clearly separate SELECT/FROM/WHERE or UPDATE/SET/WHERE clauses using carriage returns or tabs

For example:

CREATE PROCEDURE UpdateUser

@UserId int
,@StatusId int
,@
UpdateByUserId int


AS

UPDATE Users

SET StatusId = @StatusId
,ChangedBy = @UpdateByUserId

WHERE UserId = @UserId

GO