Skip to content

Don't add parameters after functions  #97

@fregante

Description

@fregante

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) => {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions