Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

A handy pattern that I like to use:

TryGetBlah(ByRef blah as BlahType) as Boolean

It kills two birds with one stone.

  1. Test that blah exists

  2. Gets blah

What I don't like is that when reading the function body, it is easy to forget that the ByRef parameter is an output. I've started suffixing Out to the parameter to make it clear that the value is an output.

TryGetBlah(ByRef blahOut as BlahType) as Boolean

But, the suffixing feels funny to me, this is because I don't like naming my parameters technically like this. It reminds me of Hungarian notation.

share|improve this question
Do you mean you habitually use ByRef even when you have no intention of changing the value, so you want to use 'Out' as an indication when it should be considered an output? – JeffO Aug 31 '11 at 17:45
@Jeff, Certainly not. blah will get set if it is set-able and true will be returned. Otherwise blah will not be set and false will be returned. Look at Integer.TryParse for example: Public Shared Function TryParse (s As String, ByRef result As Integer) As Boolean – Patrick Te Tau Aug 31 '11 at 21:51
add comment (requires an account with 50 reputation)

2 Answers

I am a constant advocate for explicit naming, so a constant naming pattern I use in methods is the words set/get in variable names for instance:

GetCarFromList(List<Car> carsToGetCarFrom, int idOfCarToGet);
SetCarState(Car carToSetStateFor, CarState stateToSetCarTo);

Under this kind of explicitness, I think I have avoided hungarian notation while still managing to make all aspects of the variables purpose explicit.

Personally I don't like the pattern of TryParse and avoid implementing them myself, I rather return null so that the consumer can use ?? when possible and avoid the ugliness of an if, especially when the use of TryParse is often in the middle of a method which makes it uglier to put the if and nest some guard logic which wouldn't be so bad at the top of the method.

Now, back to your question, I would think of maybe a naming like:

TryGetCat(ByRef catToSendOut as Cat) as Boolean

This feels less like hungarian notation to me and more like simple explicitness.

share|improve this answer
I have to use VB.net for a lot of my work and it doesn't have the elegant ?? operator. – Patrick Te Tau Sep 2 '11 at 1:05
?? is not available in VB.net. I'm unconvinced about the extra verbose naming, but I'll think about it more. – Patrick Te Tau Sep 2 '11 at 1:08
@Patrick the ?? is just a side note about why I prefer to avoid TryParse methods (though I do occasionally implement them), but i would still strongly suggest the explicit naming regardless of ones preference towards TryParse. On a side note, there is something more or less equivalent to the C# ?? operator: stackoverflow.com/questions/403445/… – Jimmy Hoffa Sep 2 '11 at 11:18
Actually, If() with two parameters is the exact same thing as ?? in C# ;-) – Meta-Knight Jan 30 '12 at 15:49
add comment (requires an account with 50 reputation)

Hungarian notation is bad, not because you are putting information in the name, but because it is redundant information at best, and misleading at worst.

Which is why you should avoid this naming convention.

Your TryGetBlah is already your naming convention, blahOut is thus redundant. What it does is introduce a chance for it to be incorrect if you refactor a particular TryGet (unlikely, but possible, for instance if you wanted to pass in the current value and return a new one).

So, the TryGet pattern is not a problem, but the naming convention should be avoided.

share|improve this answer
add comment (requires an account with 50 reputation)

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.