Why your code looks different from that of others

Why your code looks different from that of others
Photo by Tengyart / Unsplash

When you learn programming, you often do these exercises in books, or other challenges online. Once you come up with a solution, you observe that our solution is radically different from the "official solution." Should you be worried? The short answer is, no.

The prime objective of programming is correctness. You should be able to write a piece of code that produces the desired output. There are many ways to write the same program, and hence your solution might vary from that of your peers.

In addition to the primary objective of correctness, there are other objectives that professional programmers pursue. I've listed some of them below, along with a short description:

  1. Adherence to some standard: In Python's case, it could be PEP-8. Standards allow other programmers to easily understand the intention of your code.
  2. Reusability: Here, the programmer breaks his code down into parts, where each of them could be reused for other problems.
  3. Extensibility: The programmer writes code in a fashion that permits easy addition of features.
  4. Readability: The programmer uses names, abstractions, and patterns that are largely familiar to the programming community, so that teamwork have happen much more effectively.
  5. Performance: The program should execute as quickly as possible, while consuming as little memory as possible.
  6. and a lot more...

Some of the objectives above can be contradictory at times, like performance and readability.

One of the reasons why your code might be different from that of a professional is that you might only aim for correctness. A professional instinctively aims for all the above, more or less, even if it is not required. You will get there as you grow as a programmer.

The first step to any programming problem is to create a solution that works. Then, you iterate on that solution till you achieve the right balance of the objectives mentioned previously. That's how many professional programmers and I work. This iterative work is not visible to other members of the team because we only publish code that is somewhat finalized, never the intermediate work.