+ 8
[OFF-TOPIC] Have you ever created a code that worked and you had no idea why?
Then, it would be nice to explain here how you finally understood the way it worked!!!
10 Answers
+ 8
David Carroll I'm enjoying programming more and more. Definitely, you've provided a pair of valuable tips which increase my curiosity 'cause I really want to know how everything works in the background. So, be sure that I will delve into the subject until I fully understand it. Your feedback has been very constructive, thanks!
+ 7
Hatsy Rei OMG... I do recall helping you with that issue. Feels like eons ago.
Geovanny MartĂnez Forero I vaguely recall a time when I might have written code and couldn't figure out why it did or wouldn't work.
It's probably a combination of strong intuition, strong deduction and troubleshooting skills, understanding of all the moving parts, and from experience.
There are many categories of tips I could probably suggest. However, that would involve a long article, video course, or a book's worth of material to share with you. đ
So... I'll give you a few general tips to keep in mind.
1. It really helps to have a general understanding of how high level languages translate into assembly instructions and how the CPU works with those instructions. This will allow you to think through what is really happening with code you write. There needs to be some logical path between the code you write and how it eventually turns into low level instructions.
+ 7
2. Use a debugger to step through your code and study the heck out of everything that is happening. Look at the variables in scope, in global, sequence of methods calls, call stack, follow the code to understand how it all executes. You will grow as a developer with this alone.
+ 7
I highly recommend you research The Little Man Computer to see how code instructions work in the CPU at a very basic level.
This is one reference, but there are other really good ones I've seen in the past.
https://www.futurelearn.com/courses/how-computers-work/0/steps/49285
I really enjoyed seeing how this has been explained in different ways with great visuals in videos, slide decks, and web apps.
+ 6
Sigh. For some reason, posting in your activity feed is still not an option for iOS users. This has been quite the bane for both the mods and the users. We have no choice but to compromise - Geovanny MartĂnez Forero I think the thread can stay, as long as the "post that code here" part is removed (we don't want to encourage users to continue advertising codes in Q&A). I've unmarked the thread for deletion, hope this solves the issue for now. Sorry about that.
+ 6
Hatsy Rei Without doubt, a very interesting case, thanks for sharing!
+ 6
David Carroll Very nice references! đđđ»
+ 5
Anyway, getting back on topic *ahem* -
I recall the one time when I was working on a "Haskell Vs Python" assignment topic, and looking into Haskell's conditional statements, how to write them properly just to get them to work. I think David Carroll can still recall the discussion on Discord, it was more than half a year away. IIRC, something like:
let x =
if 5 < 4
then 0
else 1
gives a parse error while
let x =
if 5 < 4
then 0
else 1
works. After an hour of discussion or so, we found out that the 'if' has to be indented past the '=' operator. The rest of the indentation does not even matter.
Up till today, I'm still unsure if it's an IDE-specific issue. The code was tested on repl.it. I guess one would have as many "why does this work" as "why does this not work" instances throughout their lifetime.
+ 5
The value may not be obvious, at first, as you review this. However, it is in this simple understanding that I've been able to rationalize some pretty nuanced things in code.
+ 3
Hatsy Rei I see, thanks!!!