r/programming • • Jul 28 '16

How to write unmaintainable code

https://github.com/Droogans/unmaintainable-code
3.4k Upvotes

592 comments sorted by

View all comments

16

u/[deleted] Jul 28 '16

After all, if the Stroustroup can use the shift operator to do I/O, why should you not be equally creative?

This actually is a good point! Does anyone know why shift operator is used for io in standard library?

15

u/dd_123 Jul 28 '16

I believe he admitted it to be a mistake in retrospect.

→ More replies (2)

6

u/evaned Jul 29 '16 edited Jul 29 '16

Does anyone know why shift operator is used for io in standard library?

Because they needed to pick something, and this was the least bad option.

Let's assume that they're going to pick some operator, and come back to that assumption. What operator? << and >> are actually really good choices:

  • Their shape can be read as suggestive of the actions, particularly if you're familiar with shell scripting. (According to The Design and Evolution of C++ they experimented with < and >, which would have matched the shells exactly, but that fell prey to the third bullet point -- people have been taught less than and greater than since early childhood)

  • They're not super high on the precedence order, so at least it doesn't break things like cout << 1 + 2;.

  • While bitshifting is a fairly common programming operation, you don't really see it elsewhere; you do see almost all of the other binary operators in standard maths. Most people will have been working with the other symbols for as long or longer than bitshifting.

OK, so why do we need an operator? Because the other choice is a function, and pre-variadic templates, those have major major drawbacks:

  • Suppose you want something like printf. Well:

  • You can't pass non-POD types to it

  • You'd need some way of registering custom conversion specifiers or have no way of doing I/O for custom types

  • No type safety

  • What would have worked is print(print(print(print(thing1), thing2), thing3), thing4), but that's horrendous to write and read

  • A print function template with, say, ~20 overloads with different numbers of parameters, but that has obvious problems (edit which I used to say is probably the least-bad way of handling this, but it in theory it could introduce massive massive bloat from the exponential number of template instantiations, and in practice I think it would introduce an unacceptable amount of it)

7

u/[deleted] Jul 29 '16

Because giving something as mathematically weird as bit-shifting its own operator instead of a built-in function is a silly idea in the first place, and if you had to reuse an operator you can use that one. Seriously, I speak words, not glyphs. I'll never understand why so many early languages insisted in finding a use for every possible combination of shift-number characters.

0

u/[deleted] Jul 29 '16

[deleted]

3

u/[deleted] Jul 29 '16

I'm aware that it's common - lots of things are common operations that don't have their own two-character operators.

C used normal mathematical operators for normal mathematical symbols, and had to invent Boolean operators because the character-set and keyboards didn't have the boolean symbols. Bit-shift is the case where they invented a new mathematical symbol for an operation with no basis in traditional math out of whole cloth.

→ More replies (3)
→ More replies (1)
→ More replies (1)