Aside from it's memory efficient as compared to eagerly populating list: http://ideone.com/QxU7Sn
..it saves us a lot of boilerplate codes too, there was once in C#'s life that there's no yield return magic yet, we have to contend with iterator design pattern, and that entails a lot of boilerplates code: http://www.ienablemuch.com/2010/12/lazy-loading-fibonacci.html
Long and short of why yield return is so awesome:
http://www.ienablemuch.com/2012/08/use-components-that-uses-yield-return.html
Use yield return when returning IValidatableObect, or any list for that matter. There's no need to create a List<ValidationResult>
http://www.ienablemuch.com/2011/07/ivalidatableobject-client-side.html
"Simplicity can't be bought later, it must be earned from the start" -- DB
Friday, August 22, 2014
Thursday, August 21, 2014
Find changeset number by comment
Use VS commandline tool:
Start > Programs > Microsoft Visual Studio 2012 > Developer Tools > Developer Command Prompt for VS2012
How to find broken promises:

For some strange reason, the Visual Studio 2012 plugin Find Changeset By Comment has installation error when I add it on Visual Studio, so I have to use a commandline tool
So that's how I manage to find codes I can't remember the changeset number
That's also how I find the changesets of a code review when the engineer forgot to include the changeset numbers of their code
To automate that task install AutoHotkey, then include this script. #c means Windows+C
So when you press Windows+C this will appear:

The script then runs the TFS commandline tool. The VS plugin can't be installed on my dev machine, but at least I can find changeset anytime even Visual Studio is not open, so I got that goin' for me, which is nice
Start > Programs > Microsoft Visual Studio 2012 > Developer Tools > Developer Command Prompt for VS2012
How to find broken promises:

For some strange reason, the Visual Studio 2012 plugin Find Changeset By Comment has installation error when I add it on Visual Studio, so I have to use a commandline tool
So that's how I manage to find codes I can't remember the changeset number
That's also how I find the changesets of a code review when the engineer forgot to include the changeset numbers of their code
To automate that task install AutoHotkey, then include this script. #c means Windows+C
#c:: InputBox, UserInput, View Changeset List, Please enter a comment,,,,,,,,div.*by.*zero Run %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\VsDevCmd.bat" & tf history $/ /noprompt /recursive | findstr /i /r /c:"%UserInput%"" return
So when you press Windows+C this will appear:

The script then runs the TFS commandline tool. The VS plugin can't be installed on my dev machine, but at least I can find changeset anytime even Visual Studio is not open, so I got that goin' for me, which is nice
Wednesday, August 6, 2014
Programming without the programming language
I agree with Jon Skeet's assertion that it's better to know the idiomatic way to program with the programming language. There are cases though that using something fancy language operation like compounding a value to another variable, makes the solution look non-intuitive
I forgot how my old Fibonacci solution was intuitive to me:
Perhaps making a language-driven solution e.g., increment, makes something non-intuitive in the long run and hard to reason out
When I tried to plot the values of that old fibonacci solution, this is how a bit jumbled-looking the values are, read this from up to down, then left to right
Then when I wanted to recall that fibonacci code off the top of my head, sadly I can't, so I try solve it with a pen and a paper, following is what I came up with, and it's a little different from the old solution I've made before. This is one of few instances where not having eidetic or retentive memory is good, the brain is flushing out old memories and it is stimulated to be creative on coming up with new and elegant approaches. Read the following from up to down, then left to right
Pencil-and-paper solution:
The values' progression look symmetrical and it maps nicely to code too:
The only drawback of not having eidetic memory is your stock knowledge tend to decrease or get rusty. But who needs eidetic memory when you can store your knowledge to blog? Another advantage of having a blog is you can compare if your new approach improved
I forgot how my old Fibonacci solution was intuitive to me:
static IEnumerable<int> Fibonacci()
{
int a = 0, b = 1;
for(;;)
{
yield return a;
int n = a;
a += b;
b = n;
}
}
Perhaps making a language-driven solution e.g., increment, makes something non-intuitive in the long run and hard to reason out
When I tried to plot the values of that old fibonacci solution, this is how a bit jumbled-looking the values are, read this from up to down, then left to right
A 0 1 1 2 3 5 B 1 0 1 1 2 3 Y 0 1 1 2 3 5 N 0 1 1 2 3 5
Then when I wanted to recall that fibonacci code off the top of my head, sadly I can't, so I try solve it with a pen and a paper, following is what I came up with, and it's a little different from the old solution I've made before. This is one of few instances where not having eidetic or retentive memory is good, the brain is flushing out old memories and it is stimulated to be creative on coming up with new and elegant approaches. Read the following from up to down, then left to right
Pencil-and-paper solution:
A 0 1 1 2 3 5 B 1 1 2 3 5 8 Y 0 1 1 2 3 5 N 1 2 3 5 8 13
The values' progression look symmetrical and it maps nicely to code too:
static IEnumerable<int> Fibonacci()
{
int a = 0, b = 1;
for(;;)
{
yield return a;
int n = a + b;
a = b;
b = n;
}
}
The only drawback of not having eidetic memory is your stock knowledge tend to decrease or get rusty. But who needs eidetic memory when you can store your knowledge to blog? Another advantage of having a blog is you can compare if your new approach improved
Wednesday, July 23, 2014
Making stringly-typed code palatable via implicit operator
Bad programmers worry about the code. Good programmers worry about data structures and their relationships.-- http://programmers.stackexchange.com/questions/163185/torvalds-quote-about-good-programmer
Smart data structures and dumb code works a lot better than the other way around.-- Eric S. Raymond
Strongly-typed code is all the rage now, but some of us still manages to stash various information to string instead of structuring the information to a class.
But if you are in an unfortunate codebase and it has a dumb data structure to begin with, e.g., it has stringly-typed data, at least make a smart data structure facade. Not only smart data structure is easy to code on, it can make the codes that depends on the smart data structure very simple, smaller and easy to read.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Craft
{
class MainClass
{
public static void Main (string[] args)
{
var list = new Dictionary<string,decimal> ();
list.Add ("1_VariablePayBasis", 1337);
list.Add ("1_DecisionAmount", 168);
list.Add ("3_BasePay", 5201314);
// Dumb data structure. Smart code.
foreach (var kv in list) {
// boilerplate codes. a.k.a. Smart Code
string[] strings = kv.Key.Split('_');
int pk = Convert.ToInt32(strings [0]);
string fieldName = strings [1];
Console.WriteLine ("{0} {1} - {2}", pk, fieldName, kv.Value);
}
Console.WriteLine();
// Smart data structure. Dumb code.
// via explicit casting, being explicit with implicit :-)
foreach (var fv in list.Select(x => (FieldValue)x)) {
Console.WriteLine ("{0} {1} - {2}", fv.PrimaryKey, fv.FieldName, fv.Value);
}
Console.WriteLine();
// Smart data structure. Dumb code.
// neat implicit! it still feel a bit explicit though. we can't use var here
foreach (FieldValue fv in list) {
Console.WriteLine ("{0} {1} - {2}", fv.PrimaryKey, fv.FieldName, fv.Value);
}
}
}
public class FieldValue
{
public int PrimaryKey { get; set; }
public string FieldName { get; set; }
public decimal Value { get; set; }
public static implicit operator FieldValue(KeyValuePair<string,decimal> kv)
{
string[] strings = kv.Key.Split('_');
int pk = Convert.ToInt32(strings [0]);
string fieldName = strings [1];
return new FieldValue { PrimaryKey = pk, FieldName = fieldName, Value = kv.Value };
}
}
}
Live Code https://dotnetfiddle.net/qX2UeA
Happy Coding! ツ
Sunday, July 20, 2014
protected internal
Anne Epstein has a great NHibernate article on making an entity with a composite primary key be lazy-loading-capable and cache-ready. Just forgot to hide the ORM low-level plumbing concerns (the composite primary key) from the domain model
Similar to AdventureWorks' EmployeePayHistory. Just use protected internal to hide that ORM low-level plumbing concern
So we will not make the mistake of saving our entity through those composite primary key:
Similar to AdventureWorks' EmployeePayHistory. Just use protected internal to hide that ORM low-level plumbing concern
public class PersonPayHistory
{
PersonPayHistoryCompositePK _pk = new PersonPayHistoryCompositePK();
protected internal PersonPayHistoryCompositePK PersonPayHistoryCompositePK
{
get { return _pk; }
set { _pk = value; }
}
Person _person;
public virtual Person Person
{
get { return _person; }
set
{
_person = value;
_pk.PersonId = _person.PersonId;
}
}
public virtual DateTime RateDate
{
get { return _pk.RateDate; }
set { _pk.RateDate = value; }
}
public virtual decimal Rate { get; set; }
}
So we will not make the mistake of saving our entity through those composite primary key:
var ph = new PersonPayHistory
{
Person = session.Load<Person>(1),
RateDate = DateTime.Today,
Rate = 1337M
};
session.Save (ph);
session.Flush ();
Subscribe to:
Posts (Atom)