-1

(DONT COMMENT IF YOU HAVE BELOW 500 KARMA)

I can't make my mind on how to code these js functions.

I made EVERY function of my library throw if one of the arguments are not of it's specified type.

One part of me is saying that it's not standard js code and writing non idiomatic or non standard thing is a bad thing.
Also, that it bloats the functions, that the code gets ugly, that I already use a linter and have unit tests, that you lose some performance with needless typechecks (I could turn it off just for normal execution).

Another part of me is saying that it's valid to this that because if for some reason one of these functions get called with incorrect arguments, then it might not fail in that function but in some child function 3 levels down the stack trace.

If you want to have an example of what I'm doing, it's this:

import { abides, ofType } from 'abides'

const throws = process.env.NODE_ENV === 'test'
const $chunk = ofType(Buffer)
const $text = ofType(String)
const $write = ofType(Function)
const $offsetX = ofType(Number)
const $offsetY = ofType(Number)
const $setText = ofType(Function)
const schema = {
$chunk, $text, $write, $offsetX, $offsetY, $setText,
}

export default function typeText(args = {}) {
abides(args, schema, { throws })
...
}

Comments
  • 7
    We get karma around here?
    *walks away slowly*
    I'm not worthy of this karma you speak off.

    As for your lib, it's bloat but it's useful bloat if the codebase that uses is, uses it well.
  • 4
    Never really thought about it as I'm mostly using TypeScript.

    But I guess you are making it way too complex. I would check all the types (if it's not a performance sensitive lib) but not with abides because that appears to be overengineered to all hell and thus slow

    I would just do it as follows. With checkType being hopefully inlined by V8
    @highlight
    function foobar(width, height, name) {
    checkType(width, "number");
    checkType(height, "number");
    checkType(name, "string");
    //...
    }

    function checkType(val, type) {
    if(typeof val !== type) {
    throw new Error(`Expected parameter of type ${type}, got ${typeof val}`);
    }
    }
  • 1
  • 7
    Don't tell me what to do
  • 3
  • 0
    @torpkev that comment raised you a bit, don't disappoint me looking forward
  • 2
    @jesustricks do I really have to try and -- myself now just to be contrary?

    Bottom line, demanding people don't reply for some nonsense reason confirms that you're being an ass, nothing more
  • 0
    @12bitfloat I don't know what is sadder. The fact that you insult a lib I wrote (even though it's around 1 kb min+gzip and ok in terms of perf) or the fact that you think typeof null won't return 'object'.
  • 3
  • 3
    @jesustricks If you can't take the slightest of critizism, why did you ask in the first place? I wouldn't do it like that because it looks like it has too much overhead. Maybe it doesn't, I don't know. That's just my off the cuff answer to your question that you explicitely wanted answered
  • 1
    @12bitfloat ok, i exaggerated a bit, I will add a mention in the readme that it's a small package.

    i guess in typescript there's little need because it would illegal at compile time.
  • 0
    While nobody would be able to comment here since we don't get karma but ++'es, i think I'd qualify to comment.

    But why not below 500? We're an open community and everyone who follows the rules has the right to post and comment.
Add Comment