Click here to Skip to main content
15,891,725 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: code sexiness question Pin
Super Lloyd29-Nov-21 20:34
Super Lloyd29-Nov-21 20:34 
GeneralRe: code sexiness question Pin
HobbyProggy29-Nov-21 21:06
professionalHobbyProggy29-Nov-21 21:06 
GeneralRe: code sexiness question Pin
Super Lloyd29-Nov-21 21:17
Super Lloyd29-Nov-21 21:17 
GeneralRe: code sexiness question Pin
HobbyProggy29-Nov-21 21:53
professionalHobbyProggy29-Nov-21 21:53 
AnswerRe: code sexiness question Pin
honey the codewitch29-Nov-21 21:22
mvahoney the codewitch29-Nov-21 21:22 
GeneralRe: code sexiness question Pin
Super Lloyd29-Nov-21 22:28
Super Lloyd29-Nov-21 22:28 
GeneralRe: code sexiness question Pin
honey the codewitch29-Nov-21 22:54
mvahoney the codewitch29-Nov-21 22:54 
AnswerRe: code sexiness question Pin
Daniele Rota Nodari29-Nov-21 21:52
Daniele Rota Nodari29-Nov-21 21:52 
Storing the value of the property (SelectedObject) into a local variable (objects) is like a snapshot.

It ensures that any subsequent instruction refers to the same object.
This is important when you need consistency throughout the method, the method can be relatively time consuming and some other thread can concurrently change the property value.
You can always rely on compiler optimizations and hope that the compiled machine code will take care of such thing, or you can do it by yourself with the local variable.

Another reason could be that you know in advance that someone will add code inside the method to purposely change the object value; such change will require to put the object into a variable, like the collegue told you to do.
Doing it later will require to replace any reference to the property with references to the variable: those changes would be spread along the method polluting versioning differences.

It can even be of help if copy-paste is performed to some other method where the logic must be kept but the property to process has a different name.

These are indeed pre-optimizations, and we can argue about their usefullness and their development cost.

Additionally, the name of the property is not totally meaningful, due to the fact it is singular but it can store multiple objects (like the name of the variable unfolds): this could be an important hint for the future-you maintaining the code.
AnswerRe: code sexiness question Pin
Richard Deeming29-Nov-21 22:01
mveRichard Deeming29-Nov-21 22:01 
GeneralRe: code sexiness question Pin
Super Lloyd29-Nov-21 22:22
Super Lloyd29-Nov-21 22:22 
GeneralRe: code sexiness question Pin
Richard Deeming29-Nov-21 22:34
mveRichard Deeming29-Nov-21 22:34 
GeneralRe: code sexiness question Pin
Super Lloyd29-Nov-21 22:39
Super Lloyd29-Nov-21 22:39 
GeneralRe: code sexiness question Pin
Daniel Pfeffer29-Nov-21 22:26
professionalDaniel Pfeffer29-Nov-21 22:26 
GeneralRe: code sexiness question Pin
Richard Deeming29-Nov-21 22:35
mveRichard Deeming29-Nov-21 22:35 
GeneralRe: code sexiness question Pin
snorkie30-Nov-21 3:08
professionalsnorkie30-Nov-21 3:08 
AnswerRe: code sexiness question Pin
OriginalGriff29-Nov-21 22:38
mveOriginalGriff29-Nov-21 22:38 
GeneralRe: code sexiness question Pin
Richard Deeming29-Nov-21 22:54
mveRichard Deeming29-Nov-21 22:54 
GeneralRe: code sexiness question Pin
OriginalGriff29-Nov-21 23:22
mveOriginalGriff29-Nov-21 23:22 
AnswerRe: code sexiness question Pin
11917640 Member 30-Nov-21 0:19
11917640 Member 30-Nov-21 0:19 
GeneralRe: code sexiness question Pin
Andre_Prellwitz2-Dec-21 5:07
Andre_Prellwitz2-Dec-21 5:07 
AnswerRe: code sexiness question Pin
Gerry Schmitz30-Nov-21 10:55
mveGerry Schmitz30-Nov-21 10:55 
GeneralRe: code sexiness question Pin
Andre_Prellwitz2-Dec-21 12:12
Andre_Prellwitz2-Dec-21 12:12 
AnswerRe: code sexiness question Pin
Gary R. Wheeler30-Nov-21 12:02
Gary R. Wheeler30-Nov-21 12:02 
AnswerRe: code sexiness question Pin
Fueled By Decaff1-Dec-21 3:02
Fueled By Decaff1-Dec-21 3:02 
GeneralRe: code sexiness question Pin
Daniele Rota Nodari1-Dec-21 3:18
Daniele Rota Nodari1-Dec-21 3:18 

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.