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
20
votes
12 answers

Would you use num%2 or num&1 to check if a number is even?

Well, there are at least two low-level ways of determining whether a given number is even or not: 1. if (num%2 == 0) { /* even */ } 2. if ((num&1) == 0) { /* even */ } I consider the second option to be far more elegant and meaningful, and…
rmn
  • 2,386
  • 1
  • 14
  • 21
19
votes
9 answers

How to self-document a callback function that is called by template library class?

I have a function User::func()(callback) that would be called by a template class (Library). In the first iteration of development, everyone know that func() serves only for that single purpose. A few months later, most members forget what…
javaLover
  • 6,347
  • 2
  • 22
  • 67
19
votes
7 answers

Looping on C++ iterators starting with second (or nth) item

I am looking for a readable, elegant way to do the following in C++, here shown in Python: for datum in data[1:]: # do work. The iterators on the data in question may not support random access iterators, so I can't just use: for (mIter =…
user511493
  • 263
  • 1
  • 2
  • 7
16
votes
2 answers

Is there a shortcut for as_ref().unwrap()?

In my code I have a lot of structs with Options in them. I need to work with them in a lot of places, so my code is littered with struct accesses like car.engine.as_ref().unwrap(). This is pain for code readability. Is there a default function on…
itmuckel
  • 1,300
  • 15
  • 23
14
votes
2 answers

Is it better to reuse SqlCommand when executing the same SQL query several times?

When querying the database with the same query but different parameters, is it better to: do it in a single using, or to create two separate queries? Example of a single using: using (SqlCommand addProduct = new SqlCommand(@"insert into…
Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
14
votes
9 answers

Using true and false as the expressions in a conditional operation

I'm maintaining some code and have found the following pattern a lot: var isMale = (row["Gender"].ToString() == "M") ? true : false; instead of this: var isMale = (row["Gender"].ToString() == "M"); Is there any reason why anyone would do this?…
Chris
  • 2,959
  • 1
  • 30
  • 46
13
votes
1 answer

How do I check the equality of three values elegantly?

Say I have values a, b and c. I want to find out if they are equal. If I do if a == b == c{...} Then I get a compile error invalid operation: a == b == c (mismatched types bool and TypeOfABandC) This is pretty obvious, because this parses to: (a…
timthelion
  • 2,636
  • 2
  • 21
  • 30
13
votes
6 answers

foreach(... in ...) or .ForEach(); that is the question

Possible Duplicate: C# foreach vs functional each This is a question about coding for readability. I have an XDocument and a List of the names of the elements that contain sensitive information that I need to mask (replace with…
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
12
votes
6 answers

iOS access to Safari Reader feature through UIWebView

I am using iOS 4.3 & was wondering if there is any way that I can access the Safari's "Reader" feature through which webpages are removed of ads & other riff raff & the content takes the center stage. If one opens any article in Safari (on say…
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
12
votes
6 answers

Is wrapping STL idioms for readability a good idea?

I'm currently working on a C++ project that needs to have as few external dependencies as possible, and thus I'm pretty much sticking to STL and Boost. Until now, I've been almost exclusively living in Qt-land when it comes to C++. In general I tend…
Lucas
  • 6,328
  • 8
  • 37
  • 49
12
votes
4 answers

Proper commenting for functional programming

I've been learning scheme, and I just realized that I don't really know how to properly comment my functional scheme code. I know how to add a comment of course - you add a ; and put your comment after it. My question is what should I put in my…
Cam
  • 14,930
  • 16
  • 77
  • 128
12
votes
6 answers

if-else structure

I have these long statements that I will refer to as x,y etc. here. My conditional statements' structure goes like this: if(x || y || z || q){ if(x) do someth else if (y) do something if(z) do something else…
Halo
  • 1,524
  • 3
  • 24
  • 39
12
votes
4 answers

Is there a neat way of doing a ToList within a LINQ query using query syntax?

Consider the code below: StockcheckJobs = (from job in (from stockcheckItem in MDC.StockcheckItems where distinctJobs.Contains(stockcheckItem.JobId) group stockcheckItem by new { stockcheckItem.JobId,…
George Duckett
  • 31,770
  • 9
  • 95
  • 162
12
votes
3 answers

Access member variables directly or pass as parameter?

I noticed that even when paying respect to the single responsibility principle of OOD, sometimes classes still grow large. Sometimes accessing member variables directly in methods feels like having global state, and a lot of stuff exists in the…
Tom
  • 3,115
  • 6
  • 33
  • 38
11
votes
3 answers

How to organize Python API module to make it neat?

I'm writing a Python library that represents some web API. Right now, my library directory looks close to this: __init__.py Account.py Order.py Category.py requests.py In __init__.py, I have something like this: from .Account import Account from…