Author Topic: 7 Common Mistakes Made By C# Programmers  (Read 3785 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
7 Common Mistakes Made By C# Programmers
« on: September 13, 2010, 09:27:46 PM »
7 Common Mistakes Made By C# Programmers

http://www.codeguru.com/csharp/csharp/cs_misc/article.php/c17911__1/7-Common-Mistakes-Made-By-C-Programmers.htm

Article came in on my news feed, thought it worth passing on unread.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: 7 Common Mistakes Made By C# Programmers
« Reply #1 on: September 14, 2010, 12:05:40 AM »
Good article, I learned a better way use For and Foreach
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: 7 Common Mistakes Made By C# Programmers
« Reply #2 on: September 14, 2010, 02:37:01 AM »

yes David, that looks worthy of testing.

I notice he doesn't use either:

watch.Reset();
or
watch = Stopwatch.StartNew();
 
 ... so the timer is not restarted

Perhaps it should have been tested with a variable quantum of elements to better demonstrate his hypothesis.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: 7 Common Mistakes Made By C# Programmers
« Reply #3 on: September 14, 2010, 05:27:31 AM »

I had a little play ...
Code: [Select]

// CodeHimBelonga kdub@theSwamp 20100914

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data;

namespace ForeachTester
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            DataColumn col1 = new DataColumn("Id", typeof(int));
            col1.AutoIncrement = true;
            DataColumn col2 = new DataColumn("Name", typeof(string));
            DataColumn col3 = new DataColumn("TimeStamp", typeof(DateTime));           

            dt.Columns.AddRange(new DataColumn[] { col1, col2, col3 });

            dt.Rows.Add(null, "John", DateTime.Now   );
            dt.Rows.Add(null, "George", DateTime.Now );
            dt.Rows.Add(null, "Paul", DateTime.Now   );
            dt.Rows.Add(null, "Ringo", DateTime.Now  );

            Stopwatch watch = new Stopwatch();

            //For loop
            watch.Start();
            for (int count = 0; count < dt.Rows.Count; count++)
            {
                dt.Rows[count]["Name"] = "Modified in For";
            }
            watch.Stop();

            // The smallest unit of time is the tick, which is equal to 100 nanoseconds.
            Console.WriteLine("Ticks taken in For loop: {0}", watch.Elapsed.Ticks);
            watch.Reset();

            //Foreach loop
            watch.Start();
            foreach (DataRow row in dt.Rows)
            {
                row["Name"] = "Modified in ForEach";
            }
            watch.Stop();
            Console.WriteLine("Ticks taken in For Each loop: {0 }", watch.Elapsed.Ticks);

            Console.ReadKey();
        }
    }
}

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: 7 Common Mistakes Made By C# Programmers
« Reply #4 on: September 14, 2010, 05:33:57 AM »
Just to show that you can sometimes believe some parts of some things that you read ...
<except for : see next post>

Code: [Select]
// CodeHimBelonga kdub@theSwamp 20100914

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data;

namespace ForeachTester
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            DataColumn col1 = new DataColumn("Id", typeof(int));
            col1.AutoIncrement = true;
            DataColumn col2 = new DataColumn("Name", typeof(string));
            DataColumn col3 = new DataColumn("TimeStamp", typeof(DateTime));          

            dt.Columns.AddRange(new DataColumn[] { col1, col2, col3 });

            for (int count = 0; count < 1000 ; count++)
            {
                dt.Rows.Add(null, count.ToString(), DateTime.Now);
            }

            Stopwatch watch = new Stopwatch();

            //For loop
            watch.Start();
            for (int count = 0; count < dt.Rows.Count; count++)
            {
                dt.Rows[count]["Name"] = "Modified in For";
            }
            watch.Stop();

            // The smallest unit of time is the tick, which is equal to 100 nanoseconds.
            Console.WriteLine("Ticks taken in For loop: {0}", watch.Elapsed.Ticks);
            watch.Reset();

            //Foreach loop
            watch.Start();
            foreach (DataRow row in dt.Rows)
            {
                row["Name"] = "Modified in ForEach";
            }
            watch.Stop();
            Console.WriteLine("Ticks taken in For Each loop: {0 }", watch.Elapsed.Ticks);

            Console.ReadKey();
        }
    }
}
« Last Edit: September 14, 2010, 05:42:06 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: 7 Common Mistakes Made By C# Programmers
« Reply #5 on: September 14, 2010, 05:40:28 AM »

... but not always :-D
Code: [Select]
            for (int count = 0; count < 5000 ; count++)
            {
                dt.Rows.Add(null, count.ToString(), DateTime.Now);
            }
            Console.WriteLine("Row Count: {0 }", dt.Rows.Count);
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: 7 Common Mistakes Made By C# Programmers
« Reply #6 on: September 14, 2010, 06:26:00 AM »


so I s'pose No 8 is :
Failure to test presumptions , yes ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: 7 Common Mistakes Made By C# Programmers
« Reply #7 on: September 14, 2010, 10:52:56 AM »
WOW, what made it flipFlop over itself?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Bryco

  • Water Moccasin
  • Posts: 1883
Re: 7 Common Mistakes Made By C# Programmers
« Reply #8 on: September 14, 2010, 11:01:43 AM »
Running the code twice gives very different results. It must be retaining something in memory.
Putting the foreach above the for gives a slightly worse result.
 

Jeff H

  • Needs a day job
  • Posts: 6150
Re: 7 Common Mistakes Made By C# Programmers
« Reply #9 on: September 14, 2010, 01:27:45 PM »
 I believe you need to run the for and foreach in different programs because the Just in Time (JIT) realizes it is running over the same collections especially if you do it twice. And I thought I read somewhere foreach is faster for non-generic List and arrays. I can not remeber what I worked on yesterday so do not hold me to that.
I will try to find where I read this

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: 7 Common Mistakes Made By C# Programmers
« Reply #10 on: September 14, 2010, 06:16:31 PM »
ok, to try to even out the testing,

I've made duplicate DataTables and run each timed test twice

The results seem proportionately consistant.
Keep in mind this is timed in Ticks [of 100 nanoseconds]

Code: [Select]

// CodeHimBelonga kdub@theSwamp 20100915

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data;

namespace ForeachTester
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt1 = new DataTable();
            DataColumn col1 = new DataColumn("Id", typeof(int));
            col1.AutoIncrement = true;
            DataColumn col2 = new DataColumn("Name", typeof(string));
            DataColumn col3 = new DataColumn("TimeStamp", typeof(DateTime));          

            dt1.Columns.AddRange(new DataColumn[] { col1, col2, col3 });

            for (int count = 0; count < 5000 ; count++)
            {
                dt1.Rows.Add(null, count.ToString(), DateTime.Now);
            }
            Console.WriteLine("Row Count: {0 }", dt1.Rows.Count);
            
            DataTable dt2;
            dt2 = dt1.Copy();

            Stopwatch watch = new Stopwatch();

            //For loop
            watch.Start();
            for (int count = 0; count < dt2.Rows.Count; count++)
            {
                dt2.Rows[count]["Name"] = "Modified in For";
            }
            watch.Stop();
            // The smallest unit of time is the tick, which is equal to 100 nanoseconds.
            Console.WriteLine("Ticks taken in For loop DT2    : {0}", watch.Elapsed.Ticks);
            watch.Reset();

            //Foreach loop
            watch.Start();
            foreach (DataRow row in dt1.Rows)
            {
                row["Name"] = "Modified in ForEach";
            }
            watch.Stop();
            Console.WriteLine("Ticks taken in ForEach loop DT1: {0 }", watch.Elapsed.Ticks);
            watch.Reset();

            //For loop
            watch.Start();
            for (int count = 0; count < dt2.Rows.Count; count++)
            {
                dt2.Rows[count]["Name"] = "Modified in For";
            }
            watch.Stop();
            // The smallest unit of time is the tick, which is equal to 100 nanoseconds.
            Console.WriteLine("Ticks taken in For loop DT2    : {0}", watch.Elapsed.Ticks);
            watch.Reset();

            //Foreach loop
            watch.Start();
            foreach (DataRow row in dt1.Rows)
            {
                row["Name"] = "Modified in ForEach";
            }
            watch.Stop();
            Console.WriteLine("Ticks taken in ForEach loop DT1: {0 }", watch.Elapsed.Ticks);
            watch.Reset();


            Console.ReadKey();
        }
    }
}
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.