SparklySpartan
New member
- Local time
- Today, 11:16
- Joined
- Feb 25, 2025
- Messages
- 27
Hi everyone,
I just wrote some code to properly delete one of my data types from the database I'm working on. I originally came up with the need for it when I realized that when a record from this particular table (the "Links" table) gets deleted, the code should also check for records associated with it in another table ("Settings") and delete those as well. So I wrote a subroutine with the section in the middle (the SQL_Delete sub is a part of the codebase I'm working with, I know one could use CurrentDB.Execute, but I'm trying to follow the way things were written before I was handed the codebase).
After writing the middle part, I thought it would be good to include an "Are you sure?" message because that's what I typically do before anything gets deleted in the application. So I added the message box providing the option to bail out of the deletion. After doing that, however, I realized that one could argue I had put a limitation on the subroutine. What if I wanted to delete a link independent of the message? Perhaps somewhere else in the future I'll want to loop through a recordset of Links and delete them in bulk. I built in the ability to not show the message via an optional parameter.
Lastly, there is a form in my application that remains open most of the time called "Favorites", and what is displays is largely based on the links and the records associated to them in the Settings table. So I added one final clause to call two subs on the Favorites form that refresh what it's displaying, if the Favorites form happens to be open.
It took me less than five minutes to write all of this, but then I realized that I had just written a sub that appears to be handling 3 different responsibilities: potentially showing a confirmation message, deleting the data I want to delete, and refreshing a form. From what I've studied and been taught about programming in general, this isn't considered the best way to design a subroutine.
To be honest though, I'm not sure if this breaks SRP or not. I do agree that it does three different things, but it's not as if those things aren't tightly related and supposed to happen together most of the time anyways. Still, I'm open to your thoughts on other ways to design it, or to hear if others would also choose to design it this way.
What do you think about the conditional confirmation message? If you wanted a standard confirmation message to show in all of the 3-4 places this sub would be run from, would you opt for what I did, or use another function to handle that "UI" responsibility and let the Delete_Link exclusively handle the "Data manipulation" responsibility?
And what are your thoughts on refreshing the form? I have a gut feeling there are better places to put this code but I don't know where else I would besides repeating it in the 3-4 places which doesn't feel good to do either.
I might be obsessing over this too much. I know I shouldn't force my code to follow something like SRP just for the sake of it if it doesn't make sense within the scope and scale of the application I'm making. That said, I like to remain on the lookout for things I could be doing better at all times, because hopefully I'll learn something in the process of seeking it out. So I'd love to hear your thoughts. Thanks in advance!
I just wrote some code to properly delete one of my data types from the database I'm working on. I originally came up with the need for it when I realized that when a record from this particular table (the "Links" table) gets deleted, the code should also check for records associated with it in another table ("Settings") and delete those as well. So I wrote a subroutine with the section in the middle (the SQL_Delete sub is a part of the codebase I'm working with, I know one could use CurrentDB.Execute, but I'm trying to follow the way things were written before I was handed the codebase).
After writing the middle part, I thought it would be good to include an "Are you sure?" message because that's what I typically do before anything gets deleted in the application. So I added the message box providing the option to bail out of the deletion. After doing that, however, I realized that one could argue I had put a limitation on the subroutine. What if I wanted to delete a link independent of the message? Perhaps somewhere else in the future I'll want to loop through a recordset of Links and delete them in bulk. I built in the ability to not show the message via an optional parameter.
Lastly, there is a form in my application that remains open most of the time called "Favorites", and what is displays is largely based on the links and the records associated to them in the Settings table. So I added one final clause to call two subs on the Favorites form that refresh what it's displaying, if the Favorites form happens to be open.
Code:
' Properly deletes a link from the database as well as all of the favorites assignments associated with it
Sub Delete_Link(ID As Long, Optional confirmation_message As Boolean = True)
If confirmation_message Then
If MsgBox("Are you sure you want to delete this link and all of it's button / dropdown assignments?", vbYesNo) = vbNo Then Exit Sub
End If
Call SQL_Delete("Links", "[ID]=" & ID)
Call SQL_Delete("Settings", "(([Key] LIKE ""Favorites_Button_*"") OR ([Key] LIKE ""Favorites_Dropdown_*"")) AND [Value_Long]=" & ID)
If IsFormLoaded("Favorites_f") Then
Call Form_Favorites_f.Refresh_Captions
Call Form_Favorites_f.Refresh_Dropdowns
End If
End Sub
It took me less than five minutes to write all of this, but then I realized that I had just written a sub that appears to be handling 3 different responsibilities: potentially showing a confirmation message, deleting the data I want to delete, and refreshing a form. From what I've studied and been taught about programming in general, this isn't considered the best way to design a subroutine.
To be honest though, I'm not sure if this breaks SRP or not. I do agree that it does three different things, but it's not as if those things aren't tightly related and supposed to happen together most of the time anyways. Still, I'm open to your thoughts on other ways to design it, or to hear if others would also choose to design it this way.
What do you think about the conditional confirmation message? If you wanted a standard confirmation message to show in all of the 3-4 places this sub would be run from, would you opt for what I did, or use another function to handle that "UI" responsibility and let the Delete_Link exclusively handle the "Data manipulation" responsibility?
And what are your thoughts on refreshing the form? I have a gut feeling there are better places to put this code but I don't know where else I would besides repeating it in the 3-4 places which doesn't feel good to do either.
I might be obsessing over this too much. I know I shouldn't force my code to follow something like SRP just for the sake of it if it doesn't make sense within the scope and scale of the application I'm making. That said, I like to remain on the lookout for things I could be doing better at all times, because hopefully I'll learn something in the process of seeking it out. So I'd love to hear your thoughts. Thanks in advance!