Ask HN: Share a gem. Teach me and you.

194 points by lionhearted ↗ HN
One of my favorite discussion threads of all time, on any forum, was "Teach me and you. Give a gem" on the Civfanatics forum. It wound up being 15 pages of insights on how to play Civilization IV well, and was really a wonderful, enjoyable, community building experience.

Here was the first post:

"Share a "one-liner" simple strategy that can help beginners, seasoned players and maybe even be a gem that one of the pros can learn from.

Feel free to explain your tip, but avoid complex, in-depth strategy."

So, care to share a gem about technology, entrepreneurship, or general Hacker News type topics?

306 comments

[ 0.29 ms ] story [ 494 ms ] thread
Mine: Occasionally buy a can of compressed air and a soft cloth to clean up your computer. The few dollars will keep your expensive kit running in better condition and lasting longer.
Whiteboards are always a good investment.
To add to this, use shower board from a hardware store. The cost is usually <$20 for a 4'x8' sheet. The quality varies but the price is hard to beat.
Or, alternatively, place 4 mm thick window pane glass over your walls, or the much cheaper cellophane.
Back everything up. Now.
Occasionally try restoring your backups and check that the really important stuff is still in a usable form.
Also, every few months take a few minutes and make sure that you really are backing up what you need.

I had a client a few weeks ago that had a drive failure. We went to restore from his offsite backup copy and found that some of the files he needed weren't being backed up. A quick review of the backup job would have caught this.

And conversely, make sure you aren't backing up what you don't need.
Amen! You only know how painful this is if you've something very important. Not-backing-up cost me, shoot I don't know, at least $4,000 once, and probably more than that.

Also, answer this question: If your home burns down, will your backup still work? I like to have one or two hard copies, and one cloud copy of the most important things.

If the coefficients of a polynomial add to zero, then (x-1) is a factor.

For example, let's try and factorise f(x) = 2x^3 + 9x^2 + 4x - 15

What happens if we set x = 1? Then f(1) = 2 + 9 + 4 - 15 = 0, so (x-1) must be a factor.

It's much easier to get f(x) = (x - 1)(2x^2 + 11x + 15) = (x - 1)(x + 3)(2x + 5) if you know one of the factors in advance. This was useful at school, at least. Not so much now.

* See http://en.wikipedia.org/wiki/Polynomial_remainder_theorem

(comment deleted)
As a corollary, on the same lines of thinking,

If the co-efficients of the odd powers and the even powers are equal, then (x+1) is a factor.

Either somebody picked up the telephone while you were online typing this out, or I need to take more math classes.
Surely one of you hackers can offer us a practical application for any nifty math tricks on display in these comments.

I'm, ah, coming up blank on this one for now.

Mentally accept that bugs with your code are your fault. You'll end up fixing them faster.
Excellent advice. I'd go one step further: accept the problems in your life are your fault (responsibility). You'll end up fixing them faster.
The opposite of that is the root of a lot of trouble in this world.
If you could kick the person in the pants responsible for most of your trouble, you wouldn't sit for a month. - Theodore Roosevelt
Better yet: start calling "bugs" defects.

Also, accept that humans are imperfect and no code (whether yours or another's) will be defect-free. Fixing is more important than blaming.

Sometimes we get stuck, writer's block kicks in. Do yourself a favor, get up and go do something else. The magic elves will come and fix whatever you were stuck on, I promise.
A productivity gem from Jerry Seinfeld, heavily paraphrased.

For anything you wish you were doing every day, like working out or practicing violin or working on your startup: Get a big, yearlong calendar and put it on a wall in your bedroom. Every day you complete your task, mark a big, red "X". Eventually, you'll build up a big string of X's and won't want to break it. That incentive will push you more than you think.

And before everyone gets too excited, it's been created as a web-app already a few times!
Could you share a link?
Also, joesgoals.com
Web app has an important downside: it's not hanging on your wall. Out of sight, out of mind.
Unless you're looking at your web browser more often that your wall.
Amen. That's why, when I was seriously dieting, I had a calendar where I would write my weight every day.

Worked much better than the excel spreadsheet I had tried before (the one from the Hacker's diet).

I have such a calendar in my binder but I only do a few weeks at a time. I use the stopwatch feature on my watch to count hours worked and mark that down daily.
I used to have really yellow teeth because I brushed my teeth really rarely (thank goodness I never smoked). I've had some periods where I brushed them more often but I'd quickly fall back into hopelessness.

Then I hung a small calendar in the bathroom where I cross each day that I brush my teeth, and the "I don't want to break my win streak" feeling is so RIDICULOUSLY compelling that it's been many months now and I brush my teeth almost every day, in fact I didn't miss one day since 1 month and 3 weeks ago.

DON'T underestimate the sheer POWER of "win streaks".

My teeth are almost white now!

Strange, my teeth are light yellow and I've hardly ever missed a day. Most days I brush twice.

From this I'd gathered that tooth discoloration is permanent.

Well, I consider that they're "almost white" in relation to the piss-yellow I used to have. But the difference is dramatic nonetheless.
there's at least one show-stopping bug lurking somewhere in your code.
jfdi.

or its cousin:

maybe, but do it anyway.

Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems, fortunately, one solves the other.
My favourite variation:

Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have three problems, the original problem, the regular expression, and dealing with the annoying developer who isn't that good at simple regular expressions who will repeat this phrase everytime he comes across a regex in any codebase rather than actually figure out how they work.

Never forget that success if always a collective effort, so invest your time and energy in relationships with others.
To be a kind, you only need one crown. That is in in real life the magic moments are created from routine days and they shine because they are the exception, never the rule. So don't coin many gems or you will end up seeing them as simple rocks.
Read the spec before programming.

(This might seem obvious, but I find that it is tempting to "just see if I can get it to work" before I read the documentation. I always end up with worse code than when I take the time to read up on the technology. A similar advice is The Pragmatic Programmer's "Don't program by coincidence")

You only need one. You only need one business venture to work right to be considered successful.
Once - you are lucky, twice - you are good.
Success is simply a matter of luck. Ask any failure. - Earl Wilson
Mine : Remember a pay cheque goes a long way in alleviating the horrors of a corporate job. Not everyone can afford to be a struggling hacker waiting for the big pay-off.
Goes a long way where?
Sitting in your day-job having to use (a very flaky) ClearCase instance working on a monster code base that has no tests and has a non-deterministic build process. Think of the pay-cheque at the end of the month. Get paid, save/invest properly and use your evenings and weekends to work on "that" project.

Course, this is my personal motivational technique.

For a constant growth rate of r%, the formula for the doubling time Td is given by log(2)/log(1 + r/100) which can be simplified to approximatly 70/r.

* See http://en.wikipedia.org/wiki/Rule_of_72 for more info and background/variations that improve on accuracy in certain situations.

Not sure if it is a 'gem' but it certainly is useful:

tail -10000 somelogfile | cut -d ' ' -f 1 | sort | uniq -c | sort -n | tail

Quick one liner to get a fix on who is currently messing with your server in ways that are 'counter-productive', also a nice way to show newcomers to unix how you can string together existing unix commands to create new ones on the fly.

As for start-up advice that would fit in one line:

A practical, bug ridden, bad implementation of something trumps a theoretical, perfect implementation of the same thing.

To generalize that a bit: learn the unix cmdline utilities and how to use them together. Something as simple as

  grep "some pattern" * -R | cut -d' ' -f1 | 
    xargs sed -i -e 's/some_other_pattern/replacement/g'
will save you 15 minutes of painful searching and replacing through numerous files.
And keep a cheat sheet of the ones you've found really useful.
And always, always, always! test commands (or queries, for that matter) before executing them. For instance, my example would run 'sed' multiple times on files containing multiple matches of the string grep searches for. And it should have been cut -d':' -f1. Running it without the 'sed' command would have made both those things clear immediately. If you think you know for sure it does what you intend it to do, but you didn't test it, then it doesn't do what you think it does.
A good trick for that is to type in the whole command with 'echo' as the final one in the place of the command that actually makes changes. Then you replace the echo with the 'real' thing on the next run if the results where what you expected them to be. In the case of 'rm -rf $1. *' that would save you a lot of hunting around for backups ;)
This also works for SQL commands: use SELECT before UPDATE/DELETE with the same WHERE clause for sanity check.
This one is super important. I just got burned by this a couple of days ago. Sometimes I blow by the WHERE clause because I have a happy Enter finger.

It's also not a bad idea to do a quick mysqldump of the database you are working on, just in case.

Yeah, I made it a habit to start entering a query by typing 'WHERE', before anything else.
I always thought the missing where defaulting to all records was a bug in the spec, and would have much preferred it if 'where true' was a mandatory thing on deletes and mass updates instead of the current default.
I put my cheat sheet in a folder called ~/bin, which is in my path.
Care to share or put them on github or something? You don't need to comment them or anything. I find that looking at other people's scripts really helps me learn new ways to use the shell. Thanks.
Actually I just checked and there's only one in there -- I guess I use shell scripts a lot less than I used to. Here it is:

  #!/bin/sh

  # reform = re-format code to use different indentation
  # Usage:
  #    reform oldIndent newIndent source destination
  #
  # e.g. reform 3 4 fred.py fred1.py
  
  unexpand --tabs=$1 $3 | expand --tabs=$2 >$4
Incidently, this is typical shell script for me -- anything that requires loops or control structures, I do in Python. The predominance of comments over code is also typical for me.
I like to alias commands and put them in ~/.bash_profile for the ones that are useful. Then to see what you have saved you just type alias to get the list of all the current aliases. if there is one that could possibly do harm to your system and you dont want it to run you could do a alias possiblyBadCommand="0 && whatever command you want to save but never run." If you accidentally run possiblyBadCommand it will just exit out saying -bash: 0: command not found

    ... | sort -nr | head
...may save tail(1) some work.
I love this snippet. Awesome! Anything else you care to share?
I picked this one up on Hacker News.

http://news.ycombinator.com/item?id=1398805 http://en.wikipedia.org/wiki/Ego_depletion

The title says it all. Self-control is exhaustible. So when you're trying to make changes in your life, keep it to one simple thing at a time. Over time the one thing you decided to change stops being self-control and starts becoming a habit. Once it becomes a habit? Start training something else. You'll rack up positive changes in no time.

I wish this had been the first advice I'd received when I started dieting. After flailing around for years, I started calorie counting, and then made lifestyle changes once every few weeks. Smaller portions, whole wheat instead of white, simple things like that. Doing them one at a time made all the difference, and I finally got back down to my college weight.
Ah, and the whole dieting process has a nasty trap built right in, because your willpower diminishes as your blood sugar drops.

I don't have the study in front of me, but I recall the effect was pretty clear.

People regret things they didn't do more than things they did.
On the other hand: there's an infinite amount of things you can't/won't do in your life, vs. a finite amount of things you will do. Don't spend your time worrying about all the things you haven't done: that gets in the way of actually doing things.
On the third hand: of the infinite amount of things you can't/won't do in you life, there is only a finite amounts of things you want to do, and you won't regret not doing things you don't want to do.
On the fourth hand: of all the things you have done you would for regard some of them.
(comment deleted)
And even if what you end up doing was horrible, you will be able to laugh about it later.
On a related note: it's better to ask for forgiveness than to ask for permission.
This is certainly not universally true. (Have you ever had someone take this approach with you and your life? Ignorance combined with an intentional lack of communication can lead to mistakes that can never be really forgiven, and lost trust that can never be regained.)

That said, taking an option off of the table simply because you'd have to "break a rule" to do it can be foolish -- just be aware of why the rule exists, and what you're actually risking in breaking it.

Oh, that reminds me...
Suffer the pain of discipline or suffer the pain of regret...
The trick is that, a lot of the time, doing one thing means not doing another. If I'm working on my business, I'm not spending time with my girlfriend. If I quit my soul-crushing dead-end job to do a startup, I'm not pulling in a steady paycheck. If I exercise in my spare time, I'm not learning guitar. Which will I regret not doing tomorrow? I don't know.
For a quick and practical advice do things you regret, more often than things you dont regret.
Read up on the science of persuasion. It's the elephant in the room concerning decision-making.

I suggest 'Yes! 50 secrets from the science of persuasion', it's a fascinating read.

Don't know your limits.
Why not?
Dunno.
If you do not know your limits, or actively try and not acknowledge them, then you would engage in self deception and lead yourself onto a wrong path.

I think it is more preferable to know your limits, but not necessarily accept them and then actively work towards overcoming them.

you can't know your limits until you've hit them, and even when you hit them you have to make sure you diagnosed your limit correctly.
Buy that very technical book of something you wish you knew. Spend some of your lunchtime each day reading 5 to 10 pages.
I have a copy here at work of "The Road to Reality: A Complete Guide to the Laws of the Universe" by Roger Penrose for this very purpose!
Great advice. Books these days, especially technical books, are expensive. However I've rarely ever regretted buying a book, even the (very) few I bought but never read. Probably due to the fact that buying technical books has a minimal and bounded downside, but a huge upside.