<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Undisciplined Bytes &#187; software engineering</title>
	<atom:link href="http://www.undisciplinedbytes.com/category/software-engineering/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.undisciplinedbytes.com</link>
	<description>Web Application Development Tips, Tricks and Techniques</description>
	<lastBuildDate>Tue, 31 Jan 2012 09:07:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Entity Framework</title>
		<link>http://www.undisciplinedbytes.com/2012/01/entity-framework/</link>
		<comments>http://www.undisciplinedbytes.com/2012/01/entity-framework/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 09:07:24 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=640</guid>
		<description><![CDATA[Entity framework is the new kid on the block on the ORM realm. It’s Microsoft new technology for data access, and although it didn’t receive very good comments on its first version, things have been greatly improved in its latest release. It now looks really interesting, with some features which surpass the most seasoned ORM [...]]]></description>
			<content:encoded><![CDATA[<p>Entity framework is the new kid on the block on the ORM realm. It’s Microsoft new technology for data access, and although it didn’t receive very good comments on its first version, things have been greatly improved in its latest release. It now looks really interesting, with some features which surpass the most seasoned ORM frameworks that have been in the game for years.</p>
<p>Here’s what Entity Framework (EF) has to offer:</p>
<p><span id="more-640"></span></p>
<ul>
<li>Using EF with the <strong>Model First approach</strong>, you design a schema of the model you want to implement, and EF generates a database and the code to implement it. Now, that’s what I call saving time! Using this approach, you can get going really really quick.</li>
<li>If you already have the code for your model, then EF can generate the database to store this model as well as the rest of the code handling persistance to this database, using the<strong> Code First approach</strong>.</li>
<li>In case you already have the database, EF can generate the code using the <strong>Database First approach</strong>.</li>
<li><strong>EF uses </strong><strong>Plain .Net objects (POCOs)</strong> with navigational properties to implement your model. Very easy stuff. The way EF lets you interact with the data once it’s retrieved from the DB is piece of cake (not the fact of data retrieval itself, that can get tricky).</li>
<li><strong>Automatic code generation</strong> with T4 templates. Once you have EF set up in your project, there’s a bunch of templates to generate a lot of code for you. That doesn’t mean you must use it, but it’s there for your convenience, in case you think some templates can help you. Me myself I use some of the T4 templates, but not all of them. There’s some things I actually prefer doing manually.</li>
<li><strong>Self-tracking entities</strong> will greatly simplify the process of implementing an over-the-wire scenario, in which you have different layers spanning across different systems and you have to serialize and de-serialize your entities. EF makes this really easy.</li>
<li><strong>Local caching</strong>, to speed up your data access.</li>
<li><strong>Lazy loading</strong>, which can improve overall application performance.</li>
<li>Support for <strong>LINQ</strong> (both coming from Microsoft, you would expect this, right?).</li>
<li><strong>Convention over configuration.</strong> The hottest trend in software development. You wouldn’t think EF would miss it, would you? When starting up a new project with EF, the only thing you need to configure are the database server and its name. Nothing else. All other things are taken care of for you. Of course, if you want more control over what EF is doing, you can tweak it to meet your criteria. But that isn’t something you have do right at the beginning. This way, you can be productive much faster.</li>
<li><strong>Support for the major databases</strong>: MS SQL Server (of course), Oracle, MySQL, PostgreSQL, etc…</li>
</ul>
<p>And last but not least:</p>
<ul>
<li>Coming from a big player in the industry like Microsoft, Entity Framework has great resources and a great community behind it. If you ever have a problem with EF, or want to tweak its behavior, chances are you’ll be finding what you need really fast. If you’re a seasoned developer, you’ll find this is one of the most important features of EF.</li>
</ul>
<p>If you haven’t tried it out yet, don’t wait any longer and <a href="http://msdn.microsoft.com/en-us/library/bb399572.aspx">see for yourself what I’m talking about!</a></p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2011/12/persistence-layer-object-relational-mapping-tools/' rel='bookmark' title='Permanent Link: Persistance layer: object-relational mapping tools'>Persistance layer: object-relational mapping tools</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/03/which-database-to-use-for-a-personal-project/' rel='bookmark' title='Permanent Link: Which database to use for a personal project?'>Which database to use for a personal project?</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/01/sluggish-database-view-materialize-it/' rel='bookmark' title='Permanent Link: Sluggish database view? Materialize it!'>Sluggish database view? Materialize it!</a></li>
<li><a href='http://www.undisciplinedbytes.com/2012/01/asp-net-4-5-and-visual-studio-11/' rel='bookmark' title='Permanent Link: ASP.NET 4.5 and Visual Studio 11'>ASP.NET 4.5 and Visual Studio 11</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2012/01/entity-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET 4.5 and Visual Studio 11</title>
		<link>http://www.undisciplinedbytes.com/2012/01/asp-net-4-5-and-visual-studio-11/</link>
		<comments>http://www.undisciplinedbytes.com/2012/01/asp-net-4-5-and-visual-studio-11/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 08:45:27 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=674</guid>
		<description><![CDATA[Back in September 2011 the next versions of ASP.NET and Visual Studio were announced. Microsoft has set up a page informing about all of the new features we can find in them. Check them out:


ASP.NET Core Runtime and Framework

Asynchronously Reading and Writing HTTP Requests and Responses.
Improvements to HttpRequest handling
Asynchronously flushing a response
Support for await and [...]]]></description>
			<content:encoded><![CDATA[<p>Back in September 2011 the next versions of ASP.NET and Visual Studio were announced. Microsoft has set up <a href="http://www.asp.net/vnext/overview/whitepapers/whats-new#_Toc303354469" target="_blank">a page informing about all of the new features</a> we can find in them. Check them out:</p>
<p><span id="more-674"></span></p>
<ul>
<li><strong>ASP.NET Core Runtime and Framework</strong>
<ul>
<li>Asynchronously Reading and Writing HTTP Requests and Responses.</li>
<li>Improvements to HttpRequest handling</li>
<li>Asynchronously flushing a response</li>
<li>Support for <em>await</em> and <em>Task</em>-Based Asynchronous Modules and Handlers</li>
<li>Asynchronous HTTP modules</li>
<li>Asynchronous HTTP handlers</li>
<li>New ASP.NET Request Validation Features</li>
<li>Deferred (&#8221;lazy&#8221;) request validation</li>
<li>Support for unvalidated requests</li>
<li>Anti-XSS Library</li>
<li>Support for WebSockets Protocol</li>
<li>Bundling and Minification</li>
<li>Performance Improvements for Web Hosting
<ul>
<li>Key Performance Factors</li>
<li>Requirements for New Performance Features</li>
<li>Sharing Common Assemblies</li>
<li>Using multi-Core JIT compilation for faster startup</li>
<li>Tuning garbage collection to optimize for memory</li>
<li>Prefetching for web applications</li>
</ul>
</li>
</ul>
</li>
<li><strong>ASP.NET Web Forms</strong>
<ul>
<li>Strongly Typed Data Controls</li>
<li>Model Binding
<ul>
<li>Selecting data</li>
<li>Value providers</li>
<li>Filtering by values from a control</li>
</ul>
</li>
<li>HTML Encoded Data-Binding Expressions</li>
<li>Unobtrusive Validation</li>
<li>HTML5 Updates</li>
</ul>
</li>
<li><strong>ASP.NET MVC 4 </strong></li>
<li><strong>ASP.NET Web Pages 2</strong>
<ul>
<li>New and Updated Site Templates</li>
<li>Improved Input Validation</li>
<li>Resource Management</li>
<li>Enhanced Membership and Authentication</li>
<li>Side-by-side Execution</li>
<li>Mobile Device Rendering</li>
<li>The Maps Helper</li>
</ul>
</li>
<li><strong>Visual Web Developer 11 Developer Preview.</strong>
<ul>
<li>HTML Editor
<ul>
<li>Smart Tasks</li>
<li>WAI-ARIA support</li>
<li>New HTML5 snippets</li>
<li>Extract to user control</li>
<li>IntelliSense for code nuggets in attributes</li>
<li>Automatic renaming of matching tag when you rename an opening or closing tag</li>
<li>Event handler generation</li>
<li>Smart indent</li>
<li>Auto-reduce statement completion</li>
</ul>
</li>
<li>JavaScript Editor
<ul>
<li>Code outlining</li>
<li>Brace matching</li>
<li>Go to Definition</li>
<li>ECMAScript5 support</li>
<li>DOM IntelliSense</li>
<li>VSDOC signature overloads</li>
<li>Implicit references</li>
</ul>
</li>
<li>CSS Editor
<ul>
<li>Auto-reduce statement completion</li>
<li>Hierarchical indentation</li>
<li>CSS hacks support</li>
<li>Vendor specific schemas (-moz-, -webkit)</li>
<li>Commenting and uncommenting support</li>
<li>Color picker</li>
<li>Snippets</li>
<li>Custom regions</li>
</ul>
</li>
<li>Page Inspector</li>
<li>Publishing
<ul>
<li>Publish profiles</li>
<li>ASP.NET precompilation and merge</li>
</ul>
</li>
<li>IIS Express</li>
</ul>
</li>
</ul>
<p>These are developer previews, which mean that they should be used for <strong>testing purposes only, and never in production environments</strong> – not yet, at least.</p>
<p>In case you’re interested, Microsoft’s central hub for ASP.NET’s next version articles is <a href="http://www.asp.net/vnext" target="_blank">ASP.NET vNext section</a>.</p>
<p>Via <a title="ASP.NET" href="http://www.asp.net/" target="_blank">Microsoft&#8217;s ASP.NET web site</a>.</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2010/02/how-to-make-an-ajax-call-using-jquery-and-asp-net-i/' rel='bookmark' title='Permanent Link: Different ways to use AJAX in ASP.NET'>Different ways to use AJAX in ASP.NET</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/02/php-or-asp-net/' rel='bookmark' title='Permanent Link: PHP or ASP.NET?'>PHP or ASP.NET?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2012/01/asp-net-4-5-and-visual-studio-11/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Persistance layer: object-relational mapping tools</title>
		<link>http://www.undisciplinedbytes.com/2011/12/persistence-layer-object-relational-mapping-tools/</link>
		<comments>http://www.undisciplinedbytes.com/2011/12/persistence-layer-object-relational-mapping-tools/#comments</comments>
		<pubDate>Tue, 13 Dec 2011 10:36:16 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Documentation]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=242</guid>
		<description><![CDATA[Nowadays it&#8217;s very usual to work with object-oriented programming and relational data bases. Relational data bases are all about tables, relations and groups. However, the object-oriented programming paradigm consists of objects, attributes and inter-object relationships. When these objects need to be stored using a relational data base, it&#8217;s very obvious that these two models of [...]]]></description>
			<content:encoded><![CDATA[<p>Nowadays it&#8217;s very usual to work with object-oriented programming and relational data bases. Relational data bases are all about tables, relations and groups. However, the object-oriented programming paradigm consists of objects, attributes and inter-object relationships. When these objects need to be stored using a relational data base, it&#8217;s very obvious that these two models of representing information are very different from each other. We need some kind of translation to transfer data from one to the other.</p>
<p>If we&#8217;re using objects in our application and we want to save them, we&#8217;ll probably use some data base system. We&#8217;ll most likely establish a connection to the data base, create a SQL sentence with the values of the object (or call some kind of stored procedure), and save them to some table. Easy enough, right? Well, this may seem trivial for a small object with 4 or 5 properties. Just consider an object not so small, with 40 or 50 properties. But&#8230; what happens with associations? And&#8230; what if the object itself contains some other objects? We&#8217;ll store them in the data base as well? All of them or just some? What will we do with foreign keys? We can see that, as the application gets more complex, this translation is not so trivial anymore&#8230; As a matter of fact, storing objects in the database can become a very usual source of headaches. There are some studies which state that <strong>up to 35% of code is dedicated to the role of translating data between the application and the data warehouse.</strong></p>
<p><span id="more-242"></span></p>
<p><strong> </strong></p>
<p>And this is where <strong><em>Object-Relational Mapping tools</em></strong> come into play. ORM tools applied to databases transform a DB register to a code object and vice-versa, allowing us to abstract away from writing code to directly access the database. This will speed up development and will allow us to follow the mantra of<strong> focusing on business functionalities, not on application plumbing</strong>.</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">But, what exactly can an ORM tool help me with? Well, they usually do some things like:</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- Handle object persistance (to a database, for example). You won&#8217;t have to write code for this. And remember, less code means less bugs and less headaches.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- Access different databases. Most of these tools support communicating with different database providers (Oracle, MS SQL Server, MySql, Postgre, etc.).</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- Cache. Many of these tools offer some way of caching objects in memory, resulting in much faster data access.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- Help with design. Some of these tools also provide some kind of wizar or graphical interface to aid in the design of the schema and/or database, which leads us to the following point:</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- Create your database for you. If you have your data schema already modeled, some of these tools will not only handle how to get the data or how to write it. They can also create the database.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">So now that you&#8217;ve decided to use an ORM, take a look at the most popular ORM tools available for .NET:</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- Linq to SQL: not really a full-blown ORM, but it does help with data access.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- NHibernate: I would say NHibernate is one of the most mature ORM projects in .NET, and one of the most used as well.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- Entity Framework: Microsoft&#8217;s new kid on the block. Its first version didn&#8217;t receive very positive comments, but its latest release looks very good indeed.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">In the next post I&#8217;ll be covering Entity Framework a bit. Stay tuned!</div>
<p>But, what exactly can an ORM tool helps us with? Well, they usually do some things like:</p>
<ul>
<li><strong>Handle object persistance </strong>(to a database, for example). You won&#8217;t have to write code for this. And remember, less code means less bugs and less headaches.</li>
<li><strong>Access different databases</strong>. Most of these tools support communicating with different database providers (Oracle, MS SQL Server, MySql, Postgre, etc.).</li>
<li><strong>Cache</strong>. Many of these tools offer some way of caching objects in memory, resulting in much faster data access.</li>
<li><strong>Help with design</strong>. Some of these tools also provide some kind of wizard or graphical interface to aid in the design of the schema and/or database, which leads us to the following point:</li>
<li><strong>Create your database for you</strong>. If you have your data schema already modeled, some of these tools will not only handle how to get the data or how to write it. They can also create the database.</li>
</ul>
<p>You&#8217;re already convinced to use an ORM for your project, right? Good. Now take a look at the most popular ORM tools available for .NET:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/bb425822.aspx" target="_blank">Linq to SQL</a>: not really a full-blown ORM, but it does help with data access.</li>
<li><a href="http://nhforge.org/" target="_blank">NHibernate</a>: I would say NHibernate is one of the most mature ORM projects in .NET, and one of the most used as well.</li>
<li><a href="http://msdn.microsoft.com/en-us/data/aa937723.aspx" target="_blank">Entity Framework</a>: Microsoft&#8217;s new kid on the block. Its first version didn&#8217;t receive very positive comments, but its latest release looks very good indeed.</li>
<li><a href="http://www.llblgen.com/defaultgeneric.aspx" target="_blank">LLBLGen Pro</a>: very mature project, although it&#8217;s not the simplest of all.</li>
<li><a href="http://dataobjects.net/" target="_blank">DataObjects.Net</a>: quite new, but very promising. It already has a lot of interesting features. Definetely worth taking a look at.</li>
</ul>
<p>In the next post I&#8217;ll be covering Entity Framework a bit. Stay tuned!</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2012/01/entity-framework/' rel='bookmark' title='Permanent Link: Entity Framework'>Entity Framework</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/04/some-open-source-projects-for-net-you-should-take-a-look-at/' rel='bookmark' title='Permanent Link: Some open source projects for .NET you should take a look at'>Some open source projects for .NET you should take a look at</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/03/which-database-to-use-for-a-personal-project/' rel='bookmark' title='Permanent Link: Which database to use for a personal project?'>Which database to use for a personal project?</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/01/sluggish-database-view-materialize-it/' rel='bookmark' title='Permanent Link: Sluggish database view? Materialize it!'>Sluggish database view? Materialize it!</a></li>
<li><a href='http://www.undisciplinedbytes.com/2009/12/software-design-patterns-ii-creational-patterns/' rel='bookmark' title='Permanent Link: Software design patterns (II): creational patterns'>Software design patterns (II): creational patterns</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2011/12/persistence-layer-object-relational-mapping-tools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parallel programming with .NET</title>
		<link>http://www.undisciplinedbytes.com/2011/09/parallel-programming-with-net/</link>
		<comments>http://www.undisciplinedbytes.com/2011/09/parallel-programming-with-net/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 10:35:25 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/2011/02/parallel-programming-with-net/</guid>
		<description><![CDATA[When I’m talking to people about the new things that Visual Studio 2010 and ASP.NET 4.0 bring, and the easy it is to write code to be executed in parallel, many people just don’t care. They see it as if it wasn’t related to them. It’s true that some kind of applications don’t really need [...]]]></description>
			<content:encoded><![CDATA[<p>When I’m talking to people about the new things that Visual Studio 2010 and ASP.NET 4.0 bring, and the easy it is to write code to be executed in parallel, many people just don’t care. They see it as if it wasn’t related to them. It’s true that some kind of applications don’t really need this type of optimization… but that’s not an excuse to try to take advantage of our hardware as much as possible.  From several years ago the most common thing in any computer is to have at least two processor cores. As a matter of fact, we’re starting to see now desktop computers with 8-core processors. The trend is to have an ever-increasing number of cores in our computers. This means that any application will have the chance to work in an environment where more than one thread can be executed simultaneously.  Besides, nowadays if you want your software to perform faster, you can no longer just move it to a machine with a faster processor and forget about it. We’re approaching the maximum speed a processor can get, so improvements in next CPUs will come from the side of more threads, not a higher clock-rate.  I think parallel programming  is a key concept all programmers should care about.  ASP.NET 4.0 and Visual Studio 2010 bring new libraries, types and tools to ease multi-core development. ASP.NET 4.0 includes the new “Parallel Extensions”, which are formed by:</p>
<p><span id="more-565"></span></p>
<ul>
<li>Task Parallel Library: this new library, included in the namespace System.Threading.Tasks.Parallel, has new elements to execute tasks in parallel automatically. It’s got, for example, a parallel <em>For</em> loop, and a parallel <em>ForEach</em> loop.</li>
<li>Parallel LINQ Execution Engine: as you can guess, it’s a parallelized version of Linq to Objects. It’s the same thing, except that it will run in parallel whenever possible.</li>
<li>Coordination Data Structures: it’s a set of primitives for sincronization and thread-safe collections. For example, we have thread-safe dictionaries, stacks and buffers, and special objects for thread sincronization, such as <em>SpinWait</em> or <em>SpinLock</em>.</li>
</ul>
<p>Now, the good part about these parallel extensions is that it gets extremely easy to do parallel programming. Just by using these new features, your code will escalate to use as many parallel threads as possible. For example, in a machine with just one processor all your code will execute sequentially, just as always. But, in an 8-core-processor computer, your code will be broken down into 8 different threads, and they all will execute your code in parallel –assuming all 8 cores are available, of course.  You can learn more <a href="http://msdn.microsoft.com/en-us/library/dd460693.aspx" target="_blank">this article</a> from MSDN.</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2011/12/persistence-layer-object-relational-mapping-tools/' rel='bookmark' title='Permanent Link: Persistance layer: object-relational mapping tools'>Persistance layer: object-relational mapping tools</a></li>
<li><a href='http://www.undisciplinedbytes.com/2012/01/asp-net-4-5-and-visual-studio-11/' rel='bookmark' title='Permanent Link: ASP.NET 4.5 and Visual Studio 11'>ASP.NET 4.5 and Visual Studio 11</a></li>
<li><a href='http://www.undisciplinedbytes.com/2012/01/entity-framework/' rel='bookmark' title='Permanent Link: Entity Framework'>Entity Framework</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2011/09/parallel-programming-with-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test-driven development</title>
		<link>http://www.undisciplinedbytes.com/2011/07/test-driven-development/</link>
		<comments>http://www.undisciplinedbytes.com/2011/07/test-driven-development/#comments</comments>
		<pubDate>Tue, 26 Jul 2011 09:09:20 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=575</guid>
		<description><![CDATA[Surprisingly, there’s many programmers who still don’t know what Test-driven development (TDD) is. I think it’s a very important practice that all of us should follow, since it produces software with many less bugs.
TDD is a programming methodology that involves two other practices: test first development, and refactoring. First a set of unit tests is [...]]]></description>
			<content:encoded><![CDATA[<p>Surprisingly, there’s many programmers who still don’t know what Test-driven development (TDD) is. I think it’s a very important practice that all of us should follow, since it produces software with many less bugs.</p>
<p>TDD is a programming methodology that involves two other practices: test first development, and refactoring. First a set of unit tests is written and verified to fail. Then, code to make them pass must be written, and lastly this code is refactored. The aim of TDD is to achieve clean code that works. The main idea around this methodology is that requirements must be translated into automated tests: thus, when these tests pass, all requirements are guaranteed to have been accomplished. The application to be developed must be flexible enough to allow automated tests to be run. Each test should be small enough to exactly determine which piece of code is failing.</p>
<p><span id="more-575"></span></p>
<p><strong>Test-driven development cycle</strong></p>
<p>Once a set of requirements has been defined, a new cycle starts:</p>
<ol>
<li>Choose a requirement: select a requirement you understand and about which you can write code both to test it and to implement it.</li>
<li>Write a test: the first code you must write is a test to determine whether the requirement is met, accessing the code through its interfaces. This step makes the programmer consider the problem from the client perspective.</li>
<li>Verify the test fails: if the test doesn’t fail it’s because this requirement is already implemented, or because the test is incorrect.</li>
<li>Write the implementation: Write the simplest code that will make the test pass. Follow the KISS philosophy (<em>Keep It Simple, Stupid!</em>).</li>
<li>Run the automated test: Verify whether the whole set of tests are passed.</li>
<li>Refactor: usually used to take out duplicate code. Each change should be made in small increments, and with each increment the tests should be re-run.</li>
<li>Update the requirements list: once the requirement is met mark it as done. Besides, add more requirements that may have been detected in this cycle or design requirements, like, for example, some functionality that has been decoupled from another one.</li>
</ol>
<p>Having a repository of tests makes another good practice easier: frequent integration. Integrating our work frequently with the rest of the team allows us to check whether our code is compatible with the rest of the application, and makes it easier to detect bugs in an early phase. It is much easier and cheaper to fix bugs every few hours than facing enormous problems close to a release date.</p>
<p>Generation of tests for each functionality means that programmers can trust the code. This means that we can make big modifications deep in the core of the system that we would otherwise avoid, since we can be certain that, as long as all the tests pass, the code works as intended and the requirements are still met.</p>
<p>Another thing to keep in mind is that programmers must make the unit tests fail. Unit tests must be tested. The idea is to be sure that unit tests really work, and that they’re able to pick up an error whenever it happens.</p>
<h4>Advantages</h4>
<p>Programmers that use test-driven development from the beginning of a project find that they seldom have the necessity to use a debugger. In the few occasions that a bug is discovered, it is very easy to fix.</p>
<p>Despite of the high initial requirements of this methodology, TDD may provide a great added value to the software development process, producing higher-quality applications in less time. TDD not only offers a simple requirements validation: it can also lead the design of a program. By writing unit tests, a programmer must imagine how customers will be using the functionality, which may avoid some misinterpretations or bad implementations of a functionality.</p>
<h4>Limitations</h4>
<p>In order to follow the test-driven development methodology, tests must be automated. This may turn out to be rather complex in the following situations:</p>
<ul>
<li>Graphical User Interfaces, although there are some partial solutions proposed.</li>
<li>Distributed Objects, even though Mock Objects may help.</li>
<li>Data bases: writing tests that work with data bases is quite complex, since you need to put some known data before, and you have to verify that the contents of the database is the expected once the test is done. This makes these kinds of tests costly to develop.</li>
</ul>
<p>Test-driven development is a programming methodology that should be in place in all organizations that develop software and consider themselves serious. Unfortunately, this is not the case. I hope things start to change in the near future!</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2010/07/improving-software-quality-automatic-testing/' rel='bookmark' title='Permanent Link: Improving software quality: automatic testing'>Improving software quality: automatic testing</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/10/gui-testing/' rel='bookmark' title='Permanent Link: GUI Testing'>GUI Testing</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/08/unit-testing/' rel='bookmark' title='Permanent Link: Unit testing'>Unit testing</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2011/07/test-driven-development/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Some open source projects for .NET you should take a look at</title>
		<link>http://www.undisciplinedbytes.com/2011/04/some-open-source-projects-for-net-you-should-take-a-look-at/</link>
		<comments>http://www.undisciplinedbytes.com/2011/04/some-open-source-projects-for-net-you-should-take-a-look-at/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 12:49:52 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=569</guid>
		<description><![CDATA[We all agree that ASP.NET and Visual Studio are amazing technologies with state-of-the-art tools. One requires almost nothing more to write any kind of software – except for a database. However, there are some tools that greatly simplify that task. It depends on what you’re trying to achieve, but I’m sure some of these projects [...]]]></description>
			<content:encoded><![CDATA[<p>We all agree that ASP.NET and Visual Studio are amazing technologies with state-of-the-art tools. One requires almost nothing more to write any kind of software – except for a database. However, there are some tools that greatly simplify that task. It depends on what you’re trying to achieve, but I’m sure some of these projects will help you get your tasks accomplished faster and easier:</p>
<ul>
<li><a href="http://nhforge.org/Default.aspx" target="_blank">NHibernate</a>: NHibernate is a mature, open source object-relational mapper for the .NET framework. It&#8217;s actively developed, fully featured and used in thousands of successful projects.</li>
<li><a href="http://www.nunit.org/index.php" target="_blank">NUnit</a>: NUnit is a unit-testing framework for all .Net languages, initially ported from  JUnit. It is written entirely in C# and has been completely redesigned to take advantage of many .NET language features, for example custom attributes and other reflection related capabilities. NUnit brings xUnit to all .NET languages</li>
<li><a href="http://jquery.com/" target="_blank">jQuery</a>: jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.</li>
<li><a href="http://ayende.com/projects/rhino-mocks.aspx" target="_blank">Rhino.Mocks</a>: A dynamic mock object framework for the .Net platform. It&#8217;s purpose is to ease testing by allowing the developer to create mock implementations of custom objects and verify the interactions using unit testing.</li>
<li><a href="http://www.codeplex.com/MVCContrib" target="_blank">MVC Contrib</a>: This is the contrib project for the ASP.NET MVC framework. This project adds additional functionality on top of the MVC Framework. These enhancements can increase your productivity using the MVC Framework. It is written in C#. Founded by Eric Hexter and Jeffrey Palermo.</li>
<li><a href="http://confluence.public.thoughtworks.org/display/CCNET/Welcome+to+CruiseControl.NET" target="_blank">CruiseControl.NET</a>: CruiseControl.NET is an Automated Continuous Integration server, implemented using the Microsoft .NET Framework.</li>
<li><a href="http://code.google.com/p/sharp-architecture/" target="_blank">S#arp Architecture</a>: Pronounced &#8220;Sharp Architecture,&#8221; this is a solid architectural foundation for rapidly building maintainable web applications leveraging the ASP.NET MVC framework with NHibernate. The primary advantage to be sought in using any architectural framework is to decrease the code one has to write while increasing the quality of the end product. A framework should enable developers to spend little time on infrastructure details while allowing them to focus their attentions on the domain and user experience.</li>
<li><a href="http://sparkviewengine.com/" target="_blank">Spark View Engine</a>: <strong></strong>Spark is a view engine for Asp.Net Mvc and Castle Project MonoRail frameworks. The idea is to allow the html to dominate the flow and the code to fit seamlessly.</li>
<li><a href="http://tortoisesvn.tigris.org/" target="_blank">TortoiseSVN</a>: <strong></strong>A Subversion client, implemented as a windows shell extension. TortoiseSVN is a really easy to use Revision control / version control / source control software for Windows. Since it&#8217;s not an integration for a specific IDE you can use it with whatever development tools you like.</li>
<li><a href="http://www.castleproject.org/container/index.html" target="_blank">Castle Windsor</a>: Castle Project offers two Inversion of Control Containers. The MicroKernel and the Windsor Container. Castle Windsor aggregates the MicroKernel  and exposes a powerful configuration support. It is suitable for common enterprise application needs. It is able to register facilities and components based on the configuration and adds support for interceptors.</li>
</ul>
<p>Take a look at these projects. I&#8217;m sure you’ll be able to find some good use for them in your projects!</p>
<p>Via <a href="http://dotnet.dzone.com/articles/t0p-10-open-source-projects" target="_blank">.NET Zone</a>.</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2009/10/open-source-software/' rel='bookmark' title='Permanent Link: Open Source Software'>Open Source Software</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/08/unit-testing/' rel='bookmark' title='Permanent Link: Unit testing'>Unit testing</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/12/persistence-layer-object-relational-mapping-tools/' rel='bookmark' title='Permanent Link: Persistance layer: object-relational mapping tools'>Persistance layer: object-relational mapping tools</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/07/improving-software-quality-automatic-testing/' rel='bookmark' title='Permanent Link: Improving software quality: automatic testing'>Improving software quality: automatic testing</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/09/parallel-programming-with-net/' rel='bookmark' title='Permanent Link: Parallel programming with .NET'>Parallel programming with .NET</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2011/04/some-open-source-projects-for-net-you-should-take-a-look-at/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Which database to use for a personal project?</title>
		<link>http://www.undisciplinedbytes.com/2011/03/which-database-to-use-for-a-personal-project/</link>
		<comments>http://www.undisciplinedbytes.com/2011/03/which-database-to-use-for-a-personal-project/#comments</comments>
		<pubDate>Thu, 24 Mar 2011 14:03:16 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[backend]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=458</guid>
		<description><![CDATA[When starting a new project, the choice of which database to use usually boils down to the one the company usually uses. The systems that are usually considered are just Oracle and Microsoft’s SQL server (MS-SQL). There’s no doubt that big bucks companies have the resources to make the best-of-its-breed products, and almost no other [...]]]></description>
			<content:encoded><![CDATA[<p>When starting a new project, the choice of which database to use usually boils down to the one the company usually uses. The systems that are usually considered are just Oracle and Microsoft’s SQL server (MS-SQL). There’s no doubt that big bucks companies have the resources to make the best-of-its-breed products, and almost no other database system comes close to what Oracle and MS-SQL can offer in terms of quality, features, and highly skilled professionals around their products. Yes, all this is true, but there’s a price. And usually this price tag can only be afforded by a company, not by a sole individual.</p>
<p>So, what if I want to develop my own personal project and I require a RDBMS (<em>Relational DataBase Management System</em>)? Oracle and MS-SQL are out of the question because of their high cost. MS-SQL Express has great limitations, so, although it is free, we won’t be considering it for our purpose. Which is the best open source RDBMS?</p>
<p><span id="more-458"></span></p>
<p>Today’s most known open source RDBMS are MySQL and PostgreSQL. PostgreSQL is considered to beat MySQL in the features war. This used to be true some time ago, but it’s no longer the case anymore. 99% of the needs you will have with your RDBMS will be covered by both MySQL and PostgreSQL. Still, there’s many people who think that MySQL has improved a lot, but it’s just not PostgreSQL.</p>
<p>If you want to compare performance, fans of both systems will tell you their RDBMS is faster than the other one. On one hand, PostgreSQL has been recently paying attention to speed, and has been making its product faster and faster with every release. On the other hand, MySQL proponents say that, if configured properly, MySQL’s MyISAM tables are indeed lightweight and make for a faster database.</p>
<p>But probably, the most important thing to consider when choosing which RDBMS you should use, is which one is easier to use. I can tell you that previous Oracle experience will make you feel at home and comfortable with PostgreSQL. Still, the MySQL community seems to be much bigger than PostgreSQL’s, so, should you require some assistance when developing your project, if you’re using MySQL chances are that you will get quick help from them.</p>
<p>MySQL is licensed under the GNU GPL, but offers an alternative commercial license for those who don’t want to be restricted by that license. PostgreSQL uses the BSD license, which stipulates that the credits have to be maintained, but beyond that you can do whatever you want with it.</p>
<p>But, after all, it just all comes down to personal tastes. Just choose whatever you like and whichever you think might fit your needs better. What might be the best for one person, might be a bad choice for another.</p>
<p>Check these links for more info:</p>
<ul>
<li><a href="http://www.devx.com/dbzone/Article/29480" target="_blank">Open Source Database Feature Comparison Matrix</a></li>
<li><a href="http://en.wikipedia.org/wiki/Comparison_of_relational_database_management_systems" target="_blank">Comparison of RDBMS</a></li>
</ul>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2010/01/sluggish-database-view-materialize-it/' rel='bookmark' title='Permanent Link: Sluggish database view? Materialize it!'>Sluggish database view? Materialize it!</a></li>
<li><a href='http://www.undisciplinedbytes.com/2012/01/entity-framework/' rel='bookmark' title='Permanent Link: Entity Framework'>Entity Framework</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/12/persistence-layer-object-relational-mapping-tools/' rel='bookmark' title='Permanent Link: Persistance layer: object-relational mapping tools'>Persistance layer: object-relational mapping tools</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2011/03/which-database-to-use-for-a-personal-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Object Oriented JavaScript III: some tips</title>
		<link>http://www.undisciplinedbytes.com/2011/01/object-oriented-javascript-iii-some-tips/</link>
		<comments>http://www.undisciplinedbytes.com/2011/01/object-oriented-javascript-iii-some-tips/#comments</comments>
		<pubDate>Thu, 13 Jan 2011 12:47:38 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=531</guid>
		<description><![CDATA[We’ve already gone through the basic JavaScript concepts we need to write object oriented code, and we’ve already seen the basic pattern. But here’s a couple of tips that has proven useful many times when writing long and complex JavaScript code.
The curious case of the mutating scope
Ok, now imagine we have some fancy widget in [...]]]></description>
			<content:encoded><![CDATA[<p>We’ve already gone through the basic JavaScript concepts we need to write object oriented code, and we’ve already seen the basic pattern. But here’s a couple of tips that has proven useful many times when writing long and complex JavaScript code.</p>
<h4>The curious case of the mutating scope</h4>
<p>Ok, now imagine we have some fancy widget in our class and we want to interact with it. There are many widgets which redefine the <code>this</code> keyword to its instance instead of ours. For this case, we might want to be able to access our instance instead of the widget instance. We’ll need to redefine the scope in which the function is being executed. To do so, we can use the following function:</p>
<pre class="brush:javascript">// Creates a new function and returns a reference. This new function has its scope set to 'newScope'
this.setScope = function (originalFn, newScope) {
   return function () { return originalFn.apply(newScope, arguments); };
};</pre>
<p>We could just use the apply method on our original function, but I prefer this way since its more comfortable to work with parameters like this.</p>
<p>Be careful when using this, since this is a double-edged sword. By accessing our class scope we have all our data available. But we won’t have the widget’s scope available anymore. So make sure you don’t need to access the widget’s scope before changing the scope of the event handler.</p>
<p><span id="more-531"></span></p>
<h4>Accessing your instance from the global scope</h4>
<p>When you do something like</p>
<p><code>var Peter = new Person();</code></p>
<p>you&#8217;re creating a new instance of the class <code>Person</code>. And the variable <code>Peter</code> is just a pointer to the instance, not the instance itself. <code>Peter</code> is the way we have to access that instance from global scope: it’s just a pointer. But we have no way to know the name the pointer to the instance was given from within the instance itself. This means, we have no way to know that <code>Peter</code> is the way we have to access the instance from global scope.</p>
<p>So, if we’re ever in the need of calling a method from a global scope, we’ll need to keep an array of instances ourselves. We could be doing something like:</p>
<pre class="brush:javascript">var Person2 = function (name, surname, age) {
	var _name, _surname, _age;

	this.showData = function () {
		var str = new String();
		str = "Name: " + _name + " - ";
		str += "Surname: " + _surname + " - ";
		str += "Age: " + _age;
		alert(str);
	};	

	// ---- Constructor -----------------------------------------------
	_name = (typeof name === "undefined" ? "John" : name);
	_surname = (typeof surname === "undefined" ? "Smith" : surname);
	_age = (typeof age === "undefined" ? 20 : age);

	// Store pointers to all instances of this class, so we can access their
	// context from global code
	var _instanceIndex = Person2._instances.length;
	Person2._instances[_instanceIndex] = this;
};
Person2._instances = new Array();</pre>
<p>So now we have an array containing all the different instances of the object:</p>
<pre class="brush:javascript">var Peter = new Person2();  // Equivalent to Person2._instances[0]
var John = new Person2('John', 'Smith');  // Equivalent to Person2._instances[1]</pre>
<p>This is useful if you’re declaring some event handlers in the HTML code. Something like:</p>
<pre>&lt;div id="myDiv" onclick="Person2._instances[1].showData();"&gt;Hello world!&lt;/div&gt;</pre>
<p><strong>CAUTION!! This is not something I would recommend. This is not a good practice in JavaScript. Behaviour should be separated from the content, and with this you’re just mixing both together.</strong></p>
<p>I’m just showing this as a possibility, in case you’re in the need of something similar. But be aware that by using <code>onclick</code> event handlers like this, you’re making your code less readable and less maintainable. <strong>Be careful!</strong></p>
<p>Well, now you have to start practicing all these concepts you’ve just read. Start doing some JavaScript programming. The more you do it, the better programmer you’ll become.</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2010/12/object-oriented-javascript-ii-the-pattern/' rel='bookmark' title='Permanent Link: Object Oriented JavaScript II: the pattern'>Object Oriented JavaScript II: the pattern</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/12/object-oriented-javascript-i-the-basics/' rel='bookmark' title='Permanent Link: Object Oriented JavaScript I: the basics'>Object Oriented JavaScript I: the basics</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/01/javascript-best-practices-i/' rel='bookmark' title='Permanent Link: JavaScript best practices I'>JavaScript best practices I</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2011/01/object-oriented-javascript-iii-some-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Object Oriented JavaScript II: the pattern</title>
		<link>http://www.undisciplinedbytes.com/2010/12/object-oriented-javascript-ii-the-pattern/</link>
		<comments>http://www.undisciplinedbytes.com/2010/12/object-oriented-javascript-ii-the-pattern/#comments</comments>
		<pubDate>Tue, 21 Dec 2010 15:30:34 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=481</guid>
		<description><![CDATA[Ok, now that we’ve covered the most basic concepts of JavaScript, we can now proceed to take a look at the pattern itself.
The first feature we need to implement is private and public members. Using closures, this is easy to achieve:

var Person = function () {
	// Private elements
	var _name, _surname, _age;

	// Public elements
	this.getName = function [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, now that we’ve covered the most basic concepts of JavaScript, we can now proceed to take a look at the pattern itself.</p>
<p>The first feature we need to implement is private and public members. Using closures, this is easy to achieve:</p>
<p><span id="more-481"></span></p>
<pre class="brush:javascript">var Person = function () {
	// Private elements
	var _name, _surname, _age;

	// Public elements
	this.getName = function () {
		return _name;
	};
	this.setName = function (newName) {
		_name = newName;
	};
	this.getSurname = function () {
		return _surname;
	};
	this.setSurname = function (newSurname) {
		_surname = newSurname;
	};
	this.getAge = function () {
		return _age;
	};
	this.setAge = function (newAge) {
		_age = newAge;
	};
};</pre>
<p>By declaring functions inside the outer function <code>Person</code>, we’re creating a closure which makes the elements <code>_name</code>, <code>_surname</code> and <code>_age</code> inaccessible by members which are not inside the <code>Person</code> function. On the contrary, all elements declared with the <code>this</code> keyword will be accessible from outside of the <code>Person</code> function.</p>
<p>If we want to set some code to be executed when this class is instantiated, we can do so anywhere inside the Person function:</p>
<pre class="brush:javascript">var Person = function (name, surname, age) {
	// Private elements
	var _name, _surname, _age;

	// Public elements
	this.getName = function () {
		return _name;
	};
	this.setName = function (newName) {
		_name = newName;
	};
	this.getSurname = function () {
		return _surname;
	};
	this.setSurname = function (newSurname) {
		_surname = newSurname;
	};
	this.getAge = function () {
		return _age;
	};
	this.setAge = function (newAge) {
		_age = newAge;
	};

	// Default constructor
	this.setName(typeof name === "undefined" ? "John" : name);
	this.setSurname(typeof surname === "undefined" ? "Smith" : surname);
	this.setAge(typeof age === "undefined" ? 20 : age);
};</pre>
<p>This way, we’re creating constructors.  If the <code>name</code>, <code>surname</code> and <code>age</code> variables are given a value when this class is instantiated, that value will be assigned to the private properties; if not, then default values will be used. So we can now check how what we’ve written behaves so far. I’ll use the <a title="firebug" href="http://getfirebug.com/" target="_blank">Firebug </a>console to check what our JavaScript does:</p>
<pre class="brush:javascript">&gt;&gt;&gt; var John = new Person()
&gt;&gt;&gt; John.getName()
"John"
&gt;&gt;&gt; John.getSurname()
"Smith"
&gt;&gt;&gt; John.getAge()
20
&gt;&gt;&gt; var Peter = new Person("Peter", "Jackson")
&gt;&gt;&gt; Peter.getName()
"Peter"
&gt;&gt;&gt; Peter.getSurname()
"Jackson"
&gt;&gt;&gt; Peter.getAge()
20</pre>
<p>Notice how private properties get the the default values if we instantiate the class with no values for the constructor.</p>
<p>So we now have private and public elements, the most basic features of object oriented techniques. But there’s actually different kinds of public properties. Depending on how you declare them, some public elements will actually behave slightly different:</p>
<pre class="brush:javascript">var Person = function (name, surname, age) {
	// ---- Private members ------------------------
	var _name, _surname, _age;

	// ---- Public, privileged members ------------------------
	// Privileged members are exposed through the API and have access to
	// private members

	this.getName = function () {
		return _name;
	};
	this.setName = function (newName) {
		_name = newName;
	};
	this.getSurname = function () {
		return _surname;
	};
	this.setSurname = function (newSurname) {
		_surname = newSurname;
	};
	this.getAge = function () {
		return _age;
	};
	this.setAge = function (newAge) {
		_age = newAge;
	};

	// ---- Default constructor ------------------------
	this.setName(typeof name === "undefined" ? "John" : name);
	this.setSurname(typeof surname === "undefined" ? "Smith" : surname);
	this.setAge(typeof age === "undefined" ? 20 : age);
};

// ---- Public, non-privileged members ------------------------
// All elements included in the prototype are exposed through the API and
// do not have access to private members.
// All instances of this class will have these members available.
Person.prototype.getPersonData = function () {
	var str = new String();
	str = "Name: " + this.getName() + " - ";
	str += "Surname: " + this.getSurname() + " - ";
	str += "Age: " + this.getAge();
	return str;
};

// ---- Public, static member for current instance ------------------------
// Members defined like this are related to the object itself, not to instances.
// These members have no means of accessing instance or private data.  They can't
// access instance data because they don't have the "this" operator set to any
// particular instance.
// These members relate to the instance in which they are defined. Since we're defining
// them for the class definition, instances of this class will not have these members
// available.
Person.AreEqual = function (p1, p2) {
	return p1.getName() === p2.getName() &amp;&amp;
			p1.getSurname() === p2.getSurname() &amp;&amp;
			p1.getAge() === p2.getAge();
};</pre>
<p>Notice how depending on how you declare your public members, they will have different access level to the information within your instance:</p>
<ul>
<li>Members declared inside the closure with the <code>this</code> keyword are privileged: they have access to private members.</li>
<li>Members declared with the <code>prototype</code> keyword are non-privileged: they do not have access to private members. These members will be available in all instances of this class.</li>
<li>Members declared like “class name” + “.” + “member name” are public static members for the current instance. They don’t have the “this” operator available, and other instances of this class will not have these members available.</li>
</ul>
<p>So, here’s how our class behaves:</p>
<pre class="brush:javascript">&gt;&gt;&gt; var John = new Person()
&gt;&gt;&gt; John.getPersonData()
"Name: John - Surname: Smith - Age: 20"
&gt;&gt;&gt; Person.AreEqual(new Person(), John)
true
&gt;&gt;&gt; var Peter = new Person("Peter")
&gt;&gt;&gt; Person.AreEqual(John, Peter)
false
&gt;&gt;&gt; John.AreEqual(John, Peter)
TypeError: John.AreEqual is not a function { message="John.AreEqual is not a function", more...}</pre>
<p>Notice how the <code>AreEqual</code> method is only available for the object <code>Person</code>, not for its instances.</p>
<p>Ok, now we’ve covered public and private elements, constructors and the different access levels public members may have. We&#8217;ve gone through all we need to write object oriented code. But keep on reading to see some tips about OO code in JavaScript.</p>
<p>Stay tuned for my next post!</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2011/01/object-oriented-javascript-iii-some-tips/' rel='bookmark' title='Permanent Link: Object Oriented JavaScript III: some tips'>Object Oriented JavaScript III: some tips</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/12/object-oriented-javascript-i-the-basics/' rel='bookmark' title='Permanent Link: Object Oriented JavaScript I: the basics'>Object Oriented JavaScript I: the basics</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2010/12/object-oriented-javascript-ii-the-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Object Oriented JavaScript I: the basics</title>
		<link>http://www.undisciplinedbytes.com/2010/12/object-oriented-javascript-i-the-basics/</link>
		<comments>http://www.undisciplinedbytes.com/2010/12/object-oriented-javascript-i-the-basics/#comments</comments>
		<pubDate>Thu, 02 Dec 2010 10:06:14 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=474</guid>
		<description><![CDATA[Today’s web applications are inconceivable without the use of JavaScript. It has become a fundamental part in modern web systems. A lot of time and effort must be put into client-side programming to make our application have that modern look &#38; feel that JavaScript provides and we all like to see.
Until AJAX techniques emerged, JavaScript [...]]]></description>
			<content:encoded><![CDATA[<p>Today’s web applications are inconceivable without the use of JavaScript. It has become a fundamental part in modern web systems. A lot of time and effort must be put into client-side programming to make our application have that modern look &amp; feel that JavaScript provides and we all like to see.</p>
<p>Until AJAX techniques emerged, JavaScript was not seen as an application development platform. It was treated merely as a scripting language to implement minimal dynamic web page behavior. Up till not too long ago, all the JavaScript I needed to write was just a few lines of code to make some validations, a few animations and some AJAX calls… but this has definitely changed today. The number of JavaScript lines I am writing nowadays for the web apps I’m developing are counted in the thousands. Heck, the last module I developed at work contained more than 12000 lines of JavaScript code! This means that I can no longer just write code without order. I need to have some logic behind the process of writing code. And this is when object oriented techniques come into play.</p>
<p><span id="more-474"></span></p>
<p>Much has been written about JavaScript and how to use its object oriented features. The thing is, although JavaScript has some object oriented (OO) features, how to use them is not a trivial matter. JavaScript (JS) is quite different from other OO languages like C# or Java, so if you’re coming from these types of languages, after some while you’ll realize that JS behaves differently in a few ways. It’s not that it’s better or worse, it’s just different. And if you ever want to get to master JS, you’ll need to be aware of them.</p>
<p>There’s a bunch of different approaches on how to use OO features and techniques. The most widely spread methodology for writing classes is the <a title="module pattern" href="http://yuiblog.com/blog/2007/06/12/module-pattern" target="_blank">module pattern</a>, made popular by the <a title="YUI JavaScript library" href="http://developer.yahoo.com/yui/" target="_blank">YUI JavaScript library</a>.</p>
<p>I’ve spent some time digging through the internet trying to find what the best pattern for OO JavaScript is. Unfortunately, there doesn’t seem to be any kind of standard, and people just implement whatever better suits their needs. So, looking around, I came up with my own pattern for writing classes and object oriented JavaScript. It is somehow derived from stuff that I’ve seen throughout the web, and with some additional features I consider useful.</p>
<p>But before explaining this pattern, you first need to understand some basics about JavaScript:</p>
<p><strong>JavaScript has loose typing.</strong> At any given time, the type of a variable is the type of whatever is assigned to it. I could perfectly do something like:</p>
<pre class="brush:javascript">var myVariable = 34;
myVariable = "Hi there!";</pre>
<p>And no error would be thrown.</p>
<p><strong>JavaScript has anonymous functions</strong>. To show this concept I&#8217;ll use the function <code>setTimeout</code>, which is used for executing some code after some specified amount of time:</p>
<pre class="brush:javascript">function sayHi() {
   alert("Hi!");
};
setTimeout(sayHi, 1000);</pre>
<p>This shows a message with the text “Hi!” after 1000 ms. The same thing can be achieved by an anonymous function:</p>
<pre class="brush:javascript">setTimeout(function () { alert("Hi!"); }, 1000);</pre>
<p><strong>JavaScript has function pointers</strong>. The previous function can be expressed as:</p>
<pre class="brush:javascript">var sayHi = function () {
   alert("Hi!");
};</pre>
<p>And it would still be called in the same way, <code>sayHi().</code></p>
<p><strong>JavaScript has closures</strong>. Now, this is a little bit tricky. Closures are something which is not fully understood by all programmers. But they really aren’t all that complicated. Let’s take a look at them:</p>
<p>A &#8220;closure&#8221; is an expression (usually a function) that has variables together inside an environment that binds those variables. In other languages, when a function is called, a new stack frame is created and then destroyed when the function completes. So, if a function is called 3 times, 3 different stack frames are created with 3 different set of local variables. JavaScript behaves differently. In JS, it is as if the stack frame for the closure was not deallocated from memory when the function returns. It&#8217;s as if it was given a static piece of memory and always stayed there. So if you call the closure 3 times, the same set of local variables are accessed, having the same value. That means, having the last value they had the last time that closure was called.</p>
<p>An example to clarify things a little. Take a look at this piece of code:</p>
<pre class="brush:javascript">function initGlobalVariables {
   var someNumber = 123;
   var someString = "Hello World!";
   var someFunction = function () { alert(someString); };
};</pre>
<p>By writing a function inside some other function, we’re creating a closure. The scope of the inner function is the outer function: this means that the function assigned to <code>someFunction</code> can access the variables <code>someNumber</code> and <code>someString</code>. And every time we call the function assigned to <code>someFunction</code>, the variables will not be created and destroyed: they will remain there with the last value they had.</p>
<p>Just remember these key facts for closures:</p>
<ul>
<li>A closure is the local variables for a function &#8211; kept alive after the function has returned.</li>
<li>Whenever you use a function inside another function, a closure is used.</li>
<li>Whenever you call the outer function, a new closure is created.</li>
<li>Whenever you call an inner function of the closure, no new closure is created: you access the existing closure with the last values they had the last time you exited an inner function of the closure.</li>
</ul>
<p><strong>The <code>this</code> keyword refers to the closure within it is located.</strong> The value of the <code>this</code> keyword refers to the scope of the function. That is, the closure within it exists.</p>
<p>Ok, with this we’ve covered the very basics of JavaScript we need to understand object oriented techniques. In my next post I’ll start covering the pattern, so stay tuned!</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2010/12/object-oriented-javascript-ii-the-pattern/' rel='bookmark' title='Permanent Link: Object Oriented JavaScript II: the pattern'>Object Oriented JavaScript II: the pattern</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/01/object-oriented-javascript-iii-some-tips/' rel='bookmark' title='Permanent Link: Object Oriented JavaScript III: some tips'>Object Oriented JavaScript III: some tips</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/01/javascript-best-practices-i/' rel='bookmark' title='Permanent Link: JavaScript best practices I'>JavaScript best practices I</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/12/persistence-layer-object-relational-mapping-tools/' rel='bookmark' title='Permanent Link: Persistance layer: object-relational mapping tools'>Persistance layer: object-relational mapping tools</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/02/javascript-best-practices-ii/' rel='bookmark' title='Permanent Link: JavaScript best practices II'>JavaScript best practices II</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2010/12/object-oriented-javascript-i-the-basics/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

