Questions tagged [readability]

Readability is a subjective parameter used to measure an aspect of code quality. It is based on the assumption that code should be easily comprehensible by humans, both in its form and in its meaning.

Readability is a subjective parameter used to measure an aspect of code quality. It is based on the assumption that code should be easily comprehensible by humans, both in its form and in its meaning.

To improve code readability, the following is usually considered:

  • The code is written such that a person with poorer programming skills will be able to understand what is written.

  • Adding descriptive comments to explain every step of the way.

  • Using proper indentation and white spaces.

  • Choosing object\variable names that describe their purposes.

  • Referencing the algorithm and the responsible authors.

730 questions
11
votes
13 answers

Formatting of dynamically generated HTML - does no one care?

I have very little experience in web development, so this may be a very basic question. It's just, from the limited experience I do have (a little PHP, and a little Ruby on Rails), it seems that the way dynamically generated HTML is formatted just…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
11
votes
9 answers

Better way to pass bool variable as parameter?

I am wondering if there is a better way to write this for better readability. If you have a function like below, void animal(bool hasFourLegs, bool hasHead, bool hasBody); When you call the function, you will end up with something like animal(true,…
Eddie
  • 761
  • 3
  • 9
  • 22
11
votes
1 answer

YAML Folding without spaces

How can I break a long string in YAML (like long url, or filename/path), without newlines becoming spaces? Example input: url: > https://example.com/?what=Lorem %20ipsum%20dolor %20sit%20amet Output: { "url":…
Gargauth
  • 2,495
  • 6
  • 27
  • 30
11
votes
6 answers

How to Format Code in Research Reports

I am currently writing a formal research report, and I'll be including code with this report. Question: Is there an accepted way of displaying code in research reports? I'm thinking both in terms of font, spacing, et cetera, and whether the code…
GlenCrawford
  • 3,359
  • 3
  • 26
  • 34
11
votes
3 answers

Why does org.apache.commons.lang.BooleanUtils.isTrue(Boolean bool) use the ternary operator?

I F3'd into this for no particular reason, and was surprised to see this method implemented as follows: public static boolean isTrue(Boolean bool) { if (bool == null) { return false; } return bool.booleanValue() ?…
HellishHeat
  • 2,280
  • 4
  • 31
  • 37
10
votes
5 answers

C style casting and readability in C++

Yes I know you shouldn't use C style casts in C++, but in some cases I really think it's a lot more readable if you do, compare these two for example: d = ((double)i * 23.54) / (d + (double)j); d = (static_cast(i) * 23.54) / (d +…
DaedalusAlpha
  • 1,610
  • 4
  • 20
  • 33
10
votes
9 answers

How do I make this code more readable?

I wrote this today and I'm ashamed. What do I need to do to make this chaotic stuff more accurate and readable amongst others? switch…
Dan Ganiev
  • 1,062
  • 2
  • 11
  • 26
10
votes
3 answers

Why 'condition && statement()' is not popular in Java world?

In Javascript world (especially in React apps) I've seen expressions like [2]. However, in Java this is not so popular. 1. if(isValid) { statement(); } isValid && statement(); The outcome of [1] and [2] is identical as the statement() is not…
Grzegorz Gajos
  • 2,253
  • 19
  • 26
10
votes
11 answers

sentimental code

I've come across an article that discusses the issue of "code admiration". Basically, the author talks about how developers should be more skeptic about code they write. How we may "admire" our code too much, attach our-self to it, making us more…
unknown
9
votes
5 answers

Tools and techniques to improve comprehension of unfamiliar code?

I've realised that my greatest weakness as a programming student is my poor ability to comprehend other people's code. I have no trouble whatsoever with 'textbook' code, or clearly-commented code, but when given a program of a few hundred lines,…
Eilidh
  • 1,354
  • 5
  • 21
  • 43
9
votes
3 answers

What are some good ways of handling errors (cleanup and abort) in a function that initializes multiple resources in C?

First of all, if someone can reword the question to make it clearer, please do. A common occurrence in C programming is having several resources to be initialized/allocated in a specific order. Each resource is a prerequisite for the initialization…
Dmitri
  • 1,338
  • 1
  • 12
  • 24
9
votes
6 answers

Python readability hints for a Java programmer

I'm a java programmer, but now entering the "realm of python" for some stuff for which Python works better. I'm quite sure a good portion of my code would look weird for a Python programmer (e.g. using parenthesis on every if). I know each language…
Samuel Carrijo
  • 17,449
  • 12
  • 49
  • 59
9
votes
7 answers

How to reduce the number of if-else statements in PHP?

I found that there are many if-else statements, especially nested if else statements, these statements make my code less readable. How to reduce the number of if else statements in PHP? My tips are as follows: 1.Use a switch statement when it is…
Steven
  • 24,410
  • 42
  • 108
  • 130
9
votes
3 answers

Readable file sizes with the Twig templating system

I'm wondering if there's a built in way to output readable file sizes with the Twig templating system. Say I have something like this in my template:

This limit is currently set to {{ maxBytes }}

How could I format maxBytes to display…
James Dawson
  • 5,309
  • 20
  • 72
  • 126