Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't add parameters after functions #97

Open
fregante opened this issue Dec 18, 2023 · 11 comments
Open

Don't add parameters after functions #97

fregante opened this issue Dec 18, 2023 · 11 comments

Comments

@fregante
Copy link
Collaborator

While this reads fine:

const addOneListener = memoize(addListener, {
	cacheKey: arguments_ => arguments_,
	cache: new ManyKeysMap()
});

This is bad practice:

const addOneListener = memoize(() => {
	// some


	// inline


	// long


	// function


	// which


	// is not


	// uncommon
	return stuff
}), {
	cacheKey: arguments_ => arguments_,
	cache: new ManyKeysMap()
});

This could be enforced by a linter, since APIs like addEventListener(string, cb, options) exist natively, but ideally memoize and p-memoize should accept a single parameter:

function memoize(details: Callback | Options) {
	const {callback, ...options} = typeof details === 'function' ? {callback: details} : details;
}

It complicates types a bit but it requires the much cleaner:

const addOneListener = memoize({
	cacheKey: arguments_ => arguments_,
	cache: new ManyKeysMap(),
	callback: () => {
		// some
	
	
		// inline
	
	
		// long
	
	
		// function
	
	
		// which
	
	
		// is not
	
	
		// uncommon
		return stuff
	}),
});

Personal note: I went through this recently in webext-storage-cache, changing (fn, opts) => {} to (fnOrOpts) => {}

@sindresorhus
Copy link
Owner

I don't see why the API has to change. Just not put the function inline.

@sindresorhus
Copy link
Owner

but ideally memoize and p-memoize should accept a single parameter:

I disagree about that being ideal. I find that API quite weird. There are now two ways to do the same thing.

@fregante
Copy link
Collaborator Author

fregante commented Dec 19, 2023

Any API that allows unreadable code is not good for me, similarly to why functions should not have 5 parameters.

Inline functions are nice but they grow and people don't move them out. Should I open an issue on unicorn? 😃

@sindresorhus
Copy link
Owner

Should I open an issue on unicorn?

👍

@fregante
Copy link
Collaborator Author

I find that API quite weird. There are now two ways to do the same thing.

have you seen fetch 🙈

fetch('flowers.jpg', {method: 'POST'})
const myRequest = new Request("flowers.jpg", {method: 'POST'});
fetch(myRequest)

two ways to do the same thing.

I mean, that's pretty common, but I don’t think anyone would prefer mem({cb: () => {}}) over mem(() => {}) unless the API requires it (for good reasons, as shown here)

@fisker
Copy link

fisker commented Dec 19, 2023

Maybe we can change signature to

memoize(options?, () => {})

?

options can't be a function anyway.

@fregante
Copy link
Collaborator Author

That sounds almost good but I think (options, callback) is not a common pattern

@fisker
Copy link

fisker commented Dec 19, 2023

(Callback | Options) is not diff friendly

memoize({
-   cb() {
-     // .... indention level 2
-   },
-   cache: new ManyKeysMap,
})
memoize(() => {
+   // .... indention level 1
})

@meduzen
Copy link

meduzen commented Feb 9, 2024

Inline functions are nice but they grow and people don't move them out.

They do when you tell them. :p

@fregante
Copy link
Collaborator Author

fregante commented Feb 9, 2024

Inline functions are nice but they grow and people don't move them out.

They do when you tell them. :p

Sounds like a linter job, but that rule is complex and I think it's not in the recommended config, so it's easier to just avoid bad patterns at the root

@fregante
Copy link
Collaborator Author

fregante commented Feb 9, 2024

(Callback | Options) is not diff friendly

Lots of things are not diff friendly, like Prettier, and yet… they're worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants