4

I very often find myself in situations where I should have opened a parentheses, but forgot and end up furiously tapping the left arrow key to get to the spot I should have put it, and doing the same to get back to where I was - that, or removing one hand from the keyboard to do it with the mouse. Either way, it's a stupid mistake on my part and a poor use of time going back to fix it.

For example, if typing something like

var x = 100 - var1 + var2;

I might get to the end of the statement and realize I wanted to subtract the sum of var1 and var2 from 100 and not add var2 to the difference of 100 and var1.

I can't really expect an IDE to prevent my mistakes, but I was thinking there could be a simple enough feature that would save time when they're made. Specifically, some kind of function that, after a closing parenthesis is added where there isn't an opening one, would start ghosting in an opening parenthesis at different statements and allow the user to switch between them.

For example:

Say you have the following statement:

var x = oldY * oldX + newY / newX - left - right;

If you put a closing parenthesis after right and pressed the shortcut, the IDE would do:

var x = oldY * oldX + newY / newX - ( left - right);

press left, and then:

var x = oldY * oldX + newY / ( newX - left - right);

...then:

var x = oldY * oldX + ( newY / newX - left - right);

Anyway... Does this feature exist? If not, should it exist? What do senior programmers do when this happens?

mowwwalker
  • 1,159

2 Answers2

1

Does this feature exist?

No, IDE should not (and can not) automatically fix your code, because it has no way to know whether the formula you use has this or that arithmetic operation.

What do senior programmers do when this happens?

Serious programmers (not only senior) write proper unit tests, where such mistakes are easily detected.

BЈовић
  • 14,049
1

Senior programmers use the Next/Previous word, start of line end of line hot keys to navigate the code - if you ever get a chance, watch a vi expert in action. These things 'just happen' - you don't think "left, left, left, left, Brace, right right right", you think "brace over there" and your fingers do it for you.

Senior devs also don't worry too much about the time taken to type. We are not the Typing Pool - paid for word/minute. We add a parenthesis (or any artifact that makes the code readable, let alone correct) when needed, as we know the time to write it is trivial compared the time it will be read thousands of times.

Further senior developers would not write

var x = oldY * oldX + ( newY / newX - left - right);

As it's very odd code that does not 'look' right, meaning even more time will be spent reading it and trying to second guess what the original write wanted it to do (Any developer should be able to work out what it does).

mattnz
  • 21,490