Tuesday, 28 February 2017

100 Day Deep Work - Day 1: C# Namespaces



OK, so officially Day 1 of the #100DayDeepWork challenge inspired by Cal's Deep Work book and I've actually raced ahead of myself and done some of the proposed study out of order. Namely, the 1hr course by Mosh which was a good warm up, I should have thought of doing that first instead of something that takes 8 hours ;]

Better still, I was able to skip forward during his presentation as the material is very basic for me at at this point. Be sure to check the description section as he kindly provides the timings for each topic he discusses. You can also check out his website and courses too if you like his style (you will!).

Namespaces
One thing that came up from his video however was Namespaces. I've certainly used namespaces in my code before, heck Visual Studio automagically creates a namespace for you and as part of C# there no getting around using them. However, it's always interesting to note that you use these things without questioning, so I decided to do some Deep Work on Namespaces. There were two goo take-aways that are a bit more ingrained in my brain now:

  • Namespaces aren't arbitrary, they are describing the structure of the solution you're coding
  • They're hierarchical and so thought needs to be given to how you declare them
  • That hierarchy controls scope and so can be a powerful tool for your code design
Going ahead I'll be paying more attention to the use of Namespaces and how they appear in my code.

Nested Namespaces
As we create our namespace hierarchy we can declare them in a long form that we write out as below. For example;
namespace ExampleNamespace
{
    class ExampleClass
    {
        public void ExampleMethod()
        {
           //some stuff here
        }
    }

    // Then add a nested namespace
    namespace TheNestedNamespace
    { 
In terms of declaring them I'm not going to use nested namespaces as that feel like a great way to over complicate the layout of code. Instead I'll follow the common shorthand approach.

Shorthand
The shorthand, and I think tidier way, of declaring them is more as we usually see:
using ExampleNameSpace.TheNestedNameSpace
This seems much neater to me.

Alias
Another consideration is that if the nested namespace usings may become very long as we build our hierarchy out. A way around this is to use an Alias.

using MyAlias = ExampleNameSpace.TheNestedNameSpace
Now we can just type a line such as:
MyAlias.SomeMethod 

That's it for Day 1 of Deep Work. I had a question nagging me about iterating over multiple elements with Selenium so tomorrow I might work on that just so it's not distracting.

Mark

P.SThis is the book that gave the idea of the 100 Day Deep Work challenge, check it out!
--------------------------------------------------------------------------------------------------------------
Day 1: http://cyreath.blogspot.co.uk/2017/02/100-day-deep-work-day-1-c-namespaces.html
Day 0: http://cyreath.blogspot.co.uk/2017/02/100-day-deep-work-day-0-learning-plan.html


0 comments: