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.

I am maintaining an application and have seen something like this

Permission p = new UserModel().GetPermission(userToTestPerrmission, permissionWeCheck);
if(p.CanChange)
   //sometihing
else
   //something else

This approach looks a little "dirty" to me. The part I have problems with is this new UserModel().GetPermission(). Is this a typical way to write something like this or should it be like:

var model = new UserModel();
model.GetPermissions(...);
...
share|improve this question

1 Answer

up vote 4 down vote accepted

Is this a typical way to write something like

If that’s all the information that is required – yes, why not? I see no problem at all with this approach.

On the other hand, if code lik this is prolific this is a sure sign that the original class design is broken. But from your code it looks like this is outside of your control anyway, since UserModel is a framework class.

In general, if a class is used in such a fashion then it is over-engineered: a static method could have been used just as well (on the other hand, this may make unit testing harder because it prevents mocking).

share|improve this answer
Nicely said. If a class is used for initialization and a method call, why is it a class in the first place? (of course, this is harder to obey in a purely OO language, which quite further proves that OOP is not a silver bullet) – Zirak Aug 8 '12 at 17:55

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.