High level concepts

A couple of days ago, I was revisiting a Java piece of code that needed some changes. It's about an app that listens on a TCP socket, and receives ASCII lines that processed individually.

Each line represents commands that do different things, and they all share the same trait: they encode an operation and parameters partitioned by a separator, let's say it's '#'. Examples follow:




You can't extract the diferent fields in each line just by reading them in fixed position, because a) there may be different amount of parameters and b) each parameter can have different lengths.

So the natural solution is using the well-known string function: "split". But instead of that, what I found was something like:

String[] params = new String[100];
while (true) {
  /* code to grab the indexes here ...*/
  params[idx] = string.substr(pos0, pos1);
  /* code to exit loop if done ... */

Which is maybe what a freshman student will write in its college C class.

The problem is... this is code that was written by a programmer with at least 10 years of experience! Besides using a convoluted logic, I'd argue that this shows the programmer has a lack of knowledge for high level patterns.

The simple abstraction "separate this string into it's N parts by a delimiter", while not often seen in C, is used everywhere in all kind of code in high-level languages such as Python, JavaScript. Or even PHP.

I believe there may be need in programming to reinvent the wheel from time to time. But not to solve this simple kind of problems. And getting acquainted in these common patterns is just critical to solve real problems in software.

Keep Calm and do not Reinvent the Wheel

So that's why programmers need to be exposed to such commonplace concepts, learning how to solve common problems in different programming languages as early as possible in their careers.


Comments powered by Disqus