129
votes
Accepted
Readability versus maintainability, special case of writing nested function calls
If you felt compelled to expand a one liner like
a = F(G1(H1(b1), H2(b2)), G2(c1));
I wouldn't blame you. That's not only hard to read, it's hard to debug.
Why?
It's dense
Some debuggers will ...
52
votes
Readability versus maintainability, special case of writing nested function calls
On the other hand, the more processing you put on a line, the more logic you get on one page, which enhances readability.
I utterly disagree with this. Just looking at your two code examples calls ...
41
votes
Accepted
Why don't programming languages or IDEs support attaching descriptive metadata to variables?
Why doesn't it exist?
The direct answer here is that this isn't technically impossible, no one has just been sufficiently bothered enough by it to create a field-spanning standard that everyone can ...
39
votes
Why don't programming languages or IDEs support attaching descriptive metadata to variables?
TLDR
Decomposition makes names shorter. Do not fight symptoms, solve underlying problem instead.
Scope is important
Naming is the hardest problem, not because long names are not readable, but because ...
36
votes
Are private methods with a single reference bad style?
It's probably a great idea!
I do take issue with splitting up long linear sequences of action into separate functions purely to reduce the average function length in your codebase:
function step1()...
35
votes
Is it an (anti-)pattern for a function to have an argument to decide which other function to call?
Does this pattern have a name,
That depends on who you ask. Some folk treat patterns as only applicable to OOP and see them as more like implementation patterns in that, for example the UML used in ...
30
votes
Is it bad practice create "alias" variables to not use globals or arguments with long names in a method?
In general, creating local variables for readability is a good thing. A local variable gives a locally relevant name to something; that same thing might have a different name in a different context.
...
28
votes
Why don't programming languages or IDEs support attaching descriptive metadata to variables?
Java and C# at least have this sort of feature. Javadoc is the system for it in the Java context, Documentation comments in C# context. Indeed, that’s how IDEs provide on-hover info at all.
Generally ...
25
votes
Accepted
Use `ref` merely for clarification?
No.
For anyone who has understood what the ref keyword means, this obfuscates what the method really does. The better alternative is to pick a more descriptive name for such a method like
...
25
votes
Readability versus maintainability, special case of writing nested function calls
Your first example, the single-assignment-form, is unreadable because the chosen names are utterly meaningless. That might be an artifact of trying not to disclose internal information on your part, ...
22
votes
Readability versus maintainability, special case of writing nested function calls
As always, when it comes to readability, failure is in the extremes. You can take any good programming advice, turn it into a religious rule, and use it to produce utterly unreadable code. (If you don'...
20
votes
Accepted
How should I write a test for a pure method which doesn't return anything?
Most test frameworks have an explicit assertion for "Doesn't throw", e.g. Jasmine has expect(() => {}).not.toThrow(); and nUnit and friends also have one.
19
votes
Is there a programming paradigm that promotes making dependencies extremely obvious to other programmers?
Discoverability
Its absence plagues many organizations. Where is that tool that Fred built again? In the Git repository, sure. Where?
The software pattern that comes to mind is Model-View-ViewModel....
19
votes
Are private methods with a single reference bad style?
The answer is it highly depends on the situation. @Snowman covers the positives of breaking up large public function, but its important to remember there can be negative effects too, as you are ...
19
votes
Accepted
DRY principle often makes my code more complicated and/or more difficult to understand
DRY absolutely does not mean "use minimum number of lines possible", or "do not write code that looks like other code"
DRY refers to having code that does the same thing in two different places. But ...
17
votes
Is it possible to make long code representing a computation easier to read?
You wrote
Is such hard-to-read code considered bad code
so you definitely agree it is hard-to-read code, and if it is hard to read, it is hard to maintain and evolve - so I guess you consider the ...
15
votes
Is it an (anti-)pattern for a function to have an argument to decide which other function to call?
Multiple Dispatch
I believe the pattern you describe is called multiple dispatch.
This is a form of polymorphism. You can compare it to subtype polymorphism of the kind offered by OOP languages that ...
15
votes
Is it an (anti-)pattern for a function to have an argument to decide which other function to call?
One potential issue with this pattern is that the knowledge of which implementation should be used appears to be encoded in more than one place. That is, there appears to be a 1:1 mapping between an ...
14
votes
Readability versus maintainability, special case of writing nested function calls
@Dominique, I think in your question's analysis, you're making the mistake that "readability" and "maintainability" are two separate things.
Is it possible to have code that is maintainable but ...
13
votes
Accepted
How to write Readable Comment when we have to deal with IDs?
You should not use integer id's directly in logic. You should define constants (or enums or whatever is appropriate for your language) corresponding to the id's and then use the names. E.g.
...
12
votes
Why don't programming languages or IDEs support attaching descriptive metadata to variables?
It's a good question, and some IDEs do have the functionality you suggest, but virtually all programming languages and IDEs have the simple ability to add comments like so:
// The lowest integer ...
11
votes
Accepted
Why are functional-style chained map operations considered hard to read?
Everyone talks about code readability as a positive, but nobody actually agrees about what it means. The problem is readability depends on the person reading.
Your code use a particular approach, ...
11
votes
Is it a bad practice to use conditionals with functions that change program's state?
To me, the main code smell is that a createFile() method returns a boolean indicating success. That reminds me of programming styles from the 1970s.
A method named createFile() implies a contract ...
11
votes
Is it bad practice create "alias" variables to not use globals or arguments with long names in a method?
Creating aliases for the sole purpose of having a shorter variable name could be seen as a code smell.
In general, variable names should not be long enough to require aliases. They should be concise ...
10
votes
Is there a programming paradigm that promotes making dependencies extremely obvious to other programmers?
The best way to approach these sorts of problems is incrementally. Don't get frustrated and propose wide, sweeping architectural changes. Those will never get approved, and the code will never ...
10
votes
Accepted
Is it wrong to use flags for "grouping" enums?
This is definitely abusing enums and flags! It might work for you, but anybody else reading the code is going to be mightily confused.
If I understand correctly, you have a hierarchical ...
10
votes
What is the best way to make an if statement condition itself conditional?
add more functions:
buttonPress()
{
if(input)
{
process(a, b)
}
else
{
process(b, a)
}
}
process(a, b)
{
for(i=0 ...
}
9
votes
Why do some APIs simply throw Exception?
There is no good reason to do this. And in fact declaring a method as throws Exception makes it difficult to use. This is covered by this "example" in the Java Language documentation tree over on ...
9
votes
inheritance and polymorphism decrease readability
Neither inheritance nor polymorphism decreases readability. Using any OOP concept for the wrong reasons does.
Avoiding repeated code is not a good reason to use inheritance, you can do that by ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
readability × 205coding-style × 39
code-quality × 27
java × 23
c# × 21
design × 15
maintainability × 15
programming-practices × 14
clean-code × 13
javascript × 12
coding-standards × 11
refactoring × 10
object-oriented × 9
performance × 9
functions × 9
c++ × 8
naming × 8
design-patterns × 7
language-agnostic × 7
optimization × 7
functional-programming × 6
exceptions × 6
maintenance × 6
architecture × 5
php × 5