Live data from Hacker News

When hiring developers, have the candidate read existing code

freakingrectangle.wordpress.com

181–190 of 565 posts

Re: When hiring developers, have the candidate read existing code

#181
post #155

Earlier quoted context omitted.

Both NULL and a unique pointer value can be safely passed to free() :-). Answers to this question taught me about the candidates taste and understanding of good API design :-). Both NULL and "unique pointer" are correct answers, but all modern implementations only chose to return one of these. My follow-up question is "why ?" :-).

When malloc returns NULL, it's saying that there was some error. However, IIRC the only malloc error is ENOMEM. It's unclear why malloc(0) would run into that. (If malloc(0) did run into an ENOMEM, then NULL would be required, but the result of malloc(0) need not tell you anything about subsequent calls to *alloc functions. However, there is a possible malloc guarantee to consider.) There's some interaction with real…

That's right ! Great answer.

malloc() is a very old API. A more modern version would probably looks like:

err_code malloc(size_t size, void **returned_ptr);

The current malloc() overloads the return to say NULL == no memory available/internal malloc fail for some reason and as far as the standard goes, allowing NULL return if size==0.

So if you get NULL back from malloc, did it really mean no memory/malloc fail, or zero size passed in ?

glibc and all implementations distinguish the two by allocating a internal malloc heap header, but with internal size bookkeeping size of zero, returning a pointer to the byte after the internal malloc heap header.

The only valid things you can do with the returned pointer is test it for != NULL, or pass it to realloc() or free(). You can never dereference it.

Returning a valid pointer to NO DATA is what all modern implementations do when a size==0 is requested.

In an interview situation, discussions around all these points are very productive, telling me how the candidate thinks about API design and how to fix a bad old one, whether they know anything about malloc internals (which is essential as overwriting internal malloc header info is a common security attack), and how they deal with errors returned from code.

Remember, it was only my warmup question :-). Things get really interesting after that :-) :-).

Re: When hiring developers, have the candidate read existing code

#182

Earlier quoted context omitted.

No, that's not true. If you know anything about C and writing secure code (which I what I was probing for) you know about using malloc(). You know because you have to know something about the internals of memory management. Imagine you just read a 4 byte value off the network, and it's part of a protocol that specifies how many more bytes there are to read. You might (in error, ahem... :-) pass that value to malloc()…

I'm not a C expert and I'm stumped but curious. What does malloc(0) return and why is that important?

See the replies below. Someone just submitted a really comprehensive answer !

Re: When hiring developers, have the candidate read existing code

#183
post #172
post #144

Earlier quoted context omitted.

Agree completely. Also, good commenting skills take years to master.

> good commenting skills take years to master As a newer developer, it’s nice to hear you say that. I often find that I spend more time deliberating over comment wording and variable names than I spend writing the code itself!

You are on the right path! Most new devs don't even comment at all (and plenty of senior devs). As for variables names, I wouldn't fret about that. Yes, 'foo' isn't a good var name but the exact name doesn't matter as much as one would think.

Re: When hiring developers, have the candidate read existing code

#184

Earlier quoted context omitted.

No, that's not true. If you know anything about C and writing secure code (which I what I was probing for) you know about using malloc(). You know because you have to know something about the internals of memory management. Imagine you just read a 4 byte value off the network, and it's part of a protocol that specifies how many more bytes there are to read. You might (in error, ahem... :-) pass that value to malloc()…

That's one way to look at it. Another might be that if you write code that passes unsanitised input to anything and can get that code through testing and review then maybe you're not the kind of organisation that a candidate who knows about security wants to work for. In the end this is still a language lawyer question. It's a technicality that should never be relevant. If it is you've already gone wrong several time…

Many protocols read values of a network to specify how much is left in the packet (it's how packet boundaries are usually encoded, specifically in SMB1/2/3). So yes, no matter how paranoid you are you're eventually going to have to pass that value to something in your code :-).

Re: When hiring developers, have the candidate read existing code

#185

Earlier quoted context omitted.

No, that's not true. If you know anything about C and writing secure code (which I what I was probing for) you know about using malloc(). You know because you have to know something about the internals of memory management. Imagine you just read a 4 byte value off the network, and it's part of a protocol that specifies how many more bytes there are to read. You might (in error, ahem... :-) pass that value to malloc()…

>If you don't know or can't guess because you don't know how malloc() works, then you're not the person I'm looking for. Yea. It would be that way.

Well yes. I'm looking for competent C coders. Competent C coders know how malloc works. It goes with the territory.

Re: When hiring developers, have the candidate read existing code

#186

Please comment your code, when it is is necessary. I don't need "we need to loop here from 1 to 50", I need "we have to rate-limit this function to under 60 transactions per second due to hardware requirements", etc. If you are putting "magic numbers" anywhere, COMMENT it as to what that number is, why you chose it, etc. I'm 30 years into this game and I still come across code that takes way too long to reason about.

You can just name the variable to explain what the number is, and if you need more info, there should be a "Why?" doc somewhere explaining the context generally, WITHOUT tying itself directly to the current choice (otherwise you'll have to update the "Why?" doc every time you update the number in the code, which is almost certain to go out of sync). No need to interweave documentation and code, in most cases. Sometim…

>No need to interweave documentation and code, in most cases.

Locality is the reason, and it's a good reason. Of course, I don't want to see paragraphs of explanation inline, but a one-line comment giving context for some choice that might be confusing is much better and faster to work with than a separate WHY doc.

Re: When hiring developers, have the candidate read existing code

#187

I think we should just jump to the end game and make interviewees do ultra man triathlons while solving differential equations. Then at the finish line, they knife fight each other to find the one we let go to the next stage in the interview. That way we can be sure who has stamina and the best competitive programming problem solving skills. Something like squid game will truely find the A players. I mean, we wouldn’…

I propose, as well, that we “weed them out” with a week of hunger games, battle bots, and rocket launch tests.

In seriousness, through a decade of experience in AI, I have found that most “famous” and “exotic” algorithms that software engineers love testing (e.g. OP’s references to sorting, tree searching, and recursion) come up exactly never in my day-to-day.

Please, limit testing to the actual data structures and algorithms that dominate 99% of the proposed work. If the proposed work is not in a field you are truly an expert at, then don’t be responsible for technically interviewing this person.

In my branch of AI, if I’m not getting solely evaluated on my ability to work with tensors, vectors, graphs, …, then I realize there is no AI group at this company (or it is inferior in its autonomy). There are so many extremely useful specializations in real-world moonshots, that would otherwise get bludgeoned upon any interview within a general purpose software engineering culture.

Re: When hiring developers, have the candidate read existing code

#188

Please comment your code, when it is is necessary. I don't need "we need to loop here from 1 to 50", I need "we have to rate-limit this function to under 60 transactions per second due to hardware requirements", etc. If you are putting "magic numbers" anywhere, COMMENT it as to what that number is, why you chose it, etc. I'm 30 years into this game and I still come across code that takes way too long to reason about.

Don't do it unless you are going to treat keeping comments up to date the same way you treat keeping the code up to date. I've lost count of the number of times I've seen comments that were just wrong -- they applied to the code as it was written years ago, not today. Write good code, and it should be obvious how it works. Use comments sparingly and only when things are not obvious. Update the comments when you chang…

Code comments that are wrong can be very useful. I've found many bugs that way. If I find a comment that states an assumption that does not hold in the code, that's the first place I analyze when looking for a bug. And very often it turns out the comment was right, but the code was wrong.

Re: When hiring developers, have the candidate read existing code

#189

Except most commercial code bases are tangled rats nests and it would take a substantial effort to understand anything beyond the most basic details. My view of interviewing is it should require no special effort on the part of the engineer to demonstrate their skills. One simple way to do this is to have the engineer explain an open source project they've already built. Obviously this doesn't work for people who hav…

This is so funny. :) Thanks for a good laugh.

Re: When hiring developers, have the candidate read existing code

#190

Please comment your code, when it is is necessary. I don't need "we need to loop here from 1 to 50", I need "we have to rate-limit this function to under 60 transactions per second due to hardware requirements", etc. If you are putting "magic numbers" anywhere, COMMENT it as to what that number is, why you chose it, etc. I'm 30 years into this game and I still come across code that takes way too long to reason about.

You can just name the variable to explain what the number is, and if you need more info, there should be a "Why?" doc somewhere explaining the context generally, WITHOUT tying itself directly to the current choice (otherwise you'll have to update the "Why?" doc every time you update the number in the code, which is almost certain to go out of sync). No need to interweave documentation and code, in most cases. Sometim…

This would not work based on my experience

What actually would happen is the next person comes along and changes the code and doesn’t update the external doc because it’s probably buried among 100 other pages on confluence and they don’t even know it exists

If the comment is in the code they will see it and update accordingly

Post reply on HN