Assaf
Assaf

Reputation: 79

Define methods under global unit. Disadvantages?

I want to define global methods. For that i define their signature under global unit. What are the disadvantages of doing so ? Why does it not recommended ?

Upvotes: 1

Views: 68

Answers (2)

Thorsten
Thorsten

Reputation: 700

I recommend using sn_utils instead. It is also a singleton like global, but forces you to use its instance name: utils.foo(). You need to be aware not to pollute its namespace, e.g. use <evc-name>_foo() as method name.

Upvotes: 1

Yuri Tsoglin
Yuri Tsoglin

Reputation: 963

One disadvantage of using global methods might be that they can be called without explicitly stating the object (just foo() rather than global.foo()), and this is error prone. For example, if your struct happens to have its own method with the same name, such a call is treated as a call to the struct method rather than the global method.

Speaking more generally, one of the principles in object oriented programming is that functions (methods) belong to some object/class rather than being global.

Upvotes: 2

Related Questions