Design patterns vs coding standards

There is a great debate on the topic we are discussing here. Many a times we work in software companies, our bosses want us to implement certain design patterns to be followed through out the organization. Like giving an example, one of a senior in our company tends to fall toward caliburn Micro if we are to do windows phone development. No doubt it helped here and there but focusing on that cause a lot of time to understand that first and than implement it, and as many a design patterns are work of community so they are not 100% fulfilling 100% of the requirements.

The point is that not everybody understands every design pattern but everybody can adhere to coding standards within the organization.

a- Not every design pattern fits for every project, but coding standards can be made to fit for most of the projects.

b- Injecting design patterns might cause difficulty for on-comers in the project while implementing coding standards cause fluent code understanding for people to come later on in the project.

c- Analysts, Designers [Software Designers], Team Leads, or all in all if I call Bosses should understand this fact. I am not against design patterns but recommending a design pattern should only be made on the basis of previous experiences and recommendations not by doing a small tiny project which does not fully utilize it.

d- If we follow the basic principal of software engineering that is “Keep it Simple”, then we will definitely go for coding standards.

Good way to write code – A

Lots of time, we can increase readability and performance of our app by using good programming practices. Like I was doing once like below:

if (IsolatedStorageWrapper.LoadString(“ShowTipsAndTricks”) == “”)
{
IsolatedStorageWrapper.SaveString(“ShowTipsAndTricks”, “true”);
}
else
{
IsolatedStorageWrapper.SaveString(“ShowTipsAndTricks”, “”);
}

now this is something we do most of the times, but all this can be done in one line of code as well, see below:

IsolatedStorageWrapper.SaveString(“ShowTipsAndTricks”, IsolatedStorageWrapper.LoadString(“ShowTipsAndTricks”) == “” ? “true” : “”);

The compiler will have to traverse through less lines, and we might some less CPU cycles :)

An Introduction to New Features in C# 5.0

Introduction of New Features in C# 5.0

 1. C# Evolution Matrix

Microsoft just published a new version of C# : 5.0 beta with CLR version 4.5 (Visual Studio 11 beta). In order to get a big picture of the whole evolution of C# language, I summarized all the key features into a C# Evolution Matrix for your reference as below diagram shows:

In C# version 5.0, there are two key features: Async Programming and Caller Information.

2. Async Feature

b. Caller Information

‘Is’ Operator in C#

It is possible for a program with some local variable x:

bool b = x is FooBar;

to assign true to b at runtime, even though there is no conversion, implicit or explicit, from x to FooBar allowed by the compiler! That is,

FooBar foobar = (FooBar)x;

would not be allowed by the compiler in that same program.

The intention of the operator is to determine if the runtime value of x is actually a useful value of the type T, (*) and therefore we add a few additional caveats:
•T may not be a pointer type
•x may not be a lambda or anonymous method
•if x is classified as a method group or is the null literal (**) then the result is false
•if the runtime type of x is a reference type and its value is a null reference then the result is false
•if the runtime type of x is a nullable value type and the HasValue property of its value is false then the result is false
•if the runtime type of x is a nullable value type and the HasValue property of its value is true then the result is computed as though you were checking x.Value is T

C# is a strongly type language or a weakly typed language

A debate that can hold different point of views in different scenarios. It is impossible to say because it depends on the point of view of the speaker, it depends on what they are comparing it to, and it depends on their attitude towards various language features.

C# is a pragmatic language there is a way to override all three of those safety systems. Cast operators and “dynamic” in C# 4 override compile-time type checking and replace it with runtime type checking, and “unsafe” blocks allow you to turn off type safety and memory safety should you need to. Someone who thinks that “strongly typed” means “the language absolutely positively guarantees static typing, type safety and memory safety under all circumstances” would quite rightly classify C# as “weakly typed”. C# is not as strongly typed as languages that do enforce these restrictions all the time.

Lets keep it simple by saying that C# is a strongly typed language as well as holds features of weakly typed language as well, and it depends on how we want to mold it according to scenarios. And I guess this adaptability makes C# a strong language competitor if there is to make a choice.

Using Sterling as DB for Windows phone 8

Sterling is a nice option for NO SQL fans, who intent to save C# POCO objects into Isolated Storage container of windows phone 8. I will make life easier by pasting in the snippets for initializing the sterling engine, registering tables in a database, Saving data into the db. in a pretty straight forward manner.

Initializing the database:
public class FakeDatabase : BaseDatabaseInstance
{
protected override System.Collections.Generic.List RegisterTables()
{
return new List()
{
this.CreateTableDefinition(c=>c.Id)
};

}
}
Defining our POCO entity:
public class Info
{
public int Id { get; set; }
public string name { get; set; }
}
Registering and saving in the DB:
public partial class login : PhoneApplicationPage
{
private SterlingEngine engine;
private SterlingDefaultLogger logger;
public static ISterlingDatabaseInstance DB { get; set; }

public login()
{
InitializeComponent();
InitDB();

}
private void InitDB()
{
engine = new SterlingEngine();
engine.SterlingDatabase.RegisterSerializer();
engine.Activate();

DB = engine.SterlingDatabase.RegisterDatabase(new IsolatedStorageDriver());
IEnumerable ListItems;
ListItems = DB.Query().Select(c => c.LazyValue.Value);
var listItems = ListItems as Info[] ?? ListItems.ToArray();
DB.Save(new Info() { Id = listItems.Count() + 1, name = “Ali” + (listItems.Count() + 1).ToString(CultureInfo.InvariantCulture) });
DB.Flush();
ListItems = DB.Query().Select(c => c.LazyValue.Value);
listItems = ListItems as Info[] ?? ListItems.ToArray();
MessageBox.Show(listItems.Count().ToString(CultureInfo.InvariantCulture));
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri(“/MainPage.xaml”,UriKind.Relative));
}

private void Button_Click_2(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri(“/ForgotPassword.xaml”, UriKind.Relative));
}
}