1

due to my learning, I usually use static class methods and OOP more than pure function and functional programming. Some of my work mates dont understand why and I dont know wich way is better. This is a piece of code for example:

const DynamoDbHelper = class DynamoDbHelper {
    static getTableProperties(tableName) {
       ...
    }
    static async updateTableReadAndWriteCapacities(tableName, readCapacityUnits, writeCapacityUnits) {
        ...
    }
}
module.exports.DynamoDbHelper = DynamoDbHelper;

can also be write:

module.exports.getTableProperties = (tableName) => {
    ...
}
module.exports.updateTableReadAndWriteCapacities = async(tableName, readCapacityUnits, writeCapacityUnits) => {
    ...
}

Wich solution is the better in this case?

negstek
  • 615
  • 8
  • 29
  • 2
    If both of them work equally well (which they should) it's opinion based – UnholySheep Nov 08 '18 at 16:49
  • Mostly its just opinion based. I came from a PHP background and as such, share your liking for classes in javascript. But there really is no better way than the other. If you like the grassroots of javascript, use method 2, if you like a little bit of sugar on top, use method 1. Being new to javascript, I tend to cluch to new methods of doing things and opt for option 1 – Nelson Owalo Nov 08 '18 at 17:26
  • Static class methods are not OOP at all. – Bergi Nov 08 '18 at 17:33

1 Answers1

3

Static-only class is antipattern in JavaScript. It's valid only in languages that don't support functions as independent entities.

If a class isn't intended to be instantiated and acts as a namespace, this is what modules are for. The second snippet is how this should be done.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565