Tuesday, March 29, 2016

Null-safe Dereference in C# 6.0

Wow, how did I make this far and not know about this?

I love these, although I am still unsure as to how clean they make the code, or if they are going to make me a lazy coder by using them as opposed to what I have been doing.

I came across the null dereference because I was reviewing some code on my favorite code review site, CodeReview.  The code is being used to track downloaded applications in a set of applications that are being downloaded at one time.


if (lastRuntimeDetails != null) //at least one application was already downloaded
    if (lastRuntimeDetails.Ip != runtimeDetails.Ip || 
        lastRuntimeDetails.Port != runtimeDetails.Port)
            _requestExecutor?.Disconnect();

As I was reviewing this code there was an awkward if statement that was nested without brackets (yuck) so that was my first line of business.  The objects that were being compared were being created from a custom class called RuntimeDetails, the first object that is created is called lastRuntimeDetails and is being used to track the last application to make sure that the same application doesn't get downloaded a second time and to keep track of the IP Address and Port for each download. 

Anyway, at first I wanted to get rid of the outer if statement, it looked to me like it was extraneous, at first I was thinking that if the properties were null that they just wouldn't be equal to each other and give a false value to the condition statement.  

Comments were made about Null Reference Exceptions and a lot of thought went into how to do this, I kept thinking about the null properties, and that's when I found Null Dereference Operator, so I set up some tests to make sure that I had a good grasp on things

I created two classes, one to create objects and the other to return comparisons on the objects



public class Class1
{
    public string One { get; set; }
    public string Two { get; set; }
    public Class1()
    {
    }
}

public static class Class2
{
    public static bool returnBoolean(Class1 inputA, Class1 inputB)
    {
        return (inputA?.One == inputB.One);
    }
    

    public static bool checkOnNullProperty(Class1 inputA, Class1 inputB)
    {
        return (inputA.One == inputB.One);
    }

}

and then I eventually came up with the following tests


[TestMethod]
public void TestMethod1()
{
    var input1 = new Class1() { One = "string" };
    var input2 = new Class1() { Two = "string B Two" };
    var test = Class2.returnBoolean(input1, input2);
    Assert.IsFalse(test);
}

[TestMethod]
public void TestMethod2()
{
    var input1 = new Class1() { One = "string" };
    var input2 = new Class1() { One = "string B Two" };
    var test = Class2.returnBoolean(input1, input2);
    Assert.IsFalse(test);
}

[TestMethod]
public void TestMethod3()
{
    var input1 = new Class1() { One = "string" };
    var input2 = new Class1() { One = "string" };
    var test = Class2.returnBoolean(input1, input2);
    Assert.IsTrue(test);
}

[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void TestMethod4()
{
    var input1 = new Class1() { One = "string" };
    Class1 input2 = null;
    var test = Class2.returnBoolean(input1, input2);
    Assert.IsFalse(test);
}

[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void TestMethod5()
{
    var input1 = new Class1() { One = "string" };
    Class1 input2 = null;
    var test = Class2.checkOnNullProperty(input1, input2);
    Assert.IsFalse(test);
}

[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void TestMethod6()
{
    Class1 input1 = null;
    Class1 input2 = new Class1() { One = "string" };
    var test = Class2.checkOnNullProperty(input1, input2);
    Assert.IsFalse(test);
}

Someone pointed out that if the object itself is null it will throw a Null Reference Exception, once they said that I looked at the code again and sure enough the container was created but no object was placed inside of it.


 RuntimeDetails lastRuntimeDetails = null;

So my initial thought of removing that outer if statement was wrong, but now I had new information that could be used to make sure that the Properties didn't throw a Null Reference Exception if the object was created but the property were null.

So here is the code that I suggested to replace the original code.


if (lastRuntimeDetails != null)
{
    if (lastRuntimeDetails?.Ip != runtimeDetails?.Ip ||
        lastRuntimeDetails?.Port != runtimeDetails?.Port)
    {
        _requestExecutor?.Disconnect();
    }
}

The object could be created with null properties now and then we could remove the outer if statement entirely.  I believe that the better solution is to set the properties on creation to some default and then when they are assigned they will be different from the default so the comparison could be made without the need to check for nulls thus removing the chance that future development would have to worry about checking for nulls.  or that the initial creation of the object, instead of being set the object to a null object, we create the object with null properties and then if the object's properties are null then they will give a false when compared to anything other than null (using the Dereference)

The Code Review Question in Question

and

My Answer

Thursday, March 24, 2016

Which Coding Languages should I learn?

I read a couple of articles this morning,

1. I’m gonna puke if you compare Angular to React again; here's why!
2. Which Tech Should You Learn Now?

From the tone of these two articles it seems that JavaScript isn't going anywhere any time soon.  Another thing that I picked up from the second article is that developers with AngularJS or ReactJS experience/knowledge, on average, earn more money.  The second article also says that you should learn React (and Node) first because it is becoming more and more popular, in other words if you have the choice learn React (and Node) first then learn Angular, the article bases this on statistics given by Stack Overflow's Developer Survey and last year's JavaScript Scene Survey.

So, given that information, I am going to take it and mix it into my list.

Recall my list:
  • AngularJS
  • Ruby
  • Python
  • Java
  • C#/VB/.NET (better working knowledge)
  • JavaScript (better working knowledge)
  • Other JavaScript frameworks
  • BootStrap
  • CSS3
  • HTML5
  • BASIC
  • ALGOL
  • KTurtle
  • Programming Logic
  • Programming Design
  • BootStrap
  • CSS3
  • HTML5

I don't want to get too burned out on JavaScript Learning, so I am going to season my list with JavaScript and move some things around, maybe see if I can confuse myself between using semi-colons and not using semi-colons.

  • AngularJS
  • Ruby
  • Python
  • ReactJS
  • NodeJS
  • Java
  • KTurtle
  • F#
  • BASIC
  • Scala
  • ALGOL
then I could mix in some intermediate/advanced learning of all the stuff that I already know

  • HTML5
  • CSS3
  • BootStrap
  • C#
  • VB
  • VBA 
  • JavaScript
Season these lists with Logic and Design reading as well, also various programmer's blogs as well.

Am I missing anything?  

Should I change the order of my list for some reason?

Please feel free to comment below.

Tuesday, March 22, 2016

Which Coding Languages are the most beneficial to learn, for me?

I started thinking today about what I wanted to accomplish in the week, month, year...

Someone I know through CodeReview once said in a blog of his that you shouldn't try to eat an entire elephant without cutting it into pieces, so what I have decided is that I should pick some things that I want to accomplish and then give my self some short term goals, kind of a top down approach for the planning phase.

I think that the first thing that needs to happen is that I need to find out what languages are the ones that are going to further my love and enjoyment of programming as well as give me a future in the field of web and application development.

The list I have so far in my head

I already have a working knowledge of
  • C#
  • JavaScript
  • VB
  • .NET
  • OOP
I am thinking that I might want to learn some procedural programming as well maybe
I would also like to learn LOGO (KTurtle)a bit better, I have played with it off and on since I was a child and would love to be able to teach some new youngsters how to code in it as well.

This is a good start I think, I can now take these and order them by degree of some random value to myself or to furthering my career or ... whatever value.
  1. AngularJS
  2. Ruby
  3. Python
  4. Java
  5. C#/VB/.NET (better working knowledge)
  6. JavaScript (better working knowledge)
  7. Other JavaScript frameworks
Now I have a rough game plan of what I want to do for the year, what goals I might want to achieve and what goals I might want to table until a later date.  I can break these down into pieces or sections of learning like someone would do for a college degree or something like that.

One thing that I need to keep in mind is that I need to be able to pull from knowledge different designs for creating smaller pieces that make the entire application function the way that I want it to function and to make it efficient, I would definitely be on the look out for programming logic and design principles, I might even create a web search that I could do at least once a week to find articles about different techniques for creating the most efficient applications or sub procedures or whatever.

I am going to add into this list with things like

  • Programming Logic
  • Programming Design
I would also add things to the list like
  • BootStrap
  • CSS3
  • HTML5
Just because I want to keep up on these technologies and to make sure that I am using them appropriately and not acquiring bad habits.  I am sure that I don't know everything about these things.

I think this is where I am going to leave this (for now) and come back to it another day, the more I start thinking of things I want to be incorporated in this list of goals the farther down the rabbit hole I go.

so far this is the list that I have of things that I want to learn/keep learning:

  • AngularJS
  • Ruby
  • Python
  • Java
  • C#/VB/.NET (better working knowledge)
  • JavaScript (better working knowledge)
  • Other JavaScript frameworks
  • BootStrap
  • CSS3
  • HTML5
  • BASIC
  • ALGOL
  • KTurtle
  • Programming Logic
  • Programming Design
  • BootStrap
  • CSS3
  • HTML5
So, what do I do with this list now?


Wednesday, March 16, 2016

Code Stock

There has been a lot of talk around the office about Code Stock lately because the deadline for submitting talks is Friday at midnight (EDT).

Being a contractor it is not mandatory for me to submit a talk, but that doesn't mean that I can't submit a talk, so I have been throwing around the idea of writing something up.

When I started working here I didn't have very much (read as any) experience with AngularJS, I have worked with jQuery and AJAX a little bit and that has given me a little bit of a boost working with AngularJS, so I decided that I would talk about the process that I have been using in order to catch up to the rest of the developers here.

Check out Code Stock, sounds like it is going to be an awesome get together!

Tuesday, March 8, 2016

Git

I have learned more Git in the last 2 weeks than I have over the entire course of the time that I have had a GitHub account (@Malachi26), so I am really getting a feel for branching, pushing, pulling, merging, and all the other stuff.  I really like the way that I am able to collaborate.   

The big thing that I like about using Git though, is the way I can "save" my progress at different places in the process of coding something.  I have only had to revert back to the end of the day previous.  

When I shut down for the day I went to close Visual Studios and it asked me to save the solution, and I selected no and the next day I was missing my .sln and one of my .csproj files, so I could not open the solution. 

First I just tried to pull down the .sln file and that's when I found out that I didn't have the .csproj file either, so I went back to the repo and pulled the .csproj I needed from the last commit.  

I love working with Git and think that it is going to be very handy, I think that it is going to give me more freedom and free up some space on all my machines.  I used to have a folder for everything, now I have a master branch and a development branch at the least. 

I just need to remember what I did for the next time, or at least what commands I need to Google.  I am sure it won't be the last time that I make a mistake, but that is what Git is best at, forgiveness.

Friday, March 4, 2016

Not much to say.

Right now I am totally in learning mode, so there isn't really much for me to say other than I am really starting to like AngularJS more and more as I code with it in a real world scenario.

I can also say that being unemployed for any amount of time more than a month, really sucks.  back to work after more than 4 months of being "on vacation" I need to get used to getting up in the morning and also get used to staring at a computer screen for 8+ hours a day without my eyes bleeding.  On the other hand though I am so happy that I am back in the driver's seat, writing code again, oh how I missed the days of writing code non-stop and seeing things being created right in front of me.

now I think that it would advantageous to take the time to write about what I have learned throughout the week, (at least weekly) so that I have a good history of all the things that I am learning through this wonderful journey.  This will take a little bit to get used to actually writing about the things I have learned, but I hope that it will help to get out of the box that I am in and also give others a chance to help further my learning or introduce me better tools for the job.

Please feel free to join me on my journey through coding and learning code and repeating the cycle.


Lyle Hart III