Click here to Skip to main content
14,696,793 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.
 
GeneralRe: Worst use of var I've seen... Pin
Nathan Minier11-Apr-18 6:11
professionalNathan Minier11-Apr-18 6:11 
GeneralRe: Worst use of var I've seen... Pin
HobbyProggy10-Apr-18 2:56
professionalHobbyProggy10-Apr-18 2:56 
GeneralRe: Worst use of var I've seen... Pin
Sander Rossel10-Apr-18 8:39
professionalSander Rossel10-Apr-18 8:39 
GeneralRe: Worst use of var I've seen... Pin
HobbyProggy10-Apr-18 20:38
professionalHobbyProggy10-Apr-18 20:38 
GeneralRe: Worst use of var I've seen... Pin
Jörgen Andersson9-Apr-18 23:08
professionalJörgen Andersson9-Apr-18 23:08 
GeneralRe: Worst use of var I've seen... Pin
Sander Rossel10-Apr-18 0:50
professionalSander Rossel10-Apr-18 0:50 
GeneralRe: Worst use of var I've seen... Pin
Jörgen Andersson10-Apr-18 3:36
professionalJörgen Andersson10-Apr-18 3:36 
GeneralRe: Worst use of var I've seen... Pin
Sander Rossel10-Apr-18 8:37
professionalSander Rossel10-Apr-18 8:37 
I use anonymous types quite a lot, they're nothing like SELECT * (which is just bad practice and may have some unexpected side effects).
Sometimes you just don't need an entire entity (when working with EF) and you just select two or three fields.
Creating a class just to be used in a single function is kind of overkill and clutters your code base.
Just use an anonymous type, get the fields you want, use them in just that one function, and be done with it.
The compiler still creates an "actual class", so it behaves and performs just as if you wrote the class yourself, but without the clutter Big Grin | :-D

I also use it for ad-hoc return models in MVC.
Same issue, creating a class for use in a single function just clutters your code.
Your front-end makes it JavaScript so it loses its type anyway.

When you decide you need to make it a class later on that's perfectly possible (there's even a shortcut for it) and it has no side-effects whatsoever (as it was already a class after compilation) so it's clean and easy.

You may not need them per se, but it makes your code cleaner and easier to read.
Not using them is just limiting yourself by not using a great tool from your toolbox (unlike SELECT *, which, again, is just bad practice).

GeneralRe: Worst use of var I've seen... Pin
Jörgen Andersson10-Apr-18 21:56
professionalJörgen Andersson10-Apr-18 21:56 
GeneralRe: Worst use of var I've seen... Pin
Sander Rossel10-Apr-18 22:09
professionalSander Rossel10-Apr-18 22:09 
GeneralRe: Worst use of var I've seen... Pin
Jörgen Andersson10-Apr-18 22:17
professionalJörgen Andersson10-Apr-18 22:17 
GeneralRe: Worst use of var I've seen... Pin
Sander Rossel10-Apr-18 23:03
professionalSander Rossel10-Apr-18 23:03 
GeneralRe: Worst use of var I've seen... Pin
RickZeeland9-Apr-18 23:48
mveRickZeeland9-Apr-18 23:48 
GeneralRe: Worst use of var I've seen... Pin
PeejayAdams9-Apr-18 23:54
MemberPeejayAdams9-Apr-18 23:54 
GeneralRe: Worst use of var I've seen... Pin
F-ES Sitecore10-Apr-18 0:30
mveF-ES Sitecore10-Apr-18 0:30 
GeneralRe: Worst use of var I've seen... Pin
RickZeeland10-Apr-18 0:43
mveRickZeeland10-Apr-18 0:43 
GeneralRe: Worst use of var I've seen... Pin
HobbyProggy10-Apr-18 0:44
professionalHobbyProggy10-Apr-18 0:44 
GeneralRe: Worst use of var I've seen... Pin
Slacker00710-Apr-18 4:58
professionalSlacker00710-Apr-18 4:58 
GeneralRe: Worst use of var I've seen... Pin
F-ES Sitecore10-Apr-18 5:20
mveF-ES Sitecore10-Apr-18 5:20 
GeneralRe: Worst use of var I've seen... Pin
Slacker00710-Apr-18 5:21
professionalSlacker00710-Apr-18 5:21 
GeneralRe: Worst use of var I've seen... Pin
F-ES Sitecore10-Apr-18 5:40
mveF-ES Sitecore10-Apr-18 5:40 
GeneralRe: Worst use of var I've seen... Pin
Slacker00710-Apr-18 5:43
professionalSlacker00710-Apr-18 5:43 
GeneralRe: Worst use of var I've seen... Pin
F-ES Sitecore10-Apr-18 5:54
mveF-ES Sitecore10-Apr-18 5:54 
GeneralRe: Worst use of var I've seen... Pin
Slacker00710-Apr-18 6:05
professionalSlacker00710-Apr-18 6:05 
GeneralRe: Worst use of var I've seen... Pin
F-ES Sitecore10-Apr-18 6:11
mveF-ES Sitecore10-Apr-18 6:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.