What makes coding hard: Unknown Unknowns

Updated

What makes coding hard: Unknown Unknowns — illustration 1

When I first started coding, there were many challenges I had. (And still have!) Some of these challenges appeared to me the first time I saw an if-statement, and some of these challenges arise to this day!

Unknown unknowns:

In programming, one rule of thumb i’ve learned is: its already been done before. There is an ocean of pre-existing libraries / techniques which have already solved your problem.

For example, here is an issue I had quite early in journey: when I was building my first web-app, Schedulease, I had an entity class User, which represented my DB schema. (This is code-speak for a table in my database that stored all the user information—things like name, age, and password)

The problem I had was that, when I wrote endpoints for my api, my program pulled objects directly from my UserRepository and sending it over to the client. This is very bad and entirely insecure. (imagine every time you login to a website you’d see your password in plaintext any time you login).

User Entity

@Entity
@Table(name = "users")
@Getter @Setter
class User {
  @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private String name;

  @Column(unique = true, nullable = false)
  private String email;

  @Column(nullable = false)
  private String passwordHash; // store only hashes, never plaintext
}

UserController

// BAD: leaks fields you didn't mean to expose (e.g., passwordHash)
@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
class UserController {
  private final UserRepository repo;

  @GetMapping("/{id}")
  public User get(@PathVariable Long id) {   // returns JPA entity directly
    return repo.findById(id).orElseThrow();
  }
}

Now, anyone with a modicum of software experience will hear this example and cringe at silly the problem I just described, BUT my issue at the time was that I lacked the context to understand the gravity of my error, and its simple solution. If you don’t know what a Data Transfer Object is or you’ve never heard of the facade pattern, this issue can become pretty serious!

This may be a hot-take but, I think experts have it easy. Part of the reason I’ve fell in love with programming, is that now I have to ‘tools’ to understand the problems I face, and to solve them.

Also, by no means a master at programming. I’m still learning, but I do want to share every bit of my experience doing so!

Originally published on Medium.