Elentok's Blog

About me

Solving Coding Problems

In all of my days as a programmer a lot of people approached me with the question "Why isn't my code working?" and it's usually because of one of the following reasons:

  • Syntax errors - missing semicolons/parenthesis, bad jQuery/css selectors.
  • Not enough knowledge about the problem domain or the technology.

A couple of days ago I was helping a good friend with a coding problem and he told me: "Don't solve it, tell me the strategy to solve it", this was the first time anyone told me that and it got me thinking... he was right, by solving every problem I'm not allowing the young programmers to learn and that gave me the idea to write this post.

I tried to think of the ways I deal with coding problems and I came up with several principles.

1. Always write syntactically-clean code

I often see people writing code with no coding standard (inconsistent indentation, spacing, etc...), and when I criticize them about it they say "I'm just testing something, it's not production code, bla bla bla...".

And when they ask me to help and I take a quick look at the code and tell them they're missing a semicolon or something like that they're amazed at how I do it.

Well... there's no magic to it, I'm just always writing my code as clean as possible (perfectly indented, consistent spacing between functions and parenthesis, etc... all according to a coding standard for the language I'm currently using).

Once you get used to always writing and looking at clean and consistent code, then whenever something is out of place, it just pops out.

2. Don't write large pieces of code without seeing if it works

(In a positive form: "Write code in small iterations of write-test-write-test...").

I'm constantly seeing people creating a new project and writing a lot of code without even compiling and running. The first time they try to actually run it they have no idea what's not working and have to go over a lot of code until they figure out what's wrong.

If you do have to figure out how to fix a large piece of code, the best way is to split the code to small chunks and check if they're working. Let's look at an example: People sometimes come to me with code like this (it happens a lot lately, I'm not sure why...):

$('div.class1 a[href^=https://]')
  .doSomething()
  .doSomethingElse()...

To debug this you should use your browser's developer tools (FireBug, Chrome Developer Tools, ...). I personally prefer running test code directly in the JS console or adding console.log() calls as opposed to adding breakpoints (I don't yet trust JavaScript debuggers):

  • First, check the jQuery selector (in the browser's JS console), see if it returns the correct elements.
  • If it doesn't, you found your bug (or one of them :-))
  • if it does work, move on to "doSomething()", again, test it in the browser's JS console using the result of the selector
  • and so on...

The best habit for this is to use TDD/BDD which will force you to write smaller chunks of code in every iteration.

3. Solve the bug, not the symptom

I often see young programmers try to solve bugs by treating the specific symptoms described in a bug report instead of trying to understand why that bug is happening and what is causing it.

Make sure you understand the domain of the problem and the technologies used in the feature, because if you don't, you'll just cause more harm than good.

I hope you find this useful, Until the next time, David.

Next:Git Rebase Example