DEV Community

What are the great function/method/etc. names in popular libs/languages?

Ben Halpern on July 24, 2018

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?

Collapse
 
itsasine profile image
ItsASine (Kayla)

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:

expect(thing).toBeTruthy();

expect(thing).not.toBeLessThan(5);

expect(thing).toBeGreaterThanOrEqualTo(5);

expect(thing).toEqual(10);

expect(new Promise(thing)).toEqual(10);

Some Mocha with Chai:

thing.should.be.a('number');

thing.should.equal(10);

expect(thing).to.equal(10);

expect(new Promise(thing)).to.eventually.equal(10);

I also just like the punny names of testing languages, to be honest.

Collapse
 
ben profile image
Ben Halpern

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)

Collapse
 
tyrw profile image
Tyler Warnock

I always chuckle at React's componentDidMount vs Vue's mounted

Collapse
 
burdettelamar profile image
Burdette Lamar

I love Ruby's naming conventions foo? and foo!.

The former is for a method that returns a boolean (like IsFoo in some languages):

irb(main):006:0> s = 'abc'
=> "abc"
irb(main):007:0> s.empty?
=> false

The latter for a method that may do something possibly unexpected, such as modifying a given argument:

irb(main):001:0> s = 'abc'
=> "abc"
irb(main):002:0> s.reverse
=> "cba"
irb(main):003:0> s
=> "abc"
irb(main):004:0> s.reverse!
=> "cba"
irb(main):005:0> s
=> "cba"
Collapse
 
hoelzro profile image
Rob Hoelz

Perl's Carp module has some interesting naming choices:

# warn user (from perspective of caller)
carp "string trimmed to 80 chars";

# die of errors (from perspective of caller)
croak "We're outta here!";

# die of errors with stack backtrace
confess "not implemented";

Not to mention that Perl introduces lexical variables with the my keyword, and package variables with the our keyword!

Collapse
 
jakebman profile image
jakebman • Edited

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:

my $file = open '/dev/zero' or die "Couldn't open file";

"OR DIE!"

Collapse
 
shalvah profile image
Shalvah

confess 😂😂

Collapse
 
kungtotte profile image
Thomas Landin

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.

Collapse
 
aodev profile image
AoDev

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

Math.ceil
Math.floor

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

Collapse
 
hugo__df profile image
Hugo Di Francesco

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 🤔.

Collapse
 
ben profile image
Ben Halpern

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.

Collapse
 
ericcurtin profile image
Eric Curtin • Edited

"if (!" is clearer to most devs as it is available in more languages than "unless".

Collapse
 
kip13 profile image
kip

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):

>>> (new \DateTime('2017-01-31'))->add(new \DateInterval('P1M'))->format('Y-m-d')
=> "2017-03-03"

Carbon a good library offers some methods to avoid this behaivor and their names are really good:

>>> (new Carbon('2017-01-31'))->addMonthNoOverflow()->format('Y-m-d')
=> "2017-02-28"

If you wanna get the calculation with this behaivor, no problem:

>>> (new Carbon('2017-01-31'))->addMonthWithOverflow()->format('Y-m-d')
=> "2017-03-03"

Other methods are: createMidnightDate, localeHasDiffOneDayWords, createSafe...

Collapse
 
manzanit0 profile image
Javier Garcia

.NET's FirstOrDefault() method when accessing LINQ. I find it super clear :)

Collapse
 
twigman08 profile image
Chad Smith

Now only if I could convince people to quit thinking FirstOrDefault will always return null 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)

Collapse
 
ben profile image
Ben Halpern

Less clear along those lines is the ||= operator.

Collapse
 
robencom profile image
robencom

Hey, PHP has the coolest operator: the SPACESHIP operator <=>

Thread Thread
 
kip13 profile image
kip

The <=> is for evaluation and ||= is for assigment, the ?? null coalescing operator is more close to it but are not the same.

Collapse
 
nimmo profile image
Nimmo

Maybe in Elm, not only because it's descriptive (Maybe it has a value, and Maybe it doesn't), but when it doesn't have a value, instead of null or undefined or whatever, it's Nothing.

Which makes me think of Nathan Explosion whenever I use it.

youtube.com/watch?v=o_PMab-SGgY

Collapse
 
galdin profile image
Galdin Raphael

Android's UserManager class has something called isUserAGoat().

Here's an interesting discussion on it 😁
stackoverflow.com/questions/133753...

Collapse
 
phortx profile image
Ben Klein • Edited

I really like the convention to name mixins with-something in react and Vue. Often this feels very elegant. Like with-captcha.js or with-user.js. There couldn't be any naming that feels any smoother 😍

Collapse
 
katafrakt profile image
Paweł Świątkowski

Not sure if this counts, but PHP's T_PAAMAYIM_NEKUDOTAYIM parser token is pretty rad yet savage :D

Collapse
 
alshakero profile image
Omar Alshaker

I really like PHP's explode and implode couple. It's String.split and String.join for those who don't know.

Collapse
 
rapidnerd profile image
George

From one of my most recent projects

void iDontKnowIfThisIsGoingToWorkSoImPuttingOffUsingThisMethodBecauseImScaredOfTheOutcome()
Collapse
 
katafrakt profile image
Paweł Świątkowski

Damn, I was going to post this when I saw this thread ;)

Collapse
 
ben profile image
Ben Halpern • Edited

I thought to make this thread based on a conversation I was having in @aspittel 's great clean coding post.

I love React's dangerouslySetInnerHTML which 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

//This is dangerous, read this post about XSS:
setInnerHTML

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.

Collapse
 
jesseditson profile image
Jesse Ditson

I always thought it was kind of brilliant of the react team to hijack the reader’s brain to create a fake protected member by naming it _SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED

Collapse
 
ben profile image
Ben Halpern

Functional propaganda