When you encounter working code that is unpleasing to the eye, how likely are you to refactor it? I know code aesthetics is mostly subjective, but I'm bothered a lot by "ugly" code and find it difficult to resist the urge to clean it up.
I don't like:
Heavy function nesting
AndFinally(AndThenDoThis(DoThis(strVar, 1, DoThat("February"))), FetchValue(0))
Any more paranthesis, commas, and ampersands than necessary
CType(ctlGrid.DataSource, DataView).RowFilter = "HourlyRate > 100.00"
and would consider adding a method (or extension method) to get
ctlGrid.DataView.RowFilter = "HourlyRate > 100.00"
alternately, I would change:
rows = tblEmp.Select("AnnualEarnings > " & dEarnings.ToString & " AND Position = '" & strPosition & "'")
to this:
rows = tblEmp.Select("AnnualEarnings > ? AND Position = ?", dEarnings, strPosition)
Now this is a bit extreme, but I will even take a common idiom, like finding the first item in an array and provide a method for it:
row = tblEmps.Select("Position = 'Manager'")(0)
becomes (and, I think, is more readable):
row = tblEmps.Select("Position = 'Manager'").First
When I see this it drives me crazy
frmMain.grdEmployees.colName.Bold = True
frmMain.grdEmployees.colName.Font = "Arial"
frmMain.grdEmployees.colName.BackColor = "lightyellow"
and, with regard only to aesthetics (not performance), I end up with:
With frmMain.grdEmployees.colName
.Bold = True
.Font = "Arial"
.BackColor = "lightyellow"
End With
I could go on and on with this sort of thing. I exercise great care so that refactoring only occasionally results in introducing a bug--usually a short-lived, low-impact one.
Not everyone in our company is agreeable to this kind of thing, but I believe its a boon to long-term maintenance. I read "Don't Make Me Think" by Steve Krug a few years back. I would argue that this book (although geared toward web design) favors code written in such a way that it minimizes the mental effort necessary to digest it. In most cases, to me, this mean expressing the same code in fewer lines, but never in a tricky, obfuscating way.
How compelled are you to enhance code aesthetics? When do you leave well enough alone and what sorts of "ugliness" compels you to step in? Do you consider this practice more beneficial or more detrimental?