Dangerous Students

Thursday, June 01, 2006

Talking to Yourself

"It's strange calling yourself..."
- Naomi Watts, 'Mulholland Dr'

"Now you will see me one more time, if you do good. You will see me, two more times, if you do bad. Goodnight."
- Cowboy, 'Mulholland Dr'

Welcome! We are lifelong students with a love for learning and self-improvement. We believe that knowledge should be shared and celebrated. We seek to acquire new ideas, as well as share some tips that have helped us in the past. We hope you will be entertained and enlightened while reading dangerous students.

Here's a quick coding tip on how to more effectively use source comments. You add comments to your code to document some behavior that is not immediately obvious while browsing the code. Comments also help when other programmers read your code.

But for me, the most important use of comments is that it is a way of talking to myself while I am coding. I like leaving messages to my future self when writing code. These are breadcrumbs I leave to guide me the following day, telling me what parts of the code I need to review, or what are unfinished sections that I need to complete. Here's a sample:


// area = pi * r^2
double GetCircleArea(double radius)
{
// $$ Move pi to constants section?
const double pi = 3.14;

// $$ how about negative radius?
return pi * (radius * radius);
}

void main()
{
double r = 2.34;
double area = GetCircleArea(r);

// $$ todo: localize string
Console.WriteLine("Circle area is " + area);
}


I talk to myself when I code through the use of comments prefixed by a searchable key word. Sometimes while I am writing lines, I realize that I need to handle the logic another way. Or do housekeeping stuff such as moving literal strings to a resource file. But I don't want to lose my train of thought, so I place a marker to remind me, and move on.

Then after I test that the core logic is working. I search through all instances of "$$" in the source file and proceed to handle those error checks I failed to do. Or do no-brainer stuff that I didn't want to be distracted with previously.

Sometimes I leave the $$ stuff for days when I am blocked mentally and can't think straight. That's the best time to do the boring, menial housekeeping coding work. You know the code is complete when all the '$$' reminders you set for yourself are gone.

This is also useful when you need to ask another programmer's opinion of your coding implementation. You check in the file with the comments, and he reads through your messages/reminders, then gives you feedback or even implement some of those sections himself.

So comments are actually useful, not just for commenting the code but for talking to your future self while you're writing it.

3 Comments:

  • At 10:35 PM, Blogger robdelacruz said…

    By the way, if you literally want to talk to your future self - you can. Send an email to yourself to be delivered up to 35 years in the future:

    http://www.futureme.org/

     
  • At 6:40 PM, Blogger rmacapobre said…

    what is $$ for?

     
  • At 6:48 PM, Blogger rmacapobre said…

    $$ - nevermind .. its like a todo: i use todo: ...

     

Post a Comment

<< Home