rockyourcode: Rules, by Sandi Metz, Why Vim Works for Me, We Overestimate Social Costs, Deploy to GitHub Pages Without Jekyll, How I Setup Go With Vim, Experiment: 100 Days of Blogging
Hello π! Thanks for subscribing.
Rules, by Sandi Metz
Published on: 2021-05-24
tags: Ruby
5 Rules for writing object-oriented code
My notes from a Ruby talk from 2013 by Sandi Metz.
What are rules?
- taboos
- laws
- norms (customs), group-held beliefs
- many of are arbitrary and not enforcible
- why do we follow rules? obedience? self-interest, identity/status, authority, order, social sanctions, ignorance, habit
5 Constraints
- No more than 100 lines per class
- No more than 5 lines per method
- No more than 4 parameters per method
- No more than 1 instance variable per view
- No more than 2 class names per controller/action (a Rails-specific rule)
Why?
- bias towards small objects (POROs = plain old Ruby objects)
- bias away from dependencies
- the rules are a means to an end: consistent goals
- design choice of small objects
- objects vs. procedures
- the entire applications is harder to understand, but you don’t need to understand the whole app β you only need to understand the part you want to change
- following the rules might be in your self-interest
Other benefits of rules
- signal others
- bias to collaboration
- the belief in some rules and the willingness and self-discipline to follow them
- you can break the rules only under the supervision of trustworthy humans
Links
Why Vim Works for Me
Published on: 2021-05-23
tags: Vim, DevTools
In this post I’m going to share why I like Vim.
I use NeoVim with Tmux which allows me to quickly open new terminal panes (spaces that don’t take up a complete window).
That means I can navigate between different terminals and projects without fuss.
Reasons Why I Like Vim
- Familiarity
I’ve been using Vim as my daily driver for 2 years. I remember loving VS Code 2 years ago.
Now it’s the opposite. I’ve tried switching back to VS Code for my future day job. And it’s a slog. All my muscle memory for navigating and editing text at the βspeed of thoughtβ does not work anymore.
- Customizability
When I use VS Code, I need to customize the key mappings to fit my needs (stay as mouse-less as possible).
VS Code has a lot of key bindings that conflict with the way I work. Jumping between split windows? You’ll need to define your custom key binding. Jumping between the VS Code terminal and the code? Again, you’ll need to customize.
At that point, I already spend a lot of time customizing Vim. I can stick to using Vim, because it clearly works (for me).
- Live in the Terminal
Vim is a first-class citizen of the command-line. Vim is quick to fire up, make an edit, and close again.
See how Patrick Ecker uses Vim and Tmux:
- Modal Editing, Language, and Text Objects
Vim has different modes and that makes the editor very powerful. You have a lot of functionality in normal mode (where you cannot insert text).
Every key can have more than one function, depending on the editor mode.
Vim also has a language. For example, d
is for deleting things.
Vim features text objects, so the program knows if you want to edit text inside brackets, or sentences, etc.
dw
deletes a word.
When you know text objects and Vim verbs, you can be very precise.
Here is a video by Drew Neil, author of “Practical Vim”: Vim - precision editing at the speed of thought.
We Overestimate Social Costs
Published on: 2021-05-22
tags: Lab, Reading, Thoughts
I am listening to The Scout Mindset by Julia Galef, a book that makes a case against motivated reasoning and argues for a more honest view on our thinking.
Quote from chapter 3:
We overestimate the importance of how we come across to other people, social costs […] feel a lot more significant than they actually are.
In reality, other people aren’t thinking about you nearly as much as you intuitively think they are, and their opinions on you don’t have nearly as much impact on your life as it feels like they do.
As a result, we end up making tragic trade offs, sacrificing a lot of potential happiness to avoid relatively small social costs.
I’ve been thinking about this quote.
I’ve suffered from being too mindful of what others might think. But in the end this attitude didn’t make me happy.
For example, I’ve had a secure job as a civil servant. A job for life.
But I am much more happy solving problems with code.
Leaving a position as a tax officer in Germany is basically insane because of all the benefits of being a public servant.
I decided to do it anyway. And in the end, a lot of people didn’t think me crazy.
And I also realized that their opinions really do not matter much in the grand scheme of things.
That is not an argument to be an asshole, but an argument to take care of yourself.
Further Reading
Deploy to GitHub Pages Without Jekyll
Published on: 2021-05-21
tags: Git, DevTools, Tutorial
I’m part of a community of self-taught developers. I’m trying to help tech newbies in the discord channel.
Judging from the number of questions, new developers seem to have problems deploying static websites to GitHub pages.
In this article, I’ll show you a basic way to bring your first front-end projects to life on GitHub pages.
Prerequisites
On your computer:
You’ll also need a free GitHub account.
Create a new project
You’ll need something that you want to upload to the internet: a HTML page, optionally with CSS and JavaScript.
You can either use your own project or you can follow along and make a dummy project.
Create a new folder on your computer.
I’ll show you the example commands for Unix. Windows commands might be different, so be aware.
bash
mkdir my-github-project
cd my-github-project
Initialize Git.
GitHub works with Git. We need to initialize the project.
bash
# replace with your name!
git config --user.name "Mona Lisa"
# use the email you used for signing up for a GitHub account
git config --user.email "monalisa@example.com"
git init
Now we’ll add a special .gitignore
file with the following content:
node_modules
.gitignore
Create files.
The minimum is a HTML file called index.html
. But let’s also create a JavaScript and a CSS file.
First, we need to put everything into a folder called dist
, as that’s the folder that will be deployed to GitHub pages. You can choose the name freely, but dist
is a common one.
If you change the name, you’ll also need to change it later in step 4.
Command-line commands (in your terminal):
bash
mkdir dist && cd dist
mkdir css
mkdir js
touch index.html
touch css/styles.css
touch js/scripts.js
Put the following content inside index.html
.
As you can see, the file references a JavaScript script and a CSS file. We should add them, too.
Content of js/scripts.js
:
console.log('Hello, world')
Content of cs/styles.css
: The content of the CSS file will be a modern CSS reset, which makes a good base template for your project.
Install npm package
Now we’ll need to install the Node.js package gh-pages, which will help us to bring the site to GitHub.
First, initialize a new npm project in the root project folder (the parent folder of the dist
directory).
bash
npm init -y
Now install the package:
bash
npm install -D gh-pages
You’ll now see a new folder called node_modules
and also a new file called package.json
.
Adjust package.json
:
// previous code
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"deploy": "gh-pages -d dist" // NEW
},
// more code
(Remember to change the name of the dist
folder if you previously used a different name.)
Create GitHub project.
Go to https://github.com/new with your web browser and create a new repository.
The name should be your project name, in this example it’s my-github-project
. You can make the project public or private, it does not matter. Leave the rest of the check-boxes unchecked.
You can find a step-by-step guide on the GitHub help pages.
Commit and upload your files.
Now we need to connect the files on your local computer with the remote repository (GitHub).
Follow the guide.
In you terminal:
git remote add origin https://<your-github-name>/<your-github-repository-name>.git
Replace <your-github-name>
and <your-github-repository-name>
with the correct values for your project.
Now we are ready to upload everything to the internet!
In you terminal:
git add . # add everything to the staging area
git commit -m "Initial Commit" # add a commit message
git push origin master
# origin is the name of the remote repository
# master is the name of the git branch
If you navigate to your GitHub project in your web browser, you should see that all your files are online now.
Deploy to gh-pages via script.
The final step is to run the npm script (see package.json
):
npm run deploy
FAQ
What if I have several projects in my folder?
You should make separate GitHub repositories. Repeat the steps of this blog post for each. Repositories on GitHub are free.
Final Words
Using Git, GitHub pages and Npm is not an easy feat for a beginner.
Brad Traversy has a YouTube video about deployng to GitHub pages. He also shows you to setup a domain:
Links
- GitHub Pages Deploy & Domain by Brad Traversy
- HTML5 Template: A Basic Boilerplate for Any Project by Louis Lazaris
- A modern CSS reset by Andy Bell
How I Setup Go With Vim
Published on: 2021-05-20
tags: Go, Vim
Language Server Support for Go in Vim
Last month I found a reddit thread about how to setup Vim for Go development.
The author uses coc.nvim, a heavy-weight plugin with external dependencies. coc.nvim (“Conqueror of Completion”) offers powerful features, but you’ll need to install Node.js.
If you prefer a more lightweight option that also work with plain Vim, this blog post might be for you.
Today I’m going to share my setup:
- works with Vim and NeoVim
- minimal external dependencies (gopls)
- auto-completion support
Prerequisites
You should have a working installation of Go on your computer.
Install gopls, the official language server:
GO111MODULE=on go get golang.org/x/tools/gopls@latest
Vim Setup
Vim needs a plugin for the Language Server Protocol.
I use prabirshrestha/vim-lsp, an asynchronous implementation that works both in Vim 8 and NeoVim. The plugin uses VimL and thus has no external dependencies.
Install with native package support or a plugin manager of your choice. Example:
cd ~/vim/pack
git submodule init
git submodule add https://github.com/prabirshrestha/vim-lsp.git
git add .gitmodules vim/pack/prabirshrestha/vim-lsp
git commit
(I’m happy with vim-packager, but configuring it requires a bit more effort and time. The native package manager works well since Vim 8, but is a bit clunky.)
Let’s wire up the language server with Go.
Create a new file in your Vim folder (~/vim/plugin/lsp.vim
) with the following content:
func! s:setup_ls(...) abort
let l:servers = lsp#get_allowed_servers()
# key mappings
for l:server in l:servers
let l:cap = lsp#get_server_capabilities(l:server)
if has_key(l:cap, 'completionProvider')
setlocal completefunc=lsp#complete
endif
if has_key(l:cap, 'hoverProvider')
setlocal keywordprg=:LspHover
endif
if has_key(l:cap, 'codeActionProvider')
nmap <silent><buffer>ga <plug>(lsp-code-action)
endif
if has_key(l:cap, 'definitionProvider')
nmap <silent><buffer>gd <plug>(lsp-definition)
nmap <silent><buffer>gk <plug>(lsp-peek-definition)
endif
endfor
endfunc
# register language server
augroup LSC
autocmd!
autocmd User lsp_setup call lsp#register_server({
\ 'name': 'gopls',
\ 'cmd': {_->['gopls']},
\ 'allowlist': ['go']
\})
autocmd User lsp_server_init call <SID>setup_ls()
autocmd BufEnter * call <SID>setup_ls()
augroup END
# disable diagnostics etc.
let g:lsp_diagnostics_enabled = 0
let g:lsp_diagnostics_signs_enabled = 0
let g:lsp_diagnostics_virtual_text_enabled = 0
let g:lsp_diagnostics_highlights_enabled = 0
let g:lsp_document_code_action_signs_enabled = 0
The first function creates key bindings if the language server runs. For example, hitting ga
will give you “code actions”. The most useful code action is auto-importing the necessary definitions. Code Actions are a feature I use a lot.
You can find a list of supported commands on the vim-lsp GitHub page.
Auto-completion?
Vim already has auto-completion out of the box.
Here is an excerpt from some extra key mappings in my ~/.vimrc
(“stolen” from bluz71:
"-----------------------------
" completion mappings
"-----------------------------
" t - user defined completion (via completefunc setting)
inoremap <C-t> <C-x><C-u>
Now, when I want the language server to kick in to complete my Go code, I smash CTRL+T
on my keyboard.
How to Format And Lint
The easiest way is to use the gofmt tool from the command-line. For example,
gofmt -w .
The same goes for linting.
Install golint:
go install golang.org/x/lint/golint@latest
Usage:
golint ./...
Alternatively, you can install ALE. Here is an example setup which you can adjust for Go.
Conclusion
I’ve showed you a lightweight setup for Go and Vim. The above plugins and settings have served me well.
There are languages where I’d recommend a dedicated IDE (Java), but for Go I haven’t felt the need yet.
Links
- How to set up Vim for Go Development (on reddit)
- Vim: So long Pathogen, hello native package loading
- Let Vim do the typing
- vim-lsp
Experiment: 100 Days of Blogging
Published on: 2021-05-19
tags: Lab, this_blog, Writing
Inspired by Seth Goding and other folks like Mike Crittenden, I’m going to write a blog post each day for 100 days.
Writing daily was a habit that I followed in 2019 and 2020, until I burned out on writing.
My situation is different now. I’ve quit my job as a civil servant and will start my first job as a software developer in July.
I plan to use my free time to learn a few new things (Angular, Java), play around with things that I enjoy (Go), writing and reading.
My start as a freshly minted software developer should also give new fodder for blog posts.
I have a script that creates newsletter drafts from my blog which I will adjust. So if you’re a newsletter subscriber, you’ll get a weekly email instead of daily emails.
Thanks for reading my blog! π
Thank you for reading my blog posts.