9

So, let's say I have a few functions to deal with opening/closing of files.

Is it better to make a class with all these function declared statically or simply put the "public" function in the header file of namespace "file", and put the rest "implementation details" in the .cc file?

Below are the code samples.

It's a bit long for the namespace one, as I want to make it as clear as possible.

THANKS!!


the class implementation

Header:

#ifndef FILE_H
#define FILE_H

#include <iostream>
#include <fstream>

include "common.h"

enum Errorcode {
    FILE_CANNOT_OPEN,
    FILE_CANNOT_CLOSE
};

class file {

public:
    static common::Lines toLines(std::string filename);

private:
    static void err(Errorcode e, std::string msg);
    static void toLines(std::ifstream &ifs, common::Lines &lines);

};

#endif

.cc file:

/*just the implementation details of above class.*/

the namespace implementation

Header:

#ifndef FILE_H
#define FILE_H

#include <iostream>
#include <fstream>

#include "common.h"

namespace file {

common::Lines toLines(std::string filename);

}

#endif    

.cc file:

namespace file {

enum Errorcode {
    FILE_CANNOT_OPEN,
    FILE_CANNOT_CLOSE
};

void err(Errorcode e, std::string msg);
void toLines(std::ifstream& ifs, common::Lines &lines);

common::Lines toLines(std::string filename)
{
    std::vector<std::string> lines;

    try {
        std::ifstream ifs(filename.c_str());
        if (ifs.fail()) throw FILE_CANNOT_OPEN;

        toLines(ifs, lines);

        ifs.close();
        if (ifs.fail()) throw FILE_CANNOT_CLOSE;
    }
    catch (Errorcode e) {
        err(e, filename);
    }

    return lines;
}

void err(Errorcode e, std::string msg)
{
    switch (e) {
        default:
            std::cerr << "Unknown error.\n";
            break;
        case FILE_CANNOT_OPEN:          
            std::cerr << "file \"" << msg   
                << "\" could not be opened.\n"; 
            break;
        case FILE_CANNOT_CLOSE:         
            std::cerr << "file \"" << msg   
                << "\" could not be closed.\n"; 
            break;
    }
    std::exit(-1);
}

void toLines(std::ifstream& ifs, common::Lines &lines)
{
    std::string line;

    while(std::getline(ifs, line)) {
        lines.push_back(line);
    }

    ifs.clear();    // clear error bit set by getline()
}

}                    
Jimmy Lu
  • 4,810
  • 7
  • 25
  • 30

3 Answers3

11

Superficially, static class functions and namespaced functions are almost identical, and indeed classes were used in the early days before namespace support became widespread.

Nowadays, you should do what most expresses the logical structure (i.e. the mental model) of your program. If you are grouping related functions, it's a namespace.

However, the technical differences in a nutshell is that namespaces participate in argument-dependent lookup (ADL), while class member functions don't, but classes can be turned into templates and specialized. If any of those semantic differences is important to you, then that consideration may help you make the right choice.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 4
    @BeyondSora: Here you go: [ADL - Koenig Lookup](http://stackoverflow.com/questions/8111677/detailed-explanation-on-how-koenig-lookup-works-with-namespaces-and-why-its-a-go) – Alok Save Jan 30 '12 at 05:17
  • 1
    Now I know what ADL is. I think in my case it is probably better to use namespace instead of class. THANKS!! – Jimmy Lu Jan 30 '12 at 05:41
5

There's a simple question that covers most situations: If you made it a class, would an instance of that class make sense and accomplish something useful?

If an instance is useful, then you want a class. Otherwise, a namespace is probably a better choice.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 2
    in my case there would be no need at all for an instnace. So it seems namespace is the one for me. – Jimmy Lu Jan 30 '12 at 05:42
-2

As oop is becoming more and more popular, Class is a better choice when you develop a small project with only 1 programmer. When you developing a complex application, combine class and namespace is better.

Michael
  • 53
  • 2
  • 7
    This post is speculative, subjective, in large parts fiction, and the remainder is wishful thinking. – Kerrek SB Jan 30 '12 at 05:16
  • 4
    Yes, classes are great for object-oriented programming because they model objects. But the question specifically says that he has a set of related functions that merely need to be encapsulated, not that they model an object. He asks if a static class or a namespace is better. There's nothing particularly OOP about a static class, and in this case, using a namespace is probably a better option. I have no idea why it matters if it's a simple or complex application; good design is good design, whether it's simple or complex, one programmer or a thousand. – Cody Gray - on strike Jan 30 '12 at 06:07
  • 1
    How is oop becoming more and more popular? Hasn't it's popularity somewhat plateaued in the last 10 years or something, and even declined with the adoption of functional features into mainstream languages? – fredoverflow Jan 30 '12 at 10:38