Questions tagged [code-readability]

Code-Readability is how easy it is to understand a piece of code. Whether it be reading someones code, or writing your own.

Code-Readability is how easy it is to understand a piece of code. Whether it be reading someones code, or writing your own. This includes Coding Conventions, positioning of white space, declaration of variables and among other coding practices.

337 questions
180
votes
11 answers

Studies on optimal code width?

If you enable the "View Right Margin" in your IDE of choice, it is likely that it will default to 80 characters. I tend to change it to 120 for no reason other than it was the standard at a company I was with a few years back, and no other company…
Fostah
  • 11,398
  • 10
  • 46
  • 55
113
votes
11 answers

`if key in dict` vs. `try/except` - which is more readable idiom?

I have a question about idioms and readability, and there seems to be a clash of Python philosophies for this particular case: I want to build dictionary A from dictionary B. If a specific key does not exist in B, then do nothing and continue…
LeeMobile
  • 3,775
  • 6
  • 31
  • 32
108
votes
6 answers

Is using java Map.containsKey() redundant when using map.get()

I have been wondering for some time whether it is allowable within best practice to refrain from using the containsKey() method on java.util.Map and instead do a null check on the result from get(). My rationale is that it seems redundant to do the…
Erik Madsen
  • 1,913
  • 3
  • 20
  • 34
79
votes
4 answers

How to deal with Pylint's "too-many-instance-attributes" message?

I have just tried to lint some code with Pylint, and the last remaining error is R0902: too-many-instance-attributes (8/7) I understand the rationale behind limiting the number of instance attributes, but seven seems a bit low. I also realise that…
76
votes
6 answers

What are the benefits to using anonymous functions instead of named functions for callbacks and parameters in JavaScript event code?

I'm new-ish to JavaScript. I understand many of the concepts of the language, I've been reading up on the prototype inheritance model, and I'm whetting my whistle with more and more interactive front-end stuff. It's an interesting language, but…
66
votes
12 answers

Python list comprehension - want to avoid repeated evaluation

I have a list comprehension which approximates to: [f(x) for x in l if f(x)] Where l is a list and f(x) is an expensive function which returns a list. I want to avoid evaluating f(x) twice for every non-empty occurance of f(x). Is there some way…
Stefan
  • 8,819
  • 10
  • 42
  • 68
53
votes
4 answers

Best ways to format LINQ queries

Before you ignore / vote-to-close this question, I consider this a valid question to ask because code clarity is an important topic of discussion, it's essential to writing maintainable code and I would greatly appreciate answers from those who have…
Aren
  • 54,668
  • 9
  • 68
  • 101
42
votes
4 answers

Out of line definition of template function vs in class

I wondered if there was any advantages of declaring templates function out of line vs in the class. I'm trying to get a clear understanding of the pros and cons of the two syntax. Here's an example: Out of line: template struct MyType { …
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
41
votes
9 answers

Code-style for indention of multi-line 'if' statement?

When indenting long if conditions, you usually do something like this (actually, PyDev indents like that): if (collResv.repeatability is None or collResv.somethingElse): collResv.rejected = True collResv.rejectCompletely() However, this…
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
34
votes
6 answers

How do I align/format code in Android Studio?

Is there a way/shortcut/built-in feature that can align code for operands, like '=' signs? For example, there is a XAlign for Xcode (https://github.com/qfish/XAlign), allowing the user to select code that needs to be aligned and use a shortcut to…
Daksharma
  • 599
  • 1
  • 7
  • 9
31
votes
10 answers

Is Code For Computers or for People?

Ultimately, code compiles down (eventually) into instructions for a CPU. Code, however, (in my humble opinion) is for human beings to read, update, and interact with. This leads me to the following observation: Code that is unreadable by other…
haseman
  • 11,213
  • 8
  • 41
  • 38
30
votes
6 answers

A better way to test the value of an Option?

I often find myself with an Option[T] for some type T and wish to test the value of the option against some value. For example: val opt = Some("oxbow") if (opt.isDefined && opt.get == "lakes") //do something The following code is equivalent and…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
27
votes
3 answers

What is Cognitive Complexity in sonar report?

Now a days i switched to sonar reports for static code review and performance improvement. Under the rules section I found that the cognitive complexity of my methods are high. You can find cognitive complexity error in sonar as: Go to…
Akshay Paliwal
  • 3,718
  • 2
  • 39
  • 43
26
votes
6 answers

Does the Java compiler optimize an unnecessary ternary operator?

I’ve been reviewing code where some coders have been using redundant ternary operators “for readability.” Such as: boolean val = (foo == bar && foo1 != bar) ? true : false; Obviously it would be better to just assign the statement’s result to the…
Bakna
  • 535
  • 1
  • 5
  • 13
26
votes
14 answers

Code formatting: is lining up similar lines ok?

I recently discovered that our company has a set of coding guidelines (hidden away in a document management system where no one can find it). It generally seems pretty sensible, and keeps away from the usual religious wars about where to put '{'s…
Ned
  • 2,142
  • 17
  • 24
1
2 3
22 23