-
Notifications
You must be signed in to change notification settings - Fork 394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update the function of GetInternalGainDeviceIndex #8715
Conversation
src/EnergyPlus/InternalHeatGains.cc
Outdated
@@ -8077,6 +8077,8 @@ namespace InternalHeatGains { | |||
Found = true; | |||
DeviceIndex = DeviceNum; | |||
break; | |||
} else { | |||
ErrorFound = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this change works technically, but it seems out of place. With this change, you basically have this:
Loop over all devices
If device.name matches searchName
If device.type matches searchType
ErrorsFound = false, break, and return
But if device.type doesn't match searchType
ErrorsFound = true, break, and return
EndIf
But if device.name doesn't match searchName << new line
ErrorsFound = true, and continue the loop << new line
EndIf
EndLoop
So it's weird because you are going to loop over all the devices and you are going to end up setting ErrorsFound to true a good number of times before you find a match. Why go through the trouble of setting ErrorsFound so many times? It should've just been initialized to true before the loop starts and that's all you needed to do. Then it would remain false if the loop terminates without finding a match. And in fact, you could just simplify the logic and remove the line that sets ErrorsFound to false because it would already be initialized to false. It should be reduced to something like:
ErrorsFound = true << assume we don't find a match
Loop over all devices
If device.name matches searchName
If device.type matches searchType
ErrorsFound = false
EndIf
break and return
EndIf
EndLoop
Seems like a simpler solution, and probably worth making the change before this merges. Or at least let me know why you think we shouldn't do it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Myoldmopar When I first saw the comment about ErrorFound
I was concerned that this may be a case where the argument is passed along to many functions and might already be true when this function is called (as so many other places in the code). But in this case, the argument is localized when the function is called. Seems it would be better for this function to be a bool
and remove ErrorFound
as an argument, just to keep it clear for future devs, but maybe that's nit-picking. Or maybe it should be an int
function and keep the error arg.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be reasonable for the function to just return an integer and eliminate the error found bool altogether. Return -1 as an invalid device index. That would eliminate the need to return both an error flag and a valid index flag. And that is something we do quite often in E+ already. (Not that that alone would be precedent...)
One additional note is that if you make a change to initialize the variable to true you may need to add it to the approve list in scripts/dev/find_byref_bool_override.py
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mjwitte @Myoldmopar Thank you for the comments and notes! I will update this as an int function. Should I consider moving the related error message from RoomAirModelManager.cc inside this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I consider moving the related error message from RoomAirModelManager.cc inside this function?
I would leave it where it's at, since it uses some local information to build the error message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mjwitte @Myoldmopar The function GetInternalGainDeviceIndex
was updated. Please let me know for any further suggestions or comments. Thanks a lot!
} | ||
} | ||
return DeviceIndex; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are returning DeviceIndex
, why does it also need to be a reference parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@amirroth Thank you for your comments! The returned value for DeviceIndex
was to replace ErrorFound
in the original function GetInternalGainDeviceIndex
. In the same time, DeviceIndex
in this function (GetInternalGainDeviceIndex
) needs to be passed back to state.dataRoomAirMod->RoomAirflowNetworkZoneInfo(ZoneNum).Node(RAFNNodeNum).IntGainsDeviceIndices(gainsLoop)
within the function void GetRoomAirflowNetworkData
in RoomAirModelManager
.
What would be a better way of handling this? Any recommendations? Thanks a lot!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, if a returned index of -1 implies error then, you can use that to infer the IntGainError
flag at the call site. If there is a single error mode that can be specified using a return value (the two most common ones are -1 for IndexNotFound and NULL for ObjectNotFound or MemoryReferenceNotFound) then this is a a good pattern, especially if the error must be handled in the direct caller.
I've noticed EnergyPlus often passes Error parameters through multiple levels of functions. This pattern is less good and has been subsumed by exceptions. I assume that at one point we will do a pass that cleans that up that pattern as well, but probably not in the very near future.
@mjwitte @amirroth @Myoldmopar Many thanks for helping me improve the code! The function |
@LipingWang Please update this branch for @Myoldmopar 's final review. |
Tested this locally with develop pulled in, all good. Thanks! |
Pull request overview
NOTE: ENHANCEMENTS MUST FOLLOW A SUBMISSION PROCESS INCLUDING A FEATURE PROPOSAL AND DESIGN DOCUMENT PRIOR TO SUBMITTING CODE
Pull Request Author
Add to this list or remove from it as applicable. This is a simple templated set of guidelines.
Reviewer
This will not be exhaustively relevant to every PR.