Showing posts with label PetPeev. Show all posts
Showing posts with label PetPeev. Show all posts

Thursday, August 4, 2011

My Rules for Development

  1. If you think you need a region you really need a new class.
  2. If you are using a for loop in c# there is something wrong with your code.
  3. If you are using a foreach fix you code so you don't need it.
  4. 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.
  5. If you don't write a unit test it's not done.

Wednesday, May 18, 2011

Some Fun With Linq.

A friend of mine was asking me how I search through dictionary that contain lists of objects with Linq. After I chatted with him for a bit. I realized I don't do that.  I have one huge list that I parse out with linq.  It makes my life easier.  Here is a code example of what I would use.


        public class section
        {
            public string Group { getset; }
            public string Order { getset; }
            public string Section { getset; }
            public string Title { getset; }
        }
 
        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.

The other day I reading the greatest developer fallacy or the wisest words you'll ever hear and then today I found some code that I think illustrated his point that learning it when you need it isn't always the way to go. This code looks very much that way.  Although I think a quick Google search would have found better code then this even if you didn't know what words to look for.


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.