Wednesday, July 20, 2011

Serialization in C#

Serialization is used to convert an object into a byte stream. This is usually done to store the object in the memory, database or a file and retrieve the state of the object later. This reverse process is called de-serialization.

To make a type serialize, you need to apply SerializeAttribute to it. If you haven't specify the attribute and when you try to serialize it, it'll through SerializationException. If you want to make some fields in your class not serialized, you can decorate it with NonSerializedAttribute. 

You cannot serialize iterator types such as IEnumerable etc. See SO

Binary Serialization 

Uses binary encoding to produce compact serialization. In Binary, all members are serialized. 

XML Serialization

Serializes public fields and properties of an object, or parameters and return values of a method. This serializes object into a XML stream. This provides more readable code.

SOAP Serialization

Serialize objects into XML streams which conforms to SOAP specification (which is a protocol based on XML).

Serialization in JavaScript
Convert form data to JSON using JQuery

Sources

Wednesday, February 16, 2011

Windows Communication Foundation


WCF is a runtime and set of APIs in .NET framework for building connected, service-oriented applicatons.

Difference with Web Services

Some of  the differences between WCF and Web service is that Web service only supports HTTP protocol. But WCF supports other protocols like TCP, HTTP, HTTPS, Named Pipes and MSMQ. Another major difference is that Web Services use XmlSerializer while WCF uses DataContractSerializer which is better in performance than XmlSerializer. Serialization in WCF.

ABC of WCF (msdn)

 

A - Address :  Where is the service
B - Binding : How do I talk to the service
C - Contract : What can the service do for me

 

Authentication and Authorization in WCF (MSDN)


You can also look in to this article series on MSDN about learning WCF.


Exception Handling in WCF

Handling exceptions in WCF is different than usual exceptions in .NET. Because WCF client may or may not base on .NET. Therefore we should have a generic way to pass exceptions to all types of clients. For this we have SOAP faults. 

SOAP faults are a way to propagate exceptions from service to the client application.  .NET framework has FaultException which can raise SoapFault exception. Here Service should throw FaultException<T> instead of usual CLR Exception object. T can be any type which can be serialized. 

WCF - Exception Handling

Resources

SO - Is there any official WCF Logo 
http://stackoverflow.com/questions/50114/wcf-wtf-does-wcf-raise-the-bar-or-just-the-complexity-level
http://www.codeproject.com/Articles/139787/What-s-the-Difference-between-WCF-and-Web-Services

Monday, February 7, 2011

What you need to know about JavaScript functions

As in any other language, functions are one of the building blocks of JavaScript. You can define functions and call them in different ways. In this article we'll see some basic but important things you need to know about JavaScript functions. 

Function declaration

The basic syntax for creating function declaration is like this. declarations loads to execution context before any code is executed. Below I'll show you function declaration example and 5 ways you can call the function.


Function expression

Functions can be created like expressions as well. Function expression gets load only when interpreter gets to the line of code. Also you can give a name to a function in a function expression they are called named function expression. If you do not give a name to a function expression it'll be called anonymous function expression.

The difference between function expression and declaration is how browser loads them to execution context. With functions comes a important concept called hoisting. Usually in JavaScript variable declarations are hoisted on top of a function. Read this for more info on hoisting

function themselves are objects of type Function. It has methods like apply, call etc.. Every functions by default accepts arguments object. You can find all arguments passed from the caller from argument object. Closures are related with functions. Read this article written by me about closures. JavaScript has predefined functions such as eval, isNaN, parseInt etc. If no return value is defined, functions will return undefined.

You can pass different parameters to functions. If you pass primitive type (primitive types in JavaScript) they get passed by value. But if you pass object (such as Array) the change will be reflected outside the function. That is you're passing an reference to an object by value. There is no pass by reference in JavaScript. 


Constructor functions vs Factory functions
Constructor function uses the new keyword to create a new object, set this within the function of that object and return it.





Factory function also uses to create new objects but it does not use new keyword. In fact factory creates the object for you and returns it. You can return different types of objects from a factory. 



With factories you get better encapsulation and data hiding. 

When you create an object from constructor function the prototype will be included in that object. When you create a n object through a factory since it just returns an object, the prototype won't be available.

Some Useful Stuff

Exclamation mark in front of a function (SO1 , SO2)


Resources

Tuesday, January 25, 2011

Data types in JavaScript

As in any other programming language, JavaScript also has Primitive data types and non-primitive data types. In case you didn't know primitive data types are predefined data types which comes with the language. Non-primitive are data types which are defined by the programmer.

Data types in JavaScript
  • Primary data types
    • Number
    • String
    • Boolean
  • Composite data types
    • Object
    • Function
JavaScript consists of lot of objects in it. Following diagram shows some of them. (source)

Array
Array is a high-level, list like object. Array prototype includes methods to perform operation on it.

Array. forEach
Array.prototype.map()
Creates a new array after manipulating the array elements from the callback function which is passed to map.
There are lot of other methods available for use in Array.prototype
Resources

Tuesday, November 30, 2010

Algorithms every software developer must know

Basics in algorithms

What is Big O notation? (link Stackoverflow)
It is relative representation of the complexity of an algorithm

Sorting Algorithms

Sorting algorithms are often classified by
  • Computational complexity 
    • of elements in terms of the list size
    • of swaps
  •  Memory usage
  • Recursion
  • Stability
  • Adaptability

Popular sorting algorithms

Simple sorts : Insertion sort, Selection sort
Efficient sort: Merge sort, Heap sort, Quick sort
Bubble sort


Resources

http://stackoverflow.com/questions/33923/what-is-tail-recursion
http://stackoverflow.com/questions/tagged/algorithm

Tuesday, November 23, 2010

Modifiers in C#

Modifiers are used to modify declarations of types and type members. Following are the modifiers comes in C# (MSDN)

sealed #

  • In classes, sealed modifier prevents other classes from inheriting from it.
  • Can also use sealed modifier on a method or property that overrides a virtual method or property in a base class
    • Enables you to allow classes to derive from your class and prevent them overriding specific virtual methods or properties 
  • When applied to a method or property sealed must always used with override 
  • Structs are implicitly sealed, they cannot be inherited. This is because structs are value types 

virtual #



  • Virtual is used to modify methods, properties, indexers or event declarations and to be overriden in a derived class. 
  • virtual member can be changed in a derived class with override modifier. 
  • By default methods are non-virtual. Therefore you cannot override a non-virtual method. 
  • Virtual members are an implementation of type-based polymorphism. When you have a base class and a derived class, the derived class can re-implement the base class virtual method thus giving you dynamic entry point to the class type.
  • You cannot create private virtual members. You'll get virtual or private members cannot be private Exception.
  • virtual properties behave like abstract methods, with few differences.


http://www.dotnetperls.com/virtual

abstract #

  • indicates thing being modified has a missing or incomplete implementation
  • abstract method is implicitly virtual

default (SO)

  • For a reference type returns null
  • For a value type other than Nullable<T> returns 0 initialized value
  • For Nullable<T> returns empty value


const 

- Cannot change (compile time constants)

readonly

- Can change in the constructors. (runtime constants)


  • async
  • event
  • extern
  • new
  • override
  • partial
  • static
  • unsafe
  • volatile

Saturday, September 11, 2010

Principles in Software Development

When designing a software there are various principle you should follow 
to make the software better in different aspects. 
In this article we'll go through some of those principles.

Principle of least astonishment

Basically you should not astonish people when it comes to implementing something. For example if you have a method toString() which returns a string "not implemented", it is breaking of least astonishment principle. 

see wikipedia and Programmers - StackExchange 

Cargo cult programming
inclusion of a code or program which does not have any real purpose
wikipedia, Programmers SO 

GRASP

Consists of guidelines for assigning responsibility to classes and objects.  See this wikipedia article and this.

KISS (Keep it simple stupid)

States most systems works best if they are kept simple rather than complicating. Therefore simplicity should be a key goal in designing a system.

YAGNI (You aren't gonna need it)

It's a principle of Extreme Programming. It states that programmer should not add functionality unless deemed necessary. See wikipedia.


Other

Having a good software design is important to avoid bad design which will cause us very badly. According to Robert martin there a 3 things we must avoid when designing software.
  • Ridiglity : It's hard to change because changes affects too many other parts of the system
    • every change causes a cascade of subsequent changes in dependent modules. Can grow 2 day work to multiple weeks
  • Fragility : When you do a change, unexpected parts of the system breaks. Has a close connection with Ridiglity.
  • Immobility: It is hard to reuse component in another area of the application.
  • covariance and contravariance
Memoization 
In computing, memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

source
Memoization in JavaScript


https://www.cs.utexas.edu/~scottm/cs307/handouts/deepCopying.htm

Tuesday, August 10, 2010

Introduction to Python

Python is an interpreted, high-level, general-purpose programming language. Instagram and Google uses Python in there backend. Over the years Python has grown its territory massively

Python can be used for

  • server to create web applications. 
  • can be used alongside software to create workflows. 
  • can connect to database systems. It can also read and modify files. 
  • can be used to handle big data and perform complex mathematics. 
  • can be used for rapid prototyping, or for production-ready software development.

You can install Python from website.

Recommend tool to use python with Visual Studio Code. You can install Python extension for ease of use. 

References
https://stackoverflow.blog/2017/09/06/incredible-growth-python/

Saturday, August 7, 2010

Do not break these User Interface Design Principles


Keep the following as a checklist next time when you're designing an interfaces

  1. Keep things clear without confusing the user
  2. Make user know what is preferred action is. Especially for new users
  3. Keep user interface interaction controls close to relevant content
  4. Keep good set of default settings
  5. Guide user on what they must do. Next
  6. Give feedback for interactions
  7. Breakdown complex actions into set of simple step by step action set


Resources
Photo credit : msdn

Tuesday, May 11, 2010

Creational design patterns

Factory

Provides an interface to create objects. Rather than creating new objects using new keyword, we ask the factory to create and return them for us. 

Factory in JavaScript : method1 (addyosmani) and method2 (carldanley)





Builder

http://www.oodesign.com/builder-pattern.html

Lazy initialization

Creation is deferred until it is first used. Useful for complex objects. 

C# : http://msdn.microsoft.com/en-us/library/dd997286%28v=vs.110%29.aspx


Object pool

Useful when cost of initializing a class instance is very high.  Therefore when requester requires an instance, he can get one of already created instance.

Prototype




Singleton 

http://stackoverflow.com/questions/327955/does-functional-programming-replace-gof-design-patterns?rq=1

Saturday, May 1, 2010

Introduction to Testing


Testing is a mechanism we use to evaluate whether the software we are developing satisfies the specified requirements. Testing is a very broad subject. Here we'll just look in to some high level overview of what it is.


Types of testing

  • Manual testing
    • Takes the role of an end user and checks for unexpected behavior 
    • There are few levels like unit testing, integration testing, system testing and user acceptance testing
    • Uses test plan, test cases or test scenarios to test
  • Automation testing
    • Also known as Test Automation
    • Testers writes scripts and use other software to do testing
    • Rerun test scenarios quickly and repeatedly
    • Use tools like Selenium, VS Test Professional, IBM Rationale function tester

Testing methods

  • Black box 
    • Without having any knowledge of interior workings of the application (UI level)
  • White box
    • Detailed investigation of internal logic
  • Grey box
    • Testing with limited knowledg

Sunday, April 4, 2010

Behavioral Design Patterns

Observer

Object (subject) maintains list of its dependencies (observers). Subject will then notify observers whenever any state change occurs in it. 

Related patterns are Publish-Subscribe pattern, mediator and singleton 
http://weblogs.asp.net/fmarguerie/events-references-garbage-collecting-memory-leaks-and-weak-delegates


Iterator



Mediator

Defines an object which encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects referring from each other explicitly.

http://www.dofactory.com/net/mediator-design-pattern  
Template method
http://www.oodesign.com/template-method-pattern.html
Null object

Strategy

Defines set of algorithms that can be used interchangeably. Basically you create an interface and derive implementations from that. Clients can couple themselves with the interface. 

http://robdodson.me/javascript-design-patterns-strategy/
http://sourcemaking.com/design_patterns/strategy

Command


Saturday, March 27, 2010

Basics in C#

C# is a multi paradigm programming language which has,

  • strong typing (error if argument pass to a function does not match expected type. See strong typing vs static typing)
  • imperative (statements which changes the state in contrast to functional programming- see diff)
  • declarative
  • functional
  • generic
  • object oriented and
  • component oriented 
program disciplines.

Data Types

Value type
Directly contain data in its own memory location
Each instance has its own copy of data
Value type can be divided into Simple, Enum, Struct and Nulllable types
http://stackoverflow.com/questions/1769306/why-are-net-value-types-sealed

Reference type
Stores a reference o the data (object)
Can be divided into Class, Interface, Array and Delegate types

It is not exactly true that value types goes on stack and reference types goes on the heap


Notes
A variable is an association between a name and a slot of memory. Variable has a value which the content of memory slot is associated with. The size depends on the type of the variable.

Value of a reference type is always a reference or null. If it's a reference it should be compatible with type of the variable. The slot of memory associated with the variable is size of the reference (Int32 : 4 bytes).

Value of value type is always data for an instance of the type itself. The slot of memory is large enough to hold its value (struct with 2 integers). Value type cannot have null. Null means variable of a reference type does not point to anything. 

local variables are stored on stack, including reference type variables. 

http://jonskeet.uk/csharp/memory.html

Mutable vs. Immutable 

  • Mutable : can change, Immutable : Cannot change
  • A mutable string can be changed, immutable string cannot
  • Immutable type is a class or struct written so that it cannot be changed once it is created. 
    • Once a string is constructed, it cannot be changed. All the functions you can call on a string return a new string, instead of manipulating the string they are called on
  • Instances of immutable types are inherently thread-safe (No thread can modify it)
  • source , mutable structs are evil
  • Always make value types Mutable (Eric Lappert)

Tuesday, March 16, 2010

Structural Design Patterns

Decorator

Allows behavior to be added to an individual object either statically or dynamically, without affecting the behavior of other objects from the same class. FileReader in C# is a good example for this. Also check this article from codeproject for C#. Explore the Decorator pattern in JavaScript

Source
Example

Facade

Provides simplified interface to larger body of code such as a class library. Checkout this article by dofactory for C# and addyosmani section for JavaScript.


Source

Composite

Describes that group of objects to be treated same way. The objective is to "compose" objects into tree structures to represent part-whole hierarchies.


Proxy

It's a class acting as an interface to something else. This can be a network connecting, web service or some other resource which is expensive or impossible to duplicate


Module pattern

See (http://en.wikipedia.org/wiki/Module_pattern)


Resources

http://en.wikipedia.org/wiki/Software_design_pattern

Tuesday, February 2, 2010

Web Services with ASP.NET

Web services provide infrastructure to create distributed computing applications. Simply web services resides in a web server and enables clients to invoke its methods. In .NET, clients can communicate with web services using proxies. Usually HTTP handlers are used to implement web services.
Source : developer.mulesoft.com
You can generate the proxy class using WSDL.exe if you have the web service description which conforms to WSDL. When you use this tool a single proxy class is generated. 

See creating an XML web service proxy (MSDN)
How to generate web service out of WSDL (SO)

WSDL (Web Service Definition Language)
Describes the web service

SOAP
It is a programmable application logic accessible via standard Web protocols. SOAP (Simple Object Access Protocol) is one example. SOAP uses XML for data description and HTTP for transport. 

SOAP message consists of,
  • SOAP envelop
  • SOAP headers
  • SOAP body
RESTful Web Services
RESTful web services does not contain any contract or WSDL file. It is simple to implement and supports different data formats (JSON, XML etc.)

Serialization in Web Services
Web Services use XMLSerialization for serializing response. 
https://msdn.microsoft.com/en-us/library/564k8ys4(v=vs.110).aspx

Resources

Tuesday, December 22, 2009

Object Oriented Concepts Every Dummy Must Know

Abstraction
  • Abstraction is Only showing what is necessary and encapsulating (hiding) unnecessary parts. 
    • If you take a car you interact with it using abstractions 
      • you use gas pedal, steering wheel which hides internal details
  • It is keeping a clear separation between abstract properties of a data type and the concrete details of its implementation. 
  • It is not presenting the details but only show the parts which are necessary.  
  • Hiding implementation 
To abstract properties we can use private modifier. Abstraction is hiding implementation details using abstract classes and interfaces.


Encapsulation
  • Hiding state/data. Information hiding mechanism
  • It is the opposite of Abstraction.  
http://stackoverflow.com/questions/24626/abstraction-vs-information-hiding-vs-encapsulation
https://www.google.com/search?q=encapsulation+vs+abstraction&ie=utf-8&oe=utf-8
http://stackoverflow.com/questions/742341/difference-between-abstraction-and-encapsulation

Composition : Combining simple types to make a complex type (Car composed of Wheels, Body etc.). An object of a composite type "has an" object of a simple type.


Aggregation : (See composition vs aggregation - adobe)

Is a form of object composition.

Inheritance : When an object or class based on another object or class. 

Inheritance is "is a" relationship while, composition is "has a" relationship

Articles

  • inheritance defined statically at compile time.
  • object composition defined dynamically at run time, through objects acquiring references to other objects
  • inheritance : reuse by subclassing : white box reuse (internals of parent are visible)
  • composition : well defined interfaces: black box reuse (no internal objects are visible)



updated: 2014

Saturday, July 11, 2009

SOLID Principles

SOLID is acronym used for 5 design principles for object oriented programming. Below we'll look at what they are

Single Responsibility principle

Every class should have only one responsibility. This responsibility should be totally encapsulated by the class.


Open/Closed principle

Software entities should be open for extension, but closed for modification. This is valuable when it comes to production environments where changes to existing code may require code reviews, unit tests etc. which ensures the quality of the product. In design, inheritance is used to achieve open/closed principle. 

see wikipedia, OODesign, Objectmentor,

Liskov Substitution principle

Derived types must be completely substitutable for their base types.

see OODesign,

Interface segregation principle 

Client should not be forced to depend upon interfaces that they don't use

Dependency Inversion principle

High-level modules should not depend on low-level modules.


Wednesday, July 1, 2009

Exception handling basics

Exceptions happens when a member cannot successfully do what it's designed to do. Exceptions can be generated by CLR, 3rd party library or by user code using throw keyword.


A catch block can specify type of exception to catch. This is called exception filter. The exception type should derive from Exception. In general do not specify Exception as the exception filter.

Inside the catch block you can either return a value or throw an exception depending on the situation you're in. (When to throw an exception). Difference between throw and throw ex.

A finally block enables you to clean up actions that are performed in try block. A finally block always runs, regardless of exception is happen. Note that sometimes finally block will throw exceptions as well.


Exceptions contains a property called StackTrace. It contains names of the methods in current call stack, together with file name and line number where exception was thrown for each method. You can use Environment.StackTrace to get stack trace information when no exception is being thrown.

Sometimes you shouldn't throw exceptions. See cost of exceptions and this (SO)

Some common exceptions in .NET

NotImplementedException (msdn)
See Why does NotImplementedException exists - SO

Handling business logic in exceptions
http://stackoverflow.com/questions/5378005/when-is-it-ok-to-use-exception-handling-for-business-logic
http://programmers.stackexchange.com/questions/15570/representing-business-rules-with-exceptions

Exceptions you should not throw (SO)
Since they doesn't give any meaningful information when writing your own code you should not throw,

  • Exception
  • SystemException
  • NullReferenceException and IndexOutOfRangeException are fine





ResourcesUnhandled Exception Processing In The CLR

Tuesday, June 16, 2009

Powered by Blogger.


Software Architect at Surge Global/ Certified Scrum Master

Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

Search This Blog

Facebook