Saturday, May 30, 2015

Common Pitfalls with Immediately Invoked Function Expressions (IIFE)

I am a big fan of John Papa's Pluralsight Courses and his Angular Style Guide. If you have not checked those out definitely worth the Pluralsight subscription by themselves. But today I am going to recommend a change to John's Angular Style Guide particularly the IIFE section and how it works with Visual Studio. I have seen this behavior in Visual Studio 2010, Visual Studio 2013, Visual Studio 2015 RC. So its not particularly new. The examples are with Resharper 9 and Visual Studio 2015 RC.

Figure 1 is a straight copy and paste from John Papa's Angular Style Guide starting at line 2.  Line 1 I added to help out with Visual Studio's intellisense.

Figure 1: Recommendation from John Papa's Style Guide


First the most important change is to the IIFEs definitions. Lines 2 and 13 should pass in angular and Lines 10 and 21 should pass angular in the last pair of parentheses. Doing this remove the squiggles on lines 6 and 17. See Figure 2.

Figure 2: Defining External Dependencies in an IFFE


Second is more of Resharper Default Style Guide definition thing. For .NET Developers I think basing a Style Guide off of what Resharper complains about is a good baseline. Resharper prefers double quotes. Lines 3, 6, 7, 14, 17, 18 lose their green warning squiggles once you change out single quotes for double quotes. See Figure 3,

Figure 3: Switching to Double Quotes
Final class of issues with the recommendation are the blue squiggles on line 7 and 18. I can see the merit John's logic on putting interface before definition. But both Resharper and Visual Studio take issue with defining the functions after their usages. This is perfectly legal as JavaScript does hoisting to the top of a given functional scope. So the logger function and storage function would be hoisted to line 4 and 15 respectively. Is there a way to make Visual Studio and Resharper aware of the hoisting? For me I rather avoid the squiggly and use a file per component convention instead. If you are DRY with your code a component shouldn't grow to such a size your worried the next dev will get lost in the weeds. I think I would recommend Figure 4 unless hoisting is accepted by tooling. Basically you just look at the bottom of the file for your interface right next to where the external dependencies are listed in the IFFE.

Figure 4: Hoisting functions

I am curious to hear anyone's feedback on this. Maybe I am just missing a setting with the hoisting thing. I am aware Resharper allows you to decide between single and double quotes for string literals under Resharper -> Option from the top menu of Visual Studio see Figure 5.

Figure 5: Where you could switch to Apostrophes in Resharper




Friday, May 23, 2014

Graduation Coming? So you want a Dev Gig

This guy is ready (CC)
As I mentioned in my last post I am quickly approaching mid career. I am not that far gone that I am
jaded about junior developers.

I am however quite jaded when I see a senior developer in title and pay scale, but their chops are not quite up to snuff. They may even rank lower than a jaded college grad who thinks they know enough. First tip, don't be a dark matter developer. Your passion should be contagious and eventually confident. Be sure to relish in paying your dues. Reputation is formed when you think no one is watching. I am surprised by how many gigs I get just from networking and being a professional.

You will never get an intentional opportunity to focus on learning as you do in your first job. You will find that all you degree has afforded you is foundational knowledge and proof that you can focus and achieve goals. If you ever lose the ability to continually learn you are dying as a developer. Sure you can find a circuit of legacy apps to maintain, but your investment will depreciate quickly. And oh by the way it is good to assume you know nothing about programming straight out college. You will find you use less than 2% of your knowledge from college if you're any good. Again expect to pay dues. There will likely be a crash course in the technologies you're expected to use day to day.

Five books I would recommend for learning the grit of our business:

So I do not have a COMP SCI Degree

Congratulations you have focused on something you probably passionate about. Maybe you realized how much dust was on the Linux servers in the CS Department. Either way you clearly have valuable skills in multiple disciplines.

In effect it is not enough to say you are passionate in becoming a Developer. Its not even important at this point what focus you had in college. I ended up getting a Database Management focus with my Information Technology degree. I have an affinity for database development and administration. I have been doing line of business web development for nine years now. Although I was able to transfer my skills I learned with Oracle easily to the SQL Server data platform; it has not been my focus. I have concentrated on becoming well rounded incrementally. I set goals to get Security+, learn Server side binding, whatever in a time boxed fashion. When I wasn't working my day job I succeeded most consistently when I focused on an achievable goal in a given time period of less than three months. Expect to spend 30 minutes each day focused on learning on your on time. Have a long stretch (2-4 hours) on the weekends for deep dives. I would argue having time management and expectation management skills is in fact the key to success in any field.

Job Descriptions

Job descriptions will sometimes include:
Minimum Bachelor Degree in Computer science, Computer Engineering, Rocket Surgery, Digital Basket weaving, or related field.

This does not mean you can't hustle until you make. You are probably well qualified. This should be off putting in its lack of creativity. This will not be the last of boiler plate wording you will see in job posts.

In fact it is not uncommon companies to put a job posting out as a lure for several positions. So yes no one expects you to have ten years in Entity Framework 6.0, SISS, JAVA, PHP, and the ability to deal with whiney clients.

The job description may not even been written by anyone outside the recruiting department. In fact your potential boss or her boss may have just sent a list of keywords to the recruiter. The recruiter then crafted a plausible job description and cast a wide net.

Salary Negotiations

Eventually you will have success. Likely you spend time in front of a sales man to negotiate your salary. They will likely 'review' the data that gleamed from your interview(s). They are feeding your insecurities before they ask the tough question. If they do not have this guy they believe you will accept anything. Challenge this assumption you will likely get some time with the person who really is calling the shots and she will respect you a little more for negotiating.

Later in your career they will try to preface this with asking for a Salary history. This is a game of poker and they don't need to know your hand. If asked about Salary history remark that you consider that information proprietary and suggest a number significantly higher than what you are expecting. They certainly will not let you know the rate they bill customers before hiring you on.

Try to gather information about their likely rates before hand. Always know what the mean rates are for your market for the position your going for. Also maintain the ability to say no eventually. Even if your heart set on a dream job it helps to have a number of interviews lined up in close proximity. Even if it is a battle ship gray position that you have zero desire in accepting you will become better at negotiating when you have said no even if it is, "not in a million years no." If they bid higher than you are expecting you can work up from there almost always.

College gave me zero .NET skills

Confession time. I interviewed and landed my first .NET Dev gig having read many IT books but only one .NET book. (The latest edition) I could talk about running for National SkillsUSA Secretary and losing, competing Computer Maintenance at the national level, and myriad of other things that show intangible skills. You need to sell your potential. Its about showing passion.

It is also about being truthful about what you don't know. The hiring company will likely give some time to a Senior Dev who knows all the secret handshakes in the technology they are interviewing in. She will ask you questions you likely do not know the answers to. The goal is not to ace the questions but display honesty and more relevantly creativity under pressure.

Unlike me during my first recruitment experience you may be a well read student of the technology at hand for your first gig. Knowing your strengths and weaknesses and being practiced in giving an elevator pitch for a few of them is invaluable. Being new to the career yet polished is sometimes more useful then being jaded and experienced its all about valuing the right things. Perfection is not your weakness and it will differentiate you. At least in my book.

Saturday, May 17, 2014

.NET Framework: Building a generalized WCF DataContractSerializer

I am working on sharing some samples as a common framework through a blog series. I begin this series with this post about using Window Communication Foundation's (WCF) default serializer the DataContractSerializer outside of WCF. On Legacy projects you may still be using ASMX web service or some other technology and want to start migrating serialization to a format that will allow you to easily upgrade to WCF.

WCF is a way to do web services in .NET since .NET Framework 3.0. The sample in this post will work with all versions of WCF.

Unit Testing

I find Unit Tests a great way to share assumptions, document usage, and learn someone's coding style. In the sample I provided a sample usage of XmlDataContractSerializer in the test named SerializeToXmlString_BasicUseCaseWorks. Yes I do not have an Assert but as a side not this unit test does test that the Serialization happens without exception. 

As MSTest like most unit testing frameworks fails a test if an exception is thrown. I use MSTest because I am not doing anything that requires a particular unit testing framework. Feel free to use the one of your choice. I am not espousing its use over others.

[TestMethod]
        public void SerializeToXmlString_BasicUseCaseWorks()
        {
            var strategy = new XmlDataContractSerializer<ContactDto>();
            var data = CreateSampleData(); 
            var xml = strategy.SerializeToXmlString(data, true);
        }
 I share with you some of my favorite places using the Contact Data Transport Object (DTO) class ContactDto. The CreateSampleData mimics a Repository method, creating a collection of some of my favorite places statically although you could imagine it hitting a data store of some type.
private ContactDto CreateSampleData()
        {
            var data = new ContactDto();
            data.Email = "foreachdev@gmail.com";
            data.FullName = "Joshua Kincaid";
            data.AddressCollection.AddRange(new[]{
             new AddressDto(){
                 Line1="Admissions",
                 Line2="1 Old Dominion Way",
                 City="Norfolk",
                 ProvinceState="Virginia",
                 PostalCode="23529",
                 PreferenceOrder=5
             },
             new AddressDto(){
                 Line1="Handsome Biscuit",
                 Line2="2511 Colonial Ave",
                 City="Norfolk",
                 ProvinceState="Virginia",
                 PostalCode="23517",
                 PreferenceOrder=1
             },
             new AddressDto()
             {
                 Line1="OTTO",
                 Line2="1 5th Ave",
                 City="Manhattan",
                 ProvinceState="New York",
                 PostalCode="10003",
                 PreferenceOrder=0
             }
            });
            return data;
        }
XmlDataContractSerializer uses a base class to allow us to easily use an IOC Container to change out serializers with a config change.
public class XmlDataContractSerializer<T> : XmlSerializer<T, DataContractSerializerAdapter<T>> { }

The Rest of the Code 

XmlSerializer exposes the ISerializer interface for dependency injection purposes. You could have a IXmlSerializer or include a parameter for format if that is your preference.
using System;
using System.IO;
using System.Text;
using System.Xml;

namespace Jrf.Core
{
    /// <summary>
    /// A Serializer component for working with POCOs that has DataContract and DataMember classes. 
    /// </summary>
    public class XmlSerializer<T, U> : ISerializer<T> where U : ISerializer<T>, new()
    {
        private const string _XmlDeclaration = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
        /// <summary>
        /// Serializes class with DataContract and DataMember attributs to Xml using an in memory stream then reads it to a string.
        /// </summary>
        /// <typeparam name="T">Type to serialize.</typeparam>
        /// <param name="dataObject">The object to serialize.</param>
        /// <returns>String containing xml form of the POCO.</returns>
        public string SerializeToXmlString(T dataObject, bool includeDeclaration = false)
        {
            var output = new StringBuilder();
            var stream = new MemoryStream();
            var reader = new StreamReader(stream);
            try
            {
                Serialize(dataObject, stream);
                if (includeDeclaration)
                {
                    output.Append(_XmlDeclaration);
                    output.Append(Environment.NewLine);
                }
                output.Append(reader.ReadToEnd());
            }
            finally
            {
                //Avoids multiple dispose calls. (See FXCOP CA2202)
                if (reader != null)
                {
                    reader.Dispose();
                }
            }
            return output.ToString();
        }
        /// <summary>
        /// Serializes class with DataContract and DataMember attributs to XmlDocument using an in memory stream.
        /// </summary>
        /// <typeparam name="T">Type to serialize.</typeparam>
        /// <param name="dataObject">The object to serialize.</param>
        /// <returns>XmlDocument.</returns>
        public XmlDocument SerializeToXmlDocument(T dataObject, bool includeDeclaration = false)
        {
            var doc = new XmlDocument();
            using (var stream = new MemoryStream())
            {
                Serialize(dataObject, stream);
                doc.Load(stream);
                if (includeDeclaration)
                {
                    var root = doc.DocumentElement;
                    var dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                    doc.InsertBefore(dec, root);
                }
            }
            return doc;
        }
        /// <summary>
        /// Creates and runs the serializer.
        /// </summary>
        /// <typeparam name="T">Type of Object to serialize.</typeparam>
        /// <param name="dataObject">Object to serialize.</param>
        /// <param name="stream">Stream to dump the serilization to.</param>
        public void Serialize(T dataObject, Stream stream)
        {
            var serializer = new U();
            serializer.Serialize(dataObject, stream);
            stream.Position = 0;
        }
    }
}
Finally, we have the simple DataContractSerializer strategy.
using System.IO;
using System.Runtime.Serialization;

namespace Jrf.Core
{
    /// <summary>
    /// Provides Serializer Services via the DataContract Serializer.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DataContractSerializerAdapter<T> : ISerializer<T>
    {
        /// <summary>
        ///  Serializer method.
        /// </summary>
        /// <typeparam name="T">Type of object to be serialized.</typeparam>
        /// <param name="input">Object to be serialized.</param>
        /// <param name="output">Xml stream.</param>
        public void Serialize(T input, Stream output)
        {
            var serializer = new DataContractSerializer(typeof(T));
            serializer.WriteObject(output, input);
        }
    }
}

Wrapping up

 I will upload my code to Github as I continue this series. In the mean time, if I forgot any of the code in the walk through, please let me know.

Saturday, April 26, 2014

Invest in diversity: Why encouraging women to go Tech matters

As any human being I have vested interest in the success of the women in my life. It has deepened for me significantly in recent times with my two daughters joining the Kincaid clan.

I am making deep investments in their dreams even this early on. I am quickly approaching mid career. I have had the chance to see women continue to join the tech community in larger numbers. I have also had the chance to learn from, mentor, and collaborate with women in my daily work on a number of different teams. I think we are making good strides to encourage more gender diverse teams. This has only improved the output of projects I have been.

As a culture Americans are becoming more enamored with geek/nerd interests. It is exciting, as a parent, to imagine my kids seeing a more progressive society that embraces leadership won by results over status based leadership such as patriarch favored systems human society has popularly embraced for centuries.

The Internet is the catalyst of this progression. I remember reading 'Being Digital' during college. I am a barely when Negroponte's book was first published. At 9 years age the book was clearly showing it age. It was fascinating that even then I could see his vision of a segmented web unfolding. It is all but reality today of the ability for people to form Godinesque tribes on the web. Its quite empowering.

This progression also has its negative aspects. With the power to nurture comes the power for members to damage a community. As the community becomes more diverse it can become difficult to accept that some contributions may offend some and be seen as useful to others. One of my social media friends pointed out their offense of one such contribution.

I am conflicted about whether I should be offended by this site. It could be positive or negative for the community depending on the site owners' agenda. On one hand its easy to wave the objectify women/ booth babe flag. It is obvious the content is taking queues from mass media we see everywhere here in the US. If it makes people think programming is cool there may be some benefit. Maybe it is a terrible way of going about that. But I think there is room for everything in the spectrum of interest on the internet. I prefer my kids program than quite a number of negative activities encouraged by sexualized marketing.  I think I will keep my pluralsight membership especially with the recent infusion of investment money they received. But if Code Babes encourages others to deepen their programming skills its easy to quickly forget they even exist. I can instead focus on what interests me.

I would also argue there is significant content available from a diverse group of people from all demographics.  We have already seen the gender bias blur to a significant extent in our industry. There are people in fact concentrating on making technology more cool for women as well.

Some great example organizations for women include but are not limited to:
http://www.womenintechnology.org/
http://girldevelopit.com/
http://www.ladieslearningcode.com/

I think it is positive to see a saturation of content from many perspectives. Please get involved in organizations like these. This is what will propel our industry forward. I like close by mentioning a quote on women in the country of Saudi Arabia:

“Well, if you’re not fully utilizing half the talent in the country,” Gates said, “you’re not going to get too close to the Top 10.”  *applause* -Bill Gates

Source of quote: http://www.nytimes.com/2009/08/23/magazine/23Women-t.html?pagewanted=5&_r=0


Saturday, December 28, 2013

Why SSIS is a mystic art for many .NET Devs

First a bit about my background might help. I come from a developer background. Specifically, I work as a senior web development on large ASP.NET web sites. I have worked in .NET since just before the release 2.0 (8 years) I have a Information Technology Degree with a Database focus. I largely fell into web development due to its popularity and stayed due to my investment in the platform. SSIS is such a disappointment because it doesn't fit in my world. I instead tack it onto my world with its own set of weirdness sort of like any other framework or language like XML, Regular Expressions, etc

CLR dependency without exposure of capabilities outside of a SSIS package

The SSIS platform was built for BI folks with zero development experience. There is zero carry over with .NET to SSIS. The components from what I have been able to gleam are not reusable by a vanilla .NET application. Want to parse a CSV and show the success and failure rows in a UI? Don't want to introduce a bunch of junk: SSIS package, a table to stage success rows, a table to stage failure rows. Your out of luck. I think, if makers of the black boxes that are SSIS control flow and data flows components would expose the underlying glue as classes that .NET applications could use, we would have one standard mechanism for reading from different sources across the platform. We would also not have to introduce a third type system here is how I am counting: your programming language's type system, SQL Server, SSIS DT_* types. Please correct me if I am wrong. I love to hear a bout it.

SSIS uses Windows Workflow well kinda

If Microsoft had such an investment in Windows Workflow (WF), why did they make BIDS now SSDT. It would of been a good story to make SSIS Catalog on SQL Server as one way to host WF with an SSIS being a implementation of WF Framework. Open source it to show how it can be done. Also if you want to use say a Flat file data source in your WF package but outside of SSIS, why not?

Conclusion

I know this is kind of a rant. I know there are more low level ways to do most of what BIDS now SSDT does in .NET. I think the correlation between the two technologies could be cleaner and more transparent. I get BI Developers just want to shell in occasionally to .NET with a Script task. I think it would increase SSIS usage if there was a clear platform independence for some features of SSIS. Need to shred a Excel document in SSIS or your backend .NET application. Let's have a way to pull away the GUI and interact with the Excel document shredder directly and consistently in both applications.

Wednesday, September 25, 2013

Designing an OLTP database by Understanding OLAP Dimension Attribute Types

Introduction

First I may be throwing around some acronyms that are foreign for new comers or for folks that do not focus heavily on database design. Lets go ahead and get those defined to get everyone on the same page.
  • ETL (Extract Transform Load)- This is a process by which data is ingested from some source system to some staging or target database.This can happen over an Service Oriented Architectrue (SOA) or use flat files such as XML, CSV, Excel spreadsheet, etc.
  • OLTP (Online Transaction Processing database- If you are programming against a database for an application and do not know it type, you are using an OLTP. Database normalization is usually important. The primary use is for data storage for operations based systems such as Content Management Systems, Entity Resource Planning, User facing systems etc.
  • OLAP (Online Analytical Processing) database- Used for reporting, business intelligence and decision support systems (DSS). De-normilazation is usually preferred as data accumulates instead of changes to existing data. More on this later.
This article is great not only for people with downstream OLAP database but for learning an advance database concept for any OLTP that stores historical data. That is any system that serves as a system of record. 

 Using Type 2 Attribute Changes in OLTP

In OLAP database there are two types of what they term as 'slow changes' to any data attribute. Type 1 involves retroactively changing data that effect how historical data is reported retroactively. Imagine you change the State in a Home Address record for a given user. If you store one Home Address for a user there is no way to account for that change over time if the User Table has a foreign key field for Home Address without further data modeling.

Junction Tables jumble

We could use a junction table with a surrogate key. So for our ongoing example, the UserAddress Table could would have three fields at a minimum: UserId, AddressId, and UserAddressId. The first two fields would be a candidate key with foreign key relationships back to the User and Address tables respectively. We now have to decide whether we allowing only Type 1 attribute changes to the Address table or certain fields in the Address table or if we are allowing Type 2 changes. That is maybe its ok if the zip changes to use the same record (thus allow type 1 change) but changing any other attribute like street, state or city requires a new record. In the case where you ONLY allowing Type 1 changes set a unique key constraint on UserId and AddressId. You could also have a reason field in your UI that allows the user to let the back end know whether it was a correction or a change of address.

Danger: Avoid not using a surrogate key. As this will limit options for when you decide to go from allowing Type 1 changes to allowing Type 2 changes.

If you are allowing Type 2 changes than you have to support the ability for multiple records for any given UserId and AddressId combination. At a minimum you need to decide if there is a way of categorizing or expiring historical records.

Expiring Type 2 Design Options

  1. IsCurrent flag- Add an additional field to the junction table that marks the current address record for the user. This requires you manage this flag when a new record is added for a given canidate key combination.
  2. Integer Sequence field- This gives a chronological order to changes. Requires looking for the max old value before inserting a new record. I recommend looking at other event based system concepts that you could bake into your system early.
  3. Date based Sequence field- Using Date and potentially time to represent a sequence.
  4. Address Field FK- in the User table such as CurrentHomeAddressId.

Does Downstream Matter

I think it is important to note that regardless of downstream concerns you should only implement a Type 2 change if it is important for the direct users of your database. There is always a significant cost to this feature. The real shame is you either decide to care about retaining this level of detail or you have a watermark when it is decided it is of value. Depending on database backup retention policy; you could come up with a facsimile of the past to a certain point of course. Often times you lose a certain amount of granularity depending on what transactional data you capture. When an OLAP database is designed with any number of OLTP sources they will design a way of doing ETL that will monitor your system for changes regardless of whether you do Type 1 or 2 changes. I always recommend having standard timestamp fields at a minimum for when a record was created and last time it was modified. With this minimum consideration, Even if you only allow Type 1 changes an ETL process can still do Type 2 changes in the OLAP database regardless of your choice. This will likely give them more granularity than the timestamp they can get from a backup, and of course the OLAP folks are at the mercy of the DBAs at that point.

Thursday, July 11, 2013

What is in a Database Field Name?

I dislike Database design that use the table name as a prefix for all fields in the table. Example:CREATE TABLE MyTable(MyTableID int Primary Key,MyTableModified DATE,MyTableDescription Varchar(500),MyTableCreatedBy int...)

As opposed to:CREATE TABLE MyTable(Id int PrimaryKeyModified DATE,Description Varchar(500),CreatedBy int...)

FKs are fine but I would recommend being creative with Names. CreatedBy is a FK to the User table. But notice I didn't call it UserId. Why? What happens when you start tracking UpdatedBy?

Well what about recognizing tables in a query with say 10+ joins. Use great names for Aliases instead. You are using aliases in this case? :) I have seen people error on one of two sides. SQL Server 2008 + or newer people tend to lean towards verbose or even avoiding aliases due to intellisense. That's if they haven't done something to break SQL Server Management Studio intellisense. Or people tend to abbreviate table names. Which is fine if you are consistent with Alias names. The great thing about leaning on Aliasing heavier is it is less permanent than having to rename every field because you decide a table needs to renamed. Most of time you will not rename fields or tables. That is why naming fields and tables well is such a big deal.