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
Oldest comments (28)
I thought to make this thread based on a conversation I was having in @aspittel 's great clean coding post.
I love React's
dangerouslySetInnerHTMLwhich replaces a lot of documentation and comments and forces the user to understand what's going on. If you don't know why it says "dangerous", you can look it up.Much better than
Because the comment would never get ported across all the use cases (obviously, given it's a library).
But most code doesn't live up to the scrutiny of library code and comments, used properly, are a perfectly reasonable part of the toolbelt.
unlessin 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".
.NET's
FirstOrDefault()method when accessing LINQ. I find it super clear :)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.Now only if I could convince people to quit thinking
FirstOrDefaultwill always returnnullif 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)
I always chuckle at React's
componentDidMountvs Vue'smountedI love Ruby's naming conventions
foo?andfoo!.The former is for a method that returns a boolean (like
IsFooin some languages):The latter for a method that may do something possibly unexpected, such as modifying a given argument:
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.
Perl's Carp module has some interesting naming choices:
Not to mention that Perl introduces lexical variables with the
mykeyword, and package variables with theourkeyword!confess😂😂And those were named to fit the model of
die(exit, failing, with message)Also Perl's
oroperator is intentionally lower precedence than almost anything else so you can:"OR DIE!"
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)
Maybein Elm, not only because it's descriptive (Maybeit has a value, andMaybeit doesn't), but when it doesn't have a value, instead ofnullorundefinedor whatever, it'sNothing.Which makes me think of Nathan Explosion whenever I use it.
youtube.com/watch?v=o_PMab-SGgY
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...