Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Wednesday, June 17, 2020

The Cost of Hidden Wires

In working on the Calm codebase one of the first things I did was wire in Wallaby.js because, well... it's amazing, and I absolutely love it. Having been in a mix of C# and JavaScript, Wallaby gives me the immediate feedback I find most useful in my TDD cycle. I do have a small love affair with the test runner, but... I will be using it as an example of how I used in a manner that gave me issues once I moved Calm into CI. I don't hold Wallaby responsible for it, I just hit a snag that needed to be addressed.

I've long held the belief that starting a project comes with it a special set of skills that I admittedly continue personal growth within. Wiring a project together to fit the needs of the overall longevity of it caries a set of unknowns sometimes that may or may not be obvious to you in the moment. And often that wiring is hidden away where you're unlikely to bump into it or have a need to go seek it out. Here is my simple example.



There is a small cost in time and a little bit of re-work that I had to pay in detangling some hidden wires in my specs. Wallaby provides a setup function that I had been leaning on until now. It was taking care of wiring some dependancies that each of my spec's needed... like hooking up chai and sinon, and it looked something like this:

'use strict';

module.exports = function () {
return {
name: 'Calm',

...
setup: function() {
chai.should();
var sinonChai = require('sinon-chai');
chai.use(sinonChai);
global.sinon = require('sinon');
global.next = function() {};
},
...
};
};
Having this configuration and wiring in the Wallaby configuration file meant that I did not have to import  those into every test file I created, which was great! That's the point of hidden wires... they are out of your way and do some special sauce to make you more productive. Once I spun up a test file, I could get on with the work that I truly cared about... my tests, without having to pull in that setup every time. I had even setup up my package.json to tell you that you could not run the specs from the command line if you ran "npm run test"... you would get the message:

"scripts": {
"test": "echo \"Error: use Wallaby to run tests from within your favorite Wallaby configured IDE\"",
"start": "node app.js"
}

I naturally hit a wall the second I began wiring up Continuous Integration in Circle Ci. Up to this moment, I hadn't had a need to run the tests from the command line. Remember, I'm still very early in the lifetime of this project, so I don't have a ton of code... it's still just basically a holding page with a few experiments in it that I've done along the way:




This issue could have been addressed in several ways, some involving more complexity than others, and I'm still deciding what I want to do long term. My immediate desire was to get specs running in CI... and CI deploying the application, that was it. And I was looking for the smallest commitment I could make in getting that there from a codebase perspective because I knew I would have my hands full enough understanding the CircleCI ecosphere.

In the short term, I've chosen to write a function containing all of the same configuration, moving it out of the Wallaby configuration to somewhere that can be exercised when specs are run from the command line, like this... testHarness.js is then imported into each spec file:





This may not be where things live long term, or maybe they are... the project still has a long way to go. This was a good first step though towards getting all the specs to run in CircleCi and deployments going out when things merge into the primary branch. As for the npm configuration to to run on the command line... it's now a punt to mocha :).



I haven't lost anything in my usage of Wallaby and CI works... a win win. My point in all of this is that the hidden wiring of pieces of our system often give us some efficiency or clarity in some other part of the system. The system is more scannable when reading the codebase, reducing some level of cognitive load along the way which is for sure valuable. Anytime I can reduce the amount of stuff I have to think about is typically a great thing. It can however come out of hiding as the system evolves though, and we need to be prepared for those types of moments. In my case it was a straightforward enough fix to make progress.


Wednesday, May 20, 2020

Sometimes... you take a detour



To say it's been a while since I've touched my side project (Calm) would be a complete misrepresentation of how long it's actually been. As you can see from my last post back in... um.... 2018... it's been a little bit longer than "a while". But, that's cool, life happens and I'm back at it, and that feels good 😊.

Since I've started picking back up the pieces and hints that I left myself, I've been somewhat confused by the highly technical notes that I left behind. Current me is trying hard to remember the context of some of these notes that past me left!! Nonetheless, I'm making some progress. One string of the yarnball that I started to pull on was beginning to use an environment file for some of the application level configuration (a tenet of The Twelve-Factor App). If you're unaware of the principals behind what is described at The Twelve-Factor App, I encourage you to give their site a read. As for configuration for your app, they require a strict separation of the config from the code. Things like port numbers, database connection strings, and 3rd party api tokens.

That's all well and fine! I did hit a little bit of a snag with my unit tests gaining the appropriate load of my environment variables when running my tests through Wallaby.js though. I took a dependency on the 'dotenv-flow' npm package into my app.js file, and wrote a test that ensured it was being used and that the port I was expecting was also being used where I anticipated it should be.

Perhaps not the most useful of tests I know, but sometimes I push things to the dogmatic edge just to see if I can, and to experience what challenges there are in doing so. Lo and behold, an unexpected challenge presented itself. My port was continually getting reported as undefined, regardless of what I tried. When I ran the app on it's own via 'npm run start', the port was console.log'ed as expected. I was not as lucky from within my test suite though. I was having a hard time finding an answer, and my google foo... for whatever reason... was failing me.

That was until I stumbled on this issue in the Wallaby.js repo. The Wallaby configuration file needed to load the .env file in order to make it's contents available to my testing context apparently... like this:

'use strict';
require('dotenv-flow').config();

module.exports = function () {
    return {
        name: 'Calm',

        files: [
            {pattern: 'src/**/*.js'},
            {pattern: 'app.js'},
            //list helpers for tests last, per wallaby's suggestion
            {pattern: 'testHelpers/*.js', instrument: false}
        ],

        tests: [
            {pattern: 'test/**/*-spec.js'}
        ],

        env: {
            type: 'node',
            runner: 'node'
        },

        testFramework: 'mocha',

        setup: function() {
            chai.should();
            var sinonChai = require('sinon-chai');
            chai.use(sinonChai);
            global.sinon = require('sinon');
            global.next = function() {};
        },

        lowCoverageThreshold: 99
    };
};

Now, does my use of a .env file completely satisfy the advice given the the author(s) of The Twelve-Factor App philosophy? No, I don't believe it does. However, it is a good first step that is better than having these types of things hard coded into the application itself. I suppose the next extension of their advice would be to have CI set the environment variables on the server itself that the code is deployed to. I'll tackle that one on some other dogmatic day, lol. Did my dogmatic requirement for this project having at least 99% code coverage force me to learn something new today? It did, and that's worth the effort to me.


Monday, June 1, 2015

The Grapefruit Buck

I have had the pleasure of discovering The Grapefruit Buck, my drink of choice when I need a serious break from the code. Give it a try, it helps to brink clarity to the world ;)

  • A generous portion of ice
  • 2 shots Absolute Ruby Red Vodka
  • ½ shot St. Germain's Elderflower liqueur
  • a splash Campari Bitters
  • a splash Lime Juice
  • fill the rest with Ginger Beer
  • drop a slice of grapefruit on top
Shake it all up and enjoy!


Tuesday, April 7, 2009

Road rules for commenting your code

I'll never forget the first programming professor I had in college. He was a developer for Coke in Atlanta, GA corporate offices and taught "Intro to Java" as a side gig. He was a good teacher so far as I can remember, very capable at presenting completely foreign material to a diverse group of students in a concise and understandable way, and for that I thank him. He made himself readily available for questions, including business hours when he was on duty at Coke if you had a pressing question that you needed to get in touch with him about.

However, one of the principals that he hammered us on was that you needed to comment all of your code. This included every class, every method...on down to every variable. Closing braces should have a comment on what block it was closing off whether it be an if-elseif block, or a for loop, or the closing brace on your class. Did not matter...it had better have a comment on it or you could count on 2 points coming off of your grade.

It was not until I graduated and entered the working world that I would discover that this was in fact, not the best practice to follow. I was surprised to find that the majority of developers on a large project I was involved with at work shunned the practice of comments (not always, but most of the time you could say). Why? They were quick to point out that comments quickly get outdated, and that it's better to rely on the code to tell you the story instead of the comment. The one caveat to this was that an interface (we were coding in java) should always have comments as it is a public and published contract.

I was reminded of this professor recently after having stumbled upon a series of articles by Robert C. Martin (otherwise known as Uncle Bob). These articles provide you with a glimpse into what you can expect to find in one of the most recent books he has been involved with, Clean Code: A Handbook of Agile Software Craftsmanship. They give a just shy of humorous but pragmatic viewpoint of how comments don't work sometimes... a lot of times. I've not had the chance to read the book yet, but if the article series of excerpts are a good representation of what is in the book, then I intend to be picking it up soon.

My personal viewpoint... definitely comment code that could be confusing. If this is the case though, then perhaps you should try to find a way to refactor the code to make more sense. I agree with my friend, comment interfaces that you write. Remove old commented out code as it is outdated and most likely will never be needed again. And if it is, that's what version control systems are for. And keep comments short and to the point... don't try to tell a story with your comment. If you are, then your code could probably use a refactoring in this instance as well.