luni, 21 decembrie 2009

Bianca - decembrie 2009 (aproape 10 luni)

Sunt un vierme. Asa imi zice mama. Cum imi da pampersul jos, cum o iau de-a sanatoasa, de-a busilea, prin pat. Atunci, trebuie, neaparat sa ma agat de orice, sa trag de toate lucrurile care imi ies in cale. Distractia se termina, cand mama imi pune pampersul cel nou, curatel.
Si tot viermisor sunt cand trag de toate firele din casa. Bag in gura si rup orice este facut din hartie.

Sunt o caragioasa. Pentru ca ingan. Bat din palme. Suflu pe nas odata cu tata. Plescai cand tata imi explica cum se mesteca. Ma dau pe parchet cu premergatorul. Vai peretilor din casa. Mananc singura biscutii, fructe, rod morcovi sau carnita.

Sunt o mofturoasa pentru ca fac scandal daca nu mi se face pe plac. Daca nu sunt lasata sa bag balute in telefoane sau in ceasuri, sa rod cablurile de la calculator sau sa stau pe masa de la calculator. Si de o bucate de vreme, adorm doar daca am suzeta. Problema mare.

Sunt un arici. Pentru ca ma imi plac reclamele asa de mult incat nimeni nu mai poate discuta cu mine in timp ce le vizionez.

Si musc cu putere. In primul rand pe Tigrisor. Apoi, paiul de la canita, degetele celor din jur si multe altele. Am, de toti, opt dintisori.

Si mai am opt, opt kilograme. Si aproape saptezeci de centimetri.

Mancarea preferata: branza cu smantana si cu mamaliguta. Yami, yam. Mananc si dovlegel, spanac, ficatei, carnita de pui, etc.

Beau cei si suculet singurica. Cu paiul din canita mea.
Am mingiute multe, de toate culorile. Le bag in gura si scot diferite zgomote. Le mai si bat cap in cap.

Cu baita, ne-am mutat in cada. Acolo bat apa cu palmutele, ma joc cu dusul si cu jucariile plutitoare.

A venit si iarna. A nins frumos afara. Mama ma imbraca gros cand iesim la aer. Nu-mi prea convine. Cizmuletele sau bocanceii sunt cam grei. Nu prea pot sa fug asa de repede cat asa dori eu. Si cam imi curge nasucul uneori. Dar m-am inzdravenit pana acuma destul de repede. Doar cu homeopate. Deci, sunt rezistenta si luptatoare.

Mami si tati au impodobit bradul. Multe luminite, globulete, beteala... Numai bune de tras de ele si, evident, de bagat in gura.

Dau cu piciorul in mingiute si fug dupa ele. Bineinteles, tinuta de manute, fie de tata, fie de mama. Joc prinselea cu tata: eu in premergator, tata pe jos. Si sunt mandra cand prind lucruri care cred ca nu am voie sa le iau. Dar, totusi le iau. Si-mi mai place sa ma care tata in spate. Daca mai si topaie, distractia e garantata.

Sunt destul de vorbareata. Stiu sa zic ma-ma, ta-ta-ta, ca, na-na-na si multe alte zgomote. Ma fac inteleasa, de obicei.

Ne vedem si luna viitoare sa va spun cum mai este.

luni, 14 decembrie 2009

[Design Pattern] Bridge Pattern

Bridge Pattern:

The contract can be used as a bridge within other classes, and it allows other classes to consume the concrete implementer without knowing anything about the details.

- separates the contract and implementation in two independent areas.
- use interface in C# to build contracts.
- contract will only provide the functionality.
- contract maintains a reference to an object of type Implementor
- the implementor defines higher-level operations. The implementor's interface doesn't have to correspond exactly to contract's interface

C# Sample:

















/// <summary>
/// ICommunicator is an interface for classes which want to be able to communicate to an collection of <see cref="IImplemetorContract"/>.
/// It has a list of methods that acts on the list of IImplemetorContract objects.
/// </summary>
public interface ICommunicator
{
List<IImplemetorContract> ImplementorsContract { get; }

string GetValues(DateTime i_date);
string GetFullName(int i_index);
void StartGetValues(int i_interval);
void StopGetValues();
}

/// <summary>
/// IImplemetorContract objects act as a bridge to let ICommnucator object consume it.
/// ICommnucator has alist of IImplemetorContract objects with which communicates.
/// It has a list of method which each IImplemetorContract class should implemnt and which should be used by ICommnucator methods
/// </summary>
public interface IImplemetorContract
{
object GetValue(DateTime i_dateTime);
}

/// <summary>
/// A more specific implementator for a Stat
/// </summary>
public interface IStat : IImplemetorContract
{
string Name { get; }
string Caption { get; }
Type Type { get; }
}

/// <summary>
/// Common implementation for all Communicators
/// </summary>
public abstract class AbstractCommunicator
{
private List<IImplemetorContract> m_listImplementorsContract = new List<IImplemetorContract>();
protected Timer _timer = null;
public virtual string GetValues(DateTime i_date)
{
Dictionary<IImplemetorContract, object> _setStatValues = new Dictionary<IImplemetorContract, object>();
StringBuilder _sb = new StringBuilder();
_sb.AppendLine("Communicator: " + GetType().Name + ". Values at time: " + i_date.ToLongTimeString());
foreach (IImplemetorContract _implementor in ImplementorsContract)
{
object _value = _implementor.GetValue(i_date);
_sb.AppendLine(GetFullName(ImplementorsContract.IndexOf(_implementor)) + "\t\t" + _value);
}
return _sb.ToString();
}

public abstract string GetFullName(int i_index);

public List<IImplemetorContract> ImplementorsContract
{
get { return m_listImplementorsContract; }
}

public virtual void StartGetValues(int i_interval)
{
_timer = new Timer(GetValuesNow, null, 1000 , i_interval);
}

public virtual void StopGetValues()
{
if (_timer == null) return;
_timer.Change(Timeout.Infinite, Timeout.Infinite);
_timer.Dispose();
}

protected virtual void GetValuesNow(Object statusInfo)
{
Console.WriteLine(GetValues(DateTime.Now));
}

}

public class CSVCommunicator : AbstractCommunicator, ICommunicator
{
#region ICommunicator Members

public override string GetFullName(int i_index)
{
if (i_index < 0 || i_index > ImplementorsContract.Count - 1)
{
throw new IndexOutOfRangeException();
}
if (ImplementorsContract[i_index] is IStat)
{
return ((IStat)ImplementorsContract[i_index]).Name + "/" + ((IStat)ImplementorsContract[i_index]).Caption;
}
else
{
return ImplementorsContract[i_index].ToString();
}
}

public override void StartGetValues(int i_interval)
{
Console.WriteLine("CSV is writing values");
base.StartGetValues(i_interval);
}

public override void StopGetValues()
{
Console.WriteLine("CSV is stopping writing values");
base.StopGetValues();
}
#endregion
}

public class GuiCommunicator : AbstractCommunicator, ICommunicator
{
#region ICommunicator Members

public override string GetFullName(int i_index)
{
if (i_index < 0 || i_index > ImplementorsContract.Count - 1)
{
throw new IndexOutOfRangeException();
}
if (ImplementorsContract[i_index] is IStat)
{
return ((IStat)ImplementorsContract[i_index]).Caption;
}
else
{
return ImplementorsContract[i_index].ToString();
}
}

public override void StartGetValues(int i_interval)
{
Console.WriteLine("GUI is displaying values");
base.StartGetValues(i_interval);
}

public override void StopGetValues()
{
Console.WriteLine("GUI is stopping displaying values");
base.StopGetValues();
}
#endregion
}
public class DiskFreeSpaceStat : IStat
{
#region IStat Members

public string Name
{
get { return "DiskFreeSpace"; }
}

public string Caption
{
get { return "C Drive Disk Free Space"; }
}

public Type Type
{
get { return typeof(int); }
}

#endregion

#region IImplemetorContract Members

public object GetValue(DateTime i_dateTime)
{
ManagementObject _disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
_disk.Get();
return _disk["FreeSpace"];
}

#endregion
}

public class DiskLabelStat : IStat
{
#region IStat Members

public string Name
{
get { return "DiskLabel"; }
}

public string Caption
{
get { return "C: Drive Disk Label"; }
}

public Type Type
{
get { return typeof(int); }
}

#endregion

#region IImplemetorContract Members

public object GetValue(DateTime i_dateTime)
{
ManagementObject _disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
_disk.Get();
return _disk["Name"];

}

#endregion

}

public class DiskSizeStat : IStat
{
#region IStat Members

public string Name
{
get { return "DiskSpace"; }
}

public string Caption
{
get { return "C: Drive Disk Space"; }
}

public Type Type
{
get { return typeof(int); }
}

#endregion

#region IImplemetorContract Members

public object GetValue(DateTime i_dateTime)
{
ManagementObject _disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
_disk.Get();
return _disk["Size"];

}

#endregion
}

class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
//create communicators
CSVCommunicator CSV = new CSVCommunicator();
GuiCommunicator GUI = new GuiCommunicator();

//create implementors contract
DiskFreeSpaceStat _statFreeSpace = new DiskFreeSpaceStat();
DiskLabelStat _statLabel = new DiskLabelStat();
DiskSizeStat _statDiskSize = new DiskSizeStat();

//CSV communicator is talking with _statDiskSize and _statFreeSpace
Console.WriteLine("\t\tCSV");
CSV.ImplementorsContract.Add(_statDiskSize);
CSV.ImplementorsContract.Add(_statFreeSpace);
Console.WriteLine("First set of values for CSV: \n" + CSV.GetValues(DateTime.Now));
Console.WriteLine();

//GUI communicator is talking with _statDiskSize, _statFreeSpace and _statFreeSpace
Console.WriteLine("\t\tGUI");
GUI.ImplementorsContract.Add(_statDiskSize);
GUI.ImplementorsContract.Add(_statDiskSize);
GUI.ImplementorsContract.Add(_statFreeSpace);
Console.WriteLine("First set of values for GUI: \n" + GUI.GetValues(DateTime.Now));

Console.WriteLine();
Console.WriteLine();

Console.WriteLine("GUI present values at 2 s / CSV is writing value to .csv file at 4 s for 20 s");
GUI.StartGetValues(2000);
CSV.StartGetValues(4000);

AutoResetEvent _autoEvent = new AutoResetEvent(false);

Timer _timer = new Timer(delegate(Object _state)
{
GUI.StopGetValues();
CSV.StopGetValues();
Console.WriteLine("Finishing getting values ");
_autoEvent.Set();
}, null, 20000, 1000);


_autoEvent.WaitOne();
_timer.Dispose();

Console.WriteLine("Bye, bye now. Press ENTER to Finish.");
Console.ReadLine();

}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine(e.ToString());
}
}

vineri, 11 decembrie 2009

[C#] [4.0] Custom Dynamic Object - Code Sample




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.Dynamic;

namespace ConsoleApplication1
{
class MyDynamicClass : DynamicObject
{
private Dictionary<string, object> _properties = new Dictionary<string, object>();
//TryGetMember and TrySetMember methods implement the logic to read or write a member of the dynamic object.
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return _properties.TryGetValue(binder.Name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_properties[binder.Name] = value;
return true;
}

}

class Program
{

public static void UseDynamicObject(dynamic i_dynamic)
{
i_dynamic.Message = "Ala bala portocala!";

Console.WriteLine(i_dynamic.ToString());

i_dynamic.SayMessage1 = new Action(delegate {
Console.WriteLine(i_dynamic.Message);
Console.WriteLine("Say Message 1");
i_dynamic.ToString(); });
i_dynamic.SayMessage2 = new Action(
() =>
{
Console.WriteLine("SayMessage 2, the call SayMesage1");
i_dynamic.SayMessage1();
});
i_dynamic.SayMessage1();
i_dynamic.SayMessage2();
}

static void Main(string[] args)
{
dynamic _annonimus = new MyDynamicClass();
UseDynamicObject(_annonimus);
_annonimus.Message2 = "Hello from George";

Console.WriteLine(_annonimus.Message2 + ": " + _annonimus.Message);

Console.ReadLine();
}
}
}

joi, 10 decembrie 2009

[C#] Dynamically Generating an Assembly in Memory and using it

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Globalization;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.CSharp;

namespace GeorgeNamespace
{
public class Program
{
static void Main()
{

#region Dynamically Generating an Assembly in Memory
CodeCompileUnit _myProgram = new CodeCompileUnit();//create a program
CodeNamespace _ns = new CodeNamespace("MyNameSpace");//create a namespace
_ns.Imports.Add(new CodeNamespaceImport("System"));//<=> using System;
_myProgram.Namespaces.Add(_ns);//add namespace to program

CodeTypeDeclaration _myClass = new CodeTypeDeclaration("MyClass");//create class
_ns.Types.Add(_myClass);//add class to namespace

CodeEntryPointMethod _entryPointMethod = new CodeEntryPointMethod();//create an entry point method

_entryPointMethod.ReturnType = new CodeTypeReference(typeof(void));//set returned type

//Add Code to method
CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression("System.Console"), "WriteLine",
new CodePrimitiveExpression("Hello, World!"));

CodeMethodInvokeExpression cs2 = new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression("System.Console"), "WriteLine",
new CodePrimitiveExpression("Press ENTER to finish!"));

CodeMethodInvokeExpression cs3 = new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression("System.Console"), "ReadLine");

_entryPointMethod.Statements.Add(cs1);
_entryPointMethod.Statements.Add(cs2);
_entryPointMethod.Statements.Add(cs3);

_myClass.Members.Add(_entryPointMethod);//add entry method to class

CSharpCodeProvider _codeProvider = new CSharpCodeProvider();//create an instance of code generator/compiler
CompilerParameters _params = new CompilerParameters(null, "MyDynamicProgram", true);//set assemply name
_params.GenerateExecutable = true;//create an .exe

CompilerResults _results = _codeProvider.CompileAssemblyFromDom(_params, _myProgram); //compile the assembly

if (_results.Errors.Count > 0)//check for errors
{
foreach (CompilerError _ce in _results.Errors)
{
Console.WriteLine(string.Format("{0} : {1}", _ce.ErrorNumber, _ce.ErrorText));
}
}


//First sample of usage: generate a type on the fly/create a method and invoke it
if (_results.Errors.Count == 0 && _results.CompiledAssembly != null)
{
Type _objType = _results.CompiledAssembly.GetType( "MyNameSpace.MyClass" );
try
{
if (_objType != null)
{
MethodInfo _mi = _objType.GetMethod("Main");
_mi.Invoke(_objType, new object[] { });
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}

//Second Sample: use AppDomain to load the assembly and to load it.
//AppDomain _appDomain = AppDomain.CreateDomain("MyNewAppDomain");
//_appDomain.ExecuteAssembly(_results.PathToAssembly);
#endregion

#region Dynamic method
ClassSample _sampleDynamicMethod = new ClassSample();
_sampleDynamicMethod.InvokeDynamicMethod();
#endregion

Console.ReadLine();
}
}

public class ClassSample
{
public delegate void MyDelegate();
public void InvokeDynamicMethod()
{
DynamicMethod _dynamicMethod = new DynamicMethod("TempMethod" + Guid.NewGuid(), null, null);
ILGenerator _il = _dynamicMethod.GetILGenerator();

_il.Emit(OpCodes.Ldstr, "Hello, World! From George!");
MethodInfo _methodWriteLine = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });
_il.Emit(OpCodes.Call, _methodWriteLine);
_il.Emit(OpCodes.Ret);

//First way of calling the method
_dynamicMethod.Invoke(null, null);

//Second way of calling the method
MyDelegate _myDelegate = (MyDelegate)_dynamicMethod.CreateDelegate(typeof(MyDelegate));
_myDelegate();
}
}
}

vineri, 4 decembrie 2009

NUnit - basic

[TestFixture] attribute - applied to a class (has to be public and to have a default ctor)
[Test] attribute
- applied to methods indicating that it is a test method
- method returns void and has no parameter.
Assert class defines a collection of methods used to check the post-conditions
.AreEquals(object, object) -- the first parameter is an expected value and the second parameter is the actual value
[ExpectedException] attribute - this is the way to indicate that the test code is expecting an exception of a certain type; if such an exception is not thrown during the execution – the test will fail.
[Ignore] attribute - temporarily ignore a test method
[SetUp] attribute
- applied to methods
- it has void return type, no parameters
- put a common set of test objects to all test methods
- has the common initialization code

Steps to test with NUnit:
- Start the NUnit Gui
- Select the File->Open menu item, navigate to the location of your dll
- Click the Run button
- See the “Errors and Failures” panel
- GUI watches for the changes made to the test assemblies and updates itself when the structure of the test tree has changed – e.g. new test is added

miercuri, 2 decembrie 2009

[.NET] [4.0] Generic tuples classes

-> Microsoft .NET Framework introduces a new type called System.Tuple

Tuples are a group of properties that provide a means for you to easily group pieces of data together without having to write your own classes.
Or:
Tuple is a fixed-size collection of heterogeneously typed data.

-> You'll find tuples in languages such as Python and F#


Sample:
class Tuple
{
T1 Item1 { get; }
T2 Item2 { get; }
}

Tuple person = new Tuple ( "George", 28);
Console.WriteLine(person.Item1 + " " + person.Item2);

var _tuplePerson = Tuple.Create("George", 28);

-> This class allows you to group two different pieces of data of any type.

-> Tuples in .NET are considered immutable.
-> Like an array, a tuple has a fixed size that can't be changed once it has been created. Unlike an array, each element in a tuple may be a different type, and a tuple is able to guarantee strong typing for each element.
-> KeyValuePair is a sample of Tuple
-> All tuples are reference types (this may hit the performance).
-> Can have till 8 generic parameters
-> The last element of an eight-element tuple would be called "Rest" and it is required to be a tuple.
Tuple.Create(1, 2, 3, 4, 5, 6, 7, Tuple.Create(8));

-> Both Tuple and Array now implement these interfaces explicitly: IStructualEquatable and IStructuralComparable

One problem with tuples is your code becomes less descriptive, because tuples employ non-descript property names such a person.Item1 and person.Item2.

miercuri, 25 noiembrie 2009

[.NET] Memory management

Memory impacts on:
- performance (memory leaks, RAM Footprint)
- stability (temporary objects, memory leaks)
- scalability (RAM Footprint)
- other applications - sluggish performance (RAM Footprint, temporary objects)

.NET automatically manages the memory (creates objects onto managed memory heap, destroys object that are no more in use).
Object allocated when you use new.

Types of memory used by .NET:
- Stack:
  • local variables, method parameters, return values, and other temporary values are stored
  • is allocated on a per-thread basis
  • GC is not responsible for cleaning up the stack / GC is aware of references to objects stored on the stack.
- Unmanaged Heap
  • is used for runtime data structures, method tables, Microsoft intermediate language (MSIL), JITed code, and so forth
  • Managed code can allocate unmanaged heap memory directly by calling into unmanaged Win32® APIs or by instantiating COM objects.
- Managed Heap
  • is where managed objects are allocated and it is the domain of the garbage collector.
2 types of managed heaps:
- small object heap (SOH)
- large object heap (LOH)




Memory Management: Algorithms and Implementations in C/C++ (Windows Programming/Development)

marți, 24 noiembrie 2009

[C#] [GUI] User Control - Principles

Menus:
If you were creating a user interface in which the user chooses to display or arrange different windows, you would name the menu Windows.
The Documents menu is a drop-down menu listing all the active documents

Status Bar:
- the user can be informed about any long running activity that is occurring.

Steps to create a user control:
• Overrides for common System.Object methods (Equals() - check for ctrl id, GetHashCode(), and ToString())
• Event notification for the process of closing
public event EventHandler OnClose;

• Event notification when the current user’s principal object is changed
protected internal virtual void OnCurrentPrincipalChanged();
public event EventHandler CurrentPrincipalChanged;

GetIdValue() method -- returns a unique identifier for the instance of the user control.

Client application configuration file (App.config):
- Naming the file App.config is important. VS .NET will automatically copy the file into the appropriate bin directory, changing the name to match that of the program.
- you provide connection strings
- you can configure the data portal to communicate with a remote application server.
- you can manage the authentication:
- Remote Data Portal (with Web Services)

The main region of the form should contain a Panel control:
- resizing events can be handled more easily
- hosting process can be simplified

Use controls data binding capabilities.

[C#] [GUI] Dynamically loading a user control

Dynamically loading a user control:
1. Create the control.
2. Add the control to the form’s Controls collection.
3. register any event for that control
4. Set the control’s properties for size/position.
5. Make the control visible (Visible = true).
6. Set the control’s z-order (BringToFront()).

public partial class MyUserControl : UserControl {...};
UserControl m_Ctrl = new MyUserControl();
m_partCtrl.OnClosed += new EventHandler(CloseUserControl);
PanelMain.Controls.Add(part);
m_Ctrl.Dock = DockStyle.Fill;
m_Ctrl.Visible = true;
m_Ctrl.BringToFront();
this.Text = "Focused window - " + m_Ctrl.ToString();


Removing User Controls
private void CloseUserControl(object sender, EventArgs e)
{
MyUserControl _Ctrl= (MyUserControl)sender;
_Ctrl.CloseWinPart -= new EventHandler(CloseUserControl);
_Ctrl.Visible = false;
MainPanel.Controls.Remove(_Ctrl);
_Ctrl.Dispose();
this.Text = "Main Form";
}

!!! - failing to unhook events can cause memory leaks

luni, 23 noiembrie 2009

[c#] C# 3.0 Features


Implicitly Typed Arrays
With respect to arrays in C# 2.0, only the length could be inferred from the simple form of array initializer. Implicit typing has since been extended to arrays so that both the element type and the length are inferred from the expression, as in:

var GroupsOfStats = new[] {
new {Name = "Stat Group 1";
Stats = new [] { "Rates Rx", "Rates Tx", "First Timestamp"} ,
new {Name = "Stat Group 2";
Stats = new [] { "Rates Rx", "Rates Tx", "Last Timestamp", "Loss%"}
};

This would create a new array of a type that has two rows (row 0 and row 1). Each element is an object with two fields, Name and Stats. Members itself is an array with a different length in each case.

Object and Collection Initializers
Initializers specify values for fields or properties of objects or collections. Examples of initializers include:
Point p = new Point {X = 0, Y = 1};
List m_digits = new List { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
In C# 2.0, initialization syntax was valid only for arrays.


Anonymous Types
Anonymous types are created from object initializers. An anonymous type is a nameless class type that inherits directly from the supertype object. The members of an anonymous type are a sequence of read-only properties inferred from the anonymous object initializer used to create an instance of the type. By nesting new declarations, an initializer can specify values for an entire collection of objects. For example, the anonymous type created by this declaration:
var GroupsOfStats = new[] {
new {Name = "Stat Group 1";
Stats = new [] { "Rates Rx", "Rates Tx", "First Timestamp"} ,
new {Name = "Stat Group 2";
Stats = new [] { "Rates Rx", "Rates Tx", "Last Timestamp", "Loss%"}
};
would have two properties, Name and Stats, with Members being an array of strings. The types would have internal names and would be compatible with any other type of the same structure.
Automatic get properties are created for the listed members, so we can access the value of group.Name. There is no set property.

Lambda Expressions
In fact, the lambda calculus – a very simple language where everything is expressed in terms of functions.
A lambda expression simply defines an anonymous function. A function is something that takes one or more parameters (just as a method does) and uses them in computing some value. That value becomes the return value for the function. In C# 3, the "=>" syntax is used to write a lambda expression. You place the parameters to the left of the arrow and the expression to compute to the right.

Examples:
x => x + 1
(x, y) => x * y
() => new Beer()

(x, y) => {
var result = x + y;
return result;}

var FindTags = new Regex(@"\[(/?)(\w+)\]");
string Output = FindTags.Replace(ToRender,
m => AcceptedTags.Contains(m.Groups[2].Value) ?         
"<" + m.Groups[1].Value + m.Groups[2].Value + ">" :
m.Value);

One difference you may have spotted between lambda expressions and the original anonymous method syntax is the absence of types on the
parameters. You actually can write the types in if you wish:

(int x, int y) => x + y


[DesignPatterns] Flyweight Pattern

The Flyweight pattern promotes an efficient way to share common information present in small objects that occur in a system in large numbers. Helps reduce storage requirements when many values are duplicated.

• The intrinsic state can be shared on a wide scale, minimizing storage requirements.
• The extrinsic state can be computed on the fly, trading computation for storage.

Flyweight pattern relies on being able to divide up the application’s state into three types:
- intrinsicState resides in the Flyweight objects
- Flyweight class implements an IFlyweight interface, which specifies the operations upon which the rest of the system relies. Client maintains the unSharedState as well as a dictionary of all the Flyweights.
- extrinsicState does not appear in the system as such; it is meant to be computed at runtime for each instrinsicState instance, as required.

FlyweightFactory - Creates and manages unique Flyweight objects
Flyweight - Stores intrinsic state that is shareable among all objects (they store only a simple representation of a bigger object. for example, for a image thay keep only a Thumbnai).

Sample:
new Bitmap(filename). GetThumbnailImage(100, 100, null, new IntPtr( ));

public interface IFlyweight {
void Load (string filename);
void Display (PaintEventArgs e, int row, int col); }

Use:
• There is a very large number of objects (thousands) that may not fit in memory.
• Most of the state can be kept on disk or calculated at runtime.
• The remaining state can be factored into a much smaller number of objects with shared state.


[DesignPatterns] Composite Pattern

The Composite pattern arranges structured hierarchies so that single components and groups of components can be treated in the same way.
It has to deal with two types: Components and Composites of those components. Composite objects consist of Components.

Components:
- IComponent - default behavior for all object used in composition
- Component
- Composite - Implements the operations as applicable to composite objects. The Composite keeps as a list a local structure that consists of Components and Composites

Sample:
public interface IComponent {
void Add(IComponent c);
IComponent Remove(T s);
IComponent Find(T s);
string Display(int depth);
T Item {get; set;} }
// The Component
public class Component : IComponent {
public T Name {get; set;}
public Component (T name) { Name = name; }
public void Add(IComponent c) { Console.WriteLine("Cannot add to an item"); }
public IComponent Remove(T s) { Console.WriteLine("Cannot remove directly"); return this; }
public string Display(int depth) { return new String('-', depth) + Name+"\n"; }
public IComponent Find (T s) { if (s.Equals(Name)) return this; else return null; } }

// The Composite
public class Composite : IComponent {
List > list;
public T Name {get; set;}
public Composite (T name) { Name = name;
list = new List > ( ); }
public void Add(IComponent c) { list.Add(c); }
Component holder=null;
// Finds the item from a particular point in the structure
// and returns the composite from which it was removed
// If not found, return the point as given
public IComponent Remove(T s) { holder = this;
IComponent p = holder.Find(s);
if (holder!=null) { (holder as Composite).list.Remove(p); return holder; }
else return this; }
// Recursively looks for an item
// Returns its reference or else null
public IComponent Find (T s) { holder = this;
if (Name.Equals(s)) return this;
IComponent found=null;
foreach (IComponent c in list) {
found = c.Find(s);
if (found!=null) break; }
return found; }
// Displays items in a format indicating their level in the composite structure
public string Display(int depth) { StringBuilder s = new StringBuilder(new String('-', depth));
s.Append("Set "+ Name + " length :" + list.Count + "\n");
foreach (IComponent component in list) { s.Append(component.Display(depth + 2)); }
return s.ToString( ); } }
}

duminică, 22 noiembrie 2009

[DesignPatterns] Proxy Pattern

The Proxy pattern supports objects that control the creation of and access to other objects.
The proxy is often a small (public) object that stands in for a more complex (private) object that is activated once certain circumstances are clear.

Players:
ISubject - A common interface for subjects and proxies that enables them to be used interchangeably
Subject - The class that a proxy represents
Proxy - A class that creates, controls, enhances, and authenticates access to a Subject. Each Proxy object maintains a reference to a Subject
Request - An operation on the Subject that is routed via a proxy

Types of proxy:
Virtual proxies - Hands the creation of an object over to another object (useful if the creation process might be slow or might prove unnecessary)
Authentication proxies - Checks that the access permissions for a request are correct
Remote proxies - Encodes requests and send them across a network
Smart proxies - Adds to or change requests before sending them on

• Delaying the creation of a rich environment (virtual proxy)
• Logging in users (authentication proxy)
• Sending requests across the network (remote proxy)
• Performing actions on friends’ books (smart proxy)

Sample:

public interface IStat { string RequestValue(); }
public Stat : IStat {
public string Value { get; set; };
public string Name {get; set; };
public string RequestValue() { return this.Value} }

public class ProtectionProxy : IStat {
Stat m_stat;
string m_password = "1234";
public string Authenticate (string i_supplied)
{ if (i_supplied != m_password)
return "Protection Proxy: No access";
else
m_stat = new Subject( );
return "Protection Proxy: Authenticated";}

public string RequestValue( ) {
if (m_stat==null)
return "Protection Proxy: Authenticate first";
else return "Protection Proxy: Call to "+ m_stat.RequestValue( );}
}

Uses:
- to classes that have sensitive data or slow operations.
- in image-drawing systems
- any client/server system running on the .NET Framework

[DesignPatterns] Decorator Pattern

The role of the Decorator pattern is to provide a way of attaching new state and behavior to an object dynamically.
Decorator pattern takes an existing object and adds to it.

• The original object is unaware of any decorations.
• There is no one big feature-laden class with all the options in it.
• The decorations are independent of each other.
• The decorations can be composed together in a mix-and-match fashion.

Components:
- Client (Component and Decorator has a Client)
- IComponent
- Component
- Decorator - extends Component + usually has a ctor that take as parameter an IComponent

Stat _stat = new Stat();
Tag _tag = new Tag (_stat, "Chassis1", "Card2", "Port3" );

The Decorator pattern’s key feature is that it does not rely on inheritance for extending behavior.
• Implement any methods in the interface, changing the initial behavior of the component
• Add any new state and behavior
• Access any public members via the object passed at construction

Uses:
- Decorator pattern fits well in the graphics world.
- I/O APIs of C#:
System.IO.Stream
System.IO.BufferedStream
System.IO.FileStream
System.IO.MemoryStream
System.Net.Sockets.NetworkStream
System.Security.Cryptography.CryptoStream
- web browsers and other mobile applications
- controls in Windows app




vineri, 20 noiembrie 2009

[C#], [.NET Framework].NET Data Binding Interfaces















InterfacePurpose
IBindingListDefines data binding behaviors for collections, including change notification, sorting, and filtering (implemented by BindingList)
ICancelAddNewDefines data binding behaviors for collections to allow data binding to cancel addition of a new child object (implemented by BindingList)
IRaiseItemChangedEventsIndicates that a collection object will raise a ListChanged event to indicate that one of its child objects has raised a PropertyChanged event (implemented by BindingList)
IEditableObjectDefines single-level undo behavior for a business object, allowing the object to behave properly with in-place editing in a DataGridView
INotifyPropertyChangedDefines an event allowing an object to notify data binding when a
property has been changed
IDataErrorInfoDefines properties used by the DataGridView and ErrorProvider controls to automatically show descriptions of broken validation rules within the object

joi, 19 noiembrie 2009

[C#] Moving objects btw. layers

AppServer svr = (AppServer)Activator.GetObject( typeof(StatViewerServer), "http://myserver/myroot/statviewerserver.rem");
StatView _sv = svr.CreateStatView();
svr.UpdateStatView(_sv);

_______________________________
Another approach: create a similar class on the client

public class StatView
{

string m_statName;
eStatType m_statType;

public StatView()
{
AppServer svr = (AppServer)Activator.GetObject( typeof(StatViewerServer), "http://myserver/myroot/statviewerserver.rem");
object[] _svItems = svr.CreateStatViewDefaults();
m_statName = _svItems[0].ToString();
m_statType = (eStatType)_svItems[1];
}
}

________________
Another approach: use static methods
public StatView()
{
public static StatView CreateStatView()
{
AppServer svr = (AppServer)Activator.GetObject( typeof(StatViewerServer), "http://myserver/myroot/statviewerserver.rem");
return svr.CreateStatView();
}
}

miercuri, 18 noiembrie 2009

[Architecture] Business Level Units

Business Level Units:
- history/navigation bar (Tracking Whether the Object Has Changed). n-tier undo operations
- validation mechanism/ error provider (set of methods that have the signature similar to a delegate that return null and it is the prototype for all methods ).
- strong typed collections: especially for children objects (E.g.: in case of validation if a child is marked as Dirty, then the parent should be Dirty too).
- Data binding (can be used to link controls to properties of business objects).
- Object persistence.
- Rules (e.g.: nameRule)
- Custom authentication
- Manage roles
- Business object creation
- Transactional and non-transactional data access
- Helper types and classes


luni, 16 noiembrie 2009

[Agile] Test-Last Development / Test-First Development

Test-Last Development involves writing tests after writing the code to support the requirements for a particular task. They exercise the system as it has already been built.

-> After every small step write a developer test using your tool of choice to exercise the code that you have written.
->Do not check any code into the source repository that has not been fully tested.
-> The "testability" will drive a design that has far less coupling than code written without this in mind
-> less effective than Test-First Development but easier to adopt

Drop-backs:
-- Testing is dropped during crunch time
-- Testing is seen as an overhead
-- Tests are biased towards the solution
________________________________

Test-First Development involves writing tests before writing the production code that will support and eventually pass that test. Tests resulting from this practice tend to be a developer’s
understanding of requirements because there is no "design" at its inception.

-> Test-First development is more effective and harder to adopt
-> You want to have full test coverage of your requirements instead of test coverage of the design

- Tests that are written after the code are more likely not to be written
- A developer writing production code keeps the fact that he must be able to test this code in the back of his head while developing.
- A developer test should verify that the production code written satisfies the requirements
- Tests written after code may or may not test all of the requirements that drove that code to be written.

Steps:
- write tests based on requirements (Your test therefore will mirror these requirements)
- we decide the design and needed classes to pass all these tests (The test you wrote earlier will drive the creation of classes, methods on those classes, and their relationships to other classes in your system)
- you only write the code needed to make that test pass.
- You have not written one line of code that has not been driven by requirements.
- The last step is to Refactor—that is to modify the structure of your code without changing its behavior.

[c#] GUI questions

How can I update my user interface from a thread that did not create it?

TextBox m_textBox;
public delegate void UpdateTextCallback(string text);
void UpdateTextBox(strin i_text)
{
m_textBox = i_text;
}

m_TextBox.Invoke(new UpdateTextCallback(this.UpdateText), new object[]{”Text generated on non-UI thread.”});

See also, http://www.techinterviews.com/c-developer-interview-questions

sâmbătă, 14 noiembrie 2009

The 5- layer logical architecture

The 5- layer logical architecture:
- Presentation (interaction w. user)
- UI (creation)
- Business logic
- Data Access
- Data and Storage Management

-> from Windows forms perspective: Presentation and UI are the same : GUI
-> Web: Browser represent the Presentation (collect input from user, interact with him), manage pages is done on the server (UI).

UI:
- the logic that decides what the user sees and how interact with the app
- validate user input
- performing data formatting
- provide input to business layer

-> Windows form are almost event -driven
- > Web- forms --- a round trip to the server is necessary (instead some logic is in java scrips on the webform)
-> create reusible GUIcomponents

Business layer:
-> includes all business rules, data validation, manipulation, processing, and security
for the application.
-> Microsoft: “The combination of validation edits, login verifications, database lookups, policies, and algorithmic transformations that constitute an enterprise’s way of doing business"
-> avoid duplicate code in UI layer and Business layer

Data Access:
- interacts with the Data Management layer to retrieve, insert, update, and remove
information
- interface between the business logic and the database
- set of services: each service has a procedure (to insert, delete, update, retrieve data).
- provides mapping between the object-oriented business logic and the relational data in a data store.
- a reason for moving the Data Access layer to an application server is security

Data Storage and Management:
- it handles the physical creation, retrieval, update, and deletion of data.
- validates the data and its relationship to other data

___________________
Business objects provide an abstract representation of entities or concepts that are part of the business or problem domain.
Business object: - contains interface, behavior, data
Mobile objects are all about sending smart data (objects) from one machine to another, rather
than sending raw data.
-> moving from machine to machine.
Anchored Objects - objects are always passed by reference.
-> An anchored object never moves; it’s accessed via references
-> In .NET, an anchored object is created by having it inherit from MarshalByRefObject
Mobile Object -> The concept of mobile objects relies on the idea that an object can be passed from one process to another, or from one machine to another, by value.
-> Objects that can move from process to process or from machine to machine are mobile objects
-> in .NET, add the [Serializable()] attribute to your class definition.


vineri, 13 noiembrie 2009

[C#] Threading

Where they are used
- Windows Forms applications use event-loop threads to handle user interface events. Separate forms execute on separate threads, so if you need to communicate between Windows Forms, you need to communicate between threads.
- ASP.NET pages execute inside the multi-threaded environment of IIS.
_____________
Multitasking refers to an operating system's ability to run more than one application at a time.
Cooperative multitasking - applications decide when leave the control to the system
Pre-emptive multitasking - An application is allowed to execute for a short period before it is involuntarily interrupted by the operating system and another application is allowed to execute
Process - The physical separation of this memory and resources
Primary or main thread - initial thread (c#: Main () method - Application.Run() )
-> A process has data and threads.
-> The thread is a pointer into the instruction stream portion of a process
Time slice or a quantum - The period that the process can execute
Free threading - The concept of spawning new threads within the same process
Apartment threading- each process was granted its own copy of the global data needed to execute

Process.ProcessorAffinity - ability to control which CPU a process uses

Thread Local Storage (TLS) -contains the registers, stack pointers, scheduling information, address spaces in memory, and information about other resources in use.
An interrupt is a mechanism that causes the normally sequential execution of CPU instructions to branch elsewhere in the computer memory without the knowledge of the execution program.

When a thread voluntarily takes itself out of the execution queue for a period, it is said to sleep.
Windows prioritizes threads on a scale of 0 to 31, with larger numbers meaning higher priorities:
-> A priority of 0 can only be set by the system and means the thread is idle
-> Priorities between 1 and 15 can be set by users of a Windows system
-> Threads running in a priority between 16 and 31 are considered to be running real-time
-> 16 - Real Time idle
-> 5-9 normal background

pview - monitor/set thread priorities
_________________

System.AppDomain
- .NET Framework added one more layer of isolation called an application domain
- it is logical isolation within the process
- we can now launch several applications within the same process.
- Any time that an assembly is loaded in an application, it is loaded into an AppDomain
- A process can have multiple App domain

marți, 10 noiembrie 2009

[Bianca] In noiembrie

Avem la ultimul control 69,5 cm si 7,600 kg. Nu suntem in grafic, dar nu ne facem probleme pentru ca avem falcute si suntem vioaie.
In ultima vreme am problem cu dintii. Am 5 acuma. 3 jos is 2 sus. Dar cred ca-mi mai ies vreo cativa ca nu stiu ce sa ma mai fac. Sunt destul de maraita, suzeta imi este o prietina din ce in ce mai buna. Aproape ca nu mai adorm fara ea si, bineinteles, fara Tigrisoru'. Si cu mancarea avem probleme... Abia reuseste mama sa ma convinga sa mananc.
Dar, imi plac struguri. Sa sparg bobitele in manute. Sa crap bobitele cu dintii si sa trag de ele. Imi mai stoarce si tati uneori bobitele in gurita. Si tare bine este.
Acum vreau mai tot timpul sa stau in picioare. Daca nu cumva reusesc sa pacalesc pe vreunu sa ma ia in brate. Intind manutele si daca vad ca nu ma fac inteleasa apelez si la maraiala.
Mi-a luat mama bocancei. Par cam incomozi, asa... Abia reusesc sa ma mai misc in voie cu ei. Eu cred ca sunt facuti sa ma incurce.
Bunicuta mi-a impletit o armata de botosei, vreo doua vestute, si manusele, etc... Nu le mai stiu pe toate...
Cand nu e frig, iesim afara. Imi place sa ma dau in leaganul-chilotel... Ma iut dupa toti copii si ganguresc la ei. Dar nu prea sunt inteleasa.. sau bagata in seama.
Am fost si racita. Tuseam si-mi curgea nasul. Mare agitatie. Dar am scapat.

[C#] "var"

-> The compiler infers the type indicated by "var" from the expression used to initialize the variable, and the IL code contains the inferred type.

_____________________
var i = 10;
Console.WriteLine(i.GetType()) ; // write on console "SystemInt32"
_____________________
The following code is not compiling:
var i = 10;
i = "George";
_____________________
Use "var" to:
- Refer anonymous types
var _anonymous = new { Name = "George" };

- Refer query expressions
var _queryExpr = from devs in developers
where devs.Name = "George"
select new { devs.Name, devs.Age }

- Refer complex generic types
var _stringList = new List();

Not recommended to use "var":
- with known types (var dev = new Developer())
- with constants (var i = 5;)
->
you can't do anything with the variable without casting it back to the original type.
- with simple expression assignments (var _count = _list.Count)
- with variables where types cannot be inferred or where inferred type is not what is intended (want to use IList, but initialize with "var")

vineri, 6 noiembrie 2009

[C#] ObjectDisposedException / ToolStripButton

I see many times this exception reported by QA:

exception:
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'PopupContainerForm'.
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.Form.CreateHandle()
at DevExpress.XtraEditors.XtraForm.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.ToolTip.get_CreateParams()
at System.Windows.Forms.ToolTip.CreateHandle()
at System.Windows.Forms.ToolTip.Hide(IWin32Window win)
at System.Windows.Forms.ToolStrip.UpdateToolTip(ToolStripItem item)
at System.Windows.Forms.ToolStripItem.OnMouseHover(EventArgs e)
at System.Windows.Forms.ToolStripControlHost.HandleMouseHover(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnMouseHover(EventArgs e)
at System.Windows.Forms.Control.WmMouseHover(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

First I have suspected to be a DevExpress issue. But searching on their support site I have found the following link:
http://devexpress.com/Support/Center/p/DB11990.aspx?searchtext=System.ObjectDisposedException&p=T4|P0|0

They offer a solution that does not contain any DevExpress comtrol. They say that with that application it can be easily reproduced the problem. I can not.

So, DevExpress team declines their responsibility for this problem and put this exception on the arms of using ToolStrip buttons.

Dig in I have found another article about System.ObjectDisposedException and ToolStrip:
http://www.actiprosoftware.com/support/forums/viewforumtopic.aspx?forumtopicid=1665
Here I found out some affirmation that I can confirm:

-- "It seems like they initialize their internal tooltip to the first parent window that is there when a tooltip is displayed. However if the parent window changes, the ToolStrip never updates the internal tooltip to tell it that the parent window has changed. In your case the first parent window is disposed, thus causing the error. " - in my code the parent control is changing.

-- "ToolTip t = (ToolTip)toolStrip1.GetType().GetProperty("ToolTip", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(toolStrip1, null);
t.RemoveAll();" - use this code to remove all tooltips from ToolStripButton as a workaround

The method of System.Windows.Forms.Control.CreateHandle() looks like:

[EditorBrowsable(EditorBrowsableState.Advanced), UIPermission(SecurityAction.InheritanceDemand, Window=UIPermissionWindow.AllWindows)]
protected virtual void CreateHandle()
{
IntPtr zero = IntPtr.Zero;
if (this.GetState(0x800))
{
throw new ObjectDisposedException(base.GetType().Name);
}

.....
}

where this in this case is TopLevelControl control:


protected virtual CreateParams CreateParams
{
[SecurityPermission(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.UnmanagedCode), SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
get
{
CreateParams params = new CreateParams();
if (this.TopLevelControl != null)
{
params.Parent = this.TopLevelControl.Handle;
}
}
....
}

So, the variant with changing the TopLevelControl could be true.


[Problem] Intresting problems

100 Married Couples

Every man in a village of 100 married couples has cheated on his wife. Every wife in the village instantly knows when a man other than her husband has cheated, but does not know when her own husband has. The village has a law that does not allow for adultery. Any wife who can prove that her husband is unfaithful must kill him that very day. The women of the village would never disobey this law. One day, the queen of the village visits and announces that at least one husband has been unfaithful. What happens?
Answer it is here.

Solution 1:
1st Night: If only one has cheated.
There is only one wife who has list of cheated husband count as zero and she kills her husband. One husband is killed.
2nd Night: If no killing happens 1st night.
The wifes with count of cheated husband as one, will kill their husbands. Two husbands are killed.
3rd Night: If no killing happens 2nd night.
The wifes with count of cheated husband as two, will kill their husbands. Three husbands are killed.
nth Night: If no killing happens (n-1)th night.
The wifes with count of cheated husband as (n-1), will kill their husbands. n husbands are killed.

Solution 2:

Nothing happens.

If every husband cheats and every wife knows when another woman's husband cheats, then every wife is already aware of cheating in the village. The queen has introduced no new data.

Furthermore, if each wife is aware of 99 instances of cheating and we assume she isn't stupid, she can probably surmise through induction that all 100 husbands have cheated. If word gets out, it will be a bloodbath. The entire community will be destroyed. The only solution is to never, ever seek out any information that would prove that her own husband is unfaithful. If everyone keeps quiet, the burden of proof rests with the queen alone.

Essentially, it's a classic "garbage in, garbage out" problem. The wives subvert the data-collection process, and the algorithm fails for lack of input validation.



12 coin problem

you have 12 coins. one of them is counterfeit. all the good coins weigh the same, while the counterfeit one weights either more or less than a good coin. your task is to find the counterfeit coin using a balance-scale in 3 weighs. moreover, you want to say whether the coin weighs more or less
than is should and, and this is the real kicker, your weighs must be non-adaptive. that is, your choice of what to put on the balance for your second weigh cannot depend on the outcome of the first weigh and your decision about what to weigh for round 3 cannot depend on what happened on either your first or second weigh. for example, you can't say something like "take coin #1 and coin #2 and weigh them. if they balance, then take coins 3,4,5 and weight them against 6,7,8...if 1 and 2 don't balance, then weigh #1 vs #12..." you
have to say something like:

round #1: do this
round #2: do this
round #3: do this
if the results are left tilt, balanced, and left tilt, respectively, then coin #11 is heavier than it should be.

Answer it is here.

joi, 5 noiembrie 2009

[Management] Components of a Successful Team

Components of a Successful Team:

-> key factors: skilled developers, good communication, improving
-> team has to have core principles
- including company core values
- focus on customer
- collaboration
- managing by facts
-> good communication: communicate face - to - face, focus on facts, offer feedback
-> the most important resource is people: skilled, talented people, "team is necessary"
-> constant improvement

Building the team:
- focus on communication, create an open environment
- establish core principles
- interview process:
-- find people fitting in a culture
-- objective measurements of candidates
-- first step: a phone screen: find out basic skills and do at some level a personal introspection.
-- face -to - face interview: interview is broken up into three segments: technical, process, personal.
- process improvement

luni, 2 noiembrie 2009

[Architecture] Controlling architecture

Controlling architecture

Architecture:
-> to have guidance/reference architecture/rules/vision:
e.g.:
- we have those layers...
- you should put your module (project) here
-> teach developers to use these guidelines (see also this article)
-> communicate and enforce
-> take in consideration resistance and ignorance
-> devs tend to consider their problems as being unique, not matching any guidance.
__________________________
Ways to enforce architectural rules:
- Code inspection (code smells, code does not follow the rules)
- analyze the code structure: e.g.: do not let view layer code call directly the DB ... , code complexity (no. of dlls, classes, methods, inheritance levels), class diagram, multithreading issues, performance
- use assert/tests to check code dynamically (at run time)
- check program flow
- ZERO compiler warnings in code
- fix issues as they arise.

-> automate enforcement of architecture rules

vineri, 23 octombrie 2009

[Agile] [Patterns] Automated Developer Tests

See also.
Patterns are "discovered" and not "created."

-> A set of tests that are written and maintained by developers to reduce the cost of finding and fixing bugs - thereby improving code quality—and to enable the change of the design as requirements are addressed incrementally. Disciplined writing of tests encourages loosely coupled designs.
____________
- checking errors
- flexibility and program lifetime are improved - enable Refactoring.
- write your tests first and practiced Test-First Development
____________________

Context:

- needs to significantly improve its quality (reduce bug count)
- you decide to adopt Iterations and Simple Design
- build code using a distributed team - lack of communication
- practicing Collective Code Ownership

Forces:

- QA person and a developer must both find the bug and communicate via a tool to document the work being done
- bugs go back and forth between QA and development
- QA time to found, discovered, and fixed is greater then developer time to .... .
- Fixing one bug frequently causes another bug.
- Complex parts of the system tend to have more bugs than others
- Systems are designed to be general so that when requirements change the system can accommodate the changes.

Therefor:

- fixing bugs by finding them earlier: have developers test their code more rigorously
- automated testing
- test before checking
- document the change
- add a new test for a bug

- developers are more confident and courageous and will make design changes when needed relying on the tests that have been written to catch their mistakes.
- Read what others are writing

Adoption:
1. Commit as a team to the discipline of writing tests.
- Tests are just as important as production code
- Future complete it is only if tests are completed.
- Agree to be patient.
2. Find a tool that is easy to use. (examples: NUni, TestComplete)
- Use automatic testing tools as auxiliary testing and not the primary form of testing.
3. Treat your test code as you would your production code.
4. Try to get help:
- Bring in an experienced consultant
- Use other persons from your company that succeeded
- Read books
- Focused on TDD, Agile Development
5. Adopt Collective Code Ownership
6. Consider adopting Pair Programming
7. Start writing tests with the current Iteration
8. Within a few Iterations your team will come up against the problem of setup data
- Create a framework: Pull out the common setup code into common classes.
- Use Mocks and Stubs
9. Agree as a team on an approach and follow it.
10. Use Mock objects and Stubs to test classes that communicate with external systems.

But:
- It needs everyone on the team to be on board.
- The fact that tests are written says absolutely nothing about the quality of the production code. Adopt Pair Programming or performing regular code reviews on tests.
- Tests sometimes end up as second class citizens: we break all the rules of good design: Refactor it when the design is no longer adequate.
- Writing tests—especially for existing systems that have been written without testing in mind—is very hard.
- All tests should be running and passing all the time—no excuses.
- Code coverage becomes an overly important metric.