Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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() { ... }
    ...
}
share|improve this question
    
Are you sure you're talking about actual database stored procedures? For what database? –  Telastyn Sep 11 '13 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() –  cgoddard Sep 11 '13 at 18:13
1  
@cgoddard The concept of "storing" procedures is a database concept. That's confusing. Please use "put" instead of "store". –  user61852 Sep 11 '13 at 18:18
1  
A namespace + functions is good way to do it. It's the route all of C++'s std libs go –  jozefg Sep 11 '13 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. –  Sean McSomething Sep 11 '13 at 18:49

2 Answers 2

up vote 3 down vote accepted

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?

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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