I have been working on a library which contains a large set of functions. For the sake of simplicity, I am going to use just one set as an example.

I am not sure which is the better way, in terms of ease of use and performance, whether I should put those methods provided as individual methods, prepended with the function category, or whether it is better to create a Class containing all the method as static methods.

For Example:

Individual methods: (p.s. this could also be writen without request_ in the function name and using Namespaces instead)

function request_method() { ... }
function request_url() { ... }
function request_useragent() { ... }

Classed method:

class Request {
    public static function method() { ... }
    public static function url() { ... }
    public static function useragent() { ... }
  • Are you sure you're talking about actual database stored procedures? For what database?
    – Telastyn
    Commented Sep 11, 2013 at 18:11
  • Database? What Database? I am not using this as an ORM or anything like that, just a way to store procedures. So if I want to get the current URL, i would call request_url(), or Request::url()
    – topherg
    Commented Sep 11, 2013 at 18:13
  • 1
    @cgoddard The concept of "storing" procedures is a database concept. That's confusing. Please use "put" instead of "store". Commented Sep 11, 2013 at 18:18
  • 1
    A namespace + functions is good way to do it. It's the route all of C++'s std libs go Commented Sep 11, 2013 at 18:28
  • 3
    It depends on the language you're using. Some language, like Java, won't allow you to put functions anywhere but inside a class while others let you do so; some give you namespaces/packages to help collect related functions - the best way to organize things depends on your development environment. As for 'performance', don't worry about it unless you have a demonstrable bottleneck you need to optimize - writing clear, maintainable code should be the #1 priority by default. Commented Sep 11, 2013 at 18:49

2 Answers 2


The key is to put logically related functions in the same namespace. Prefixing as a "namespace by convention" is a bad idea. Depending on the language this has different meanings.

  • In a language like Java you have no choice you're going to have a utility class.
  • If you're in a language like Haskell, OCaml, or Python, these should be in a module.
  • In C++ you have namespaces for the same purpose.

By doing this you leave the burden of making sure you avoid naming clashes to your compiler. With prefixes you're liable to run into something like compiler_typecheck_ast. Does this refer to the class in the "namespace" compiler_typecheck or is it a function in the compiler namespace that typechecks an ast?


In your case these methods are all meant for working accessing request data so they are not I wouldn't say they are just utility method. They are actually very related to each other and putting them inside a Request method is completely logical.

But when talking about truly utility methods, for instance, methods for working with arrays, putting them inside a generic static may not be the true OOP way but offers a lot of practical advantages. From making code navigation easier in the IDE to being able to use autoload and extending these classes depending on your application architecture.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.