News for hackers. Stuff that matters.
Re:How to win the coding interview by sundvor on Monday May 30, 2016 @11:28PM (#11804368)
Very interesting article, thanks for writing! I have but one, eh, comment:

> Your code should be commented

Sure.. but:

  > * @param {string} stringToTest - the string to test.

  >   // make sure we have a string.
  >    if (typeof stringToTest !== "string") {
  >

  > function isPalindrome(stringToTest) {
  >  ...
  >    // if we get here, it's a palindrome
  >    return true;
Do we really need to explain that stringToTest means string to test? I for one find such low level comments almost completely redundant. I prefer strong variable naming and other ways of creating clean code to littering the code with comments that the code expresses naturally.

Comments should IMHO be reserved for explaining special complexities, any particular gotchyas, etc.

Re:How to win the coding interview by bhntr3 on Tuesday May 31, 2016 @03:25AM (#11805214)
I came here to make the same comment (pardon the pun.)

This article takes such an expert tone. But it's just opinion. For me, that comment would be a red flag.

He also says to write the most efficient code possible. Then doesn't. See the comments at the end of the post.

Overall, I think what this shows is that as an interviewee, the best thing you can do is try to get an idea of the interviewer's expectations as quickly as possible. People love saying here that it's not a test scenario. But it is. The interviewer has a preconceived notion of what you should and shouldn't do and you need to meet that notion.

As an interviewer, recognize it's easy to think you're smarter than the other person when you already know the answer. Don't be pompous. Don't assume you know better. Try to set expectations very clearly. Try to really listen to the interviewee and understand why they're making the choices they are.

Personally, I would be frustrated by this interview. I feel like this article is a thin justification of a lot of standard interview practices that don't actually work. It and his further comments take a condescending tone that makes me worry he wouldn't be open to approaches he hasn't seen.

Reminds me of a time an interviewer basically gave up on an interview because I told him there were O(N) algorithms for sorting strings with a fixed alphabet. He said "Best case sorting is O(nlogn)" and I could hear in his voice that for him the interview was over. I knew what radix sort was but in the moment I couldn't actually explain or implement it without looking it up and things just went downhill from there.

Re:How to win the coding interview by tamana on Tuesday May 31, 2016 @10:58AM (#11806869)
You might get have done better if you remembered the criterion correctly. Almost every data type used in computing is represented by non-fixed-length strings over a fixed alphabet (usually {0,1}).

Radix sort is O(n) in log(dictionary size) which is only a useful bound when the data set is larger than the dictionary (this is a severe but not fatal requirement. Some datasets have many duplicate values, and anyway in practice log(n) factors in real world systems are practically constant factors: an 1log(n) algorithm will be slower than a 100(1) algorithm.)

You can misstate a theorem, or forget the proof, OK, but doing both is poor form.

Re:How to win the coding interview by bhntr3 on Thursday June 02, 2016 @12:39AM (#11820408)
You're right. I misstated it here and was clearly lying when I said I understood it.

Another good tip for interviewees: If you don't really understand something well enough to explain it, sometimes it's better not to bring it up than to introduce it and look silly. Sucks. But you're more likely to lose points than gain them for just kind of knowing something exists.

Also applies to hacker news comments ;)

Re:How to win the coding interview by fenomas on Tuesday May 31, 2016 @12:13AM (#11804519)
Couldn't help but chuckle at this:

    'use strict'; // avoid ambiguity and sloppy errors
If the line that starts (or ought to start) virtually every JS file you ever write can't stand without explanation, one wonders what can!
Re:How to win the coding interview by twblalock on Tuesday May 31, 2016 @12:20AM (#11804544)
n++; // increment n
Re:How to win the coding interview by hvidgaard on Tuesday May 31, 2016 @02:36AM (#11805048)
I've had students that did this. It's a real pain to read code like that, and the 2nd time they handed it in, it was an automatic fail. Comments explain assumptions, and often "the why", never "the what" unless it's obscure because of optimizations.
Re:How to win the coding interview by BinaryIdiot on Tuesday May 31, 2016 @04:28AM (#11805403)
> It's a real pain to read code like that, and the 2nd time they handed it in, it was an automatic fail

That seems incredibly heavy handed and not helpful in the learning process. I was a professor's aid for several programming courses and many students simply made comments like that constantly because it helped them drill it into their head what it did. It's a little annoying but it doesn't hurt the readability.

Automatically failing them on a second turn-in seems very anti-student and anti-learning to me. Teaching through negative reinforcement is the worst way to teach.

Re:How to win the coding interview by kagamine on Tuesday May 31, 2016 @06:43AM (#11805775)
I felt when I was a student that some teachers just wanted everyone to know how clever they were and sometimes that could be achieved by putting someone else down. Someone who wasn't in a position to talk back. The good teachers gave criticism, pointed to the code standards for the course and were fair in their marking. The best teachers asked questions that made the students produce better work and took time to explain and discuss. They all knew which category they were in.
Re:How to win the coding interview by hvidgaard on Tuesday May 31, 2016 @08:17AM (#11806083)
You can see my reply to the parent comment. I always sit down and talk with the students, but for us to talk about the code, we need to be able to read the code. Unneeded comments make it significantly harder to read code.

First time we talk about why I don't want "the what" comments, only "the why" comments. If they still do it in the next handin it's a fail unless they've explicitly provided me with a reasonable reason to do it.

Re:How to win the coding interview by tamana on Tuesday May 31, 2016 @11:00AM (#11806885)
In science, there is no such thing as why, there are only deeper levels of what. And those levels are subjective and relative to personal experience.

Your comment merits a failing mark, downvotes, because you made the same subjectively wrong statement twice.

Re:How to win the coding interview by hvidgaard on Wednesday June 01, 2016 @02:32AM (#11812438)
Programming is not a science. "Pad to multiple of 256 otherwise libX will fail" is exactly what a comment is for, and it is the "why", no the "what" - the what is already readable in the code.
Re:How to win the coding interview by crispyambulance on Tuesday May 31, 2016 @09:48AM (#11806462)
I think folks are getting hung up on nitpicking the comments. I never heard of anyone getting rejected strictly because of a stray unnecessary comment in a code test for hiring.

Any interviewee is going to make assumptions about what the interviewer wants. Some of these assumptions will be wrong and some will not be worth the time to ask about (like commenting style). If the interviewee solved the problem and/or displayed a desirable level of competence that's good enough. Interviewers do the coding exercise merely to screen out people that have grossly misrepresented their skills.

On the internet folks like to imagine they're in a position to "immediately reject" a job applicant because of a single "red-flag" that indicates some profound shortcoming. This is rarely the case.

Re:How to win the coding interview by hvidgaard on Tuesday May 31, 2016 @08:10AM (#11806063)
I sit down with them individually and talk about the handin. For any meaningful feedback to happen, we need to see the code. This is not programming 101, but courses that require you to have passed advanced programming, so I expect them to hand in without comments like this:

   i++ //increment variable because used clicked
It's harmful for readability and takes a lot longer to read. If they hand me a second handin with such comments, after our first session, I'm not going to tell them again.
Re:How to win the coding interview by vkjv on Tuesday May 31, 2016 @09:42AM (#11806433)
While I might not agree with the GP that this should be an automatic failure, I do think it is very important to emphasize to students that when the code is working, they are not done. They still need to go back, re-format, clean things up, and possibly add tests.

Working is the lowest bar of quality.

Re:How to win the coding interview by MustardTiger on Tuesday May 31, 2016 @10:36AM (#11806737)
> It's a little annoying but it doesn't hurt the readability.

I find it absolutely destroys readability. It is incredibly hard to follow code written twice.

Re:How to win the coding interview by rickdale on Tuesday May 31, 2016 @10:37AM (#11806741)
I agree with the other commenters that say thats a bit harsh to completely fail them. I was in my third CS class in college and I left out a semi colon on a test and my teacher said, none of the code would work from here, so the answer is just 100% wrong. Even though the rest was 100% correct. I made the argument that if I was sitting at a computer, that would have been less than a 5 second mistake, or something I would have noticed in the syntax highlight, but he insisted it was a complete fail. I never understood that, but I think it relates back to the days when the professor learned on punch cards and one small error was more costly than today.

Another thing is, when I was learning to program, commenting was something I used to keep things light. Sometimes inside jokes or random bits of code that were terrible that would make me laugh once I had the final solution. In my opinion, unless the assignment was designed to teach proper comments, why not let students be creative with their comments, even if its redundant to you.

Re:How to win the coding interview by hvidgaard on Wednesday June 01, 2016 @05:32PM (#11818362)
Failing for missing semi colon seems harsh, but in reality, when students are told to run their code before handing it in, a missing semi colon tells you exactly that the students didn't run the code. If a student fails to meet simple basic requirements, they cannot expect some leeway.

That said, in a test when hand writing code it should only be pseudo code. Expecting learners to write syntax correct code by hand is stupid.

    > why not let students be creative with their comments, even if its redundant to you.
Because I have to read every comment, decide if it's relevant or not, and delete it if it isn't. "End of line comments" on every single line is not helping anyone - "using for loop to iterate over the array" is bloody useless, because it's right there in the code. Keep in mind this is CS students at least 2 years in.
Re:How to win the coding interview by rickdale on Wednesday June 01, 2016 @08:03PM (#11819347)
Keep in mind this is CS students at least 2 years in.

I wasn't keeping that in mind. Thats makes a lot of difference. Pretty sure I studied under the same rules.

Re:How to win the coding interview by hvidgaard on Thursday June 02, 2016 @02:22AM (#11820717)
I wasn't exactly clear on that, to be fair.
Re:How to win the coding interview by vsl on Wednesday June 01, 2016 @02:52AM (#11812509)
Because he's trying to teach them how to write good, readable, maintainable code.
Re:How to win the coding interview by di4na on Tuesday May 31, 2016 @05:16AM (#11805550)
As someone that was a student until last year, the real hard problem is that we have teachers that want this type of comments. Having different standards for different teachers the same day is a lot of context switching...

A better idea would be to get a standard code style for all course...

Re:How to win the coding interview by ogsharkman on Tuesday May 31, 2016 @08:50AM (#11806220)
My school actually had standard coding styles for each language they taught. Naturally, they all wanted a metric fuckton of comments (luckily the TAs didn't care as much about the comments part of those guides).
Re:How to win the coding interview by heartbreak on Tuesday May 31, 2016 @11:03AM (#11806896)
Particularly with beginners, I think there's a lot of "What were they thinking when they wrote this??" going on when a professor or TA reads code that is completely unintuitive (Take a look at /r/learnprogramming for some examples). Having comments everywhere makes it easy to say, "Oh! S/he was trying to reset all of the variables in the program by calling main(null); (actual example from the subreddit). How would you ever know that without a comment explaining why main(null); is called in the middle of a class?
Re:How to win the coding interview by scrabble on Tuesday May 31, 2016 @07:25AM (#11805894)
I have had a teacher that said you needed to comment every single line because that's how code is written in the real world.

I told him he was wrong and argued with him on more than one occasion. In the end, I just did it his way because there wasn't a good alternative.

Re:How to win the coding interview by pklausler on Tuesday May 31, 2016 @12:36PM (#11807574)
> In the end, I just did it his way because there wasn't a good alternative.

Ironically, that is quite a bit like the real world.

Re:How to win the coding interview by jfoutz on Tuesday May 31, 2016 @10:01AM (#11806530)
Sounds like a job for a perl(or whatever) script.

Might be fun to do some light ad hoc parsing.

Also, with that rule, I can't resist

    // this line left intentionally blank.
Re:How to win the coding interview by kevhito on Tuesday May 31, 2016 @11:01AM (#11806887)
Ahem:

    // this line left intentionally non-blank.
Re:How to win the coding interview by dagw on Tuesday May 31, 2016 @06:51AM (#11805798)
Did you sit down with student and try to understand why they wrote comments like that, explain exactly why comments like that where bad and try to show them a better way, or did you just fail them and hope they'll work out all those things by themselves?
Re:How to win the coding interview by hvidgaard on Tuesday May 31, 2016 @08:11AM (#11806065)
Always after the first such handin. The second, not so much.
Re:How to win the coding interview by kriro on Tuesday May 31, 2016 @10:21AM (#11806655)
Why not run a parser that removes all comments over all code that is turned in. It's easier to read the code with no comments and if there are no comments the sections that should be commented really pop out (you can then check to see if there are indeed comments in the original file).

Simplistic comments actually help a lot when learning (even if they are just a reinforcement). Quite often our students write the comments first and then fill in the code which is perfectly fine.

It's also more interesting to let them freeflow it and then explain later why it might not be a good idea. The learning effect is stronger if they can go back and read their own overcommented code.

Re:How to win the coding interview by heartbreak on Tuesday May 31, 2016 @11:04AM (#11806906)
Sometimes the comments are valuable, even when every line is commented. A syntax highlighter scheme that had lower contrast for the comments would be a better workaround.
Re:How to win the coding interview by fao_ on Tuesday May 31, 2016 @06:56AM (#11805810)
For my course it is part of the criteria. They want every single line commented.

It's a nightmare of irritation.

Re:How to win the coding interview by Merad on Tuesday May 31, 2016 @07:18AM (#11805875)
Having been a TA for CS1 classes, I can understand the rationale behind it to some degree. It's ridiculous how many students go online and ask for someone to do their homework or even copy chunks of code off of stack overflow. Forcing them to explain every line is one way of judging how well they actually understand what they're writing (if they do at all). Of course it's going to be annoying to anyone who already has the slightest experience... but what can you do?
Re:How to win the coding interview by mathgeek on Tuesday May 31, 2016 @08:38AM (#11806170)
> It's ridiculous how many students go online and... copy chunks of code off of stack overflow.

You could easily replace "students" with "coders" and that statement is still true. Just an observation.

Re:How to win the coding interview by hvidgaard on Tuesday May 31, 2016 @08:18AM (#11806092)
For the first programming courses I can understand the commenting, but I've been TA for advanced courses where many of the students have passed the compiler class. They don't need comments anymore, and it really detract from the readability of the code.
Re:How to win the coding interview by bbcbasic on Tuesday May 31, 2016 @12:27AM (#11804564)
and return the original value
Re:How to win the coding interview by twblalock on Tuesday May 31, 2016 @01:17AM (#11804750)
++n; // don't touch this unless you know what you are doing
Re:How to win the coding interview by bbcbasic on Tuesday May 31, 2016 @01:42AM (#11804843)
n = ++n; // Extra thread safety
Re:How to win the coding interview by ry_ry on Tuesday May 31, 2016 @02:52AM (#11805096)
$.incrementNumber(n, 1); // From stack overflow
Re:How to win the coding interview by bbcbasic on Wednesday June 01, 2016 @05:42PM (#11818455)
Confession. Have written many comments stating "copied from stackoverflow http://..."
Re:How to win the coding interview by danappelxx on Monday May 30, 2016 @11:40PM (#11804406)
I agree. If a comment can be automatically generated by a computer (stringToTest -> 'the string to test' can be automatically generated), it should not be a comment at all.
Re:How to win the coding interview by billsparks on Monday May 30, 2016 @11:41PM (#11804408)
That particular comment (the @param) is for JSDOC. It's so that one can generate documentation for the code. http://usejsdoc.org/about-getting-started.html
Re:How to win the coding interview by sundvor on Monday May 30, 2016 @11:51PM (#11804436)
Ah, thanks for that.

I'm a Vic20 + Commodore 64 Basic -> Amiga Assembly -> ColdFusion -> C# dotNetCore person myself; mostly back end, but I'll check out JSDoc for when going down that path again next.

Re:How to win the coding interview by marlag on Tuesday May 31, 2016 @01:52AM (#11804882)
Interesting learning streak. I took the same route, started on Vic 20 at the age of 10, but skipped on the amiga assembly, something I suffer from on a daily basis, so good choice.
Re:How to win the coding interview by sundvor on Tuesday May 31, 2016 @04:27AM (#11805400)
Thanks! :) The Vic 20 at about the same age here too; assembly was great, but staying for well over a decade on ColdFusion just because there was a lot of work in it (and, comfortable) was not.

I forgot to mention some PHP thrown in there, but I'm glad to have been making the transition to C# in the past year. Working with a strongly typed language is quite a revelation, and some fantastic things are happening with the language and framework these days too (dotNetCore).

Re:How to win the coding interview by LionessLover on Tuesday May 31, 2016 @02:25AM (#11804995)
When used in the WebStorm IDE JSDoc type comments give you type checking (if the type-inspection is enabled) and autocompletion suggestions.
Re:How to win the coding interview by nulagrithom on Tuesday May 31, 2016 @12:48PM (#11807699)
The comment is still optional. You don't have to give @param a description. @param stringToTest would suffice. http://usejsdoc.org/tags-param.html
Re:How to win the coding interview by meowface on Tuesday May 31, 2016 @02:31AM (#11805028)
"// throw if we didn't get a string" before a throw statement two lines away from "make sure we have a string" would annoy the hell out of me in production code.
Re:How to win the coding interview by bobbles on Tuesday May 31, 2016 @02:40AM (#11805063)
I can see a case for it if during the next 2 years of development more code is added between 'make sure' and 'throw...' because you do not want to want to worry about refactoring comments when code gets moved...
Re:How to win the coding interview by dclowd9901 on Tuesday May 31, 2016 @12:38AM (#11804598)
Right? I know developers who swear by comments and ones that swear by self describing code. You can't assume you're interviewing with one or the other.
Re:How to win the coding interview by deciplex on Monday May 30, 2016 @11:43PM (#11804415)
I agree. Too many comments makes it hard to sort out which are the comments that actually provide insight to how the code works (or is supposed to) from the comments that are just bullshit the author put there so he could say he puts a lot of comments in his code.
Re:How to win the coding interview by wp1 on Tuesday May 31, 2016 @03:00AM (#11805122)
In a whiteboard interview, comments can/should just be said out loud to the interviewer.
Re:How to win the coding interview by pc86 on Tuesday May 31, 2016 @08:15AM (#11806075)
Comments should explain the why of code, not the what or how.

From a technical standpoint you should read uncommented code and more often than not be able to tell exactly what it does (barring a lot of poorly named methods or similar architectural issues). Any comments should simply explain the business rules or decisions that led to that code. Maybe you are knowingly violating a best practice for your framework or language because following it would break business logic down the pipeline or something. This is the perfect time for a comment explaining that so a well-meaning junior developer doesn't refactor it six months from now.

Re:How to win the coding interview by EpicEng on Tuesday May 31, 2016 @01:38AM (#11804827)
And perhaps the interviewer should have been more explicit in their requirements. I have to admit I would never think to comment whiteboard code (and my production code would not contain comments like that).