I'm a lawyer. A highly specialized lawyer. I will share what I know about this question.
1. This is a problem for me too. Even though I have been doing this for a long time there are a lot of situations where I am asked for a referral and I don't know anyone good. Sometimes I don't know anyone at all.
2. When this occurs the first default thought is "Go to a Juggernaut Law Factory". All of them claim to have the best people in the world in every possible specialty. You pay through the nose. You hold your nose and hope that the work product is adequate. Sometimes it is. Sometimes it is astonishingly good. Sometimes it's embarrassing as hell how badly the work is done. (The same goes for hiring the Big 4 accounting firms).
3. If you don't want to go that route, your next job is to identify your problem as precisely as possible so you can start to find your way down the referral chain until you find someone you want to hire.
4. I will make reference to my specialty to give you an example, but in any area of law the same concepts hold true.
5. I am an international tax lawyer. I do primarily inbound (foreign money, companies, and humans entering the United States) work. To find me it is not sufficient to specify "tax" as your problem. That's like saying "I need some code" in software land, I'm guessing. You need to be highly specific. "I have a U.S. company that will sell 50% of itself to a foreign investor. My foreign investor needs a U.S. tax advisor in order to structure the deal properly, and I need a U.S. tax advisor to make sure I don't f--- something up for myself."
6. For me, my next step is to start calling buddies who are lawyers and I ask them the "who do you know" question. That is easy for me because I know a bunch of people all over the world in my field of expertise. For you, trying to hire a specialist, it isn't. So you start with a generalist. A generic tax person, in my field. If the person has a sense of integrity and this is something that is beyond them, they will decline to do your work and will instead say "Call Fred. He does that kind of thing."
7. Keep dialing. Get a name. Talk to that person. He/she is a statistical sample of N = 1. My suggestion is that you pay some money here. Don't get free advice. I might chat with you for a few minutes but if you're serious you're going to sit down and talk serious stuff for money.
9. What you're going to take away from that first meeting is an expert's sense of what your problem is. DO NOT TRY TO GET AN ANSWER TO THE PROBLEM. Just get the expert's idea of how the problem is defined. Also get some of the insider technical jargon used to describe the problem. Words. Laws. Etc.
10. From here on you are going to find more people. You are armed with better information. Go ask Mr. Duck D. Go or someone else, using the jargon you have now learned. See what that turns up. Keep dialing. Repeat your quest by starting with a generalist and focusing in until you hit a specialist.
11. Once you have talked to two or three people, pick the person who gave you the best vibe. Specifically: did you understand what this person said when he/she talked to you? Was this person a complete asshat? Is this person accessible?
12. Do not make your decision on price. Long ago when my hourly rate was $400 per hour, a client hired me and said "Do you know why I hire $400/hour lawyers instead of $200/hour lawyers? Because $400/hour lawyers get things done."
13. I'm not saying that you should hire without regard to price. I'm saying it is a cost/benefit decision. Most people approach hiring a lawyer as a pure cost decision. This is one of our screening metrics for who we take on as customers (or not). If people can't see the benefit from hiring our firm, then we don't want them. And if we can't see the benefit we can give -- and if we can't articulate that clearly -- then we shouldn't take the job.
14. Pro tip. For tax, at least, but I think in other areas too -- look to see who is doing a lot of lecturing and writing. Look at the courses and seminars on offer at www.calcpa.org, for instance, if you're interested in accounting people in particular fields. Who is talking about your problem? (Shameless plug: I love giving presentations on international tax topics and do a lot of it.)
That's it. I think your analogy to finding a good programmer is apt. I'm trying to hire a lawyer right now to work for me. I face the same problem -- how do I know that this person is competent? Will I be able to work side by side with him/her?