Don'T End The Week With Nothing: Patrick Mckenzie
Don'T End The Week With Nothing: Patrick Mckenzie
End The
Week
With
Nothing
by Patrick McKenzie
Curator
Lim Cheng Soon
Contributors
Rick Webb
Patrick McKenzie
Tom Martin
Csaba Okrona
Padraig Brady
Hadi Hariri
Julia Evans
Steve Pear
James Long
Leo Babauta
Advertising
Published by
Netizens Media
46, Taylor Road,
11600 Penang,
Malaysia.
Proofreaders
Contact
Emily Griffin
Sigmarie Soto
Illustrators
Lorenz Hideyoshi Ruwwe
Joel Benjamin
Ebook Conversion
Ashish Kumar Jha
Printer
MagCloud
Dont
End The
Week
With
Nothing
by Patrick McKenzie
Hacker Monthly is published by Netizens Media and not affiliated with Y Combinator in any way.
Contents
FEATURES
Nothing
By Patrick
Mckenzie
Illustration by Lorenz Hideyoshi Ruwwe [hideyoshi.deviantart.com]
PROGRAMMING
16
SPECIAL
By Tom Martin
19
By James Long
By Csaba Okrona
23
33
39
By Leo Babauta
By Pdraig Brady
27
By Hadi Hariri
29
By Julia Evans
31
By Steve Pear
FEATURES
the
economics of Star
Trek, there have
been three broad approaches in the
past:
hen looking at
4 FEATURES
What we know
Lets start with the facts.
The Federation is clearly not a
centrally planned economy, and
therefore obviously not communist.
Individual freedom of choice is very
obvious. Everyone chooses their
careers, and there are many mentions of this throughout the series
witness every single time someone waxes nostalgic about why they
chose to enter Starfleet. Witness
Bashir going on about why he
wanted to be a doctor instead of a
tennis player. Witness Wesley dropping out of Starfleet. Witness Vash
being an archeologist and Kasidy
Yates being a cargo ship captain.
Private ownership still exists the
biggest examples, to me, are Siskos
restaurant and Chateau Picard,
but many other examples abound
from all the trinkets everyone owns
in their quarters. Crushers family
owns a (haunted) cottage on some
old-Scottish settlement planet. The
Maquis routinely refer to our land,
which they presumably owned,
6 FEATURES
8 FEATURES
10 FEATURES
I concentrate
more on the needs of
established software
businesses, but recently Ive been
asked for some advice by people
who are still in the trenches working at a traditional day job.
Theres absolutely nothing wrong
with day jobs. Most people have
them. Theyre an honest living.
Some people really enjoy the
particular one that they have. If
your day job is right for you, that
is wonderful and I will not secondguess your decision.
Many people nurse dreams of
entrepreneurship because their
day job is not quite right for them.
Heres my story.
I used to be a salaryman at a
Japanese mega corporation. The
social expectation is that the company insulates the employee from
all risk, and in turn, the employee
swears themselves body and soul to
the company.
I worked 70 to 90 hour weeks for
three years. This isnt particularly
out of the ordinary for white collar
employees in Japan. It didnt strike
me while I was a salaryman that I
was going to continue doing it until
retirement, largely because the
amount of work was killing me, and
I worked after those hours on my
own projects.
Somebody asked me recently
how I managed to stay motivated
to work for the 91st through 95th
hours every week. Answer: because
I wanted to end the week with
something.
sually
11
12 FEATURES
13
14 FEATURES
Amy Hoy has a great metaphor for this: stacking the bricks.
[hn.my/bricks] Seen from the
outside, you might say That person
with an impressive career? Its like
they have a sheer wall made out of
awesome. I could not hope to ever
have a wall like that. Seen from
the inside, it looks like one day of
delivering a single good conference
talk, a few weeks spent writing an
OSS library, another day writing
the definitive blog post on getting
multiple Ruby versions playing
together, a few months shipping a
product used by many people, an
hour recording a podcast. Brick by
brick, stone by stone, the wall gets
higher.
Consumption Is Sometimes
Valuable, But Creation Moves
You Forward
Ill close with my usual advice to
peers: reading this was valuable
(knock on wood). Watching Jasons
video is valuable. Rolling up your
sleeves and actually shipping something is much, much more valuable.
If you take no other advice from
me ever, ship something. Youll
learn more shipping a failure than
youll learn from reading about a
thousand successes. And you stand
an excellent chance of shipping a
success people greatly overestimate how difficult this is.
Just dont end the week with
nothing. n
Patrick McKenzie is a small software
developer. He made Bingo Card Creator
and Appointment Reminder. He blogs
at kalzumeus.com/blog and teaches
people how to sell more software at
training.kalzumeus.com
Reprinted with permission of the original author.
First appeared in hn.my/week (kalzumeus.com)
Illustration by Joel Benjamin.
15
PROGRAMMING
16 PROGRAMMING
and run the server weve built (hint: if you usually have
an instance of Redis running locally now would be a
good time to stop it):
> src/redis-server
17
18 PROGRAMMING
(integer) 3
redis 127.0.0.1:6379>
(integer) 1
redis 127.0.0.1:6379>
(integer) 43
redis 127.0.0.1:6379>
(integer) 55
redis 127.0.0.1:6379>
(integer) 86
rand 10
rand 100
rand 100
rand 100
fter a few
Fetch a file from another branch without changing your current branch
Local cherry-picking. Gotta love it. Imagine youre
experimenting on your current branch and you suddenly realize you need a file from the oh-so-distant
branch. What do you do? Yeah, you can stash, git
checkout, etc., but theres an easier way to merge a
single file in your current branch from another:
$ git checkout <OTHER_BRANCH> -- path/to/file
19
Making typos?
Git can autocorrect you.
$ git config --global help.autocorrect 1
$ git dffi
WARNING: You called a Git command named 'dffi',
which does not exist.
Continuing under the assumption that you meant
'diff' in 0.1 seconds automatically...
Autocomplete, anyone?
If you download this file [hn.my/gitcomplete] and
modify your .bash_profile by adding:
source ~/.git-completion.bash
Or say you dont want this for all files (*), only scala
sources:
*.scala filter=stripWhitespace
20 PROGRAMMING
git-fsck
$ git fsck --full
Before you play the blame game, make sure you check
youre right with:
$ git blame -w
$ git blame -M
$ git blame -C
files n
21
Inappropriate use
Shell is the main domain specific language designed to
manipulate the UNIX abstractions for data and logic,
i.e., files and processes. So in addition to being useful at
the command line, its use permeates any UNIX system.
Correspondingly, please be wary of writing scripts that
deviate from these abstractions and have significant
data manipulation in the shell process itself. While flexible, shell is not designed as a general purpose language
and becomes unwieldy when not leveraging the various
22 PROGRAMMING
UNIX tools effectively. A good knowledge of the various UNIX tools goes hand in hand with effective shell
programming.
Stylistic issues
First Ill mention some ways to clean up shell scripts
without changing their functionality. Note: I use
a shortcut form of the conditional operator below
(and in my shell scripts), when doing simple conditional operations, as its much more concise. So I use
[ "$var" = "find" ] && echo "found" instead of the
equivalent:
if [ "$var" = "find" ]; then
echo "found"
fi
[ "$var" ] || var="value"
Setting a variable if its not previously set is a common
idiom and can be more succinctly expressed as:
: ${var="value"}.
Do:
mkdir -p "$dir" #also creates a hierarchy for
you
rm -f "$file" #also never prompts
Robustness
Globbing
In the example below to count the lines in each file,
there is a common mistake.
for file in `ls *`; do
wc -l $file
done
23
#!/bin/sh
tf=/tmp/tf.$$
cleanup() {
rm -f $tf
}
trap "cleanup" EXIT
touch $tf
echo "$tf created"
sleep 10 #Can Ctrl-C and temp file will still be
removed
#temp file auto removed on exit
Echoing errors
If you just echo "Error occurred" then you will not
be able to pipe or redirect any normal output from
your script independently. Its much more standard
and maintainable to output errors to stderr like echo
"Error occurred" >&2. Note: you can echo multiple
lines together as in the following example:
echo "\
Usage: $(basename $0) option1
more info
even more" >&2
Portability
There are two aspects to portability for shell scripts.
Theres the shell language itself, and there are the various tools being called by the script. Well just consider
the former here. To support really old implementations of shell script, one can test with the heirloom,
for example. But for a contemporary list of portable
shell capabilities, see the The Open Group spec, which
describes the POSIX standard. Check out the Autoconf
info on shell portability, which lists details you need to
consider when writing very portable shell scripts, and
the ubuntu dash conversion info.
Its much better to test scripts directly in a POSIX
compliant shell if possible. The bash --posix option
doesnt suffice, because it still accepts some bashisms. However, the dash shell, the default interpreter
of shell scripts on ubuntu, is very good in this regard.
One should be testing with this shell anyway due to
the popularity of ubuntu, and dash is easy to install on
Fedora.
24 PROGRAMMING
Bashisms
bash is the most common interactive shell used on
UNIX systems, and consequently, syntax specific to
bash is often used in shell scripts. Ive never needed
to resort to bash-specific constructs in my scripts. If
you find yourself doing complex string manipulations
or loops in bash, then you should probably consider
existing UNIX tools instead, or a more general scripting language like python.
[ "$var" == "find" ] && echo "found"
Shell script cant assign variable values in conditional
constructs, so the double equals is redundant. Moreover it gives a syntax error on older busybox (ash) and
dash at least, so avoid it.
echo {not,portable}
Brace expansion is not portable. Its most useful at the
interactive prompt, and can easily be worked around in
scripts.
Signal specifications
Be wary when specifying signals to the trap builtin,
as mentioned above. I was even caught by this in my
timeout script. That script handles the CHLD signal,
which, for bash at least, can be specified as sigchld,
SIGCHLD, chld, 17 or CHLD, only the last of
which is portable.
echo $(seq 15) $((0x10))
The command above containing both $(command
substitution) and an $((arithmetic expression)) is portable. Traditionally one did command substitution using
backquotes like seq 15. Thats awkward to nest, however, and its not very readable in the presence of other
quoting. $((arithmetic expressions)) can also be handy
for quick calculations, rather than spawning off bc or
expr. Bash supports the non-portable form of $[1+1]
for arithmetic expressions, which you should avoid.
echo --help
Ive used echo in all the examples above for convenience, but one should be wary about using it, especially if you pass variable parameters. echo implementations vary on how they handle escaped characters
and options, so one really should use printf instead, as
it has a more standard implementation across systems.
Performance
Well expand here on our globbing example to illustrate some performance characteristics of the shell
script interpreter. Comparing the bash and dash
interpreters for this example where a process is
spawned for each of 30,000 files, shows that dash can
fork the wc processes nearly twice as fast as bash.
$ time dash -c 'for i in *; do wc -l "$i">/dev/
null; done'
real
0m14.440s
user
0m3.753s
sys
0m10.329s
$ time bash -c 'for i in *; do wc -l "$i">/dev/
null; done'
real
0m24.251s
user
0m8.660s
sys
0m14.871s
The above script is by far the most efficient solution. It illustrates the point that one should do as
little aspossible in shell script and aim to use it just to
connect the existing logic available in the rich set of
utilities on a UNIX system.
Disk seeks
It is worth giving special attention to this, since disk
seeks are so expensive, and since shell script is designed
to deal with files which commonly reside on disks. If
you check for the presence of 2 files with [ -e FOO -o
-e BAR ], then the check isnt short circuited and 2
disk seeks are performed. The bash -format of [ -e FOO
|| -e BAR ] does short circuit the second test; however, its better to use the [ -e FOO ] || [ -e BAR ]
conditional format, which is both portable and efficient.
Traditionally this last example would have used 2 processes, one for each of the [. But modern shells implement [ internally, so there is no such overhead. n
Pdraig Brady is a long time open source contributor and is a
maintainer of the GNU coreutils project. He currently works for
Red Hat on the OpenStack project.
Reprinted with permission of the original author.
First appeared in hn.my/shellmistake (pixelbeat.org)
25
Refactoring to Functional
Why Class?
By Hadi Hariri
In College
Teacher: We are surrounded by
objects in the real world. These can
be cars, houses, etc. Thats why its
very easy to associate real world
objects with classes in Object Oriented Programming.
2 weeks later
Jake: Im having a bit of hard time
with these objects. Can you give me
some guidance?
Teacher: Sure. Theres actually a
couple of more or less formal processes to help you, but to sum it up,
look for nouns. And verbs are like
methods that can be performed on
the class. The behavior, so to speak.
Jake: Well, that seems reasonable.
Thanks!
Jake graduates.
26 PROGRAMMING
Jake: And?
Jake: DTO?
Jake: OK.
A year later
Mary: Jake, Im just reviewing this
class of yours, theres not much
behavior in it.
Jake: Yeah, well, I wasnt sure if
that behavior belonged in the Customer class or to the Accounts class,
so I placed it in this other class
called CustomerService.
2 years later
Mathew: Jake, looking at this class.
Its tightly coupled to a specific
implementation.
Jake: Huh?
Mathew: Well, youre creating an
instance of Repository inside the
Controller. How you going to test
it?
Jake: Hmm. Fire up a demo
database?
3 years later
Over a beer
Jake: You know, Pete, Ive been
thinking. They teach us that we
need to think in terms of objects
and identify these with nouns
among other techniques. We then
need to make sure that we name
them correctly, that theyre small,
that they only have a single responsibility and that they cant have too
many dependencies injected into
them. And now theyre telling us
that we should try and not maintain
state because its bad for concurrency. Im beginning to wonder,
why the hell have classes at all?
4 years later
Anna: Jake. This class, why did you
name it Utils?
27
Paths to Being a
Kernel Hacker
By Julia Evans
for advice
about how to get started with
kernel programming, and was
basically told:
once tried asking
28 PROGRAMMING
Heres the source for chmod_common, with some comments from me:
static int chmod_common(struct path *path,
umode_t mode)
{
struct inode *inode = path->dentry->d_inode;
struct iattr newattrs;
int error;
// No idea what this does
error = mnt_want_write(path->mnt);
if (error)
return error;
// Mutexes! Prevent race conditions! =D
mutex_lock(&inode->i_mutex);
// Check for permission to use chmod
error = security_path_chmod(path, mode);
if (error)
goto out_unlock;
// I guess this changes the mode!
newattrs.ia_mode = (mode & S_IALLUGO) |
(inode->i_mode & ~S_IALLUGO);
newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
error = notify_change(path->dentry, &newattrs);
out_unlock:
mutex_unlock(&inode->i_mutex);
// We're done, so the mutex is over!
mnt_drop_write(path->mnt); // ???
return error;
}
29
30 PROGRAMMING
CSS
.outer-div
{
padding: 30px;
}
.inner-div
{
margin: 0 auto;
width: 100px;
}
HTML
<div class="outer-div">
<div class="inner-div">
</div>
</div>
CSS
.outer-div
{
padding: 30px;
text-align: center;
}
.inner-div
{
display: inline-block;
padding: 50px;
}
HTML
<div class="outer-div">
<div class="inner-div">
</div>
</div>
HTML
<div class="outer-div">
<div class="inner-div">
</div>
</div>
HTML
<div class="outer-div">
<div class="inner-div">
</div>
</div>
31
SPECIAL
32 SPECIAL
All the cords sit hidden on this ledge, making the desk look
nice and clean
33
The two end pieces joined. The pipe clamps are bending but
I fixed that after I took the picture.
Joining the two end pieces. You can see the dowel holes.
34 SPECIAL
Ridges cut into the aprons for the table top fasteners. I used a
circular saw so it was difficult.
First coat of stain. The color is red mahogany. I cant believe I did this
when it was humid outside, as it became kind of gummy.
Second coat of stain.
I used a milder brown
color for this coat.
I used dowels and wood glue to join the aprons
with the legs
35
The back ledge will hold all the cords and other untidy stuff
out of sight.
The final base, stained and finished the same as the top.
36 SPECIAL
Dashboards
StatsD
Happiness
*Hosted Graphites mantis shrimp / eagle breeding program has been unsuccessful thus far
37
he end of procrastination
is the art of letting go.
Ive been a lifelong
procrastinator, at least until recent
years. I would put things off until
deadline, because I knew I could
come through. I came through on
tests after cramming last minute,
I turned articles in at the deadline
after waiting until the last hour, I
got things done.
Until I didnt. It turns out
procrastinating caused me to miss
deadlines, over and over. It stressed
me out. My work was less-thandesirable when I did it last minute.
Slowly, I started to realize that procrastination wasnt doing me any
favors. In fact, it was causing me a
lot of grief.
But I couldnt quit. I tried a lot of
things. I tried time boxing and goal
setting and accountability and the
Pomodoro Technique and Getting
Things Done. All are great methods,
but they only last so long. Nothing
really worked over the long term.
Thats because I wasnt getting to
the root problem.
38 SPECIAL
39
40 SPECIAL