It's a little quick-and dirty but it's the way it's commented that makes the point I'm going for here.
It's a little quick-and dirty but it's the way it's commented that makes the point I'm going for here.
# Check if it's something that looks like an ident response
if( input =~ /^(\d+)(|\s),(|\s)(\d+)$/ )
p1 = $1.to_i
p2 = $4.to_i
else
sane = false
end
Don't do this (comment a block of code).Rather make a function that does the same and name it for example "ValidateIdentResponse". Write a test for it. Call that function from this function. You get shorter, less-complex main function and you don't need that silly comment.
(EDIT: Formatting)
Edit: Rather I should say; It's not always an option that makes things better.
If you're writing a comment for a block of code is definitely a good time to think "Should this not be a separate function?" (I clearly need to do this more myself too.)
Another important consideration is whether or not the code block you split out makes any assumptions about the current state of the program (or object or whatever), and if those assumptions not being met would result in non-obvious bugs. (If this is true, you should probably wait until there's actual duplication somewhere before splitting the function out, IMO.) This is obviously not an issue with pure functions, but for most programs, most functions aren't pure.
See http://number-none.com/blow/john_carmack_on_inlined_code.htm... for further perspective on this.
http://blog.codinghorror.com/code-tells-you-how-comments-tel...
# Do NAT table lookup
n = natlookup( p1, p2 )
is clearly unnecessary. If natlookup doesn't do a NAT table lookup, it's a badly named function.