What's your Modus Operandi for solving a (programming) problem?

Viewed 3544

While solving any programming problem, what is your modus operandi? How do you fix a problem?
Do you write everything you can about the observable behaviors of the bug or problem?

Take me through the mental checklist of actions you take.

(As they say - First, solve the problem. Then, write the code)

30 Answers

I can't believe no one posted this already:

Write up your problem on StackOverflow, and let a bunch of other people solve it for you.

To paraphrase Douglas Adams, programming is easy. You only need to stare at a blank screen until your forehead bleeds. For people who are squeamish about their foreheads, my ideal architect-and-build for the bigger problems would go something like this. (For smaller problems, like George Jempty I can only recommend Feynman's Algorithm.)

What I write is couched in an on-site business setting but there are analogues in open-source or distributed teams. And I can't pretend that every, or even most, projects pan out this way. This is just the series of events that I dream about, and occasionally come to pass.

  1. Get advanced, concise warning of what the problem is likely to look like. This is not the full, final meeting, but an informal discussion. Uncertainty in certain specification details is fine, as long as the client (or manager) is honest. Then take a piece of paper or text editor, and try to condense what you've learned down to five essential points, and then try to condense those to a single sentence. Be happy you can picture the core problem(s) to be solved without referencing any of your documentation.

  2. Think about it for maybe a couple of hours, maybe playing with code and prototyping, but not with a view to the full architecture: you should even do other stuff, if you've time, or go for a walk. It's great if you can learn about a job an hour before home time in order to deliver a decision around midday the next day, so you get to sleep on it. Spend your time looking at potential libraries, frameworks, data standards. Try to tie together at least two languages or resources (say, Javascript on PHP-generated HTML; or get a Python stub talking RPC to a web service). Flesh out the core problems; zoom in on the details; zoom out to make sure the whole shape is still distinct and makes sense.

  3. Send any questions to the client or manager well in advance of a meeting to discuss both the problem and your proposed solution. Invite as many stakeholders and your programming peers along as is convenient (and as your manager is happy with.) Explain the problem back to them, as you see it, then propose your solution. Explain as much as you can; pitch the technical details at your audience, but also let your explanations fill in more details in your own mental model.

  4. Iterate on 2 and 3 until everyone is happy. Happiness is domain-specific. Your industry might require UML diagrams and line-item quotations, or it might be happy with something jotted on a whiteboard with an almost invisible drywipe marker. Make sure everyone has the same expectations of what you're about to build.

  5. When your client or manager is happy for you to start, clear everything. Close Twitter, instant messenger, IRC and email for an hour or two. Start with the overall structure as you see it. Drop some of your prototype code in and see if it feels right. If it doesn't, change the structure as early as possible. But most of all make sure your colleagues give you a couple of hours of space. Try not to fight fires in this time. Begin with a good heart and cheer, and interest in the project. When you're bogged down later on you'll be glad of the clarity that came out of those first few hours.

How your programming proceeds from there depends on what it actually is, and what tasks the finished code needs to perform. And how you ultimately architect your code, and what external resources you use, will always be dictated by your experience, preference and domain knowledge. But give your project and its stakeholder team the most hopeful, most exciting and most engaged start you can.

The following applies to a bug rather than building a project from scratch (but even then it could do both if reworded a bit):

  1. Context: What is the problem at hand? What is it preventing, doing wrong, or not doing?

  2. Control: What variables (in the wide sense of the word) are involved? Can the problem be reproduced?

  3. Hypothesise: With enough data on what is occurring or required, it is possible to hypothesise, that is, to draw a mental image of the problem in question.

  4. Evaluate: How much effort, cost, etc, will the correction require? Determine if it's a show stopper or a minor irritant. At this point, it may be too early to tell, but even that is a form of evaluation. This will allow prioritisation.

  5. Plan: How will the problem be approached? Does it require specifications? If so, do them first.

  6. Execute: A.K.A. The fun part.

  7. Test: A.K.A. The not-so-fun-part.

Repeat to satisfaction. Finally:

Feedback: how did it come to be this way? What lead us here? Could this have been prevented, and if so, how?

EDIT:

Really summarised, stop, analyse, act.

I write it on a piece of paper and start with my horrible class diagram or flowchart. Then I write it on sticky notes to break it down to "TO DO's".

1 sticky note = 1 task. 1 dumped sticky note = 1 finished task. This works really well for me so far.

Add the problem to StackOverflow, wait about 5-10 minutes and you usually have a brilliant solution! :)

No one has mentioned truth tables! But that's probably because they're usually only mildly helpful ;) (although your mileage may vary) I used one for the first time yesterday in my 8 years of programming.

Diagramming on whiteboards or paper have always been very helpful for me.

When faced with very weird bugs. Like this: JPA stops working after redeploy in glassfish

I start from scratch. Make a new project. Does it work? Yes. Start to recreate the components of my app one piece of a time. DB. Check. Deploy. Check. Until it breaks. Continue until it breaks. If it never breaks. Ok. You just recreated your entire app. Discard of the old one. When it breaks. You pinpointed the exact problem.

  1. I think - what am i looking for?
  2. What method best solves this problem?
  3. Implement it with solid logic - no code
  4. Pseudo code
  5. code a rough cut
  6. execute

These is my prioritized methods

  1. Analyse
    a. Try to find the source of your problem
    b. Define desired outcome
    c. Brainstorm about solutions
  2. Try on error (If I dont want to analyse)
  3. Google a bit around a. Of course, look around on stackoverflow
  4. When you get mad, walk away from pc for a cup of coffee
  5. When you still mad after 10 cups of coffee, Go sleep a night to think about the problem

GOLDEN TIP
Never give up. Persistence will always win

Related