Blog Stats
  • Posts - 148
  • Articles - 0
  • Comments - 92
  • Trackbacks - 14

 

Coding like writing a book

If you write better code then it's just easier to read it.
Just have a look at below examples and you will know what I am talking about.

Code Sentence that appears in my mind
public int GetTotalCost(DataTable products)
{
    int totalCost = 0;
    foreach (DataRow product in products.Rows)
    {
        totalCost += (int)product["Price"];
    }
    return totalCost;
}
For each product that is in products
get its price and add it to the total cost.
public int GetTC(DataTable dt)
{
    int tc = 0;
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        tc += (int)dt.Rows[i]["Price"];
    }
    return tc;
}
For each i in range from 0 to dt.Rows.Count – 1 get ith
element of
dt.Rows and get its price. Then add the price to tc.

Using OR mapping tools we can even replace DataTable with ProductCollection and DataRow with Product.

Comments have been closed on this topic.
 

 

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
Copyright © Pawel Pabich