re: Is there such thing as a bad programming language? VIEW POST

FULL DISCUSSION
 

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.

 

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);
 

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

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

code of conduct - report abuse