Hacker News new | ask | show | jobs
by nikz 5502 days ago
May be slightly off-topic, but I feel a little pain behind my eyes whenever I see the hack-around-namespaces function naming (e.g tt_whatever, for "ThumbTack" presumably).

Surely a singleton class would be a nicer way to do this (or just leave the "tt_" off, why is it necessary, you're not overriding core functions)?

1 comments

We actually had a debate in our engineering dept about how we wanted to handle functions like this, and in the end decided on tt_function_name() in the global function space.

We decided against putting them in explicit namespaces, because we felt these were convenience functions and they should be convenient to use. We felt the convenience of:

if (tt_str_startswith($url, 'https://)) { }

over something like:

if (tt/global/string/starts_with($url, 'https://)) { }

was worth the lack of organization. We have <10 of these types of global functions in our code base and we are very careful about adding new ones. All other classes and functions are either in a namespace or class context.

We went with the tt_ prefix because we wanted to cover our bases and really explicitly ensure we never will collide with a PHP function, and we felt it isn't too unwieldy.

As for using a singleton class, since 5.3 we prefer to use namespaces instead.

Isn't this what use is for?

    use tt\global\string;
    if (string\starts_with($url, 'https://)) { }
Sure is, and we do organize our 'use' statements the same way one would organize 'import' statements in a python file.

However in this context again we felt that the convenience of just being able to throw these functions in place without having to worry about putting the proper use statement was worth the tradeoff.