Structuring my Code

09/09/2018 20:27 BST

I thought I shared this video, the way how Kat Zien explains the structuring the code is quite similar to how I structure my code.

I honestly did not think about the Hexagonal Architecture while I was writing well structured code and it is pretty much inline with that architecture, in terms of code maintenant.

I do agree with Kat on using init function, they can cause issues especially while running the test, it’s can accidentally initialise the database connection especially if you’re using continuous integration such as Travis CI, where is no database, there two ways on getting around that either you use build tags or better yet avoid using init functions, with something like a context system like ctx (That the tool I use for dependency injection for this site, it’s pretty cool, no yaml or xml to worry about, like you do with symfony, sorry couldn’t help it. 😀)

As for mocks instead of using subpackage, I would rather use a new build tag // +build debug and use a double extension *.mock.go, I find it’s cleaner that way; I also use that convention with sql *.sql.go and html *.html.go so I can easily identify where html and sql are later on without the build tag of course. 🙂

The biggest API I’m using on my site is redis, using that api for the entire site would of been a very bad idea in my humble opinion, because I would find it’s very difficult to create a mock for that API and also if they stop maintaining the API, than I would have to replace the API for the entire site, that not fun, so instead I created a small API in front of the big API, than use the small API for the entire site and it’s also easier to create a mock for the small API and if they do stop maintaining the big API it’s will not have big impact, because I only have to update the small API and that it. 👍

I find writing good code, self documenting, maintainable and the most important part fun, fun fun! Who can’t say no to that? 😉

| |