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
3
votes
4 answers

From-Import while retaining access by module

The title is a little hard to understand, but my question is simple. I have a program that needs to take the sqrt() of something, but that's the only thing I need from math. It seems a bit wasteful to import the entire module to grab a single…
Schilcote
  • 2,344
  • 1
  • 17
  • 35
3
votes
3 answers

Switch statement into while loop or while loop into case blocks?

I'm refactoring some code I wrote and I have to decide if I wanna put a switch statement into a while loop or repeat the while loop into each case blocks. The two different ways are something like the following pseudocode: Option 1) while (cnt > 0)…
3
votes
3 answers

Is there a more readable way of making a long chain of && statements?

Suppose I have a long, complex list of conditions, which must be true in order for an if statement to run. if(this == that && foo != bar && foo != that && pins != needles && apples != oranges) { DoSomethingInteresting(); } Typically, if I'm…
Iain Fraser
  • 6,578
  • 8
  • 43
  • 68
3
votes
4 answers

Why use LINQ-expressions?

I use Resharper, and when I make a few lines of code like this: foreach (var posCombination in possibleCombinations) { if (posCombination .Count == combo.Count && posCombination .Select((l, i) => combo.Contains(l)).All(b => b)) { …
Nate-Wilkins
  • 5,364
  • 4
  • 46
  • 61
3
votes
2 answers

More readable way for null reference checks

In a lot of apps I write, I find myself assigning a default value for a subclass of a DBML object. Here is an example of the current way I'm having to do it: var viewModel = new RandomViewModel { ContactName = (Order.Customer != null ?…
arserbin3
  • 6,010
  • 8
  • 36
  • 52
2
votes
4 answers

Working with IComparable.Compare without magic numbers

I really hate working with IComparer - to this day, after years of working with .Net, I still get regularly confused by those 1s and -1s. Can I somehow replace the Compare result values with some self-explanatory names without having to cast the…
Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
2
votes
1 answer

What is the best way to format long chained methods?

What is the best way to format code with chained methods? Especially if it goes on for a long time? If you have a chain of three or so, you can put it on one line, but it gets cumbersome after you have a lot and it makes debugging difficult. FYI,…
Joe
  • 7,922
  • 18
  • 54
  • 83
2
votes
1 answer

How can I access elements of cv::Vec4f by readable name?

I'm using the cv::HoughCircles() function from OpenCV to detect circles in an image. The function takes an output parameter of type cv::OutputArray to provide the results. Based on the example code I am using a std::vector as output…
oliver
  • 6,204
  • 9
  • 46
  • 50
2
votes
2 answers

Java: Avoid too many parameters in constructor

I have multiple service classes ServiceTest1, ServiceTest2, SeviceTest3 I have a factory class UserFactory, which creates a new User instance on every request UserFactory has a method createUser() which gets called for every request Currently,…
MrRobot9
  • 2,402
  • 4
  • 31
  • 68
2
votes
2 answers

Defining the values of a field in a database in the Laravel code to make the code more readable

Currently, I am writing a laravel application with a part for sending messages between the staff at the company and the clients. So, I have a field named "status" in the database. In this field, a value of one indicates that the message is waiting…
ZOHREH
  • 37
  • 6
2
votes
1 answer

Initialize C struct vs assign members

I want to know what is the best practice for initializing struct members. For instance, for this struct which option is better and why? In terms of especially readability and performance? typedef struct { int a; char b; } str_t; Option…
uffu
  • 43
  • 6
2
votes
11 answers

How to make this code more readable

This is a part fo php code, which use the contentArray, which is a JSON, and generate the UI to the user, it generate html tags, also, it generate js code too.... It works, but I think the code is pretty difficult to read and maintain, any ideas??? …
Tattat
  • 15,548
  • 33
  • 87
  • 138
2
votes
1 answer

Is there a cost associated with frequent object attribute access?

One thing I mostly follow when writing code is if I see some object-attribute or method is being accessed multiple times within the same method, I tend to assign it to a variable and re-use it everywhere. List itemAList = alpha.doX(..., ...,…
Shabirmean
  • 2,341
  • 4
  • 21
  • 34
2
votes
1 answer

Shorten an if statement whose statement are reversed

I have the following situation: string = "abc" if (string[1] == "b" and string[2] == "c") or (string[1] == "c" and string[2] == "b"): print("ERROR") Is there a solution to shorten this in a pythonic way? I see that (string[1] == "b" and…
Larsus
  • 101
  • 1
  • 12
2
votes
2 answers

Indentation when using multi-line strings

I sometimes need to use multi-line strings, but in a nested block. This works, but the readability is really poor: CONDITION1 = CONDITION2 = CONDITION3 = True if CONDITION1: if CONDITION2: s = """jkljkj dfkjslfds sqjdlqkj""" elif…
Basj
  • 41,386
  • 99
  • 383
  • 673