12 ms·
Nice article! In my opinion, this level of abstraction may be going a little far: GOCMD=go GOBUILD=$(GOCMD) build GOCLEAN=$(GOCMD) clean GOTES
by clra 9y ago
Nice article!
In my opinion, this level of abstraction may be going a little far:
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
It might seem like a good idea to constantize everything, but in practice, those commands are never going to change.
By just invoking them directly in the targets you'll come out with something more readable and no less maintainable:
build:
go build -o $(BINARY_NAME) -v
- kqr 9y agoBut you may want to add a compiler flag, or change the path of the binary. If the binary is called in multiple places it does make sense to constantize it.
- clra 9y agoPossibly, but by moving flags up to the constants you also run the risk of obfuscating things further. A little bit of repetition when it comes to compiler flags (especially in Go where there are relatively few) is a good trade for the added clarity it conveys. Invoking a particular binary is valid, but even there, you only need one constant, not one for every subcommand.
- firethief 9y agoOr stick in a wrapper.
- loudmax 9y agoI don't have much experience with makefiles but for shell scripts, I tend to think that your approach is correct as long as you're working with small or fairly trivial files. If the file is small enough to see all at once (and your terminal window or editor isn't outrageously large), then mentally, you can take in the whole thing at once. Once the file starts to get even a little bit complicated, you'll want those abstractions.
- marcus_holmes 9y agoEspecially when dealing with Go, which embraces "a little bit of repetition"
- cesarb 9y agoThat is a common Makefile design pattern. It allows you to call "make GOCMD=path/to/custom/prerelease/go" to run with an alternative go version, for instance. (But I agree, the other variables seem overkill.)
- iambvk 9y agoDon't we need to use "GOCMD ?= ..." in the makefile to allow overridding GOCMD from command line?
- wruza 9y agoDepends on use case. There is no religion, make is just a tool for your needs. Your experience should dictate. Mechanism, not policy — the good old and mostly hyped-out principle.
- jaytaylor 9y agoYes, good catch. The `?=' operator will only define the variable if it doesn't exist (and thus allows overriding via: GOCMD=/foo/bar/baz make