182
votes
Accepted
Is there a specific reason for the poor readability of regular expression syntax design?
There is one big reason why regular expressions were designed as terse as they are: they were designed to be used as commands to a code editor, not as a language to code in. More precisely, ed was one ...
124
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 ...
120
votes
Accepted
Is it inadvisable to make a function that essentially renames a built-in function?
As others have already mentioned: don't create a function with a name that is similar to that of a builtin, standard-library or generally widely used function but change its behavior. It is possible ...
114
votes
Is it inadvisable to make a function that essentially renames a built-in function?
If you make a function like that where minimize(4, 10) returns 10, then I'd say that is inadvisable because your fellow programmers may strangle you.
(Okay, maybe they will not literally strangle you ...
88
votes
Accepted
Is a big boolean expression more readable than the same expression broken down into predicate methods?
What is easier to understand
The latter approach. It's not only easier to understand but it is easier to write, test, refactor and extend as well.
Each required condition can be safely decoupled and ...
67
votes
Accepted
Filtering foreach loops with a where condition vs continue guard clauses
I would regard this as an appropriate place to use command/query separation. For example:
// query
var validItems = items.Where(i => i.Field != null && i.State != ItemStates.Deleted);
// ...
51
votes
Is splitting up a function into several inner functions an anti-pattern?
Inner functions are not an anti-pattern, they are a feature.
If it doesn't make sense to move the inner functions outside, then by all means, don't.
On the other hand, it would be a good idea to ...
51
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 ...
44
votes
Is a big boolean expression more readable than the same expression broken down into predicate methods?
If this is the only place these predicate functions would be used, you can also use local bool variables instead:
private static bool ContextMatchesProp(CurrentSearchContext context, TValToMatch ...
41
votes
Is a big boolean expression more readable than the same expression broken down into predicate methods?
In general, the latter is preferred.
It makes the call site more reusable. It supports DRY (meaning you have less places to change when the criteria change, and can do it more reliably). And very ...
39
votes
Is there a specific reason for the poor readability of regular expression syntax design?
Historical perspective
The Wikipedia article is quite detailed about the origins of regular expressions (Kleene, 1956). The original syntax was relatively simple with only *, +, ?, | and grouping (......
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()...
34
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 ...
29
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
Specify optional parameter names even though not required?
I'd say that in the C# world, an enum would be one of the best options here.
With that you'd be forced to spell out what you're doing and any user would see whats happening. It's also safe for future ...
27
votes
How to remove a function or feature when using TDD
Removing a public method is not "refactoring" -- refactoring is changing the implementation while continuing to pass existing tests.
However, removing an unneeded method is a perfectly reasonable ...
26
votes
Is it inadvisable to make a function that essentially renames a built-in function?
Aliasing a function is fine, but don't try to change the meaning of existing terms
It's OK to create an alias of the function - common libraries do that all the time.
However, it's a bad idea to use ...
25
votes
Are `break` and `continue` bad programming practices?
Yes you can [re]write programs without break statements (or returns from the middle of loops, which do the same thing). But you may have to introduce additional variables and/or code duplication both ...
Community wiki
25
votes
Is there a specific reason for the poor readability of regular expression syntax design?
The biggest problem with regex isn't the overly terse syntax, it's that we try to express a complex definition in a single expression, instead of composing it from smaller building blocks. This is ...
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, ...
23
votes
Is a big boolean expression more readable than the same expression broken down into predicate methods?
If it's between these two choices, then the latter is better. These are not the only choices, however! How about breaking up the single function into multiple ifs? Test for ways to exit the function ...
20
votes
Accepted
What should plugins use: hooks, events or something else?
The main difference between a hook and event is loose coupling versus tight coupling.
A hook is a generic way to broadcast that something has happened. You can add new hooks without having to ...
20
votes
Specify optional parameter names even though not required?
does it make sense to write:
var ids=ReturnEmployeeIds(includeManagement: true);
It is debatable if this is "good style", but IMHO this is as least not bad style, and useful, as long as the code ...
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.
20
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'...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
readability × 194coding-style × 36
code-quality × 27
java × 21
c# × 20
maintainability × 15
design × 13
programming-practices × 13
javascript × 12
clean-code × 12
coding-standards × 11
refactoring × 10
object-oriented × 9
performance × 9
functions × 9
c++ × 7
language-agnostic × 7
optimization × 7
design-patterns × 6
functional-programming × 6
naming × 6
exceptions × 6
maintenance × 6
architecture × 5
php × 5