Readability measures how easy code is to read and understand.

learn more… | top users | synonyms

20
votes
6answers
2k views

How to remove a function or feature when using TDD

In texts about TDD I often read about "remove duplication" or "improve readability" during the refactoring step. But what makes me remove an unused function? For example let's say there is a class C ...
2
votes
1answer
129 views

Choice of variable names and types in graph algorithm

I am learning algorithms and data structures from this awesome resource Algorithms. Rather than doing dry reading I am trying to re-write all the code myself so that I can learn coding as well as ...
7
votes
1answer
178 views

Why does FSharpLint suggest, “In F# code, use 'e1 || e2' instead of 'e1 or e2'”

If I write the expression, if not (expr1 or expr2) then value I get the following warning from FSharpLint, In F# code, use 'e1 || e2' instead of 'e1 or e2'. Why is using || preferred over using ...
4
votes
1answer
184 views

When designing a directory structure should filenames include folder names?

Suppose I have two directories: house-1 and house-2. Each house directory needs to have files describing its windows and doors. I am trying to understand how to name the files and directories. My ...
61
votes
11answers
6k views

Is a big boolean expression more readable than the same expression broken down into predicate methods? [closed]

What is easier to understand, a big boolean statement (quite complex), or the same statement broken down into predicate methods (lots of extra code to read)? Option 1, the big boolean expression: ...
0
votes
0answers
57 views

Using counter or constant for parameter position

Suppose that I have the following Java code: int i = 0; PreparedStatement statement = con.prepareStatement("SELECT * FROM table " + "WHERE field1 = ?, field2 = ?, field3 = ?..."); ...
22
votes
7answers
2k views

Specify optional parameter names even though not required?

Consider the following method: public List<Guid> ReturnEmployeeIds(bool includeManagement = false) { } And the following call: var ids = ReturnEmployeeIds(true); For a developer new to ...
8
votes
3answers
172 views

Indentation in a multi-language file [closed]

In files that contain multiple languages (ie. template files), are there any best practises regarding indentation? I mostly use this: <div> IF FOO <div> ...
14
votes
2answers
1k views

Filtering foreach loops with a where condition vs continue guard clauses

I've seen some programmers use this: foreach (var item in items) { if (item.Field != null) continue; if (item.State != ItemStates.Deleted) continue; // code } instead ...
2
votes
3answers
648 views

Why is JSX good, when JSP scriptlets are bad?

React.js provides JSX as an XHTML-like syntax for constructing a tree of components and elements. JSX compiles to Javascript, and instead of providing loops or conditionals in JSX proper, you use ...
0
votes
1answer
109 views

Code readability on non-trivial loop variables [closed]

My co-worker and I discussing about the code readability on simple loops. Suppose a method takes an integer parameter and does some operation on the elements of an array depending on the parameter. ...
139
votes
9answers
11k views

Is there a specific reason for the poor readability of regular expression syntax design?

Programmers all seem to agree that readability of code is far more important than short-syntaxed one-liners which work, but require a senior developer to interpret with any degree of accuracy - but ...
5
votes
2answers
221 views

Exception clutter, how necessary is it? [duplicate]

Let's say I have a few methods that access the File System, and I want them to be a bit robust, I want to throw errors so the user can react: If the file doesn't have read / write rights, I want to ...
0
votes
0answers
134 views

Does the term “readability” refer also to the transparency of code functionality? [duplicate]

Question: Does the term "readability" refer also to the transparency of code functionality? For Example: I read that a downside of Dependency Injection is a sacrifice to readability. The idea being ...
6
votes
1answer
237 views

Whether to abstract small repeating code segments in HTML templates?

(The framework used in question is AngularJS so the question can be translated as "Whether to introduce Directives for small repeating code segments in Views") As a developer who focuses on Java ...
2
votes
2answers
244 views

Source file shouldn't be more than 100 SLOC [closed]

I just started working on a baseline that is rooted in Ada. Many of the older Ada programmers insist that the source files shouldn't be more than 100 SLOC. I researched this online and I have not ...
4
votes
1answer
1k views

Should I avoid using style like `for k, v in dict_sample.items()`?

Today I was viewing my colleague's code and I saw a function like this: def manager_skill_tree_func(*args, **kwargs): """# manage_skill_tree: Initialize the manage skill tree """ ...
12
votes
4answers
724 views

Single method with many parameters vs many methods that must be called in order

I have some raw data I need to do many things to (shift it, rotate it, scale it along certain axis, rotate it to a final position) and I am not sure what the best way to do this to maintain code ...
0
votes
3answers
64 views

Returning reference type passed to method

I'm asking this from .NET (C#) point-of-view, however, I'd preferire language agnostic answer. Is it better to return the reference type when some value is changed in method or not? I'm mostly ...
2
votes
5answers
1k views

When, if ever, should I daisy chain functions?

I'll try to be as brief as possible in respect to your time. In a program divided up into many functions by which it is intended that they execute themselves one after another, when (if ever) is it ...
3
votes
2answers
232 views

Naming self or this something else, a good idea?

I'm finding that my code becomes more readable when I make a local reference to this/self, and/or call it something that makes sense in the context. "self" or "this" doesn't really indicate what kind ...
2
votes
2answers
284 views

At what point should you collapse many parameters into (e.g.) struct to improve readability in function headers?

While making a struct creates some overhead at run-time, packaging a bunch of frequently-used-together variables can dramatically increase code readability. How do you balance the two? I was just ...
0
votes
1answer
30 views

Accessing struct attribute: casting vs field name

I was just going over some kernel code, I noticed that rb->__rb_parent_color = (unsigned long)p | color; is kinda wired. The first field of a struct is accessed by casting the pointer and not by ...
17
votes
7answers
2k views

What can one do to improve readability of math-oriented code in C#, Java, and similar? [closed]

As both a C programmer and a C# programmer, one of the things I don't like about C# is how verbose math functions are. Every time you would have to use a Sin, cosine, or power function for example, ...
1
vote
0answers
237 views

API for accessing CTS and DSR signals of serial port [closed]

I'm trying to improve another abstraction of the serial port. The project involved often needs to check one of the single control signals CTS and DSR. This is currently done by a call to ...
28
votes
8answers
6k views

Is splitting up a function into several inner functions an anti-pattern? [duplicate]

Imagine a long and complicated process, which is started by calling function foo(). There are several consecutive steps in this process, each of them depending on result of the previous step. The ...
3
votes
1answer
247 views

How reduce the usage of Magic Strings?

In the application database there is a configuration table with this schema: Table: ReleaseProperty ReleasePropertyID ReleaseID Name Value Currently to retrieve a specific property I pass to the ...
1
vote
3answers
196 views

Redundant function to clarify purpose? [duplicate]

Since what matters is not how the code does it but what it does, would you consider wrapping a function with a different name just to clarify it's behavior in certain situations a good practice? ...
5
votes
1answer
1k views

In Java, why use bit hacks when non-bitwise technique it is more readable?

Is there any legitimate use for bit manipulation hacks in higher-level languages such as Java? I can see them being useful in speed-sensitive low-level and computation-intensive programs, e.g. ...
7
votes
1answer
322 views

Restructuring a large Chrome Extension/WebApp

I have a very complex Chrome Extension that has gotten too large to maintain in its current format. I'd like to restructure it, but I'm 15 and this is the first webapp or extension of it's type I've ...
1
vote
4answers
303 views

Is there a good way to communicate the intent that a class only contains referentially transparent methods?

My workplace mostly uses C# for writing code. I'm trying to figure out a good way to indicate that a class with referentially transparent methods is intended to be referentially transparent (i.e., ...
17
votes
4answers
907 views

What should plugins use: hooks, events or something else?

Consider an app that allows plugins to react to its program flow. I know 2 ways to achieve this: hooks and events 1. Hooks Use calls to empty functions inside the main program flow. These functions ...
4
votes
1answer
262 views

Exceptions catching clutter code and make it hard to read

When I read well written code that checks to make sure all the input is of the correct format and type, and also catches all the things that possibly could go wrong, I find it difficult to understand ...
0
votes
1answer
219 views

Switch / select case etc. compared to If … ElseIf … Else [duplicate]

I don't understand why the switch or equivalent is so popular in languages. To me, it seems like it had a place back in the days when the alternative was lots of nested if statements in the else part ...
21
votes
7answers
1k views

How do I manage a very large set of rules and magic numbers in my program?

I'm somewhat new to programming (I'm a mechanical engineer by trade), and I'm developing a small program during my downtime that generates a (solidworks) part based on input from various people from ...
1
vote
2answers
223 views

Is input validation necessary?

This is a very naive question about input validation in general. I'm a MATLAB user (1.5 years old) and I learned about input validation techniques such as "parse" and "validatestring". In fact, ...
1
vote
1answer
683 views

Get subset of associative array. Which of these is more readable? Is there another reason to use one over the other (or a different method)

If I have an array that has more info than I need and I want to pull out a subset of its name value pairs I have come up with 2 methods. Both rely on this: ...
13
votes
4answers
1k views

Which way to terminate reading loop is the preferred approach?

When you have to iterate a reader where the number of items to read is unknown, and the only way to do is it to keep reading until you hit the end. This is often the place you need an endless loop. ...
10
votes
5answers
741 views

Can a pure-functional solution to this problem be as clean as the imperative?

I have an exercise in Python as follows: a polynomial is given as a tuple of coefficients such that the powers are determined by the indexes, e.g.: (9,7,5) means 9 + 7*x + 5*x^2 write a function to ...
1
vote
4answers
295 views

What's the most readable way of echoing from PHP?

Should I use <?php if(!$user->is_logged_in()){ echo '<p id="login">Click <a href="login">here</a> to log in</p>'; } ?> or <?php ...
1
vote
1answer
447 views

Readability vs benefits of polymorphism

We are dealing with a lot of CRUD operations in our application. Each database table has one or more corresponding SQLContainer instances to perform various types of operations. All of these ...
4
votes
4answers
6k views

Usage of magic strings/numbers [closed]

This is somewhat controversial topic, and I guess there is as many opinions as there are programmers. But for the sake of it, I want to know what are the common practices in business (or in your work ...
7
votes
6answers
2k views

Why do many languages not support named parameters? [closed]

I was just thinking how much easier it would be to read code if, when calling a function, you could write: doFunction(param1=something, param2=somethingElse); I can't think of any drawbacks and it ...
11
votes
5answers
609 views

Code repetition vs multi responsible method

I try to follow the Single Responsibility Principle (SRP) and also to omit Code Repetitions. However there are often places where there are code repetitions that are nothing more than code blocks of ...
1
vote
3answers
2k views

Storing a value vs calling reference for repeated use in Java

I have an old habit of avoiding calling references multiple times, both for easier to read/maintain code, and for possible efficiency. But I'm wondering which is more efficient (memory, performance, ...
0
votes
2answers
517 views

Good or bad code? Or “a secret reason”?

I think this code: if(file_exists("amodule.inc.php")) require_once("amodule.inc.php"); is misleading because of the use of the require_once. I think that - to keep the logic and "wording" ...
3
votes
2answers
1k views

Extracting lambda expressions from linq queries and readability

Every now and then when I have a complex lambda, I do something like this: Func<SomeObject, bool> equals = o => o.ID == someID && o.Name == someName && IsAdd || ... var ...
2
votes
2answers
389 views

How much functional programming expertise can programmers be expected to have? [closed]

I'm coding in a non-functional language with functional mechanisms (C# to be specific, but you could substitute, say C++ and use function pointers, or what have you) on a small team. It's my habit to ...
0
votes
1answer
98 views

Good practice to use namespace or prefix to indicate what file function is from? [duplicate]

For example, I have the function generate_salt() in encryption.file but the person may not know where generate_salt() is from. Using a namespace like encryption::generate_salt(), or ...
11
votes
6answers
1k views

Do sigils make source code easier to read?

In most programming languages, variables do not have identifying characters like they do in PHP. In PHP you must prefix a variable with the $ character. Example; var $foo = "something"; echo $foo; ...