Wednesday, 31 July 2013

?? Operator in C#

The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand.

A nullable type can contain a value, or it can be undefined. The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type. If you try to assign a nullable value type to a non-nullable value type without using the ?? operator, you will generate a compile-time error. If you use a cast, and the nullable value type is currently undefined, an InvalidOperationException exception will be thrown.

int? x = null;
// y = x, unless x is null, in which case y = -1. 
int y = x ?? -1;

Useful links

Nullable Types

?? Operator

The C# ?? null coalescing operator (and using it with LINQ)

Wednesday, 24 July 2013

Windows Keyboard Shortcuts

Calculator Keyboard Shortcuts

Press this key To do this
Press this key

Alt+1

To do this

Switch to Standard mode

Press this key

Alt+2

To do this

Switch to Scientific mode

Press this key

Alt+3

To do this

Switch to Programmer mode

Press this key

Alt+4

To do this

Switch to Statistics mode

Press this key

Ctrl+E

To do this

Open date calculations

Press this key

Esc

To do this

Press the C button

Full list is available here
http://windows.microsoft.com/en-gb/windows/keyboard-shortcuts#keyboard-shortcuts=windows-7

Visual Studio Keyboard Shortcuts

A good collection of keyboard shortcuts for Visual studio is available @ http://www.shortcutworld.com/en/win/Visual-Studio_2010.html

Monday, 22 July 2013

Unit Testing JavaScript

What is QUnit?

QUnit is a powerful, easy-to-use JavaScript unit testing framework.

Getting Started

A minimal QUnit test setup:

<!DOCTYPE html>
<html:gt;
<head:gt;
<meta charset="utf-8">
<title>QUnit Example</title>
<link rel="stylesheet" href="/resources/qunit.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="/resources/qunit.js"></script>
<script src="/resources/tests.js"></script>
</body>
</html>

The contents of tests.js:

test( "hello test", function() {
  ok( 1 == "1", "Passed!" );
});
Useful links

qunitjs.com

http://qunitjs.com/intro/

Wednesday, 17 July 2013

How to open link in a new tab in iPad

To open a link in a new tab, long-press that link. Choose the command "Open in New Tab" from the menu that appears.

How to force links in Blogger to open In New Tab

By default, all links and hyperlinks in Blogger opens in your current window. To change this behavior follow these steps
  1. Login to your Blogger account
  2. Navigate to TEMPLATE > Edit HTML
  3. Search for the tag in your template’s code. Hint: Use Ctrl+F to find the code.
  4. Copy the code below and Paste it below the tag

    <base target='_blank'/>

  5. Hit the "save template" button and you are done.

Wednesday, 10 July 2013

Cookies: Frequently Asked Questions

What is a cookie?

A cookie is a piece of information in the form of a very small text file that is placed on an internet user's hard drive. It is generated by a web page server, which is basically the computer that operates a web site. The information the cookie contains is set by the server and it can be used by that server whenever the user visits the site. A cookie can be thought of as an internet user's identification card, which tell a web site when the user has returned.

History of cookies

Cookies for the internet were originally developed in 1995 by the Netscape Communications Corporation. The word 'cookie' comes from 'magic cookie,' a term in programming languages for a piece of information shared between co-operating pieces of software. The choice of the word cookie appears to come from the American tradition of giving and sharing edible cookies.

What is the purpose of cookies?

Cookies make the interaction between users and web sites faster and easier. Without cookies, it would be very difficult for a web site to allow a visitor to fill up a shopping cart or to remember the user's preferences or registration details for a future visit.

Web sites use cookies mainly because they save time and make the browsing experience more efficient and enjoyable.

Web sites often use cookies for the purposes of collecting demographic information about their users.

Cookies enable web sites to monitor their users' web surfing habits and profile them for marketing purposes (for example, to find out which products or services they are interested in and send them targeted advertisements).

Are there different types of cookies?

Cookies come in different flavours: Session, or transient cookies

Session cookies

Cookies that are stored in the computer's memory only during a user's browsing session and are automatically deleted from the user's computer when the browser is closed.

These cookies usually store a session ID that is not personally identifiable to users, allowing the user to move from page to page without having to log-in repeatedly. They are widely used by commercial web sites (for example, to keep track of items that a consumer has added to a shopping cart).

Session cookies are never written on the hard drive and they do not collect any information from the user's computer. Session cookies expire at the end of the user's browser session and can also become no longer accessible after the session has been inactive for a specified length of time, usually 20 minutes.

Permanent, persistent, or stored cookies

Cookies that are stored on the user's computer and are not deleted when the browser is closed. Permanent cookies can retain user preferences for a particular web site, allowing those preferences to be used in future browsing sessions.

Permanent cookies can be used to identify individual users, so they may be used by web sites to analyse users' surfing behaviour within the web site. These cookies can also be used to provide information about numbers of visitors, the average time spent on a particular page and generally the performance of the web site. They are usually configured to keep track of users for a prolonged period of time, in some cases many years into the future.

Find out more

Thursday, 4 July 2013

ASP.NET Javascript Encode on server side

NET Framework Version 4.0 and above and has a method called

HttpUtility.JavaScriptStringEncode()

which can be used to encode string values for JavaScript. It is a handy method which will take care of all special characters in the string including new line characters. http://msdn.microsoft.com/en-us/library/dd991914.aspx

http://itmeze.com/2011/03/21/javascript-encode-on-server-side-medium-trust-environment/

Underscore or this - C# best practices

Background

C# and .NET have been around for a long time now and the standards and abilities of the language just keep getting better and better. However there is no getting away that some of the more core principles of this language originated from C and C++ created many many years ago. When C and C++ was first created tools were basic and the IDEs around were not very advanced and often programs were coded in notepad or similar tools. For speed and productivity common conventions came about that allowed developers identify code easier.

  • Prefixing the type on to a variable e.g. intAge
  • Adding underscores to internal class variables to indicate they are as such

These were perfectly fine back in the day when working with poor tools but now with Visual Studio and all of the features we have in modern IDEs a lot of this has changed

Underscore vs this keyword

Now comes my main gripe of standards that seem to be lingering on and one, mostly from C and C++ developers and incorrect information. C# has a keyword that has been in it a long time call the ‘this’ keyword which has the following benefits/features

  • Indicates that something you are accessing is from the instance of the class you are using
  • As soon as you type it Intellisense is narrowed down to only instance level code constructs

This feature has been built in to the language and Visual Studio to support having a keyword to replace the need to use anything artificial.

But Microsoft use underscore argument

I have the above argument as a reason why we should be using underscores but here is my responses/reasons why they are wrong

  • Their argument is based upon underscore in the source code. If you look at newer source code (.NET4)
  • There is far less use of underscores
  • Coding everywhere uses ‘this’ keyword
  • Looking on forums you can find Microsoft employees stating that they do not use underscores but still some of the developers that have been around a long time do
  • http://msdn.microsoft.com/en-us/library/ms229045(v=vs.100) which shows .NET 4 suggest conventions lists
  • Do not use underscores, hyphens, or any other nonalphanumeric characters. as one of the standards, so Microsoft are most definitely not suggesting the use of underscores
  • Why the hell would they add the ‘this’ keyword if it wasn’t to be used
  • Stylecop the internal tool created by Microsoft to check code for standards compliance doesn’t like underscores but likes the ‘this’ keyword

Summary

As you can tell I hate underscores and developers trying to tell me it’s clever when it clearly isn’t. We aren’t in the coding dark ages anymore so there isn’t any reason to name things non logically with random crap characters all over the place. The ‘this’ keyword was added in to the language for a reason so use it. Basically if I see code with underscore I will assume your old or ignorant.

Thanks to http://scottreed.eu/csharp/underscore/