Category Archive Programming

The Pragmatic Programmer 20th-anniversary edition

Do I really need to read the pragmatic programmer 20th anniversary edition? I mean I read the original one years ago and loved it. So why waste the time? Well to be honest because I loved it! Why not see what I forgot and what is new?

But I don’t have time: I’m trying to bring my high blood pressure back down through exercise and getting my 135kg body through martial arts training and healthy eating while doing a full-time job. Frankly, I have a stack of new books to read some fiction, some fact and I’m studying Google’s SRE so is another book really a good idea? Well, what programmer doesn’t have a huge stack of books… Code Complete, SRE Workbook, Working with Legacy Code.

How about an audiobook? YES! Perfect. I can listen to it while I walk the dog and while I am cleaning the house! Which was a great idea, until the audiobook started trying to read a Linux grep instruction out loud. Soon after it was reading out semaphor code in the concurrency section and I quickly realised I needed to read it, in order to … well … read it.

Eventually, I did. Overall it was a great read and as always there were parts in it that I had forgotten over the last 4-5 years since I last read it. Well worth taking the time! The audiobook helped, but it sort of needed to pause and pop-up code on a window instead of reading it out loud.

To anyone thinking about reading this, go for it! It’s oriented for junior programmers, which I was when I originally read it, but there are themes, which I recognise have been subtly around me for the last 5 years without me even noticing they were from the book. It definitely had a huge influence on my career and even if you only read the beginning there are some concepts such as the broken window example, which have really helped me in job interviews.

Azure Services and Concepts

This is another notes dump. I’m working through a Pluralsight course called Microsoft Azure Fundamentals (AZ-900). These are some of my notes from doing this course.

Don’t Rely on this to Learn

I don’t recommend you read the notes. They might lure prospective future employers to my sight and I might look back and read them, but they’re not really there for people to learn from. However, Pluralsight is there for people to learn from. Because they’re not intended to be a source of learning they won’t be written to cater to learners. I will miss whole chunks of information, where I already knew that and I might drop whole sections if I feel it’s not relevant knowledge for my future career.

I tend to prefer to study things that aren’t bound to a specific technology or company, so I will avoid too much time being spent on this, so I can free up time to focus on other things. This is one of the major reasons for this Azure related gap in my knowledge. I would rather learn to make applications than use them and Azure provides a number of Microsoft-bound services. Also, since as I am a student, I am prone to misunderstanding and my notes might reflect a misunderstanding or out-of-date information.

Instead of reading this course pop over to Pluralsight for a small subscription fee, you can see the content of this spread out among a number of videos with multimedia. It will be quicker to learn from than this. For developers and sys-admins who tend to earn £20k+, they should be affordable at £24 per month and if someone is not a developer or a sys-admin, then this course is probably a bit beyond them at this stage.

AZ900 Content

This is about the specific products provided by Azure, which can be used in making a solution. Some things covered will be concepts like data centres, regions, ARM, Azure CLI, ARM Templates, Azure virtual network gateways, storage accounts, platform solutions like IoT, AI and big data solutions. This should cover the concepts portions of the AZ900 exam.

Sources for these notes

The majority of this is taken from Neil Morressy’s course on Pluralsight. This will reflect notes taken during a video so they might be a bit spacial. Grammar will go out the window, while I’m trying to keep up so good luck if you’re still reading. I’m also going to insert Vlad Catrinescu’s course content.

What is Cloud?

What is a Cloud? How does it help us? For a lot of people cloud just means online. The author Mr Catrinescu seems to suggest EVERY organisation out there is using Cloud computing… I don’t think this is true. I mean I tend to avoid absolutes like everything, but I know in my own limited company we use cloud and in my other employer, we provide a Cloud-based solution so there’s two that do use it. But some organisations like to avoid computers altogether, builders or wholesalers the odd company here or there. But I think any website is most likely going to be hosted on some sort of Cloud solution.

Once upon a time, we had dedicated servers for everything. Each networked application installed was often required to run on a dedicated server to ensure quality through dedicated processing power. Each one had its own CPU, RAM etc. Organisations were spending a ton of money on servers which mostly sat there unused. I mean not all applications are used all the time and yet they need dedicated server space.

We introduced virtual machines so something could have dedicated processing and more but on less architecture… there are still some more costs and there’s plenty of hardware requirements. This is more cost effective than before but there is space for improvement.

Cloud allows for a companies to pay for Saas without worrying about hardware maintenance, secure server rooms, and more. The cloud provider makes sure there are enough shared resources to provide the solution. The provisioning of new VMs and more is done for the user and provided in an almost instant manner. Services can be billed by their running time so you’re not paying for services when you’re not using them.

CAPEX and OPEX

Capex (capital expenditures) involve buying and setting things up which are going to be used for many years. Their costs can’t be deducted in the year it was spent some of it needs to be carried over into the future years.

Opex on the otherhand (operating expenditures) are deducted in the year they were used.

Why Use Cloud?

You can pay based on how many hours a virtual machine is up for you. You can set it to go up based on metrics for example if the cpu was at 70%. This is autoscale.

Cloud providers safeguard against loss or hardware failure. They can have services in other countries where they might not be affecgted by the same natural disaster for example. These provide redundancy.

Why Use Azure?

Azure is the most important cloud provider to use since it is the most used and it is used by 85% of the FTSE 500 companies.

X As A Service

We will be covering Iaas, Saas and Paas (Infrastructure as a service, Software aas and Platform aas). The main difference between these Services i the amount which is provided and who manages what.

With your typical On-premises servers you need to manage everything. With Iaas (Infrastructure as a Service) the hardware is set up by Microsoft or the Cloud provider while the software is entirely managed by the user.

With Paas the entire platform including things like the runtime, Operating system, any middleware, SQL server IIS are all managed by the provider and only the Application / Data is managed by the user.

Eventually we look at a Saas solution. This is where you are licensed access to a piece of software like Office365 where the management of the application is entirely provided by the supplier.

Catrinescu gives a good example of this using pizza. Check out his course on pluralsight.

I like to think of it as running a screening of a movie. Do it yourself, is where you hold the screening at home, and show the movie using your TV and DVD player etc. Infrastructure as a service is a bit like if you rent the room to run a screening but bring everything like the projector, DVD player and movie, Platform as a service is a bit like renting a room at the cinema, they provide nearly everything but you bring the movie. Finally, Software as a Service is like buying tickets for the cinema.

Deployment Models

Private and Public Cloud

There are two main strategies. Public Cloud and Private Cloud. If I were to guess on what Private Cloud means I would guess correctly, that in this circumstance the rented infrastructure is private to the organisation that commissioned it and not shared with other people.

With the other type the public cloud then some of the hardware can be shared between different cloud clients so the same hardware is being used by multiple people. This means you can save money especially while the application is remaining dormant.

Hybrid Cloud

A third-type enters the arena called the Hybrid which is essentially a mixture of the two with Orchestration and Automation between the two.

Community Cloud

The final type is essentially in my opinion a shared private cloud, it is called a “Community cloud”. This is where a cloud is used across an entire community of organisations, but not accessible to people outside of that community. This can be like a cloud used for government bodies, but not outside government bodies. Often when all the organisations on the cloud are from the same industry or community there are similar security concerns, data protection requirements and legal requirements which makes this really suitable to join resources.

Most Microsoft’s solutions are public cloud. Microsoft also has options for private and hybrid cloud solutions.

Azure stack connected scenarios – Hybrid
Azure stack disconnected scenarios – Private
Azure Government – Azure offerring specific to government bodies including FedRAMP, DOD, CJIS

Other Cloud Community offerings are Azure China and Azure Germany with specific requirements set to the needs to these particular countries. I assume Azure China is specific to China’s needs which require that the Azure be isolated from the world-wide internet and Germany has some Data Protection Laws that are very strict and specific.

Data Centres

Neil Morrissey in his course available on Pluralsight comments about Data Centres. A data centre is essentially a building which just hosts lots of infrastructure for providing cloud services. Naturally for security reasons they don’t publish the address of their datacentres.

Many of them include what’s called ITPACs which stands for IT PreAssembled Components. This is essentially a stack of servers and hardware with its own power supplies and coolant system. Microsoft also experimented with shipping containers of ITPACs so they could easily bring a whole one back online when one goes down but that model is being replaced now.

A datacentre generally complies with various security protocols such as FedRAMP, ISO 27001, HIPAA, SOC 1 and 2.

To enable separation, data is encrypted and kept separate from other customers. It is also stored 3 times over in each data centre for redundancy and you can also permit your data to be stored in multiple data centres for additional recovery.

These data centres consume a lot of power, but Microsoft is carbon neutral since 2012 and aims to be 100% renewable energy by 2025.

Data Regions

When you set up most services with Azure there is an option to choose where the data is stored. In the form of a “Location”. This is an area where there are a collection of Data Centres and this allows for you to choose a country and area of the country relatively close to your customers or in a place where specific laws operate. You can specify a region like UK South, Central United States, Japan West, Korea South, etc.

Some services are global and you don’t specify a region such as Azure Active Directory.

Not all azure services are available in all regions. If you want to see what is available where you can go to https://azure.microsoft.com/en-gb/global-infrastructure/services/ which will allow you to filter services offered by Azure and see what regions they are available in. Also certain VMs with certain levels of processing power might be restricted to certain regions or have different prices for different regions.

For example here I filtered for Genomics and found it’s not available in Canada for Central US regions.

Table from Microsoft website with ticks in columns where Microsoft Genomics is available. There are no ticks in the columns entitled Canada, Central US, North Central US or West Central US.

Any service where the “Non-regional” column is ticked are the ones where you are not required to pick a column as they are global services.

For more information on this, I recommend Neil Morrissey’s course on Pluralsight which is part of the Azure fundamentals AZ-900 path learning. This covers all basic concepts and information, which are relevant to Azure.

Azure Geography

This is an area that might include one or more regions. Often an entire Geography is subject to similar laws about data residency so they should mostly be entire countries where the law is the same.

Microsoft might copy data to other data regions other than the one you have selected for redundancy purposes but they would not copy it outside the Geography. This is particularly the case when you select something like Geo-redundant storage.

Region pairs

This pair is often two twinned regions, which are located over 300+ miles apart based in a single geography. They are so far apart because it allows for large redundancy against large scale issues for example Texas going without power a few months ago. If there was another data centre in a region just the other side of the state it too might be without power suffering in the strange snow-storm, which was probably caused by climate change. (Side note: Climate change is real yo! Companies didn’t want their climate damaging effects challenged, so it was cheaper to brainwash most of the US and whole pockets of the internet than actually investing in renewable energy so they released a ton of false propaganda to suggest Climate Change wasn’t real. It was real ask any scientist who works in the field and isn’t on an Oil company’s payroll).

When you select “Geo-redundant” storage then Microsoft automatically copies your data across to the other region in the Region Pair. When a disaster affects both Regions in a region pair one of the two pairs is prioritised.

Availability Zones

An availability zone is within a single Azure Region and may contain multiple data centres. Some regions just contain one data centre, but some contain 3 or more availability zones. You can organise zone redundant storage to replicate your data across different data centres in the region, but that data won’t be replicated across multiple Azure Regions, it will just remain in one Azure Region within one Azure Geography.

This is all explained thoroughly in Neil Morrissey’s course on Microsoft Azure’s Services and Concepts available via PluralSight. This April I think Pluralsight is doing an offer for them. I am not paid to recommend them, I’m just finding the courses available really useful and trying to step up my knowledge speedily and consistently with what’s required for my job.

Resource Groups

Information to follow.

Adding Azure App Insights logging to Durable Functions

Firstly, when I approached this my question was “what are durable functions”? That question is best answered by beginning with what are Azure Functions. I need to prefix this with I am not someone who is in the know. I am making these notes as I learn things and I am no expert. It is likely this will contain information that is outdated or just plain wrong. Also I don’t plan to keep it up-to-date, this is simply a dumping ground for my notes.

Azure functions are a part of Microsoft’s solution to the serverless programming paradigm through Functions as a Service (Faas). This is coding for a solution without any concern for infrastructure. You write your code and azure automatically creates a box for it to run in and uses your code to achieve the desired response / action.

Your code can be triggered in a number of ways including a timer event such as running once an hour. It can run if there are messages on a queue waiting to be processed and run for each queued item. It can also run as a result of a http request.

The result of the design of this process is that the function is stateless. No matter what happens the function would always run the same, it never remembers anything between the times that it runs and never gets into a new state where it would run any different from how it would run otherwise.

Also Azure Functions runs on a consumption-based pricing model, which means you are only charged when you’re using it. Because there’s no infrastructure you do need to pay for it. Until such a time as the function needs to run there’s no server required to sit and wait for incoming requests. Then when you receive a request Microsoft ups a container, which is probably based on a VM on one of their machines that you’re totally unaware of. The implementation details of the infrastructure is totally handle by Microsoft so you don’t experience the server and instead only pay when you need to run briefly on one. An advantage of this is that when you need lots of infrastructure because for example there are loads of messages on the queue right now, the host can up lots of infrastructure and scale to meet your needs without you needing to pay for infrastructure that might not be used again for a while.

Often an Azure functions has a binding which can easily link its output to a SendGrid, CosmosDB / blobstorage which allows for many different outputs.

So that’s Azure Functions. But what I might make a function “Durable”? Well is an extension to Azure Functions which results from trying to solve a number of limitations in the serverless paradigm.

When running the Serveless paradigm is limited due to its stateless nature. Durable functions provides a means to handle state. In order for state to be achievable in Azure functions without this extension you would need to rely on some kind of storage like blobs or databases.

Also Azure functions has difficulty handling what’s called fan out and fan in workflows where one function can execute many function and then another continues when all those functions have finished. Fan out is a description of the workflow diagram that results.

Also Azure functions have difficulty trying to handle errors when they could occur anywhere in the workflow. Without using this extension for a function to call another function, it would normally rely on message queues or http requests to trigger the additional functions. In order to handle errors a system needs to be written to allow the error to propagate such as passing back the error state to the http caller or processing a poison queue.

With durable function you can write a function, which calls other functions and waits for a response. This is known as an Orchestrator function and it calls “action functions”. The Orchestrator can then sleep while waiting for activity functions to complete. Activity functions can receive and return data. The Orchestrator function is only the workflow and does not perform any actions. So Durable functions allows you to write better workflows isolated from other code.

In order to start an Orchestrator Function running you need to write a OrchestrationClient binding which allows you to start a orchestrator running from a normal azure function or get information about an orchestration. So for example imagine you have items on a queue. A normal Azure function is bound to the queue and triggers when each item is on the queue. It then uses the OrchestratorClient binding to call an Orchestrator function which defines a workflow. As it flows through the work flow it triggers a number of action functions. Each time a function is run various App Insights records are made recording success and metrics to enable better diagnosis of issues, analysis of processing needs and costs and better feedback.

Durable functions also allows for waiting for human interaction before continuing.

Under the hood

Durable functions uses Azure Storage to implement its workflows. It will therefore need a connection string to do so. It uses Message Queues to trigger other action functions, storage tables to store the state of current orchestrations and in a format called event sourcing. Event sourcing requires that everything is stored by appending new records rather than overwriting previous records which allows for better diagnosis of issues. When an orchestrator wakes up it can then play through this list to see what parts of the work flow have completed.

Creating a Function App

Luckily Visual Studio, when you enable the workload for azure development in the VS installer, has the boiler plate for a typical function app. A function app is a container for multiple functions. You can easily deploy each function into a separate function app, but sometimes you might benefit from group similar or related functions together. In the case of durable functions where they are involved in the same workload it is likely to become useful, due to the common deployment process.

Included in the boiler plate is a proj file which declare the AzureFunctionsVersion, includes a reference to the Azure Functions SDK. Also included are some settings file such as the host.json which explains which version of Azure functions is used and what logging is done. There is also a properties folder with some server dependencies for deployment and a local version.

The boiler plate includes a sample function according to whatever you requested. So for example a Http Request based function has one of those.

You can add more functions by right clicking on the project, clicking Add and clicking New Azure Function. You will need one for the Orchestrator function.

Before you can add any durable azure function’s stuff we will need the Nuget package Microsoft.Azure.WebJobs.Extensions.DurableTask and a using directive

using Microsoft.Azure.WebJobs.Extensions.DurableTask;

Then you just add the IDurableOrchestrationClient interface as a parameter. The SDK should inject the orchestration client you need.

public static class FunctionCallsOrchestration
    {
        [FunctionName("FunctionCallsOrchestration")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get")]
            HttpRequest req,
            [DurableClient] IDurableOrchestrationClient orchClient,
            ILogger log
        )
        {
            throw new NotImplementedException();
        }
    }

First things first you will need to extract from the Http Request whatever data you intend to send to the function. I am not going to explain how to do that because it could be stored in any number of ways and getting data from a http Request is a different issue to working with Azure Functions, but I will include this boiler plate code for anyone that might benefit from it. It gets the string from the Http requests body, assumes that is a json encoded data object and creates the variable data based on it.

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);

Once you have your data then you can use the Orchestration Client that you injected as parameter to create a new orchestration function. If you remember you will know that this is the function that handles the work flow of your Durable Azure Function. It doesn’t do any of the actions itself it just figures out where we are in the process and orchestrates other functions to do the work, known as Action Functions. You need to pick a name which we will use when we are creating the Orchestrator for the name of the Orchestrator here, I have chosen O_RepondToName. As far as I can tell there seems to be a standard of prefixing Orchestrator Functions with “O_” and Action functions with “A_” particularly where coders are likely to have the same or similar names for each.

var orchesrtrationId = await orchClient.StartNewAsync(Orchestrators.RespondToNameConst, Guid.NewGuid().ToString(), name);

Finally it is helpful to respond with the means to get progress on the running of this orchestration. Note this uses the orchestrationId generated when the orchestration function was called and the Http Request message that was received to generate a response.

return orchClient.CreateCheckStatusResponse(req, orchesrtrationId);

Okay so that should compile, but it is not complete. When it attempts to run the function it will not find a function with the name O_RespondToName. So we need to create the Orchestrator function.

You can just create a basic static class to provide this. You can also create a static method to outline the Orchestration. It needs to have the attribute FunctionName with the name of the Orchestration function you chose earlier in this case “O_RespondToName”.

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

namespace FunctionApp1
{
    public static class Orchestrators
    {
        [FunctionName("O_RespondToName")]
        public static async Task<string> RespondToName(
            [OrchestrationTrigger] IDurableOrchestrationContext orchContext,
            ILogger log)
        {

            throw new NotImplementedException();
        }
    }
}

You will notice I included a couple of parameters. The most significant of which is the IDurableOrchestrationContext which we will be using. By prefixing it with the attribute OrchestrationTrigger this informs the compiler that this is an Orchestration function.

Obviously when we received the Http Request we removed a piece of data from it and passed it to the Orchestrator. We can do this with Queues as well. We passed the data to the Orchestrator when we called it so we might want to grab that data back now. We can do that with the GetInput function on the context.

var input = orchContext.GetInput<string>();

This doesn’t have to be a string.

Now for my first Activity I have chosen to call a function called “A_GetFullName”. Again I’m using the convention of beginning the Action Function names with A_ and prefixing O_ to the beginning of Orchestrator Function names. You will note that I specify the type of data that is returned, in this case a string. And once again we are calling a function that has not yet been created but we will do that next. I like to replace these function names with consts after they’re made to help link up the reference for anyone who is reading your code later.

var firstActionOutput = await orchContext.CallActivityAsync<string>("A_GetFullName", input);

Something to be aware of, here is that the await keyword works a little bit differently from how you might expect. In Durable functions this Orchestrator will queue up the Activity function to run using its connection to Azure storage (or in a dev environment azure storage emulator) and then it will record where it got to and stop running the Orchestrator so you’re not paying for functions running. The next time it runs the function it will run from the beginning to, but reaching lines like this, it will be able to determine from its records whether that Action Function has completed running and will be able to keep going through until it reaches a point where it has to wait for Action function to complete or it can return data.

Same process for second action…

var secondActionOutput = await orchContext.CallActivityAsync<string>("A_Greet", firstActionOutput);

Finally I am going to return something so I can get the data using the Durable Functions REST API later.

You can return the data you generated or just a string whatever you need.

return new
            {
                a = firstActionOutput,
                b = secondActionOutput
            };
// or
return secondActionOutput;

Now just like we did before we have an Activity or Action Function which is not yet defined and we need to define it. We can do so through the same process we did before however the parameters are slightly different. The OrchestrationTrigger is replaced with the ActivityTrigger which is not an Orchestration context but instead whatever the argument is that we had intended to pass into this function.

Here is a sample. I removed any references to my own servers and api keys replacing with just “google.com/api”. You will notice I have deliberately made distinct paths through the function where some do not rely on external servers so I can make sure that the function is working, even if the dependency is not available. This is simply for test purposed when setting up.

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Microsoft.Extensions.Logging;
using System.Net.Http;
using System.Threading.Tasks;

namespace FunctionApp1
{
    public static class Activities
    {
        [FunctionName("A_GetFullName")]
        public static async Task<string> GetFullName(
            [ActivityTrigger] string input,
            ILogger logger)
        {
            if (input == "Grey" || input == "Gray")
            {
                return "Graeme Chetcuti";
            }

            if (input == "John" || input == "Jonny")
            {
                return "Agent John Smith";
            }

            if (input == "neo")
            {
                return "Mr Anderson";
            }

            //Won't work don't want to reveal my details
            //Also not example of good code
            var httpClient = new HttpClient();
            var result = await httpClient.GetAsync($"google.com/api/GetfulName?name={input}");
            var data = JsonConvert.DeserializeObject<FullName>(await result.Content.ReadAsStringAsync());
            return data.fullName;
        }

        [FunctionName("A_Greet")]
        public static async Task<string> Greet(
        [ActivityTrigger] string input,
        ILogger logger)
        {
            if (input == "Graeme Chetcuti")
                return "Hello Dolores Day";

            if (input.Contains("Anderson"))
            {
                return "Take the blue pill Neo!";
            }

            if (input.Contains("Agent"))
            {
                return "Goodbye Smith";
            }

            //Won't work. Don't want to reveal any of my details.
            var httpClient = new HttpClient();
            var result = await httpClient.GetAsync($"google.com/api/GetGreeting?name={input}");
            var data = JsonConvert.DeserializeObject<FullName>(await result.Content.ReadAsStringAsync());
            return data.greeting;
        }
    }
}

So that gives us our basic Durable function. Next we need to add application insights loggin.

Adding Distributed Tracing to Durable Azure Functions Logging

Okay so one of the issues with logging these is that, as we said before the function is called a number of times. Each time the Orchestrator function is called it could end up logging a different piece of data in application insights which does not give us any continuity with regards to exceptions and how the function arrived at certain points. By adding the following code to the host.json file the logging becomes correlated with other log messages arising from the running of the same Orchestration.

  "extensions": {
    "durableTask": {
      "tracing": {
        "DistributedTracingProtocol": "W3CTraceContext"
      }
    }
  },
  "logging": {
    "applicationInsights": {
      "httpAutoCollectionOptions": {
        "enableW3CDistributedTracing": true
      } 
    }
  },

When an error occurs, this allows you to see where in the running of Orchestrations it was. This is often called distributed tracing.

Check Status Object

When we originally wrote the return statement of the function we returned a Check Status Object which does more than just check status.

When this function is called is immediately starts the Orchestration Function and returns a few URI. These include a URI for checking the status and ultimately getting the data that has resulted from the completed running of the function. Another which is for adding more information for example when a user has responded to question and the last one is to terminate a long running orchestration which is no longer needed.

All you need to do is create an object which is ready to receive this response and then use the URIs when required.

Code

I have included my code on git hub.

https://github.com/gchetcuti/experimenting-with-azure-functions

Primitive Obsession

I just learned this expression… I was complaining about the code using only basic datatypes and my boss agreed that it looked like “primitive obsession”. My reaction: What??? there’s a term for that???

A lot of the time I recognise a pattern in coding that works and I see issues with other patterns and I quickly develop rules in my head for good code. Sadly these rules do not necessarily exist in other people’s heads. One of the things I struggle with is my general assumption that everyone sees the same things I do and experiences them as I do. They don’t, I am really weird and often I need to remind myself of that.

Not speaking the lingo

A lot of the time when I try to explain the rules in my head to someone they say “that’s not a thing”. But it is obvious to me… then you end up disagreeing with someone. Here be monsters. The problem here is someone just told me I’m wrong. I can see the logic in what I am saying and when they don’t agree. Then, my brain assumes they just can’t see the logic I see, so I will show them. Then they hear me something that sound a lot like “No! You’re wrong”. Suddenly two people will fight to prove they are right even if they realise they were wrong all along.

Alternatively, when you know the special name for something, i.e “Primitive Obsession”, you just need to flash it, like it’s a detective badge or a VIP access all areas. Other devs suddenly recognise what I am talking about or otherwise look it up with a reputable source and walk away impressed.

The frustrating part is just because I might not know the special jargon name for it… it was still “a thing”. I just invented it. Just like the guy who invented it and established the name. The logic was sound, it just needed to be spoken by Martin Fowler and not me.

I guess I am trying to find a way to cultivate the understanding of logic and the ability to express it in a way that other people can understand. Sadly the only person who seemed to really communicate things well to me was the architect in an old company who couldn’t afford to pay me average wage.

Today, I learned about primitive obsession. This is where a developer sticks to the primitives of a programming language. Primitives are the basic building blocks of data within the language. For a language like C, these would be char, int, float… hopefully you get the picture. With regards to programming languages like C#, I think strings are also considered primitives too.

One of the great things about primitives is that everyone who uses the language knows how they work. They don’t need to do any reading. A string is a string and anyone who has used C# will know what a string is, unless they were doing something VERY niche in it.

Alternatively when you only use primitives then there’s a few things which I have spotted can go wrong.

A String is a String

Let’s imagine one of my most irritating issues… accidently swapping one parameter for another. Imagine you write a method and all its parameters are strings (I was dealing with one these recently). Now what if you wanted to rearrange the order of the parameters… well in places where the method was called you wouldn’t get an error message because it’s thinking login(string, string) not login(user, password). What if someone accidently wrote login(password, user) … the system does not complain.

Complex datatypes mashed together

Often a complicated datatype containing all sorts of data ends up mashed together in a string. A perfect example of this is a URI. A uri consists of a scheme (expressed as https://), a domain (such as google.com), sometimes a routing structure or directory struction (/search) and sometimes a query string (?source=hp&q=hello). The query string could also be broken down into multiple parameters such as source and q which have values hp and hello. Now imagine you want to figure out what one of those is. You need to work through massive string manipulation techniques to figure it out. For example finding the value of q in the querystring. You need to jump to the string which is after the ? then it is preceded by a ‘q=’ that either immediately follows the question mark or immediately follows an ampersand after the question mark and it ends with either the end of the string or the next ampersand. It’s starting to sound complicated because it unnecessarily is. Compare this code:

Query.Parse(uri.Query)['q']  

It would be even greater if the query string wasn’t a bloody primitive either then it would just be

uri.Query['q']

And this is all on the assumption the string is correctly formatted as a Uri in the first place. That’s another matter.

Validation

Now imagine that you spend the beginning of every method that received a password and a username as a string checking for validation. Is the password field Hashed correctly? Does it have the right check digit? Is it blank? Does the user name have an email format with an @ and a domain like it is supposed to? Well with a basic data transfer object (or a POCO class) you can leave validation to the creation of the class and just assume it is valid from then on.

Passed by reference

When you pass a method a primitive in its arguments, in most high-level languages you pass the actual data. However with complicated objects that might contain multiple pieces of data in most high level languages you pass a reference to a object. It is always possible that this data could be updated in the process and something could change. The code which is executing will always be working with the latest version of the data. But with primitives, it will be working with whatever the value was when it was passed the data.

Conclusion

So there’s a number of issues caused by using primitives for everything. Please don’t be afraid of a POCO (plain old clr object) class. A POCO class or a DTO class (data transfer object).

These classes mostly consist of a collection of primitives so you’re not far from it. They don’t have much logic in them, maybe a little validation, but they’re pretty simple and they make sure the data you’re using doesn’t get sent as the wrong argument, help keep things valid, give you simpler access to a complicated data structure. They allow you to pass things by reference which are dynamically updated when things happen in other threads. Don’t do it for the word though… do it because you see the logic.

Assembly Language

So if you couldn’t tell I’m actually a computer programmer by day. By night I’m a bit of a drag mother, who loves board games, mysticism, mystic artistic expression and to engage in philosophical and religious study. (Yes I am all over the place.) But … computer programmer.

When program computers, I often find that I work with high-level languages which do a lot of stuff for me. A perfect example of this is memory management and garbage collection. So often I don’t have a good idea of what the high-level language is doing for me.

Having spoken to friends who teach computer science at some of the best universities in the world, I have found one thing to be quite consistent. They like to take a bottom up approach. They go from electronics and switches to device style programming to high-level languages. I am trying to fill a hole in me that is left by the insecurity of not having the best degree for Computer Science, Law. Law enables me to engage with the customer and fully coordinate a meeting and make sure what we deliver meets exactly what the customer said they wanted, but I am often coding without a CS degree and that’s a bit like building a house on sand at times.

I hope to fill that gap with a number of different small projects like programming arduino projects, metal coding and assembling coding. Doing an Udemy course on programming for the Atari seemed a great place to start. I also have an actual Sega Mega Drive and a course from pluralsite on the memory registers of that so I should be able to take what I learn and apply it to the mega drive… but here goes.

This is the course I am working through:

https://www.udemy.com/course/programming-games-for-the-atari-2600/

Spec

When coding in assembly language, you’re writing specifically for the hardware you have so you need to be aware of the processor you have and all the other hardware and how it can be referenced from the motherboard. This becomes a crucial part of programming in assembly.

One of the crucial parts of programming is knowing the CPU which in the case of the Atari was the MOS 6507 (a cheaper version of the MOS 6502), which had 1.19 MHz. In the Sega Mega Drive it was the Motorola 6800 after the price had come down a bit.

Atari also had the Television Interface Adapter chip (TIA). This allowed for interaction with a cathode ray tube television in place of monitor used for modern computers.

The Atari had 128 bytes of RAM in the 6532 RIOT chip (RIOT = RAM input output and timer)

The Atari is used with a Cartridge being plugged directly into the motherboard which is a ROM of 4kb.

There are different versions of the Atari, but the Udemy course is focused on the “heavy sixer” which is version of the machine which was released in 1977 which has 4 switches on the console.

CPU

The CPU has a pin to indicate it is ready for an action it receives an incoming current and it is often indicated with RDY on diagrams.

There is also an interruption request pin indicated IRQ. In using the cheaper 6507 CPU there is not IRQ pin because it is not needed. To make the 6507 cheaper than the 6502 the interruption request was removed, NMI was removed and the address bus was slight more limited in size.

Registers. These are memory inside the CPU for current. We often refer to the Atari 2600 as an 8 bit machine because it can store 8-bit values in each register (1byte).

The data bus is 8 pins (or more in a 16/32/64-bit machine) is designed to put data in and out of registers.

The address bus is 16 pins sticking out of the CPU which indicate a memory location where something might be stored in ROM or RAM etc.

There are 2 chips for tv interaction (TIA), one is for NTSC TVs and the other for PAL TVs.

Additional pins tell us whether we are reading or writing to the CPU, some are for timers, resets, power and grounding. There is also the request bus ready state pin, don’t ask me yet what this does!

The CPU we are using has 1.19 million ticks per second.

Inside the CPU we are using here is 7 special areas. The pins can be used for input an output into the various areas. Those areas are the ALU and 6 addressable registers. A register is like a very fast access memory for a CPU.

ALU

The first part is the Arithmetic Logic Unit (or ALU). This is capable of a number of very simple addition, subtraction and binary operations. For multiplication and division it might have to do multiple operations.

PC

The register marked as PC is the “Program Counter”. I think this part of the CPU indicates where the program is going to go next. It might indicate what line of code to run next for example. I am not sure about a lot of this information so don’t learn from me go to Learn Assembly Language by Making Games for the Atari available on Udemy. It’s a cheap fun course with all the information you will need and it will be organised in a more sensible order for learning.

This contains memory addresses so in our 6507 processor this is 16 bits long.

SP

The next part is known as the “Stack Pointer”. When using the stack in memory we have a pointer to indicate where the latest value in memory is. Let’s imagine you’re adding to memory like pushing to an array in JavaScript, the SP will always point to the latest value and if you pop values off the stack it will retreat to previous values.

This contains memory addresses so in our 6507 processor this is 16 bits long, but the last 8 digits are always “00000001…”.

P

This next one is the Program flags. Instead of being a binary sorted number, this is a series of binary flags which can be switched on and off to indicate anything that happened in the last calculation run by the CPU. This could have a flag on to indicate that it failed in the previous calculation for example overflow where the calculated value was too big to be represented in the binary values available.

This could include whether the result was negative or zero. This contains 8 flags so is 1 byte in size.

The right most bit is called the “Carry Flag”. It determines whether during an addition it was required to carry a flag over to the next binary number, but could not do so because there are only 8 bits the carry flag is switched to 1. For example if you take the maximum number and added one then it would carry that 1 over when it completes the number and it would be too big to save.

The second to right flag is the “zero” flag. (XXXXXXX1X where X is a number that relates to a different flag) this would indicate that the result of the previous calculation was zero.

The third to right flag is used for IRQ disabled. Since this part of the process is not used it’s not relevant to this. (XXXXX1XX).

The forth flag is BCD (binary coded decimal). This is an accurate form of calculation, but it is slow so it is not always used. It indicates whether we were using BCD mode or not.

The fifth flag from the right is the break, which indicates a break signal caused the processor to interrupt what it was doing. (XXX1XXXX). Warning this might not be correct. Learn from the course; not from me!

The 6th flat form the right is unused by the processor just ignore that value.

The 7th flag from the right indicates that an overflow occurred and the value was unable to store the result of the arithmetic operation. It can be represented by a V. An example of this is when adding to a number written in two’s complement (where the 8th digit is negative) is the positive numbers are carried over to the next column and it’s a negative number. For example 0000 0001 + 0111 1111 = 1000 0000 which means 1 + 127 = -128 so this flag is activated to note that answer might not be quite right.

Finally the 8th flag (N) indicates whether the result was a negative number. (1XXXXXXX)

X,Y and A

The X and Y registers are general-purpose registers, which we can use for storing the data that we are working with. The A register is the accumulator, which is used by the ALU for doing work on the data. When the ALU performs a calculation one of the values comes from the accumulator.

These registers are all 8 bits in the 6502 / 6507 processor.

Binary

Obviously an important part of programming for electronics is understanding how a wire essentially can have a current or not and that is its state of 0 or 1, but you also need to understand how this can be abstracted as a number.

I already knew this from my A-level and GCSE Computing. But a quick review of it was useful.

SRE

What is SRE?

These are just my notes about Systems Reliability Engineering. This is something I have chosen to study as part of my work for a company called BankBI. By all means read it if you will, but I’m not really writing for readability. This is just a notes-drop from various courses.

SRE is often described as “what happens when devs design operations”. It’s hard to say for definite, that it is one thing or another because different aspects of SRE appeal to different authors.

Often there is a lot of focus on delivering feature after feature, while assuming that once a system is up then it is going to stay that way. When has that ever happened? SRE is concerned with recognising and planning for failure. SRE involves among other things, setting goals for the reliability of a system and making sure that when the system falls short of a goal, the team focuses on reaching the goals of reliability rather than new features. Obviously there’s more to it than that, but it’s one of the key things that comes up early in many definitions.

SRE sits along side DevOps. Some people like to conflate the two, but they have distinct aims. Nearly every author is against comparing them like they are opposites and only one can be implemented. In a nutshell, DevOps gets the Sys Admins and Developers to work together and SRE gets Support team / Operations and Developers to work together. It would tend to suggest us Devs aren’t good at working with anybody since there’s all these grand initiatives in place to get us to work well with people!

SRE is a child of Google’s inner workings. For years Google kept it to themselves and then suddenly they went public.

The Problem

One way that we can define SRE is to consider the problem that we are trying to solve. We can define SRE as “a collection of principles, ideals and ideas which are brought in to solve a divide between Ops and Development”.

Why would there ever be a divide in a company? Well in this case it come because these two teams while seeming to work very closely together actually have very different difficulties and problems that they face. They face customers having very different experiences so they end up seeing customers with very different desires.

The developers will traditionally hear about the latest features wanted by customers and potential new customers. By always considering the desires for new features they will reasonably assume that customers always want new features. Their focus will be to deliver new features as fast as possible to help make the customers and to help the sales team to onboard new customers who want different features.

Operations (or Ops) are managing the system on an ongoing basis. They can see how many computing resources are required and they are often the first point of call went something goes wrong. They are often face-to-face with customers who are experiencing bugs or instability. They will likely assume that customers want stability and no bugs.

As you can see these two teams often have an very different idea of what the customer wants and SRE is the solution to prevent the potential warfare between these two factions.

In SRE you set a target for reliability and stability. When the system falls short of that target then development’s priority changes from delivering new features to dealing with stability. With DevOps the development team and the Ops team begin to overlap more.

Core Tenets of SRE

This highest priority of any SRE team or agent is the following things:

  1. Availability
  2. Latency
  3. Performance
  4. Efficiency
  5. Change Management
  6. Monitoring
  7. Emergency Response
  8. Capacity Planning

Toil

While this is not a definition of SRE it is a concept that comes up in a few of the definitions. Toil is hard work. It often refers specifically to repetitive tasks which do not require much brain to do. In SRE these tasks are usually automated to save time. Remember if it takes longer to automate it then to do it a few times you need to think about whether it is worth automating. In an environment that is always changing it might not be worth it because that might only be done a few more times.

The student surpasses the master

Okay, he hasn’t surpassed me just yet… but I wrote in March about having a junior called Gareth. I look forward to the end of the month, where I not only celebrate his birthday with him, but also his second month in a programming role! Well done to Gareth who got his first programming role.

I have a junior!

So You may have seen my “I made it” post in 2016 when I joined the ranks of programmers, something I had been trying to do for a while. Well, it is time to turn around and give back to the communities I have formed around me and give them the opportunity that I had to seize for myself.

I have met a couple of people that wanted to become programmers and basically I vowed to help them, because I remember how difficult it was for me. First I worked with a chap for a couple of months, but sadly because of his disabilities, he felt that he just didn’t have the energy to work at it. I decided to let him go and not push him, because I really needed someone who wanted it so much, that they could work independent of me more. I recently found that in Gareth.

Gareth has been learning programming by himself and with my encouragement he has really put the effort in, completed online courses and continues to learn with me.

I am finding with Gareth that I use a lot of jargon that leaves him feeling baffled and sometimes with a full week in between sessions he forgets what he has already learned, so we have written a bullet point list of the stuff we have covered already and before each session we will quickly run through and speak briefly about the stuff in the list to remind him of what he already knows, to cement the jargon and to start each session reminding him of just how much stuff he really has learned in a very short space of time.

We also have plans for him to complete a small test project for my employer and a specific application for Dragged Around London my company. I am excited to see where this goes!

Arduino Programming

Most of the coding I do is very high level and so far removed from the physical technology I often feel like I don’t actually know the basics of what my code is doing on system level. I feel like I want to round out my programming knowledge so I’m working with a fun Arduino and I bought a collection of LEDs, resisters, capacitors, a bread board, a motor, a sensor and I think I’m going to have a little bit of fun!

My first project turned out very well. My hardware equivalent of a hello world project was a multi-coloured LED that flashed the colours of the rainbow in order combining different channels of RGB, but many more to come.