/    Sign up×
Community /Pin to ProfileBookmark

Utility class best practices

Hi,

I’m writing a utility class to contain common functionality used throughout an app, but that isn’t provided standard by JS (or not widely supported).
The utility class contains methods related to a few different areas. Strings, arrays, url formatting, etc.

The problem I’m having is that of separating namespaces and logically grouping the class.

At the moment my utility class is flat, and methods look like this:

[CODE]
otUtil.arrayContains()
otUtil.arrayRemove()
otUtil.arrayWhatever()

otUtil.stringContains()
otUtil.stringWhatever()

otUtil.urlEncode()
otUtil.urlWhatever()[/CODE]

This seems a bit of a pain though, having the type prefixed on every utility function.
I considered a possibility of creating namespaces, so that you access functions like this:

[CODE]
otUtil.array.contains()
otUtil.array.remove()
otUtil.array.whatever()

otUtil.string.contains()
otUtil.string.Whatever()

otUtil.url.Encode()
otUtil.url.whatever()[/CODE]

This has the benefit that we don’t need to prefix types in function names, and also makes the API more clean by introducing clear namespaces for each group of functions.

I have also seen people implementing functionality directly on the prototype of objects they use. For example you can add the contains() method to the array prototype, so that you can do

[CODE]
var a = []
//do stuff with a
if (a.contains(b) {
//do stuff here
}[/CODE]

This seems reasonable, except it only works for well defined types that are instantiated from that prototype (this is not the case for a lot of objects that I want to call utility functions on).
It also makes it harder to see usages of the utility functions, because if we use the above method you can easily search for “otUtil.methodName(” in the codebase to find places of use.

I am not overly experienced with Javascript, so I do not know the best practices when creating utility classes (I know this isn’t a class, but you know what I mean) in this language.
I would greatly appreciate hearing peoples insight, advice and techniques for doing this elegantly.

Cheers

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@omnicityJan 23.2015 — Messing with prototypes is somewhat controversial - in my opinion you should only consider this for very small projects, where you have tight control of the code now, and always will. It does lead to much cleaner code though...

What don't you like about the way you have been going so far?
×

Success!

Help @johnsmithy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...