<?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>Uncategorized &#8211; Cognim &#8211; Internet development</title>
	<atom:link href="https://www.cognim.co.uk/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.cognim.co.uk</link>
	<description>Enterprise system implementation. Making the complex simple</description>
	<lastBuildDate>Tue, 10 Nov 2015 10:10:24 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
<site xmlns="com-wordpress:feed-additions:1">91553907</site>	<item>
		<title>Gulp and Visual Studio: Code quality</title>
		<link>https://www.cognim.co.uk/gulp-and-visual-studio-code-quality/</link>
					<comments>https://www.cognim.co.uk/gulp-and-visual-studio-code-quality/#respond</comments>
		
		<dc:creator><![CDATA[Darren Hall]]></dc:creator>
		<pubDate>Tue, 10 Nov 2015 09:58:30 +0000</pubDate>
				<category><![CDATA[Gulp]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<guid isPermaLink="false">http://www.cognim.co.uk/?p=5258</guid>

					<description><![CDATA[This is Part 4 is my series on using Gulp with Visual Studio. So far we&#8217;ve covered setting up Visual Studio for use with Gulp, processing SASS, and getting the files ready for deployment. Part 1: Setting up Gulp with Visual Studio Part 2: Getting SASSy Part 3: Deployment: minification and concatenation Part 4: Code [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>This is Part 4 is my series on using Gulp with Visual Studio. So far we&#8217;ve covered setting up Visual Studio for use with Gulp, processing SASS, and getting the files ready for deployment.</p>
<ul>
<li><a href="https://www.cognim.co.uk/getting-started-with-gulp-and-vs/" target="_blank">Part 1: Setting up Gulp with Visual Studio</a></li>
<li><a href="https://www.cognim.co.uk/gulp-and-visual-studio-sass/">Part 2: Getting SASSy</a></li>
<li><a href="https://www.cognim.co.uk/gulp-vs-minification/">Part 3: Deployment: minification and concatenation</a></li>
<li>Part 4: Code quality: tests and linting</li>
<li>Part 5: Thoughts on Gulp</li>
</ul>
<p>In this post I want to talk about JavaScript code quality.</p>
<h3>Code quality: tests and linting</h3>
<p>JavaScript is a notoriously quirky language &#8211;  the tricky &#8220;this&#8221; operator, lack of block level scoping, the ability to use variables before declaring them, &#8220;==&#8221; vs &#8220;===&#8221;, etc etc.</p>
<p>We can do a number of things to tighten up our code quality. We can run tests to ensure our code works as expected, and we can run a linting program to catch any syntatic mistakes before running our code. Combining this with Gulp gives a nice workflow for development.</p>
<h4>Linting with JSHint</h4>
<p><a href="http://stackoverflow.com/questions/8503559/what-is-linting" target="_blank">Linting</a> is a process of running a program to analyse code for potential problems, catching bugs before the program gets executed. Douglas Crockford created <a href="http://www.jslint.com/" target="_blank">JSLint</a> in 2011, and of course there is a <a href="https://www.npmjs.com/package/gulp-jslint" target="_blank">gulp plugin</a> for it. However, I prefer to use <a href="http://jshint.com/" target="_blank">JSHint</a> &#8211; a community offshoot of JSLint that allows for more customisation and is slightly more relaxed in its defaults. You can find a good comparison of the two <a href="http://stackoverflow.com/a/10763615" target="_blank">here</a>.</p>
<p>First, I would seperate out any third-party libraries or frameworks you have into their own folder(s). In my opinion this is good practice anyway, but especially if we are running linting &#8211; we don&#8217;t want failures on code we haven&#8217;t written! Next, we want to add the <a href="https://www.npmjs.com/package/gulp-jshint" target="_blank">plugin</a> to our package.json:</p>
<pre class="lang:js decode:true ">{
  "scripts": { "install": "echo Done" },
    
  "devDependencies": {
    "gulp": "~3.9.0",
    "gulp-load-plugins": "~0.10.0",
    "gulp-jshint": "~1.11.2",
    // other plugins here
  } 
}</pre>
<p>Then, add the Gulp task to our gulpfile:</p>
<pre class="lang:c# decode:true" title="gulpfile.js">var sources = ['./Scripts/*.js', '!./Scripts/libs/*.js'];

gulp.task('watch-lint', function () {
    gulp.watch(sources, ['lint']);
});

gulp.task('lint', function () {
    return gulp.src(sources)
        .pipe(plugins.jshint({
            strict: true,
            predef: [""]
        }))
        .pipe(plugins.jshint.reporter('default'))
        .pipe(plugins.jshint.reporter('fail'))
    ;
});</pre>
<p>Here&#8217;s a line by line breakdown of the important stuff:</p>
<ul>
<li>Line 1: Define the JavaScript files we want linting, excluding any third party libraries and frameworks</li>
<li>Line 3-5: Set up a watch to run the linting task whenever we make a change to our JavaScript</li>
<li>Line 7: Set up our linting task</li>
<li>Line 8-12: Send our files to JSHint. JSHint has a number of options you can set to define how lenient or strict you want it to be. You can look at the <a href="http://jshint.com/docs/" target="_blank">docs</a> to get an idea of what you can set. Here I&#8217;m only using the strict option (<a href="http://www.nczonline.net/blog/2012/03/13/its-time-to-start-using-javascript-strict-mode/" target="_blank">here</a> is a good blog explaining strict mode). There is also a section to define any global variables you&#8217;re using that should be ignored &#8211; for example if you&#8217;re using Angular you&#8217;ll want to add &#8220;angular&#8221; here</li>
<li>Line 13-14: Send the output of the linting to some inbuilt reporters &#8211; one to output the results to console and one to raise an error for any failures</li>
</ul>
<p>Now we can add the watch task to our default task, and we&#8217;ll have linting every time we change our code.</p>
<h4>Tests with Karma</h4>
<p>One of the great things about Gulp is that you can get a Gulp task to run other Node based packages directly. For example, you can run <a href="http://karma-runner.github.io" target="_blank">Karma</a> within a task, without having to use a plugin. Combined with <a href="http://phantomjs.org/" target="_blank">PhantomJS</a>, a headless browser, you get a great unit testing framework for your JavaScript. First, add the packages:</p>
<pre class="lang:c# decode:true " title="package.json">{
  "scripts": { "install": "echo Done" },
    
  "devDependencies": {
    "gulp": "~3.9.0",
    "karma-jasmine": "~0.3.6",
    "karma": "~0.13.9",
    "karma-phantomjs-launcher": "~0.2.1"
    // other plugins here
  } 
}</pre>
<p>Then, we&#8217;ll need to add our karma config file (karma.conf.js) at the root of the project :</p>
<pre class="lang:js decode:true" title="karma.conf.js">module.exports = function (config) {
    config.set({
        files: ['./Scripts/*.js',
                './Scripts/Specs/**/*.js',
        ],

        exclude: ['./Scripts/libs/*.js'],

        frameworks: ['jasmine'],

        autoWatchBatchDelay: 1000,

        reporters: ['progress'],

        browsers: ['PhantomJS']
    });
};</pre>
<p>One thing I&#8217;ve found using Karma in Visual Studio: often Karma can crash in Visual Studio with an &#8220;EBUSY&#8221; error. I haven&#8217;t found an adequate explanation for this yet &#8211; I think it&#8217;s because when you modify and save a file Visual Studio writes a temporary file to replace the file you are saving, which doesn&#8217;t sit well with Node. This can be mitigated by setting the &#8220;autoWatchBatchDelay&#8221; to 1000, which waits 1 second after any change before running tests.</p>
<p>Finally, our gulp task:</p>
<pre class="lang:js decode:true">var Server = require('karma').Server;

gulp.task('runTests', function () {
    new Server({
        configFile: __dirname + '/karma.conf.js',
        singleRun: false
    }).start();
});</pre>
<p>Here, we are simply firing up Karma from within a Gulp task and passing in our karma config file. We are also telling Karma to watch our files for any changes and re-run our tests if they change. If you are going to run this task on a build server (and you should), you&#8217;ll need to configure the singleRun variable here. This can be done by using <a href="https://www.npmjs.com/package/yargs" target="_blank">yargs</a> to pass in a variable to switch this behaviour.</p>
<p>There we are! There is a commit of all the changes <a href="https://github.com/edwardridge/Gulp-and-Visual-Studio/commit/92ba6c092e262261171c642aa765129238e00283">here</a>, which includes a sample test. I highly recommend using both linting and unit tests when writing JavaScript code, and Gulp is great at running these tasks for us.</p>
<p>Next, in the last past I want to share my thoughts on Gulp, specifically within the .NET ecosystem.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.cognim.co.uk/gulp-and-visual-studio-code-quality/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5258</post-id>	</item>
		<item>
		<title>Re-usable, informative validation error messages in Angular</title>
		<link>https://www.cognim.co.uk/re-usable-informative-validation-error-messages-in-angular/</link>
					<comments>https://www.cognim.co.uk/re-usable-informative-validation-error-messages-in-angular/#respond</comments>
		
		<dc:creator><![CDATA[Darren Hall]]></dc:creator>
		<pubDate>Wed, 27 May 2015 14:32:28 +0000</pubDate>
				<category><![CDATA[Uncategorized]]></category>
		<guid isPermaLink="false">http://www.cognim.co.uk/?p=4912</guid>

					<description><![CDATA[A while ago (at NDC London) I attented a two day course on Angular by the great K. Scott Allen, who spoke about using the ngMessages directive to provide re-usable validation on forms. While ngMessages is good, there is no way of providing detail on validation failures. For example, if a text input has to [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>A while ago (at NDC London) I attented a two day course on Angular by the great K. Scott Allen, who spoke about using the <a href="https://docs.angularjs.org/api/ngMessages/directive/ngMessages" title="ngMessages">ngMessages</a> directive to provide re-usable validation on forms. </p>
<p>While ngMessages is good, there is no way of providing detail on validation failures. For example, if a text input has to be at least 5 characters long, you can provide a message saying &#8220;your entry is too short&#8221;, but not &#8220;your entry needs to be at least 5 characters&#8221;. This is frustrating for an end user, who would need to keep entering characters until they reach the unknown limit. You could hardcode the limit into the validation message for the input, but this would defeat re-usability which is one of the main benefits of ngMessages.</p>
<p>Therefore, I&#8217;ve come up with a directive that provides re-usable, informative validation error messages. It works with HTML validation attributes as well as custom validators. You provide a template with the error messages (much like ngMessagesInclude). This template can access the property of each validation attribute and use it to display a more informative message.</p>
<p>A Plunkr is available <a href="http://plnkr.co/edit/ualgs5GI7DTFGaZxHzCp" title="here">here</a>.</p>
<p>To use it, add the validator.js code and ngMessages to your Angular app. Then, create a template for the error messages:<br />
<code><br />
<!-- file: validation-messages.html --></p>
<div ng-messages='error' ng-messages-multiple>
<div>Please enter at least {{ minlength }} letters</div>
<div>Please do not enter more than {{ maxlength }} letters</div>
<div>Please enter text beginning with {{ startWith }}</div>
</div>
<p></code></p>
<p>N.B: the &#8220;startWith&#8221; message is for a custom validator that checks that the input starts with a particular character.</p>
<p>The containing div with &#8220;ng-messages&#8221; needs to be kept as is, while the inner divs containing the validation messages can be changed to your own custom message. Note the binding within each message.</p>
<p>Next, we can use the validator and the template on an input:</p>
<p><code><br />
< input type="text" 
      name="exampleTextBox" 
      ng-model="name" 
      minlength="5" 
      maxlength="10" 
      start-with="s" 
      validator="validation-messages.html" 
    /><br />
</code></p>
<p>In the last attribute, we specify we are using the validator and pass in the location of the template to use. Also note you <b>must give the input a name and use ngModel for the validator to work</b>.</p>
<p>Now, if we try to enter some input that is fewer than 5 characters, greater than 10 or doesn&#8217;t start with s we get a friendly error message telling us exactly what went wrong <b>and why</b>. If we fail more than one validation, we get a message for each failure.</p>
<p>The name of the binding within each message matches the name of the validation attribute, and the value it is bound to matches the property value of the attribute. So, &#8220;Please enter at least {{ minlength }} letters&#8221; for minlength=&#8221;5&#8243; becomes &#8220;Please enter at least 5 letters&#8221;.</p>
<p>You can see a fully working example in the Plunkr.</p>
<p>Next time, I&#8217;ll go through the code for the validator directive.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.cognim.co.uk/re-usable-informative-validation-error-messages-in-angular/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4912</post-id>	</item>
		<item>
		<title>SQL DATETIME with slashes or hyphens returning incorrect data</title>
		<link>https://www.cognim.co.uk/sql-datetime-with-slashes-or-hyphens-returning-incorrect-data/</link>
					<comments>https://www.cognim.co.uk/sql-datetime-with-slashes-or-hyphens-returning-incorrect-data/#respond</comments>
		
		<dc:creator><![CDATA[Darren Hall]]></dc:creator>
		<pubDate>Wed, 27 May 2015 14:26:29 +0000</pubDate>
				<category><![CDATA[Uncategorized]]></category>
		<guid isPermaLink="false">http://www.cognim.co.uk/?p=4909</guid>

					<description><![CDATA[I’ve just spent a fun hour wondering why two seemingly identical queries were returning different data. It turned out to be an issue with DATETIME in SQL Server. SQL interprets a DATETIME of the format “yyyymmdd” differently to “yyyy/mm/dd” or “yyyy-mm-dd”. The former is &#8220;unseperated&#8221; while the latter is &#8220;seperated&#8221;, and the &#8220;seperated&#8221; format depends [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>I’ve just spent a fun hour wondering why two seemingly identical queries were returning different data. It turned out to be an issue with DATETIME in SQL Server.</p>
<p>SQL interprets a DATETIME of the format “yyyymmdd” differently to “yyyy/mm/dd” or “yyyy-mm-dd”. The former is &#8220;unseperated&#8221; while the latter is &#8220;seperated&#8221;, and the &#8220;seperated&#8221; format depends on the current language and date format of SQL Server. If you have the date format set to the British standard &#8220;dmy&#8221; (as I did), then the month and day portion of the input get swapped. This can cause an error of <span style="color: #ff0000;">&#8220;The conversion of a varchar data type to a datetime data type resulted in an out-of-range value&#8221;</span> if the last two digits are greater than 12, as SQL Server attempts to use the greater than 12 number for the month.</p>
<p>Alternatively if you have a number less than or equal to 12, you won&#8217;t get an error but you will return incorrect data. This is much more confusing and much more problematic.</p>
<p>For example, the following queries (on AdventureWorks) are identical, except for the format of the date in the query. The first is &#8220;unseperated&#8221;, the second is &#8220;seperated&#8221;:</p>
<p>Query 1 (unseperated):<br />
<code>SELECT [DueDate], [OrderQty], [UnitPrice]
FROM [Purchasing].[PurchaseOrderDetail]
WHERE DueDate &gt; '20060410'</code><br />
Query 2 (seperated):<br />
<code>SELECT [DueDate], [OrderQty], [UnitPrice]
FROM [Purchasing].[PurchaseOrderDetail]
WHERE DueDate &gt; '2006-04-10'</code><br />
If you run these with the dateformat set to &#8216;dmy&#8217; (SET DATEFORMAT &#8216;dmy&#8217;) you’ll see that the second query returns data from the 4<sup>th</sup> January rather than the 1<sup>st</sup> April.</p>
<p>This affects DATETIME, but works correctly for DATE and DATETIME2. Converting the string to either DATE or DATETIME2 only brings in data after the 1<sup>st</sup> April, as expected:</p>
<p>DATE:<br />
<code>SELECT [DueDate], [OrderQty], [UnitPrice]
FROM [Purchasing].[PurchaseOrderDetail]
WHERE DueDate &gt; CONVERT(DATE, '2006-04-01')<br />
</code><br />
DATETIME2:<br />
<code>SELECT [DueDate], [OrderQty], [UnitPrice]
FROM [Purchasing].[PurchaseOrderDetail]
WHERE DueDate &gt; CONVERT(DATETIME2, '2006-04-01')</code><br />
However, SQL automatically converts a string into a DATETIME.</p>
<p>Microsoft recommend using DATE or DATETIME2 over DATETIME: <a href="https://technet.microsoft.com/en-us/library/ms187819.aspx">https://technet.microsoft.com/en-us/library/ms187819.aspx</a></p>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.cognim.co.uk/sql-datetime-with-slashes-or-hyphens-returning-incorrect-data/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4909</post-id>	</item>
	</channel>
</rss>
