Questions tagged [readability]
Readability measures how easy code is to read and understand.
205
questions
0
votes
2
answers
236
views
C# readability - async/await or return for wrapping asynchronous functions?
Say I have an asynchronous method:
public async Task DoThing(int x)
{
// ...
}
Now, I want to wrap this method with a new method. Each of these two options are functionally equivalent:
public ...
35
votes
10
answers
12k
views
Why don't programming languages or IDEs support attaching descriptive metadata to variables?
As developers, we often face the challenge of balancing meaningful variable names with code readability. Long, descriptive names can make code harder to read, while short names may lack context. For ...
3
votes
4
answers
1k
views
Representing vectors as arrays of points vs. as data structures
I'm writing a program in Java where I need to represent the position, scale, and other 3-dimensional properties of objects in a world using vectors.
I can use either of these two approaches:
...
0
votes
4
answers
372
views
Code quality: expressiveness vs. conciseness
I've been wondering about code expressiveness vs. conciseness lately. What I mean by that is that a lot of modern programming languages offer features to express statements in a very short manner, ...
1
vote
2
answers
199
views
What is an appropriate length for function names? [closed]
I'm writing a C++ class, CBookSpellDef which has the following methods:
int GetIndexOf(const char *psz);
char* GetNameAtIndex(int index) { return m_spellTypes[index].szName; }
char* ...
0
votes
5
answers
294
views
When, if ever, should a private member variable, used in one function, be made into a local static variable?
Lately, I have been playing with this idiom:
Changing a private member variable into a local static variable when:
member to singleton class used in only one function
member is mutex for a shared ...
1
vote
0
answers
81
views
golang: pattern for handling message queues? Are named functions anti-idiomatic somehow?
Had a discussion today in how to implement services that work with messages coming in from event queues. We call these services processors. One of us argues for using several functions, while the ...
0
votes
1
answer
111
views
Assigning to fields in function or by function?
While writing the constructor for class A in Python, I calculate some of the class's fields using function fun(). This function is never used outside the constructor of instances of this class.
In ...
4
votes
2
answers
2k
views
Should a method modifying object passed as a parameter return the modified object? [duplicate]
I have some incoming request - it's an instance of class generated from api specification - POJO with public getters/setters.
I would like to normalize some values. For example dimensions (to use ...
3
votes
5
answers
721
views
What is the best way to make an if statement condition itself conditional?
Apologies if this is answered elsewhere here but this specific question is somewhat hard to search for as you might see. I'm writing a script currently that essentially has a simple if-then within a ...
2
votes
1
answer
511
views
Does "happy path to the left edge" break Python conventions?
I found the short article Align the happy path to the left edge quite helpful in improving readability of functions. Briefly, reading down the left edge of the function should step you through the ...
0
votes
1
answer
162
views
When to Push or Publish
I'm in the process of redesigning our entire process control system (a daunting task, really!), and want to make the right decisions to how each system in the ecosystem communicates with each other.
...
0
votes
3
answers
546
views
Should I send the current or previous state with my state changed event?
I have an event that is raised when my job objects change state (e.g. initializing, running, complete, etc). The event is to be invoked from my set method in the State property, but before I make it ...
-3
votes
1
answer
147
views
Using braces on new line convention, but should I do this for one-line methods? [closed]
I've been using the braces on a new line convention for my java project:
public String class()
{
something;
{
However I have quite a few methods, like getters, that could be written on one line ...
0
votes
1
answer
247
views
Excessively verbose and cryptic comparisons in Java
I don't know if this is the right place to ask more of a "philosophical" question.
The more I code in Java, the more I have to bear with Comparable<T>. And the more I bear with this ...
0
votes
1
answer
321
views
Is there a reason why you shouldn't use 'integration variables'?
I tend to create a variable whenever I need an uncircumstantially same value as to that of an earlier value more than once (the values are not happening to be the same by chance rather they actually ...
13
votes
6
answers
5k
views
Is it bad practice create "alias" variables to not use globals or arguments with long names in a method?
Is it OK to create a variable which only purpose is to increase readability?
Example:
public class Example
{
private final HowLongCanARepositoryNameReallyBeRepository ...
44
votes
13
answers
9k
views
Is it an (anti-)pattern for a function to have an argument to decide which other function to call?
Consider your have an enumeration that looks like this:
enum ProcessingType {
CONFABULATION, RETICULATION, SPLICING;
}
And you have a web-service that looks like this:
class WebService {
...
1
vote
4
answers
221
views
When should a method depend on a data source and NOT have it declared as a parameter?
I was assigned a code review to one of my colleagues.
I posed the following, which I wanted to share here in order to hear whether I am right or wrong.
Consider the following code snippet:
public void ...
1
vote
3
answers
221
views
Is there a standard on where to put the `/` in file & directory substrings?
Let's say I have 2 variables dir_name = mydir and file_name = file1, each to be used in formatted strings. There're a few ways to insert a / in a string with those 2 variables.
dir_name = mydir/; ...
0
votes
3
answers
4k
views
Is it any more efficient to reuse a variable than to create a new object?
In a project I am working on we load various entities from the database, do some work then attempt to save the resulting entities.
If saving the result is not successful, we return a Result object ...
8
votes
4
answers
3k
views
Use local or nested function for readability?
I found in the catalog of Refactoring by Martin Fowler, with Kent Beck book that they mention Extract Function refactoring.
It is a good practice to wrap your related code into local functions to ...
0
votes
1
answer
325
views
CQRS for avoiding heavy joins
I've been wondering about what is a perfect use case for CQRS where the benefits overcome the complexity and the cost with come with the package. So for me to better understand it, I want to share ...
0
votes
2
answers
201
views
DRY Violation for Logical Code Organization and Readability
I have a block of code that branches into 2 pathways, let's call them the "simple" and "complex" branch, based on user input.
Either the simple or complex logic has 4 steps, let's call them A, B, C ...
-4
votes
1
answer
779
views
Is using yoda conditions with c# justified?
It's mind bending experiencing reading code like :
"Aggregation".Equals(evt.Id))
I was unsuccessful in trying to talk the person out of this style, maybe I'm wrong?
Or maybe I wasn't articulate ...
1
vote
2
answers
737
views
Design pattern for executing and reverting the tasks in same order
I have a Task in my project , to complete that i have call(REST) multiple external systems. If my call fails at some level , i have to rollback all my previous calls(making call with undo action) .
...
1
vote
2
answers
1k
views
Using "magic" strings for single-use error reasons?
I'm writing a fairly large piece of logic, during which there are 6 points where things could go wrong and execution should stop after logging the error. The error is also stored in an object.
...
-2
votes
4
answers
354
views
Is judicious & plausible selection of class fields a valid refactoring step?
I'm a great fan of refactoring but I've been wondering about the issues raised by refactoring.
Fowler advises refactoring
to make code readable to all users
to make the code structure more sensible, ...
3
votes
2
answers
240
views
Explicit type and final in stream lambdas
This argument has been going on for weeks:
Bot.getGuild().getMembers().stream()
.filter((final Member m) -> m.getRoles().size() == 0)
.filter((final Member m) -> !m.getUser()....
1
vote
1
answer
116
views
How to avoid confusion with possible referenced before assignment when it won't be (Python) [closed]
I have some code equivalent to this:
state = True
if xyz:
state = False
try:
value = foo()
except:
handle_exception()
state = False
if state:
bar(value)
On the last line it will ...
2
votes
2
answers
146
views
How should I call splitted functions? centralize them? or call it one next to one?
According to Is it OK to split long functions and methods into smaller ones even though they won't be called by anything else?, I should split long functions into smaller functions even if they ...
1
vote
2
answers
125
views
How to profile before optimising for readability
When optimising for speed, profiling first can help focus the effort on the parts of the code that bring most benefit. Since speed can be measured objectively, the benefit can also be measured ...
0
votes
2
answers
340
views
Reuse domain entity logic
Usually the domain logic should be contained in the domain entity, rather than being put in specialised external classes called from the entity.
This is good to prevent that somebody comes later, ...
3
votes
3
answers
551
views
Drawing lines with dashes in logs
It can sometimes ease readability to "draw" lines in a logfile. For example:
noise
noise
----------------------------- Starting Operation ABC
blabla
blabla
----------------------------- End of ...
0
votes
2
answers
460
views
Should default values be explicitly stated in configuration files for libraries or frameworks?
In one of the projects I work for, we have just added karma to run unit tests.
When reviewing the MR of a coworker, I noticed the karma.conf.js (the configuration file) has 120 lines, which seems to ...
2
votes
1
answer
165
views
Is "equivalent" functions / methods readability worth its maintanability cost?
First of all, I am sorry if it is a duplicate, I could not think of any "correct" wording to search about this.
Second let me clarify: By "equivalent" I mean methods that are meant ...
1
vote
1
answer
441
views
Metrics that measure the readability of the function
Functions are the first line of organization in any program.I believe writing them well improves the readability of code to some extent.I am working on metrics that describe how readable the function ...
1
vote
2
answers
4k
views
Using the same name for setter and gettter methods for a boolean member variable
Say I have class with a boolean member variable fancy:
public class MyClass {
private boolean fancy;
}
Case 1. I could the define the setter and getter as follows:
// getter
public boolean ...
-1
votes
5
answers
645
views
less functions and complex code or more functions and readable code?
I am currently making an html-based editor, i know there are lots of editors out there especially html based but i want to do it myself once. What i am concerned about is, for e.g, when a user press a ...
-2
votes
2
answers
763
views
Code readability vs extensibility and flexibility, what is the relation?
I have been reading a bit about code readibility and found conflicting information about the following: some say that flexibility (and-or extensibility) actually increase readability, while some ...
4
votes
5
answers
1k
views
Is it a bad practice to use conditionals with functions that change program's state?
The title might be a bit vague, so let me explain. Let's assume we have a function that does something (changes state of the program), for example a function that creates a file. That function returns ...
63
votes
10
answers
12k
views
Readability versus maintainability, special case of writing nested function calls
My coding style for nested function calls is the following:
var result_h1 = H1(b1);
var result_h2 = H2(b2);
var result_g1 = G1(result_h1, result_h2);
var result_g2 = G2(c1);
var a = F(result_g1, ...
0
votes
1
answer
174
views
Logging parametrized logs while maintaining readability
I have a LogFormatter class which looks like below
@Sl4j
class LogFormatter {
public static String format(String taskType, String taskId, String message) {
return String....
4
votes
4
answers
465
views
Why are functional-style chained map operations considered hard to read?
In the comments to an answer of another question, I proposed the following Java code as a better way of writing a more procedural-style variant of the same operation:
employeeService.getEmployee()
...
6
votes
6
answers
2k
views
DRY principle often makes my code more complicated and/or more difficult to understand [duplicate]
This often happens in my projects. Sometime I have this part of code that is very similar to this other part, yet a few lines makes it complicated to keep the code clean and without duplication. Here ...
0
votes
1
answer
311
views
Do all Get/SetAccess, Access, and Dependent property attribute combinations have valid use cases?
If You are Unfamiliar with OOP Matlab
For those not familiar with matlab, matlab provides a variety of access attributes when creating member variables. All member variables are essentially C# ...
1
vote
4
answers
266
views
When is it justified to write implicit conditions? [duplicate]
As a rookie programmer, I've only recently been digging into benchmarking, and have been comparing various ways of accomplishing the same task in terms of speed.
Most of the time, the result is ...
0
votes
5
answers
197
views
Is setting field's fields in a submethod without passing argument a good pattern?
Simplified a little, I have this code:
public class TaskProcessCases{
private Assessment assessment ;
public void execute() { // the actual name
for (Case case : cases) {
...
1
vote
1
answer
191
views
How to write Readable Comment when we have to deal with IDs?
How to write Readable Comment when we have to deal with IDs?
I will explain this with example.
+---------------+-----------------+
| permission_id | permission_name |
+---------------+--------------...
6
votes
2
answers
3k
views
Using lambdas to improve readability of a C++ function
I'm looking to improve the readability of a lengthy C++ function. This function contains a number (> a dozen) variables that are used throughout. The main logic of the code is a long list of condition ...