4 Tips for Improving Code Readability

Most readers will never read an entire article. More specifically, most readers will never make it more than 75 words into an article, and they will make that decision in seconds. According to a 2014 Time Magazine article, 55% of readers on the internet will stop reading a website after just 15 seconds. For the average adult—who reads about 300 words a minute—that is only about 75 words. For a 1,500 word article, that is only about 4% of this article—less than this first paragraph.

Bearing that in mind, many are likely to scan an article, rather than read any sizable portion of the text, before making the decision to continue reading. In software articles, one of the most obvious parts of the article that can aid or dissuade in keeping the reader's attention is code snippets. Concise, well-formatted code snippets can go a long way in helping a reader understand the purpose of the article, while simultaneously signaling to the reader that we, the author, have taken the effort to ensure that code is readable.

Java 8 Java.Time Package: Parsing Any String to Date [Code Snippets]

In one of my projects, I received a requirement that stated that while parsing a text file, Strings denoting a date or a timestamp are expected to be in many different formats that are not known in advance, yet all of them represent a valid date or timestamp needed to be parsed properly. So, the solution I came up with is this: To have a set of formats stored in the property file, and when a String needs to be parsed, the formats are read from a file and attempts to parse the String are made sequentially with each format until it is parsed successfully, or until we run out of formats. The advantages of this solution are that if you discover a valid String that was not parsed successfully, all you will need to do is to add a new format to your properties file and no re-compilation and re-deployment is needed. Also, this way, you can set your priorities: Say if the US date format is preferable to the European one, just place US formats first and only after the European ones. Also, in Java 8, the format Strings allow for the optional format sections denoted by '[]'. So, several formats actually may be combined into a single one with optional sections. For example, instead of:

MM/dd/yyyy

MM-dd-yyyy

MM.dd.yyyy


The Problem of String Concatenation and Format String Vulnerabilities

If JavaScript is your programming language of choice, you probably don't have to worry about string concatenation all that much. Instead, one of the recurring problems you might encounter is having to wait for JavaScript's npm package manager to install all of the required dependencies. If that sounds all too familiar, and you have some time on your hands until npm is done, you might as well spend it reading about how string concatenation works in other languages.

In this blog post, we examine why string concatenation is a complicated topic, why you can't concatenate two values of a different type in low-level programming languages without conversion, and how string concatenation can lead to vulnerabilities. We'll also explain how format strings that contain placeholders for certain types of data can cause serious trouble if they are controlled by an attacker. And, we'll conclude with a simple way to fix them.