DEV Community

Discussion on: Is there such thing as a bad programming language?

Collapse
 
calebwin profile image
Caleb Winston

There's definitely a lot more than syntax - allowing objects to be null is a great example of a language design decision that has implications both in the syntax and the semantic.

Something like this in Kotlin...

manager = employee?.department?.head?.name

would look like this in Java...

manager = null;

if (employee != null) {
  if (employee.department != null) {
    if (employee.department.head != null) {
      manager = employee.department.head.name;
    }
  }
}

These examples make it clear that while null safety improves code readability, it can also increase the robustness of your software. A single change in a condition or accidentally using a value outside of its null check in the Java example can easily lead to errors at runtime.

Collapse
 
tiguchi profile image
Thomas Werner • Edited

This is why Java 8 introduced Optionals. It's not syntactic sugar so it's more verbose than the Kotlin example, but it does the same thing:

String manager = Optional.ofNullable(employee)
                         .map(Employee::getDepartment)
                         .map(Department::getHead)
                         .map(Person::getName)
                         .orElse(null);
Collapse
 
calebwin profile image
Caleb Winston

Oh nice! I clearly haven't used Java in a while; I do still think the elvis operator looks a bit cleaner though.

Thread Thread
 
tiguchi profile image
Thomas Werner

Oh man... now I cannot unsee Elvis in the "Elvis operator" :-D