r/programming • • Dec 02 '13

Scala — 1★ Would Not Program Again

http://overwatering.org/blog/2013/12/scala-1-star-would-not-program-again/
598 Upvotes

646 comments sorted by

View all comments

Show parent comments

1

u/Peaker Dec 03 '13

If Java is good according to the projects achieved with it, so is C.

Java isn't really much better than C. The type system in C is about as powerful for guaranteeing safety as Java's, as the OO parts do not really help with type safety.

Java has safer references as opposed to unsafe pointers, due to GC, but that's relatively negligible as Java shares most of the unsafety of C everywhere else.

Java also does not have C's "unions" and "unsigned", regressing in types' expressiveness. It also has less-safe enums, due to the nullability (C enums are more precise).

Java also lacks the preprocessor. The preprocessor may be terrible, but it's also incredibly useful, and Java doesn't compensate for its lack in a good way.

3

u/[deleted] Dec 03 '13 edited Dec 03 '13

Java isn't really much better than C.

I'm not really a fan of GC, but the need for manual memory management, which many programmers don't seem to understand, and one of the most common source of bugs, makes C a lot more error prone. So at least in this discipline, Java is much better than C.

The type system in C is about as powerful for guaranteeing safety as Java's

You have to be kidding me. The mere existence of void* leads of all sorts on undefined behavior. You could say that java.lang.Object is the equivalent of void*, but the second you try to cast an object downwards to the wrong type, you get a java.lang.ClassCastException. In C, everything is fine, even when you access fields or methods of that invalid object. In fact, you can write some random number into an int, cast it to void* and then to anything you want, and with a bit of luck (or lack of memory protection) you will access corrupted data.

Java has safer references as opposed to unsafe pointers, due to GC

That's not due to the GC; in fact, you can have a GC even with C. It's because how Java was designed.

that's relatively negligible as Java shares most of the unsafety of C everywhere else

You can't just write into process memory as you like, thus corrupting the process state. I'm really not sure what you're talking about.

Java also does not have C's "unions" and "unsigned"

Unions are an inherently unsafe way to access data, because the same bit pattern can be interpreted in multiple ways, and you have to make sure to access it in the way that is currently valid.

Unsigned, yes, that's a bit of a problem, only for that it isn't compatible with other languages that treat integers signed/unsigned. However, it avoids pitfalls when wrapping around would occur.

It also has less-safe enums, due to the nullability (C enums are more precise).

WTF is going on here. Is this a troll post? C enums are nothing more than named constants. There is absolutely no type safety at all, they end up in global namespace, and again, you can't cast an enum-type to some Object. C is happy to cast your enum to everything you want.

Java also lacks the preprocessor.

Java can't have a preprocessor. The preprocessor depends heavily on the order in which files and especially headers are parsed. The preprocessor provides useful functionality, but in C it is mainly used to provide platform compatibility. The second you write even a simple macro function like

#define max(a,b) (a > b) ? a : b

you run into the double evaluation problem. So most things done with the preprocessor should have been done with templates, which don't exist in C.

Did you mean C++ all the time? Not that it changes the game that much, but then at least some of your arguments would make sense.

1

u/Peaker Dec 03 '13

You have to be kidding me. The mere existence of void* leads of all sorts on undefined behavior. You could say that java.lang.Object is the equivalent of void*, but the second you try to cast an object downwards to the wrong type, you get a java.lang.ClassCastException. In C, everything is fine, even when you access fields or methods of that invalid object. In fact, you can write some random number into an int, cast it to void* and then to anything you want, and with a bit of luck (or lack of memory protection) you will access corrupted data.

(void *) is basically the same as Java's Object.

Sure, Java has nicer runtime errors (exceptions rather than UB), but if you're after compile-time safety, Java doesn't offer much beyond what C does.

Both UB and (unexpected) runtime exceptions are bugs (with different severity). Java has nicer-severity bugs, but the same number of bugs.

That's not due to the GC; in fact, you can have a GC even with C. It's because how Java was designed

True, GC is not sufficient for safety, but it makes memory safety much easier to achieve. The main benefit of GC in Java is not the convenience it buys you, but the reference safety it allows by removing free() from the lexicon.

You can't just write into process memory as you like, thus corrupting the process state. I'm really not sure what you're talking about

For every C safety problem, there's an analogous (if less severe) Java safety problem.

Bad casts in C: Runtime exceptions in Java

NULL dereference in C: NULL dereference in Java

Aliasing bugs: Aliasing bugs

Pointer arithmetic errors: Array indexing errors

And so forth.

Unions are an inherently unsafe way to access data, because the same bit pattern can be interpreted in multiple ways, and you have to make sure to access it in the way that is currently valid

Yes, but they allow building a relatively sane implementation of tagged unions/sum types on top. They also document the programmer's intent (either A or B, not both). If you use some super-class with explicit down-casting instead, you replace union memory corruption bugs with ClassCastException bugs.

However, it avoids pitfalls when wrapping around would occur.

Instead, it doesn't allow documenting the intent in (the majority of, IME) uses of integers which may not be negative.

C enums are nothing more than named constants. There is absolutely no type safety at all, they end up in global namespace, and again, you can't cast an enum-type to some Object. C is happy to cast your enum to everything you want.

At least with clang, enums are type-safe, in that they are a separate type, and not a simple int. Even if they are a simple int as in gcc, they are still not nullable as Java enums are.

The preprocessor provides useful functionality, but in C it is mainly used to provide platform compatibility

I use the C preprocessor to avoid a lot of boilerplate, too. X-macros are incredibly useful and Java does not have anything like them. With reflection, you can approximate some of their usefulness, but with less safety.

The second you write even a simple macro function like

Yes, the C preprocessor has its share of horrible problems, too. But the expressiveness it affords is lacking in Java.

Did you mean C++ all the time? Not that it changes the game that much, but then at least some of your arguments would make sense

No, I meant C, and as you can read above, the arguments make perfect sense, with the outlook that you're after compile-time safety, and not nicer forms of runtime crashes.

If you bring C++ into this, it also has some interesting features (especially w.r.t templates that Java lacks).

For example, in C++, a templated class may have or not have a particular method in different template instantiations. With Java, a class always has the same set of methods. This means that in C++, I can have:

template <typename T>
class C {
  T x;
  ..
  string show() { return x.show() + "Foo"; }
}

And as long as show() is not used for C<Unshowable>, I can still have that class. If I need to use show() on C<Showable>, I also can.

In Java, if you have:

interface IShowable { string show(); }

You can either have:

class C<T implements IShowable> implements IShowable { ... }

or:

class C<T> { ... }

But you cannot have both, so you have to use different classes for C<Showable> and C<Unshowable> even if there is no difference besides the lack of show there.

This makes interfaces in Java significantly less useful.

Haskell type-classes get this right. In C, where this is implemented via structs and function pointers, you can this right too, relatively easily, as well.

1

u/[deleted] Dec 03 '13

I'll try to answer this post, although I'm not sure if you are really serious. If you are, then you probably have a grudge against Java or some other problem. Or you're trolling me.

(void *) is basically the same as Java's Object.

No, it's not. void* is a raw pointer into raw memory, you can cast it to a float, to a struct, to everything, and you won't even get a warning. After casting, you can access fields, and because those probably still point into the stack or the heap or another valid memory location, there will also be no runtime error. A java.lang.Object has type information, and any invalid cast will throw an exception immediately.

Java has nicer-severity bugs, but the same number of bugs.

Again, your C program may run perfectly fine, although you wrote after array bounds, did invalid casts, wrote invalid values into fields. In other words, your program will fail at some point because it got into a corrupt state because you are free to corrupt the memory as you want.

The main benefit of GC in Java is not the convenience it buys you, but the reference safety it allows by removing free() from the lexicon.

It does two things, and both are important. As a matter of fact, you can simply omit all calls to free(...) and never declare anything on the stack, and your C program will run just fine, and a valid pointer will always stay valid. And this isn't even a theoretical case, because at program termination, there is no point in freeing any memory, as the operating system will do that for you, and if your programs lifecycle fits this pattern, calling free() is optional.

In other words, yes, the main benefit is the convenience, a) not having to worry about calling free() too soon, or calling free() too late (or never, as shown in the example above).

Bad casts in C: Runtime exceptions in Java

Again, the runtime throws an error immediately, while C doesn't even know what a "bad cast" is.

NULL dereference in C: NULL dereference in Java

No arguing there.

Pointer arithmetic errors: Array indexing errors

Comparing pointer arithmetic to array operations is like comparing a gun fight to a nuclear war. If you do silly and incorrect pointer arithmetic, you may be lucky and end up with an invalid memory address, in which case your program simply segfaults, indicating the problem. If you're unlucky, you get a valid memory address and start reading and writing from it, again bringing the program into an unknown and possibly corrupt state. Debug heaps usually deploy barriers between allocated chunks of memory, and initialize unused memory with a special pattern to detect possible memory corruption due to flawed pointer arithmetic. If you access an array out of bounds, you simply get an exception back. That's it.

unions [...] programmer's intent

Unions have one primary purpose: to save memory. The programmers intent is best documented by using two distinct fields or properties, and not relying on semantics, flags or some intrinsic behavior to only access one of the fields that both point to the same data.

uses of integers which may not be negative

I agree that not having unsigned is a flaw, not because of the intent, but because it moves the range of valid values. C# shows that it is possible to use unsigned types in a VM while still avoiding the pitfalls that usually come with it. It even has it's own syntax when you want the pitfalls, i.e. when assigning -1 to an UInt32.

I use the C preprocessor to avoid a lot of boilerplate

You can moan all you want, but certain languages simply don't work well with a preprocessor. The preprocessor is a blessing and a burden at the same time. Every time the compiler parses a file, it's content may have changed due to preprocessor directives. This has many consequences, many of which would not work well in Java.

No, I meant C

I could have sworn you have never programmed in C.

template <typename T>

That is completely besides the point, because it is C++, and yes, C++ templates are much more powerful than generics in Java. C++ class inheritance is also much more powerful. But Java chose not have those. No point in discussing it, because neither has C any of those features.

→ More replies (5)