Questions tagged [readability]
Readability measures how easy code is to read and understand.
198
questions
0
votes
1
answer
54
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 ...
3
votes
2
answers
163
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
706
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 ...
1
vote
1
answer
280
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
108
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
347
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
123
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
201
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
299
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 ...
12
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
8k
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
218
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
218
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
2k
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
2k
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
229
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
188
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
633
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 ...
0
votes
2
answers
502
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
353
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
190
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
141
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
121
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 ...
1
vote
2
answers
305
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
441
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
256
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
162
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
393
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
3k
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
539
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
679
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
884
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
163
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
418
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
1k
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
281
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
261
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
195
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
189
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 ...
5
votes
3
answers
451
views
How to avoid falling into the trap of not commenting code? [duplicate]
I've been working on a project for about a month now. I am extremely familiar with the code and understand it to the extent where I feel that it is so easy to understand, that most methods don't need ...
9
votes
5
answers
691
views
Is it possible to make long code representing a computation easier to read?
Long methods are generally considered bad, however in my code I have some hard-to-understand long methods (more than 50 lines). I have trouble making those methods easier to read because a single ...
15
votes
4
answers
7k
views
Is it wrong to use flags for "grouping" enums?
My understanding is that [Flag] enums are typically used for things that can be combined, where the individual values aren't mutually exclusive.
For example:
[Flags]
public enum SomeAttributes
{
...
-1
votes
2
answers
177
views
Database optimization vs readibility
I'm working on some code that checks for some invariants in the database. For example to check that an element is not repeated the code checks for a database error since the invariant is coded at ...
146
votes
6
answers
16k
views
Are private methods with a single reference bad style?
Generally I use private methods to encapsulate functionality that is reused in multiple places in the class. But sometimes I have a large public method that could be broken up into smaller steps, each ...
6
votes
1
answer
685
views
Use `ref` merely for clarification?
I wrote a FillSomeData(ParameterData param, Result res) method, which populates res with some data (just like it says on the label).
Result is a class, not a struct, so FillSomeData actually affects ...
1
vote
3
answers
187
views
Optimizing polymorphic objects when there's only one implementation
Let's say I have an interface called ParentClass. ParantClass has two implementations, ParentClassA and ParentClassB. There is also the ChildClass interface, with a ChildClassA and ChildClassB ...