Ask HN: Do senior engineers have a mental model before coding?
The feedback was you can’t do this when compiling time is long and development will be slow. I’ve heard of developers that don’t use debuggers because they have a mental model in their head.
Do most senior software engineers take time to build a mental model before coding and testing?
I prefer to create a draft quickly to build a mental model. Is my way of logging to console frequently and coding before building a mental model a junior habit that I should work on?
Am I unlikely to pass a FAANG interview like this?
Edit: It wasn’t a LeetCode problem. I had to implement an API where each call would change the state of the application on certain conditions. I heard the problem and clarified it on the whiteboard. I coded each function while talking and ask for clarifications.
[+] [-] jrockway|6 years ago|reply
Doing the design work is what separates senior and non-senior engineers. The more details you get worked out before they are solidified by having 10 other systems depending on them, the better.
If you just do stuff with a rough idea of the direction you want to go in, it's very easy to get sidetracked by something that doesn't matter. It's also very easy to build something nobody wants. By having people agree on some sort of semi-detailed design that describes assumptions, corner cases, the API, etc... you dramatically increase your chances of building the computer program that people want.
I am not sure how you test for this in an interview... but it is important. If you don't design things before building them, that is something you should start working on.
[+] [-] karmakaze|6 years ago|reply
Usually, I'll think about a problem, some of the inherent characteristics, or desired behaviours then choose some applicable techniques/technologies, languages, algorithms, libraries. Then think about how to compose the program from parts to be developed. This all happens naturally without conscious sequencing.
That's what's great about being current here, I find out about things I don't need to use yet but could be useful at a later time. Or discover something that would have made a past project better if I'd known of it.
[+] [-] paladin314159|6 years ago|reply
Your ability to build that mental model depends on both your experience/skill and the complexity of the problem. For simple problems, a more senior engineer might be able to come up with a mental model instantly because it's similar to lots of things they've done before. For really complex problems, it probably takes everyone a long time to build the correct mental model and you'd want to balance that with what you can figure out by diving into the implementation.
My guess would be that the interviewers' line of thinking is that if you couldn't build a mental model for a problem quickly, that means you're not proficient with problems like that. Which is probably true, but opens up the question of whether the interview is testing the right type of problem, of course.
[+] [-] bb88|6 years ago|reply
To be fair, 90% of most business logic is simple and straightforward, and should be written that way.
The real test of a senior engineer is to quickly see the hard 10% that isn't straightforward, and to design the rest of the system to minimize the effects of the complexities introduced by the hard 10%.
[+] [-] squirrelicus|6 years ago|reply
That being said, executing the code while typing it is critical. You cannot build a mental model of all edge cases. At best, your personal discipline will allow you have habits around preventing and catching edge cases. But you won't find enough edge cases just by thinking about the code to type. You do have to discover them by running the code, and the sooner you discover them, the faster you will be at producing high quality code.
As far as how much time I spend building a mental model vs coding... It's at least half mental model time. If I happen to predict enough edge cases, then it might be 90% of the time. But the critical part is that I'm not done until I've exhausted the edge cases I can imagine. If I discover sufficiently big edge cases that brick my code, it's time to step away from the computer and fix my broken mental model.
Note: tests are a way to do this, but so is white box poking and prodding at your program in debug mode.
[+] [-] mstade|6 years ago|reply
(The last nota bene of your comment is particularly striking to me. I love tests and I try to automate them as much as possible, but sometimes you just gotta prod and poke the thing to really get a feel for it.)
[+] [-] einpoklum|6 years ago|reply
[+] [-] nomel|6 years ago|reply
I think it's best to start with something like an outline. This could be comments, tests, sparse pseudo-code, or some level of documentation that explains what you're trying to do.
Once you have a rough idea of the pieces that are required and will roughly fit together, understanding the consequences of the decisions you make while implementing those pieces will come more naturally, and can help prevent rework. Most architecture mistakes, and often bugs, are from not understanding the consequences, which isn't really possible without having the mental model.
[+] [-] johnrob|6 years ago|reply
[+] [-] jconley|6 years ago|reply
In practice, in an interview, this is the wrong approach. I've failed interviews for this as well.
Most of what differentiates a Senior Engineer is communication, mentorship, ability to compromise on product requirements, set realistic expectations, design solutions to big problems, think in the large and hold the whole system in their head, design API's, things like that.
I no longer do interviews that require me to write code. They're just ridiculous for the type of skills I'd hope a company would value in a Senior Engineer.
[+] [-] ohyes|6 years ago|reply
This is just something that happens while I'm "thinking about how to approach the problem." I have an idea of what a computer can do and (hopefully) what the steps might be to solve the problem I'm trying to solve. I'll test out some things in my head and then start typing.
I think different people are different, using a console that doesn't tell me that you didn't have a mental model of how the program would work, it just tells me that you like to test as you go. That's a good, careful, practice and not a red flag to me at all.
read-eval-print loops are a time honored tradition in a number of communities. I've written entire programs by typing into a REPL and dribbling the results as I go. Languages with long compile times are genuinely unfun to work in.
So i guess my question for you, if you didn't have a mental model, how did you know what to type into the console?
Also, if developers didn't need debuggers because of their mental model, why are there bugs in the code that they are working on?
Mental models are necessarily imperfect.
Personally, I prefer your approach to writing a big huge chunk of code, assuming it is perfect, and testing it at the very end only to find all of the bugs.
[+] [-] Pfhreak|6 years ago|reply
I don't usually try to maintain an entire state in my head, but I do try to make sure each of those blocks is something that I can reason about and hold the entire state of. If I'm finding it hard to figure out what each block does with data, I subdivide and write two comments.
For example, I might write something like:
// Iterate over every transform and do the matrix math
and realize that there's too much for me to hold mentally, so I'll do this:
// Iterate over every transform
// Invert each matrix
// Get the position vector
// Multiply by the character's scalar
... etc.
After that all scans, then I'll start writing the actual code.
[+] [-] ToFab123|6 years ago|reply
[+] [-] antiuniverse|6 years ago|reply
Having heard these claims myself, I firmly believe that developers who eschew debuggers are either severely handicapping themselves out of misguided bravado, or at best are too lazy to invest minimal learning time to establish basic proficiency for huge reward. There's no reason not to avail yourself of extremely powerful tools like that.
That said, maybe don't start with text-mode debuggers...
[+] [-] vegannet|6 years ago|reply
[+] [-] decasteve|6 years ago|reply
Debugging by printing the state of variables too frequently and resorting to line-by-line debugging of every function can be inefficient if it becomes a crutch for the developer. This could be due to a lack of confidence or a lack of understanding — or just habitual.
[+] [-] pmikesell|6 years ago|reply
>> I happen to believe that not having a kernel debugger forces people to think about their problem on a different level than with a debugger. I think that without a debugger, you don't get into that mindset where you know how it behaves, and then you fix it from there. Without a debugger, you tend to think about problems another way. You want to understand things on a different _level_.
[+] [-] mrunkel|6 years ago|reply
I liken developers who proudly say they don’t use debuggers to surgeons who don’t take X-rays.
Sure, you could, but why make your life harder?
[+] [-] marshray|6 years ago|reply
Today, I use a variety of tools and languages and ... hmmm... I don't actually remember the last time I used a debugger on my own code.
[+] [-] professorTuring|6 years ago|reply
Sometimes a bug just doesn't pop when debugging due to differences in the memory management of the debug mode or race conditions in multi-threaded due to different timings...
So, "text-debugging" (like printf("I'm here") debugging) sometimes is the only way to find precious bugs.
Back in the day most of my colleagues only relied in printf debugging. But we were just a bunch of linux geeks developing kernel modules, linux apps and QT apps (QT brought a nice IDE for debugging, truth to be told)...
[+] [-] fergyfresh|6 years ago|reply
I also do not think Senior Engineers have better mental models of these code trivia questions than Junior Engineers do on average. Seniors spend more time on architecture and facilitating Juniors to be more productive so I dk.
I have never been in an interview where piecing out the problem solving and debugging outside of whatever the IDE/editor of choice was frowned upon.
Maybe they just wanted to see you solve the solution in the pair programming window and thought you were copy/pasting your solutions from someone that was helping you off screen.
[+] [-] james_s_tayler|6 years ago|reply
Don't try to get the right answer. Try to get the right interviewer.
[+] [-] codingslave|6 years ago|reply
[+] [-] bm1362|6 years ago|reply
Most interviewers seemed to give an easy problem followed by a medium/hard. I think I succeeded, purely based on years of doing leetcode in the past and having a job at another FAANG-like.
[+] [-] dboreham|6 years ago|reply
I'm going to say that the point about edit compile run cycle is valid, but only in the context of a programming environment that actually has a long cycle. They didn't ask you to use one of those. So ultimately they're wrong or at least so uptight and annoying that you shouldn't go work for them.
[+] [-] mntmoss|6 years ago|reply
[+] [-] iamwil|6 years ago|reply
However, if I'm designing a known thing, where I understand what it's suppose to do; I usually devise a mental model of the thing I'm trying to build before I start committing to code.
Also, having a mental model for the system that you're designing makes it apparent whether someone else can pick up and understand your system.
I also don't really use a debugger. Not sure why though.
[+] [-] meowface|6 years ago|reply
[+] [-] codingdave|6 years ago|reply
But because of those differences, it may have been the right call to not move forward with you. That is not a judgment against you. It is an acknowledgement that you don't work the way they like to work, and may have had a hard time melding into the existing team. Or it may have worked, but hiring managers try to avoid risk where possible.
That is fine. Getting turned down for a job that you were perfectly capable of doing is a reality in this industry. Find another place, try again.
[+] [-] peterhi|6 years ago|reply
Good mental models == Lots of experience
I suspect that the senior that interviewed you would be out of his depth if he moved into another domain. I definitely would. But then again having lots of experience helps you identify the patterns - sorry models - quicker and recognise the problems that you will need to guard against
"Building mental models" sounds like a deliberate cognitive act. To be honest it is more like recognising which part of an existing system would best fit the problem and what changes are need to be made to get a better fit
Which kinda makes it sound a lot less glamorous :)
Anyhow let me repeat:
Good mental models == Lots of experience
[+] [-] bradleyankrom|6 years ago|reply
[+] [-] ebj73|6 years ago|reply
On the other hand, there's the eternal conflict between the waterfall model and the iterative development model. The waterfall model dictates planning (almost) everything ahead, while the iterative model says plan sufficiently far ahead, then start coding, then take a break and re-evaluate, do some more planning, do some more coding, rinse, repeat. I certainly prefer an iterative model, and building prototypes, as you do. But even then, I'd say professional development work is 90% planning ahead, and only 10% actual coding. Probably even less.
[+] [-] nurettin|6 years ago|reply
My plan was to detect new files and file modifications using two async tasks. Whenever a new file arrives, open a stream and read. Whenever a file is modified, find the previously opened stream and read. Every time I read, I push it onto an async Queue which will be consumed by another async task. That would emulate something similar to go channels. Multiple IO tasks, that sounds like a job for async/await. This was my mental model before I coded a single line.
After a few dozen minutes and a single confusing error (I forgot to pass the asyncio loop into the queue and the sleep functions) it was done. I even made it so that it would output the newly appended logs to the stdin of another process or another file. That way I could decouple my log watcher from the program that uses it.
I also opted for detecting file changes by using an async timed loop instead of inotify which would both simplify coding and buffer the new data to reduce read/write operations.
4-5 years ago, I would have experimented with code and tested different ways of accomplishing the same thing. Rather than favoring code experimentation, I had a product idea and implemented a minimal but complete example by reading docs .
[+] [-] usgroup|6 years ago|reply
It’s a lot easier to build in your head than to write code. You might go through a dozen ways of doing something in your mind which would take an age to do in code.
Further, writing code to produce a mental model is limiting because every line you write naively commits you to fewer possibilities. You can be a lot more revolutionary in your head at no added cost and without throwing away hours of work.
[+] [-] zmj|6 years ago|reply
Your mental model tells you that if the system is in a given state, and receives a given input, what "should" happen. You can then look for evidence whether those things happened and narrow down a bug.
It also works the other way. If, given this input, that happened, your mental model tells you what the system state must have been. That can help you work backwards to identify a bug that put the system into a bad state.
[+] [-] blihp|6 years ago|reply
[1] It takes effort in the early years, but like anything else you work at it does become second nature. I often don't consciously decide to build a mental model for simple tasks... it just happens reflexively with minimal time and virtually no effort. Now the model may be crap/wrong... but that's the value if it: as soon as what you're doing/observing doesn't match the model, you know you have a problem and need to course correct.