R1D25 The C Programming Language

I realize I am kind of all over the map, but I learn best when I am doing 50 things at once. Ast least that is what I tell myself. It could also be the reason why I am a master of none. 🙂

I’ve struggled to get through K&R for a while. I figured I could spend the next few days of 100 days of code to make my way through this book before making my way back to the C# course which was starting to make my head hurt a little bit.

Of course, this turned out to not be the best idea because I spent way too much time trying to solve a seemingly very simple problem in C. It’s a problem I’ve gotten stuck on several times as I have made my way through this book in the past.

The problem is:

Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

This seems stupidly simple. But I can’t seem to figure it out, and I refuse to google for the solution just yet.

I tried an approach using getchar() and putchar() but my output either contains no spaces, a jumble of words, or something else. I’ll take another crack at this tomorrow and might resort to either pulling out gdb to step through my stupidly simple code, or go figure out the internals of how getchar() and putchar() work in the standard library.

By the end of this, I will know more about these two functions than I ever wanted to.

R1D24 Everyone Has Written a Function

I am not 100% sure if it counts, but I am going to write about spreadsheet software in this post.

To be clear, I suck at using spreadsheets. I spend way more time trying to get stuff to look pretty than I do thinking about how to solve problems with the tool. I am certain that I know about 5% of all the things that are possible.

I was motivated by Joel Spolsky’s Excel training video a few years ago and have tried to get better at using it.

I spent a little bit of time with my sales enginering hat on making a kick ass spreadsheet when I realized pretty much everyone in the world has written a function ast least once in their life thanks to the magic of spreadsheets.

Especially since I’ve spent the last couple of days diving back into functional programming, I can’t think of a better example of what functional programming looks like than what you put into a random cell starting with the equal sign in Excel.

The specific problem that I was trying to solve was to calculate a weighted percentage match based on the priority and sum of one column compared to a “Yes” or “No” in a seprate column. This is where the SUMIFS function comes into play.

Component Priortity Vendor A Vendor B
Ability to do X 4 Yes No
Ability to do Y 2 Yes No
Feature A 3 Yes Yes
Feature B 1 No Yes
Feature C 5 Yes Yes
Weighted Percentage Match: 93% 60%

The specific calculation for the weighted percentage match comes out to be

=SUMIFS($C3:$C7, D3:D7, "Yes")/SUM($C3:$C7)

Which translates to “sum the priority values all the yes’s and divide by total priority sum”. This means that at least based on the priorities that I have defined Vendor A is a 93% match compared to Vendor B.

Spreadsheets are amazing because they give you half the power of a database with half the power of a REPL. You can slice, dice, transform, and visualize data in ways that would be very difficult to accomplish with a pure programming langauge solution. Use them.

 

R1D23 Reverse Polish Hello World

I took a brief break from C# and cracked open a book I got a few months ago about the F# programming language. Functional programming is making a comeback it seems. I’ve done some work in Clojure and Lisp in the last few years. In addition I heard Javascript referred to as “Lisp in C’s Clothing” so that might count as well.

If JS is Lisp in C’s Clothing, I have no idea what clothing F# is wearing.

Rather than the traditional “Hello World” the first bits of code that we wrote was an implementation of a reverse polish notation calculator program.

Reverse Polish Notation Calculator in F#

The code for the function itself reminds me of writing grammars for ANTRL. I have no idea what “|” “|>” or “::” are doing in this context, but I can’t wait to find out. I was starting to get discouraged, but then following this code example the author reassures us.

“Don’t be discouraged if the RPN calculator code doesn’t make much sense right now; that’s the point! ”

Excerpt From: Dave Fancher. “The Book of F#: Breaking Free with Managed Functional Programming.” iBooks.

I am excited that I got all of this working out of the box on my Macbook. F# comes baked into the latest version of the dotnet core SDK. You can start a new F# project with the following incantation:

dotnet new console -lang F# -n MyFirstFSharpProject

You can run it with:

dotnet run

Like any good Lisp, F# comes with a built in REPL. It seems you need to install mono in order to get this to work. I was able to do it with homebrew.

brew install mono

Then you can fire up an F# repl with

fsharpi

You can test it out and make sure it works with a simple example.

> let greeting = "Hello from the F# REPL!"
- greeting;;

// output should be 
val greeting : string = "Hello from the F# REPL!"
val it : string = "Hello from the F# REPL!"

I’m looking forward to learning a bit more F#. It is one of the out of the box supported languages on Azure Notebooks.

R1D22 More Sorting and Searching Algorithms in C#

I kept working on the third course in the C# series on edX. We went over insertion sort, selection sort, linear search, and binary search.

The best part about this course is the videos that show an animation of how each of these algorithms actually works. This was a topic that I struggled with a lot during my CS degree so if you are unsure of how a specific algorithms works either watch a video that steps through it piece by piece, or use a debugger to walk through the code sample. For example the video below is from the course that walks through what binary search looks like.

After going over some basic sorting and searching algorithms we moved on to algorithm analysis and I reunited with my treasured old friend; “Big O notation”.

Studying CS feels like a long lesson in futility sometimes. It is summarized by this quote form the course:

In fact, some in the industry argue that algorithm analysis is a futile task today based on computing hardware resources and the cost of programmer time to do the analysis.

Despite this, I still think the basics are worth knowing.

Functional Programming Humble Bundle

In other news, there was yet another programming related Humble Bundle this week that offers a ton of books about functional programming languages from O’Rielly. Naturally I picked it up and added it to my endless books that I may never get around to reading. For $15, this is a treasure trove so I highly recommend everyone take a look.

R1D21 Sorting Algorithms in C#

Today I started the Algorithms and Data Structures in C# course on edX. I am having flash backs to the many late nights I spent staring at my awful Java code during my CS program trying to figure out how various sorting algorithms work.

So far this is definitely the most challenging course of the series. If you do one module per day its amazing how quickly they take you from “this is a variable” to “here is how to implement bubble sort”.

I switched from Visual Studio on Windows to Visual Studio Code for Mac. I figured that since my ultimate goal is to write an ASP.NET Core applications this seems like a logical time to let go of the crutches offered by Visual Studio and do things “the hard way”. VS Code is such a wonderful text editor. Specifically for C# the Intellisense extension and The C# Extensions extension makes development a breeze.

I’ve never really used a debugger, and I  kind of regret that because it would have saved me a lot of headache during my CS program. The debugger that is available in Visual Studio Code for Mac is really wonderful.

Debugging a Sorting Algorithm in Visual Studio Code

As an example, you can observe the state changes step by step while walking through a bubble sort algorithm to see the numbers moving around. It might take me slightly longer than one module per day to get through this course, but I am looking forward to refreshing my memory on both data structures and algorithms.

R1D20 Memory Management in C#

I wrapped up the Object Oriented Programming in C# course on edX with an overiview of memory management in C#.

Object Oriented Programming in C# edX Certificate

Like Java, .NET offers garbage collection but only for “managed” resources. This means that for unmanaged resources like files, network connections, or database connetions, you must perform the garbage collection yourself. This becomes slighly easier with the IDisposable interface, but also makes C# a bit more complex when compared to other langauges.

Looking at things like the IDisposable Interface in the .NET documentation really makes the docs shine. Not only is there detailed information about the various things related to this interface there are also a handful of different types of examples on how you can actually use this thing. It’s one of the best docs I’ve seen and is something I am going to reference and keep in mind as an example of what I wish every document would look like.

Overall I really enjoyed this course. Like the first course it was a bit on the short size, but it was one of the best overviews of Object Oriented Programming that I’ve seen in all of my learning.

Tomorrow I am going to start the last course in this introductory series to C# which is about Data Structures and Algorithms in C#. I am looking forward to wrapping this up so that I can move on to doing some neat things with ASP.NET Core.

R1D19 Moonshots Day 2; Clickjacking for the greater good.

On day two of my moonshot, I glue together all of the missing pieces to get the data to flow from pardot to inka.

Pardot allows you to inject arbitrary javascript into any form that you create. I used this to my advantage and basically stole the submit button event, temporarily sending data back to my own server for further processing (and eventual order input into inka.io) and then resubmitting the form as usual.

The code to get this to work on the JS side looks something like this:

(function() {
    var form = document.forms[0]

    function sendOrder(order, event) {
        xhr = new XMLHttpRequest();
        xhr.open("POST", "https://$MY_SERVICE", true);
        xhr.setRequestHeader("Content-type": "application/json");

        xhr.onreadystatechange = function() {
            if(xhr.readyState = XMLHttpRequest.DONE && xhr.status == 200) {
                event.target.submit();
            }
        }

        xhr.send(order);
    }

    form.addEventListener("submit", function(event) {
        event.preventDefault();

        // Code to Process Form 

        sendOrder(order, event);
    }
})();

The key thing to keep in mind is that if you do event.target.submit() before the XMLHTTPRequest finishes, then it may never process the full request. This is why we pass it into the function, and perform the resubmission of the form after the response has gone through.

This was a fun little hack, but the solution is far from perfect.

Problems

The biggest risk here is that anyone can submit this form now 10,000 times without any real checks in place. As much as we love our customers, we don’t really want to send 10,000 T-shirts to spam bots.

It’s also overly complex and the form parser is very jenky because it’s making some very poor assumptions about what this from will always look like. Since the form is ultimately generated by pardot, it is subject to change without notice at any time.

Further Improvements

Solving the first problem would be required before I felt comfortable having this live in production. Some ideas would be to do some rate limiting, or intelligent checking to verify that the form submission came form a real human being (perhaps counting the time that it took for them to fill out a form?)

However any sort of javascript validation is error prone as we have seen in this lovely $170 Million mistake.

Another improvement would be to send an email to someone in the event that we were not able to process an order so that a human being could attempt to do it manually.

Despite some of theses setbacks, I was able to have pardot send from data to my laptop via ngrok, process the data, and automatically place a T-Shirt order on inka.io.

R1D18 Moonshots Day 1

LaunchDarkly has an internal hackathon that they call Moonshots. I was excited to participate in my first one.

My project was to try to integrated a pardot form with inka.io in order to prevent a human being from having to manually transfer data from one place to another when a LaunchDarkly fan requests a T-Shirt.

This turned out to be a lot more challenging than I initially thought. Pardot makes it very easy for marking teams to create various forms and automate the results of those forms to other systems like SalesForce. They also make it fairly easy to perform custom actions by way of form handlers. The problem is that they don’t allow you to do both. You either need to re-invent the wheel and do all of the email automation and Salesforce integration stuff yourself, or try to come up with a different solution.

On the other side of the equation, inka.io has a decently documented API. Unfortunately it does not quite work a documented and I spent a few hours trying to figure out how to get it to work with a guess and check model.

I made a small python service that receives information from pardot and sends it over to inka. This involved writing a light weight wrapper around the inka API and making a handful of objects to perform data validation outside of the form (since inka is very particular about the data that it gets).

I was able to send data to inka without any issues, but getting data from pardot into the service was a whole other issue which I hacked around on the next day.

R1D17 Nose Dive into OOP in C#

I worked through module two of the Object Oriented Progrmaming in C# course on edX.

  • C# supports the notion of anonmous classes which seems to make it very simple to create new read-only objects on the fly without having to create the class first.
  • Visual Studio has handy #region and #endregion functionality that allows you to make collapsable regions in your code. If you have a file with many lines of code in it, using regions is a great way to keep things organized and also make things easier to look at in the UI.
  • C# does not support multiple inheritance.

One thing that is a bit confusing (and I am sure will come back to bite me in the ass later on) is how new classes are magically usable once they are created. i.e. unlike in python and java where you must import everything at the top of a file, once you add a class in Visual Studio is becomes immediately usable in order parts of the code without having to explicitely import it. This is handy, but also seems like it could create some issues in the long run in the sense that it is not immediately obvious which parts of the code are being used from one file to the next with these implicit imports.

It is not immediately clear to me, but I suspect that this is only true in the context of a namespace. It seems that C# allows you to “magically” use all members of a namespace without having to import them explicitly.

Abstract classes and abstract methods. You can create “sealed” classes which cannot be inherited. Interfaces are classes without implementation. I feel like a good analogy for this would be header files in C, C++ or Objective-C. An interface is really what an API refers to in a traditional sense (not like REST API). It is a “programming contract” that guarantees users of the implemented class that the functionality of that class will implement everything that is defined in the interface.

In the lab portion of the course we refactored our “school simulator” to have Students and Teachers inherit from a base class of Person.

Tomorrow I hope to wrap up the last module of the course which has to do with memory management and then move on to the last course of the series that deals with data structures and algorithms in C#.

 

 

R1D16 Object Oriented Programming in C#

After wrapping up the Intro to C# course I began the next course in the series which covers Object Oriented Programming in C#. C# is an object oriented language, similar to Java. This means that running all of your code out of a single “main” method is possible (and is exactly what we did in the last course) but goes against the spirit of the language itself.

I also read a very timely blog post today about when to create a new class in C# by K. Scott Allen. This is one of the most well written articles i’ve read regarding this topic and comes with some really great advice and rules of thumb. My biggest takeaway was this:

If you write a sentence saying you can use the class to ___ and ___ in a system, then it might be time to look at making two classes instead of one.

Cool Stuff

  • C# support partial classes (as well as partial structs and interfaces). This means that you can define the class accross multiple different source files. I can’t think of an immediate time where I wanted this feature in another langauge but its cool to know that it is there.
  • For simple class properties, C# comes with auto-implemented getters and setters so if you are not doing any sort of custom validation or logic, then you can simplify the code.
public class Person {
    
    private string name;

    // traditional getters and setters 
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }

    // auto-implemented getters and setters 
    public string Name { get; set; }
}
  • If you are using Visual Studio it makes it dead simple to create the getters and setters automatically after your field has been defined. Some of the refactoring functionality of Visual Studio is really amazing.

The first module was a basic overview of OOP so nothing to exciting is happening yet. I did get a chance to review the built in generic collections that are available as a part of C# / .NET — this is somethign that I wish I paid more attention to when I was learning pyton because I continue to find better built in data structures for various tasks on a daily basis.