Click here to Skip to main content
15,915,076 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 5:11
professionalNathan Minier11-Apr-18 5:11 
GeneralRe: Worst use of var I've seen... Pin
HobbyProggy10-Apr-18 1:56
professionalHobbyProggy10-Apr-18 1:56 
GeneralRe: Worst use of var I've seen... Pin
Sander Rossel10-Apr-18 7:39
professionalSander Rossel10-Apr-18 7:39 
GeneralRe: Worst use of var I've seen... Pin
HobbyProggy10-Apr-18 19:38
professionalHobbyProggy10-Apr-18 19:38 
GeneralRe: Worst use of var I've seen... Pin
Jörgen Andersson9-Apr-18 22:08
professionalJörgen Andersson9-Apr-18 22:08 
GeneralRe: Worst use of var I've seen... Pin
Sander Rossel9-Apr-18 23:50
professionalSander Rossel9-Apr-18 23:50 
GeneralRe: Worst use of var I've seen... Pin
Jörgen Andersson10-Apr-18 2:36
professionalJörgen Andersson10-Apr-18 2:36 
GeneralRe: Worst use of var I've seen... Pin
Sander Rossel10-Apr-18 7:37
professionalSander Rossel10-Apr-18 7: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 20:56
professionalJörgen Andersson10-Apr-18 20:56 
GeneralRe: Worst use of var I've seen... Pin
Sander Rossel10-Apr-18 21:09
professionalSander Rossel10-Apr-18 21:09 
GeneralRe: Worst use of var I've seen... Pin
Jörgen Andersson10-Apr-18 21:17
professionalJörgen Andersson10-Apr-18 21:17 
GeneralRe: Worst use of var I've seen... Pin
Sander Rossel10-Apr-18 22:03
professionalSander Rossel10-Apr-18 22:03 
GeneralRe: Worst use of var I've seen... Pin
RickZeeland9-Apr-18 22:48
mveRickZeeland9-Apr-18 22:48 
GeneralRe: Worst use of var I've seen... Pin
PeejayAdams9-Apr-18 22:54
PeejayAdams9-Apr-18 22:54 
GeneralRe: Worst use of var I've seen... Pin
F-ES Sitecore9-Apr-18 23:30
professionalF-ES Sitecore9-Apr-18 23:30 
GeneralRe: Worst use of var I've seen... Pin
RickZeeland9-Apr-18 23:43
mveRickZeeland9-Apr-18 23:43 
GeneralRe: Worst use of var I've seen... Pin
HobbyProggy9-Apr-18 23:44
professionalHobbyProggy9-Apr-18 23:44 
GeneralRe: Worst use of var I've seen... Pin
Slacker00710-Apr-18 3:58
professionalSlacker00710-Apr-18 3:58 
GeneralRe: Worst use of var I've seen... Pin
F-ES Sitecore10-Apr-18 4:20
professionalF-ES Sitecore10-Apr-18 4:20 
GeneralRe: Worst use of var I've seen... Pin
Slacker00710-Apr-18 4:21
professionalSlacker00710-Apr-18 4:21 
GeneralRe: Worst use of var I've seen... Pin
F-ES Sitecore10-Apr-18 4:40
professionalF-ES Sitecore10-Apr-18 4:40 
GeneralRe: Worst use of var I've seen... Pin
Slacker00710-Apr-18 4:43
professionalSlacker00710-Apr-18 4:43 
GeneralRe: Worst use of var I've seen... Pin
F-ES Sitecore10-Apr-18 4:54
professionalF-ES Sitecore10-Apr-18 4:54 
GeneralRe: Worst use of var I've seen... Pin
Slacker00710-Apr-18 5:05
professionalSlacker00710-Apr-18 5:05 
GeneralRe: Worst use of var I've seen... Pin
F-ES Sitecore10-Apr-18 5:11
professionalF-ES Sitecore10-Apr-18 5: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.