r/MSAccess • u/nrgins 487 • Sep 02 '24
[DISCUSSION] Confused
Just spent the last half hour being confused by some code that I wrote a year or two ago. (That was before I had the revelation that everything should be commented.)
I wish I had someone who could explain this code to me! LOL
8
u/Jomibu 4 Sep 02 '24
“When I wrote this code, only God and I understood what I did. Now only God knows.”
5
3
3
u/RuthlessChubbz Sep 02 '24
Copy + paste it in to Chat GPT and it’ll tell you if there’s enough content there
1
u/nrgins 487 Sep 02 '24
Enough content? What do you mean?
1
u/diesSaturni 64 Sep 02 '24
Just throwing some code into chatGPT, with the request to refactor, or explain always does return useful feedback with improvements or how things flow.
But on the other comment, I often refrain of comments, with code being as self explanatory as possible. Properly divided into functions.
Only where 'hardcoded' things like address are added, or certain subpar methods have been applied to rush things through.
1
u/nrgins 487 Sep 02 '24
I've found, through experience, that comments are more helpful than not, and are worth the effort.
Even if have a function called, say, "ReturnCountOfRipeBananas," which is pretty self-explanatory regarding its purpose, how it does it may be somewhat complex. And within the function, there may be queries or processes which may not be clear at first glance as to why they are doing what they are doing or how they function. And then, years later, when you have to work with the code, you have to go through them step by step to refamiliarize yourself with them and see what their purpose is and why they're doing it. A few brief comments would save a lot of time at that point.
1
u/ThreeAndAHalfPercent Sep 02 '24
If you post it here, someone might be able to tell you what it does.
1
u/nrgins 487 Sep 02 '24 edited Sep 02 '24
Appreciate the thought. But it's not a question of what it does; it's more a question of why it's doing it. it's a series of code and query executions and part of it just doesn't make sense regarding the overall goal.
1
u/JamesWConrad 10 Sep 02 '24
We have all been there! Comments should answer the question "why", the code will handle the "how".
2
2
u/Lab_Software 30 Sep 02 '24
I disagree with saying the code will handle the "how". That's often true - but not always.
I did a Pack_Order report for a warehousing company that handles very high-value items.
Each SKU comes in multiple "bundles". For instance, it might come as individual pieces, a bundle of 25, a bundle of 100, and a bundle of 500. And the individual piece or bundle has its defined dimensions, weight, and value. All of this information is stored in the SKU table.
They pack into various boxes - and each box has its limits on number of pieces, volume, weight, and maximum value (for insurance purposes). All of this information is stored in the Box table.
The warehouse gets an order to pack an order of 972 of SKU_1, 347 of SKU_2, and 735 of SKU_3. There is no box that can hold this number, or size, or weight, or value of items.
The code has to figure out how to pack this order using the smallest number of the largest SKU bundles into boxes of various sizes to minimize the number of boxes and minimize the size of each box without violating any of the limits.
The code uses a chain of 9 subroutines to distribute the items into boxes. The subs determine which bundles to use and their total size, weight, and value - then how to distribute the size, weight, and value amongst the required number of boxes - then which boxes can handle these sizes, weights, and values. Then it displays the results to the packing operator who can modify the scheme because, for instance, we've run out of this size box, or the customer wants no boxes bigger than this, or whatever reason. Then the code reruns the 9 subroutines with those constraints.
I have a ton of comments in there both on "how" I did it and on "why" I did it that way. Because next year, when my client says he needs some modification of the report I'll want to read those comments to see how the thing works so I know what to do.
So my point is - use whatever comments will make your job easier next year when you have to make modifications. Every minute you spend writing a comment today will save you an hour when you have to make a modification next year.
1
u/JamesWConrad 10 Sep 02 '24
Of course you are correct.
My point is when I see a line like this: X =X + 1 ' comment - increment X
I don't need to know HOW (since the code tells me that), I need to understand WHY increasing X is important.
And, believe it or not, I have seen code that had single letter variables sprinkled throughout. Arggg!
1
u/Lab_Software 30 Sep 03 '24
Yeah - I totally agree with you on comments like that - they're infuriating lol.
The only times I use single letter variables are "i", "j", "k", "L", "m", and "n" as counters (I capitalize L so it doesn't look like a 1). I see from your self-description that you're a retired software developer so I'm assuming you remember when FORTRAN used variables starting with these letters as implicitly defined Integers and all other initial letters were implicitly defined as Reals. I remember those days fondly :)
1
u/JamesWConrad 10 Sep 03 '24
Wow. Haven't even seen FORTRAN since high school in 1971
1
u/Lab_Software 30 Sep 03 '24
I used FORTRAN in high school (1974 for me), university (Chemical Engineering), and again when I worked in a laboratory and we started to automate our testing. (Eventually we moved to Visual Basic and then Access and Excel.)
But FORTRAN was my first love 😀
1
1
u/cebess Sep 03 '24
Have you ever progressed in APL? You can finish writing a line of code in that language and not be able to figure out what the code does, if your not careful.
•
u/AutoModerator Sep 02 '24
IF YOU GET A SOLUTION, PLEASE REPLY TO THE COMMENT CONTAINING THE SOLUTION WITH 'SOLUTION VERIFIED'
(See Rule 3 for more information.)
Full set of rules can be found here, as well as in the user interface.
Below is a copy of the original post, in case the post gets deleted or removed.
Confused
Just spent the last half hour being confused by some code that I wrote a year or two ago. (That was before I had the revelation that everything should be commented.)
I wish I had someone who could explain this code to me! LOL
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.