4 ms·
>Look at how many much useless text we have here. "measurementOf" and "SideOfBox" add nothing but clutter to the naming, and writing out practically the same th
by nendroid 6y ago
>Look at how many much useless text we have here. "measurementOf" and "SideOfBox" add nothing but clutter to the naming, and writing out practically the same thing 4 times suggests we could abstract this into a data structure.
First off this "clutter" exists in the English language itself yet I hear no one complaining. I don't communicate with other people using shortcuts and context aware abbreviations like your suggesting. I literally say "here are the measurements of the box" both in written documentation and by sound, what black magic says that this is so wrong to do in code?
Anyway here it is:
struct measurementsOfBox = {
measurementOfLeftBottomSideOfBox
measurementOfRightBottomSideOfBox
measurementOfLeftTopSideOfBox
measurementOfRightTopSideOfBox
}
There is Nothing wrong with above code. Clutter doesn't harm readability it just harms aesthetics. And useless? Are you sure? Even if it was useless what harm does it do?
Now you could argue that the clutter itself can hurt reading efficiency. But honestly think about it. That's like 5 seconds of extra reading out of your life. It's not a big deal.
Most programmers just have this version of OCD. I get it the struct looks really ugly, but there is nothing logically wrong with it.
I mean you could make it more elegant like this:
struct Box {
x
y
z
t
}
But this could lead to all kinds of other issues. For example because I didn't label anything with "measurement" now the reader can mistake the values for the "positioning" of the box as opposed to "measurements" of the box.
You made a common mistake here in your naming in assuming that "measurement" was a useless prefix. It's not... but that's besides the point because every program writer can make that mistake. That's why when you add a bit more clarity to your naming you have a larger chance to avoid this mistake at a small cost of adding some ugliness to your code.
>1) It's overly verbose. More than 3 words is a warning sign to me.
Warning sign of what? It's a similar warning sign that your brain fires off when you're alone in the dark in the woods. There's nothing to fear logically but your brain kicks off warnings regardless. Same with this, your brain kicks off some sort of warning but when you work it out logically there's Nothing. Verbose code is not bad just like verbose documentation is not bad.
>2) It's specific rather than generic. For instance if I name a function "sortSheepByHoofSize", it implies the reader know what hoofs are, cares about them and knows how to measure them. Whereas when naming it "sortSheep" or perhaps even just "sort", it's immediately understandable on a surface level to practically everyone.
Data referring to a specific concept or a generic concept is a structural decision made by you. I chose data referring to a specific concept. This happens in code. Not everything is generic and for specific things there's nothing wrong with using very specific names.
>3) Following on from 2), this type of naming lacks context. We should leverage the context of surrounding code and abstractions to make naming understandable, instead of trying to pack all the meaning into one name. Oftentimes there's repeated information in names that could be inferred from context instead.
>>We should leverage the context of surrounding code and abstractions to make naming understandable, instead of trying to pack all the meaning into one name.
There's no downside into packing more info into a name. Sure it can get to a point where it's unreasonable but in general there's nothing wrong with me calling something a measurement when that's what it is... This is an aesthetic issue that programmers react due to human bias, but there's nothing intrinsically wrong with prefixing measurement onto box. There's nothing wrong with repeated information either.
By prefixing measurement onto the Box struct I let everyone know that these are measurements on the box and not the position of the box. It's ugly but ugliness has nothing to do with readability or structure.
This is the bias programmers need to get rid of.
Find beauty in the structure of your code and find readability in the naming. Don't make the mistake of trying to find beauty in naming. Nobody wants to decipher code with poetic naming.
Have you guys heard of literate programming by donald knuth? He's taking what I'm talking about to extreme heights.
- deleted 6y ago[deleted]
- deleted 6y ago[deleted]
- olejorgenb 6y agox, y, z, t is not the suggested alternative. leftBottom, rightBottom, was clearly the intended alternative. A "Length" suffix could be added to make it clear it's not coordinates, but I think it's better to encode this into the struct naming: struct BoxDimensions { topLeft, topRight, bottomLeft, bottomRight } In code working with these objects it's likely both obvious and necessary to know that the code works with dimensions. Ie.: being reminded of this each time you encounter a variable it just noise. Code with longer names are harder to scan. boxDim.measurementOfLeftBottomSideOfBox - boxDim.measurementOfRightBottomSideOfBox vs boxDim.bottomLeft - boxDim.bottomRight The second only need a glance, the first you have to read/scan multiple times to dig out which delta it is. If it's important to distinguish between measurements and true values it would be better to encode this in the struct variable name. Having distinct types for measurements and true value would be a hassle no? trueBox.bottomLeft - measuredBox.bottomLeft vs trueBox.trueLengthOfBottomLeftSideOfBox - measuredBox.measurementOfBottomLeftSideOfBox The second one is just exhausting to me at least.
- nendroid 6y agostruct BoxDimensions { topLeft, topRight, bottomLeft, bottomRight } I agree it is a better name. My version was deliberately designed to hurt your eyes to show you that the pain is just psychological. There is no intrinsic difference between your naming versus mine other than mine takes a millisecond longer to read the longer names. My version is quite ugly as well, but ugly naming has no negative effect on your code. It's all about understandable naming. Not every concept can fit into a beautiful name as you did here. What if the box was a very specific box out of many boxes and I needed to specify the details? DimensionsOfBlueBoxFromRoom253 = { ... } >The second only need a glance, the first you have to read/scan multiple times to dig out which delta it is. That's only because you already know what Dim means. Many many times I see abbreviations that are unknown. For example: TranBox.Dow Would it be clear to you that Trans means translation and Dow means down? >Code with longer names are harder to scan. Same with english. It's a small price to pay but people tend to enjoy reading english more than programming abbreviations and shortcuts. You spend an extra second reading a line, but you gain much more clarity about the intention of the programmer. Where with an abbreviated name you can often be unsure of what the programmer meant and you'd have to dig into the surrounding context. trueBox.bottomLeft - measuredBox.bottomLeft See? already I don't even know what you mean by true. True can mean anything. Could it be you're referring to a box with the the word true printed on it? A layman will not understand the difference between a truebox and a measuredbox. The below is infinitely better: BoxWithEstimatedDimensions BoxWithActualDimensions Perhaps it's ugly and offensive to your aesthetic tastes, but there's zero ambiguity here. In fact I would use snake case for even more clarity: Box_with_Estimated_dimensionS That variable name is not elegant but there is literally zero way I can misunderstand the meaning. Note how I combined snake case with alternating camel case above. This really pisses some people off, but when you think about it mixing camel case with snake case is just aesthetic bs that have nothing to do with the ultimate goal of your programming style: Readability. Also note the capital S at the end of the name. Believe it or not the capital S has ZERO effect on the quality, readability and even the verbosity of your source code yet this is what programmers will bitch and moan about the most. >The second one is just exhausting to me at least. Exhausting like the english language is exhausting? Exhausting like commenting your code is exhausting? People don't complain about the verbosity of English and all I'm suggesting is bringing the verbosity of programming a bit closer to English so that the understandability of your code is ALSO closer to english.