If you're going to reuse those little tiny functionettes, then sure, it might be worth doing, but to do it for readability is misguided. Comments are a perfectly reasonable way of indicating logical blocks within code.
If you're going to reuse those little tiny functionettes, then sure, it might be worth doing, but to do it for readability is misguided. Comments are a perfectly reasonable way of indicating logical blocks within code.
func DownloadAndVerifyThing(path string) error {
var url string
{ // Build URL
...
url = [..]
}
{ // Fetch
...
}
{ // Verify file.
...
}
}
I don't do this very often (and I'm having trouble locating an example off-hand, although I'm sure there must be a few in my public code), but it can be pretty useful at times. // set up data to be tested
var fred = ...
var cathy = ...
{ // check fred does something correctly
var result = fred.reproduce(2);
assert(result.length() == 2, "fred should have 2 children");
}
This prevents variable result from escaping and contaminating the next test, which happened an awful lot before I started doing this.Then maybe better ways; suggestions welcome.
fred = begin
user_finder = ...
user_finder.find('fred')
end
Again, not always "self-documenting" as whipping out a new function, but useful if you need to separate chunks and keep the code linear.The only issue with Ruby itself is that the scope is not lexical, so you can still use `user_finder` outside the block above (linters can catch it, though). But it's still worth to separate code without having a brand new method.
Comments like these very often end up lying to my face and make me lose precious time. I nowadays tend to semi-ignore them and just read the code anyway. YMMV, I guess.
I suppose if you prefer to ignore comments, they are more likely to get out of date, which may be why your mileage varies.
(Obviously you should endeavor to write good names anyway).
At some point the system does depend on its authors doing the right thing.
But I don't completely disagree that they can have their place. They just get used as crutches for unreadable code a lot of time, that's all.