- If you think you need a region you really need a new class.
- If you are using a for loop in c# there is something wrong with your code.
- If you are using a foreach fix you code so you don't need it.
- If you think you want to use exceptions to control flow stop now and go to your room and think about what damage you may do.
- If you don't write a unit test it's not done.
Thursday, August 4, 2011
My Rules for Development
Wednesday, May 18, 2011
Some Fun With Linq.
public class section { public string Group { get; set; } public string Order { get; set; } public string Section { get; set; } public string Title { get; set; } } static void Main(string[] args) { var sec = new List<section>(); sec.Add(new section { Group = "1", Order = "1", Section = "1.1", Title = "one one" }); sec.Add(new section { Group = "1", Order = "3", Section = "1.3", Title = "one three" }); sec.Add(new section { Group = "1", Order = "2", Section = "1.2", Title = "one two" }); sec.Add(new section { Group = "2", Order = "1", Section = "2.1", Title = "two one" }); sec.Add(new section { Group = "2", Order = "2", Section = "2.2", Title = "two two" }); sec.Add(new section { Group = "3", Order = "1", Section = "3.1", Title = "three one" }); //creates a diction with all the sections in order grouped by group. var jmp = sec.OrderBy(j => j.Order).GroupBy(c => c.Group).ToDictionary(c => c.Key, d => d.ToList()); //gets one section form a section var wantedSection = sec.FirstOrDefault(c => c.Section == "2.2"); //return null if no iteem found //gets one section in the order you set in order var sectionOne = sec.Where(c => c.Group == "1").OrderBy(c => c.Order).ToList(); }
Wednesday, February 16, 2011
Date Time you are doing it wrong again.
System.Text.StringBuilder mxIsDt = new System.Text.StringBuilder(); mxIsDt.Append(DateTime.Now.Year.ToString()); mxIsDt.Append(DateTime.Now.Month.ToString()); mxIsDt.Append(DateTime.Now.Day.ToString()); mxIsDt.Append(DateTime.Now.Hour.ToString()); mxIsDt.Append(DateTime.Now.Minute.ToString()); mxIsDt.Append(DateTime.Now.Second.ToString()); string mxDate = mxIsDt.ToString();
Which converts into around 70 lines of IL and takes just 19160 milliseconds to complete. Now my question is why wouldn't you use ToString on DateTime to do this.
var d = DateTime.Now.ToString("yyyyMdhms");Which takes 8 lines or less in IL and takes 1213 milliseconds to complete. It's way less code easier to maintain and examples on how to do this can be found all over the web.
Wednesday, June 30, 2010
Proper Use of Properties
Today we are talking about properties. How to use them and more importantly how not to use them.
When dealing with properties like they wore written before 3.5 you should have some thing that looks like this.
private string _test;
public string Test
{
get { return this._test; }
set { this._test = value; }
}
I love properties like this it’s only slightly less cool then using the 3.5 version of a property. The thing I don’t like about a property like this is when a programmer decides he doesn’t need to user the property and accesses the _test variable directly inside of the class to make a change. So like this
this._test = "some string";
I consider this bad programming. It makes maintaining code much more difficult and adds an unnecessary level of complexity. You have to look at two things now to see how a value gets set or modified. To do all the access through the property takes about the same amount of work and ends up in a much cleaner code file.
this.Test = "Some string";
Is actually one less key stroke and is so much easier to work with.