Naming things is hard, but what are the ones that have stood out to you as great choices that have stood the test of time?
For further actions, you may consider blocking this person and/or reporting abuse
Naming things is hard, but what are the ones that have stood out to you as great choices that have stood the test of time?
For further actions, you may consider blocking this person and/or reporting abuse
Grenish rai -
Oliver Bennet -
Sotiris Kourouklis -
Gabrielle Niamat -
Top comments (28)
Testing frameworks are fun for this because by nature of what the code does, it ends up being a source of truth for how the app behaves. Thus, the test specs can kind of read like documentation.
Assuming
var thing = 10;
Some Jasmine:
Some Mocha with Chai:
I also just like the punny names of testing languages, to be honest.
Yeah, I find this especially true in Ruby, where whitespace rules can make things almost plain english (though some take this way to far. It's a balance)
I always chuckle at React's
componentDidMount
vs Vue'smounted
I love Ruby's naming conventions
foo?
andfoo!
.The former is for a method that returns a boolean (like
IsFoo
in some languages):The latter for a method that may do something possibly unexpected, such as modifying a given argument:
Perl's Carp module has some interesting naming choices:
Not to mention that Perl introduces lexical variables with the
my
keyword, and package variables with theour
keyword!And those were named to fit the model of
die
(exit, failing, with message)Also Perl's
or
operator is intentionally lower precedence than almost anything else so you can:"OR DIE!"
confess
😂😂It's not that the name itself is great, but Python's
len()
keeps reminding me how well thought-out the early Python language was.One global function with a short but intuitive name that consistently does exactly what you expect it to no matter what data structure you throw at it.
unless
in Ruby, although it's more of an operator.Everything in JavaScript (not a fanboy of course), I can't think of a specific method that's named right but I also can't think of any that are named wrong 🤔.
Operators are allowed—and anything related to the idea of naming things.
I too really like
unless
, though I see it get overused sometimes where!
would be clearer."if (!" is clearer to most devs as it is available in more languages than "unless".
A common problem in PHP (maybe in some other langs too) is the way to move between dates and get the incorrect value, for example when you decrease or increase a month(s):
Carbon a good library offers some methods to avoid this behaivor and their names are really good:
If you wanna get the calculation with this behaivor, no problem:
Other methods are:
createMidnightDate
,localeHasDiffOneDayWords
,createSafe
...The start of my journey as a programmer is a peculiar one in that regard.
As a non-native English speaker, when I started to code, a long time ago, I simply didn't understand anything. In fact, I thought that most reserved word of any language were just that: some "random strings" with no meaning outside of the programming language. It's later, when I had learned English that I got some Ah ah! moment.
Obviously for an English speaker: "if, case, switch, while, random" are just normal words. It wasn't for me. (I started to play around with code when I was a child, under MS-DOS 6)
So, talking about some great choices, my biggest and relatively recent "Ah ah! moment" has been with
I could never remember which one would round up and which one would round down.
Until a day like any other day, my brain said: "hey, wait a minute: isn't "floor", because the floor is low and "ceil(ing)" is up?" Awesome. XD
Maybe
in Elm, not only because it's descriptive (Maybe
it has a value, andMaybe
it doesn't), but when it doesn't have a value, instead ofnull
orundefined
or whatever, it'sNothing
.Which makes me think of Nathan Explosion whenever I use it.
youtube.com/watch?v=o_PMab-SGgY
.NET's
FirstOrDefault()
method when accessing LINQ. I find it super clear :)Now only if I could convince people to quit thinking
FirstOrDefault
will always returnnull
if it can't find anything.It does what it says it does, default value. It just so happens that for objects that is
null
(I say this because I had to prove to someone once that their if statement would always evaluate to true. Lol)
Less clear along those lines is the
||=
operator.What does ||= do?
Alex
Hey, PHP has the coolest operator: the SPACESHIP operator <=>
The
<=>
is for evaluation and||=
is for assigment, the??
null coalescing operator is more close to it but are not the same.