Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> I think one advantage of computers is that what you produce at the end has to work

No that's not true, that something works is beginner level stuff in many cases. In some cases you're right though, because what has been asked is a super tough problem to crack.

I'll showcase the moments in which the "it has to work" requirement is not enough.

To show this example, I already have to break 2 conventions that are a no no, in my opinion. To other devs: I'm commenting everything (I can't assume someone who works in law to know code, he/she might, he/she might not) and I'm using globals for easier variable reassignment, to ease the example for readability.

Let's get started. The requirement: print 5 to the console of your browser [1].

Open up Chrome Dev Tools (view -> developer -> developer tools -> console)

Type in the following:

  five = 1 + 1 + 1 + 1 + 1 //compute 1 + 1 + 1 + 1 + 1 and save it in the computer with the name five
  console.log(five) //outputs 5

  five = 0 //reset

  // loop 5 times, and every time the pc adds 1 to the saved part of the computer named five
  for (loopCount = 0; loopCount < 5; loopCount = loopCount + 1) {
    five = five + 1
  }
  console.log(five)

  five = 5 //save the number 5 in the computer with the name five
  console.log(five) //outputs 5

  console.log(5) //also outputs 5

  FIVE = 5 //all-caps is a convention for that something is a constant, like a constant number such as 5

  console.log(FIVE) //outputs 5 as well, and also probably the way programmers want to see it.
Some of these methods have been really silly and if you catch that during an interview, you won't pass. People can point out that most of these methods happen one way or another, but not when you have a simple requirement such as "output 5". In fact, if that truly is the requirement, then I would go for:

  console.log(5)
And I'd consider the first 2 entries totally nonsensical (unless I asked it with a funny voice or asked them to do whatever / be creative), the 4th entry slightly questionable but fine and the 5th entry fine.

[1] Read the following link to have some context about what the JavaScript console is. Don't worry about not understanding the code or the technical terms. You're not missing anything, as far as context is concerned, I checked. Read it until the first paragraph of "Running JavaScript".

https://developers.google.com/web/tools/chrome-devtools/cons...



A question: is really fifth the best ? Seeing FIVE=5 always irks me, as it's an extra indirection for nothing. What do I gain with it ? The ability to change it to FIVE=6 ? OUTPUT=5 maybe but then console.log(OUTPUT) gains you nothing. Maybe NB_LEGS=5 and console.log(NB_LEGS)


This is why I put the word probably in the comment and why I stated that for this particular requirement I actually don't think this is the best approach.

You can always screw around with code, redefining code blocks in C would be fun to do.

But I did have situations where FIVE = 5 changed to FIVE = "five" or FIVE = "5" or FIVE = "vijf" (Dutch) or FIVE = "....." (ok, the last one is hypothetical, but maybe you want five points because you tick them off in a for-loop? I've seen something clever-ish like that in a particular fun Fizz Buzz example).

The layer indirection is indeed a classical trade-off that you do or do not want to make.

Regardless of that, your question proves the point I want to make to kbos87.


Just continuing the discussion, I fully agree with you on principle. I stand by the fact that FIVE is not really a good constant. You will not change five unless you also change the code to handle it, so it just adds another part to modify. And if you never intend to change it or reuse it.. well the only point for making its symbol is to give it meaning, which FIVE does not. But well at a point it's like discussing colours.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: