readability

How Can I Make This Python Code More Usable And Readable?

Beginner in python, but been programming for about 5 years now. I suspect I have a lot to learn about doing things the object oriented way, but I know the basics. I planned on programming a calculator that shows it's work for the challenge and knowledge i'll gain from it. I just started and this is what i've got, and it just looks really...

How can this 6 line method be refactored to be more readable?

Hi everyone, I'm trying to clean up this ridonkulously ugly method here, that's crying out for refactoring, but I'm not sure what kind of structure would do this best (i.e. a case statement, or simply a carefully formatted if then statements) At first glance, it looks like it would be an ideal place for a case statement with a few well...

query strings - use row ids or human readable values?

Building a form, I'm wondering if there is any significant advantage to having the query string show values in a more human readable format. example index.php?user=ted&location=newyork&...and so on rather than index.php?user=23423&location=34645&... and so on On the one hand, having the query string a little more readable allows the ...

How to reduce the number of if-else statements in PHP?

I found that there are many if-else statements, especially nested if else statements, these statements make my code less readable. How to reduce the number of if else statements in PHP? My tips are as follows: 1.Use a switch statement when it is suitable; 2.use exit() statement when it is feasible; 3. Use ternary statement when it is fe...

Colorize negative/positive numbers (jQuery)

I'd like to color numbers in a table for better readability:  green for positive (+00.00); red for negative (-00.00) and; black for default case (no sign) Many thanks! ...

Have any identifier case readability studies been done?

Different languages have different standards for identifier casing. Most discussion I see about this subject revolves around a specific language, and is loaded with personal opinions. And these strong opinions all stem from the decision of one person or a group of people who created the language. I'd love to see a graph showing just how...

Elegant coding, how to deal with hidden/invisible HTML code ?

I am developing an ask-and-answer website. There is a "Choose as best answer" button besides each answer, this button should be visible to the asker but should be invisible to other viewers. Other part of the web page is almost the same. So how can I code this web page? Should I check the viewer identity every time to determine whether o...

Would you use num%2 or num&1 to check if a number is even?

Well, there are at least two low-level ways of determining whether a given number is even or not: 1. if (num%2 == 0) { /* even */ } 2. if ((num&1) == 0) { /* even */ } I consider the second option to be far more elegant and meaningful, and that's the one I usually use. But it is not only a matter of taste; The actual performance ma...

Should I make my python code less fool-proof to improve readability?

I try to make my code fool-proof, but I've noticed that it takes a lot of time to type things out and it takes more time to read the code. Instead of: class TextServer(object): def __init__(self, text_values): self.text_values = text_values # <more code> # <more methods> I tend to write this: class TextServer...

two-way list comparison in C# unit test

In my C# unit tests, I often query for a list of rows based on a list of IDs. I then want to ensure that 1) for all the IDs, there was at least one row found that has that ID and 2) for all the returned rows, each row has an ID that is in the list of IDs to find. Here is how I usually ensure that: Assert.IsTrue(ids.All( id => resu...

Is It Possible To Simplify This Branch-Based Vector Math Operation?

I'm trying to achieve something like the following in C++: class MyVector; // 3 component vector class MyVector const kA = /* ... */; MyVector const kB = /* ... */; MyVector const kC = /* ... */; MyVector const kD = /* ... */; // I'd like to shorten the remaining lines, ideally making it readable but less code/operations. MyVector ...

Simplifying if statement logic

I have seperated out a test to determine if two schedule items overlap because of the unreadability of it. Is there any application to assist in simplifying a logic statement? Example: (originally a faulty example, but exposes reasons I request this) if (x < y && y < z && x < z) could be reduced to if (x < y && y < z) My code ...

Obfuscated C# Code

Years ago their used to be a contest to see who could produce the most obfuscated C code, and some of the results were dramatically unreadable. C was like that. You could really screw things up with the preprocessor in particular. However, many of the newer features of C# offer an amazing opportunity to obfuscate code to. I was wonderin...

Adding whitespace to web page source so that I can read it.

I'm curious about the web page I'm viewing. I use the "view--page source" and get a window with the html. I cut and paste this into notepad++. I manually parse through adding whitespace to make it readable to me. Is there a better way to do the last step? I'm hoping something has been written which automates this process, giving the...

How to make long parameter lists readable?

I've developed a natural aversion to long parameter lists in functions. While this is to some extent a good thing, sometimes long parameter lists are the lesser of two evils compared to code duplication or ridiculously long functions due to "manual inlining". What's a good way to at least make some of these monstrosities human-readable...

Readability of heavy function call nesting?

I've often seen it argued that heavily nested function calls should not be used because they are unreadable. However, using temporary variables instead creates a lot of unnecessary verbosity and forces the reader to mentally link each temporary variable to what it represents. When looking at the way Lisp code is typically formatted, it...

foreach(... in ...) or .ForEach(); that is the question

This is a question about coding for readability. I have an XDocument and a List<string> of the names of the elements that contain sensitive information that I need to mask (replace with underscores in this example). XDocument xDoc; List<string> propertiesToMask; This can be written in two ways, using traditional foreach loops, or usi...

Coding style - keep parentheses on the same line or new line?

Suppose you are calling a function, where there's clearly a need to break down the statement into few lines, for readability's sake. However there are at least two way to do it: Would you do this: return render(request, template, { 'var1' : value1, 'var2' : value2, 'var3' : ...

How to get levels for Fry Graph readability formula?

Hi, I'm working in an application (C#) that applies some readability formulas to a text, like Gunning-Fog, Precise SMOG, Flesh-Kincaid. Now, I need to implement the Fry-based Grade formula in my program, I understand the formula's logic, pretty much you take 3 100-words samples and calculate the average on sentences per 100-words and s...

Readability and IF-block brackets: best practice

I am preparing a short tutorial for level 1 uni students learning JavaScript basics. The task is to validate a phone number. The number must not contain non-digits and must be 14 digits long or less. The following code excerpt is what I came up with and I would like to make it as readable as possible. if ( //set of rules for invalid...