views:

516

answers:

6

Just wondering what the general consensus was about returning enums from methods indicating the status. The notion of returning codes (ints) is pretty innate in oldschool systems programming (C) but I am wondering if the methodology for indicating the status has changed.

thanks

Edit: I am aware that enums are basically int values. I'm inquiring about the sheer practice of having methods start throwing around status codes (be them enums or ints) to indicate statuses altogether. It seems like a good practice but I was wondering if there is any negative feelings about the idea.

I would always choose an enum over an int return for a status code.

+3  A: 

if All you want is a status returned then an enum is a much better way to go vs just an int for a number of reasons, it forces consistency on what means what so you don't have to remember what return code 1 means, making your code much more maintainable.

Bob The Janitor
+1  A: 

As an enum is basicly just an int, it'll work just fine. You get the added bonus of intellisense.

That is assuming you want the status of your function call. If you are looking for a system/overall status you may want to expose that as an event(s).

Boo
+6  A: 

If you are returning enums to indicate errors instead of throwing exceptions you are, of course, in a state of sin.

Are callers really interested in the state of something? I've generally found that sort of thing in C-style function libraries where there are no objects to represent state. This depends on what you are trying to do, but if you are (say) creating code to manage communication sockets, I'd strongly recommend wrapping them in classes and allow client code to detect their state through properties rather than as the result of an Open() method.

Pontus Gagge
Well I guess that really depends on the severity of the error, right? For instance if I have a private dictionary and a method for adding entries, should I throw an error if the operator forgot to check to see if the key is taken? It seems like a simple return code could save a lot of time / expense
Chance
with try / catches and the process of bubbling up the exception for something fairly trivial.
Chance
The answer to everything is "it depends". In the dictionary case, provide two methods, one throwing and one overwriting (like e.g. System.Collections.Generic.Dictionary<>). However, with a return code, naming the method becomes tricky since it's doing two things -- generally a code smell.
Pontus Gagge
+1  A: 

Returning raw integers (for status and things like that) and not even hiding their numeric nature by constants (or if the language does not support real constants, then variables that are treated as constants by convention) is a very bad practice, even in old school languages like C. It is a source of a lot of bugs and endless headaches.

Also, C#-style enumerations provide (some) type safety and make less space for all sorts of errors and bugs. You should definitely use them. So it's really an improvement over returning an integer and then comparing it to an integer constant. If the language provides an usefule feature, why not use it?

DrJokepu
+1  A: 

Just thinking out loud here but why not just return an object (class), the Type which will depend on how you need to use it. For example one class might just contain a message string while another might contain a number of records updated integer.

cyclo
A: 

Pontus Gagge has it right: Wrap your methods in a class and expose state through a property.

Also, bear in mind the danger with using enums: you can cast any integer as any enum. This will compile and run just fine:

public enum Status
{
    Good,
    Bad,
    Indifferent
};

public class Program
{
    static void Main(string[] args)
    {
        Status s = (Status) 30;
    }
}

It's basically unsafe to assume that a variable of an enum type contains one of the possible values; it may include an impossible value.

Robert Rossney