Question

As in the title. Why does the Stack class need a method to return a reference of the top object? I've always been told, that methods suggest there's some computing involved and that simple objects should be returned with properties instead. Peek() method has no parameters and on a code level it's (I think) a simple operation.

The question is: is there a speciffic reason for that? Any hidden behaviour that impacts performance?

EDIT: I don't know the class implementation, but f.e. if the method uses enumerator beneath, then iterating to the last element many times would be unwise. On the other hand if it's a single IList, then it should not have any bigger impact on the performance.

Was it helpful?

Solution

Peek is a verb, so in my book Peek() should be a method. However with a different name it could also be a property.

Remember that any property has associated get and/or set methods, so you'd end up with a method either way.

OTHER TIPS

I understand the question as "Why is it a method, not a property".

One reason can be consistency - all access methods are actually methods. It totally is a matter of styl, because I see no reason to not have it as a property, from a pure code perspective.

The documentation doesn't mention exactly what collection is actually used to handle the stack, but basically there is no other, more efficient way to give you access to only the "top" of that collection. We also don't know if the "top" is even the first or last element, it's possible that the stack keeps track of which element is the "top" and doesn't actually delete the popped members (at least not every time they are popped) in order to avoid having to move every other element (again, assuming they are using an array-like rather than linked-list-like structure).

I'd say as the value is not a property of the Stack itself, but a result of evaluating the current content of the stack, in my opinion a method is more suitable than a property.

And as C. Evenhuis mentions in his answer, Peek is a verb, so a method is more logical. And as peek is the common terminology for this operations on stacks it makes more sense to use that then to use a new/different term just to use a property.

I agree, could be a Property as TopItem or something because that makes intitial sense, but would it throw an exception or return null if empty? Properties should not throw exceptions, according to MSDN.

From Stack's source reference, you'll see code involved to throw a specific exception.

    // Returns the top object on the stack without removing it.  If the stack
    // is empty, Peek throws an InvalidOperationException.
    public virtual Object Peek() {
        if (_size==0)
            throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EmptyStack"));
        Contract.EndContractBlock();
        return _array[_size-1];
    }

This now differs from how the above property concept would be handled. So one vs the other.

edit- added Property documentation. And not the winning answer, but providing further explanation about the logic on why it is not a property.

This is encapsulation, it's purpose is to restrict access to an objects components: http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top