Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to structure utility class

I have several utility functions. What is the best way to package these up, and then import them?

This is what I am trying to do:

import * as util from './util'  export class myClass{      constructor()      {            util.doSomething("test");      } } 

Then in the class:

export class Util{     doSomething(val: string){ return val;}      doSomethingElse(val: string{ return val;} } 

The error message I get from VS is:

Property doSomething does not exist on type util.

like image 957
Greg Gum Avatar asked Sep 25 '15 20:09

Greg Gum


People also ask

How do you define a utility class?

What is Utility Class? Utility Class, also known as Helper class, is a class, which contains just static methods, it is stateless and cannot be instantiated. It contains a bunch of related methods, so they can be reused across the application.

How do you create a utility class in java?

Methods in the class should have appropriate names. Methods only used by the class itself should be private. The class should not have any non-final/non-static class fields. The class can also be statically imported by other classes to improve code readability (this depends on the complexity of the project however).

What is the use of Util class?

utility class is a class that defines a set of methods that perform common, often re-used functions. Most utility classes define these common methods under static (see Static variable) scope. Examples of utility classes include java.

WHAT IS Utilities in java?

util Package. It contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).


2 Answers

If you create a file utils.ts which contains

export default class Utils {     static doSomething(val: string) { return val; }     static doSomethingElse(val: string) { return val; } } 

then you can simplify your client code like this:

import Utils from './utils'  export class MyClass {      constructor()      {          Utils.doSomething("test");      } } 
like image 79
k7sleeper Avatar answered Oct 04 '22 04:10

k7sleeper


There's a couple problems here:

  1. You're not instantiating anything, and doSomething is an instance method
  2. When you do import * as util, util represents the module, not an object in it.

If you want Util, you should just import that:

import { Util } from './util' 

Next, you should instantiate Util, before finally calling the method on it:

var u = new Util(); u.doSomething("test"); 

Here's your code patched up:

import { Util } from './util'  export class MyClass{      constructor()      {          var u = new Util();          u.doSomething("test");      } } 

All that said, there seems to be something odd about the way you're using your utils. This is totally personal opinion, but I wouldn't invoke methods that "do something", i.e. cause side effects, in a constructor.

Also, the methods in Util don't really look like they need to be in that class, since the class holds no state that they depend on. You can always export regular functions from a module. If you wrote your utils module like this:

export function doSomething(val: string) { return val; }  export function doSomethingElse(val: string) { return val; } 

you'd be exporting your functions directly and would sidestep the instantiation hassle, and in fact your original code would work correctly as is.

like image 24
Asad Saeeduddin Avatar answered Oct 04 '22 02:10

Asad Saeeduddin