Underseer
Contributor
This is just intended as a thread for discussing what people consider more readable in code.
I needed to parse a first name out of a cell that contained first and last name in Excel.
So the parse was
But whoops, this resulted in a first name with an extra space tacked on the end. Fencepost error! So which fix would you consider more readable?
Or
Yeah, I know this is pretty fucking trivial, but I'm just curious about the attitudes other people have about readability. See, I'm not a professional programmer by trade. If I write code of any kinds, it's very short and once I write it, I probably won't look at it again for many years, so I tend to overcompensate on the side of clarity and explanations. I put way more comments than a pro would because I fucking need the extra comments.
Of course my preference would be for the TRIM() version just because if I look at this again years from now, I would probably think "What the fuck is that -1 doing there?" Sure, it would only take me a couple of seconds or less to figure out why, but with the TRIM() version it's immediately obvious what's going on and why. At least to me.
And yes, I'm aware that cell formulas in a spreadsheet do not count as "code," but I thought this illustrates a basic principle of readability.
Anyway, if you have any better examples of readability, especially anything that regularly bothers you, please feel free to offer them here.
I needed to parse a first name out of a cell that contained first and last name in Excel.
Code:
D5 = "John Smith"
So the parse was
Code:
=LEFT(D5,FIND(" ",D5))
But whoops, this resulted in a first name with an extra space tacked on the end. Fencepost error! So which fix would you consider more readable?
Code:
=LEFT(D5,FIND(" ",D5)-1)
Or
Code:
=TRIM(LEFT(D5,FIND(" ",D5)))
Yeah, I know this is pretty fucking trivial, but I'm just curious about the attitudes other people have about readability. See, I'm not a professional programmer by trade. If I write code of any kinds, it's very short and once I write it, I probably won't look at it again for many years, so I tend to overcompensate on the side of clarity and explanations. I put way more comments than a pro would because I fucking need the extra comments.
Of course my preference would be for the TRIM() version just because if I look at this again years from now, I would probably think "What the fuck is that -1 doing there?" Sure, it would only take me a couple of seconds or less to figure out why, but with the TRIM() version it's immediately obvious what's going on and why. At least to me.
And yes, I'm aware that cell formulas in a spreadsheet do not count as "code," but I thought this illustrates a basic principle of readability.
Anyway, if you have any better examples of readability, especially anything that regularly bothers you, please feel free to offer them here.