readability

Improving legibility on conditional statement

时光毁灭记忆、已成空白 提交于 2020-01-05 10:30:25
问题 I am building a HTTP server for my android device. I am using a lot of IF-ELSE statements to handle differnt requests. As I will be sharing my code with other people for later use, I will have to make it as legible as possible. Right now, I can't even read my code with ease. I think the problem comes from using a lot of IF-ELSE statements in one class. For example. if(purpose.equals("readProfile"){ ..... } else if(purpose.equals("writeProfile"){ ..... } .... I tried classifying them in

Readability vs Performance comparison

对着背影说爱祢 提交于 2020-01-05 08:28:27
问题 I was reviewing some of the code present in a project I'm working on and found things like these: string personName = currentPerson.Name; personModel.editPerson(idNr, personName); The above is a simple example but it may as well be like below: string idNr= currentPerson.IdNr; string personName = currentPerson.Name; string age = currentPerson.Age; ... string editor = AuthenticatedUser.Name; personModel.editPerson(idNr, personName, age, gender, whatever, nationality, ..., currentTime, editor,

How to serialize a python dict to text, in a human-readable way?

独自空忆成欢 提交于 2020-01-04 14:00:45
问题 I have an python dict whose keys and values are strings, integers and other dicts and tuples (json does not support those). I want to save it to a text file and then read it from the file. Basically, I want a read counterpart to the built-in print (like in Lisp). Constraints: the file must be human readable (thus pickle is out) no need to detect circularities. Is there anything better than json? 回答1: You could use repr() on the dict , then read it back in and parse it with ast.literal_eval()

naming of physical quantities in python

被刻印的时光 ゝ 提交于 2020-01-02 10:18:02
问题 I would like to establish a good naming scheme for physical/mathematical quantities used in my simulation code. Consider the following example: from math import * class GaussianBeamIntensity(object): """ Optical intensity profile of a Gaussian laser beam. """ def __init__(self, intensity_at_waist_center, waist_radius, wavelength): """ Arguments: *intensity_at_waist_center*: The optical intensity of the beam at the center of its waist in W/m^2 units. *waist_radius*: The radius of the beam

Introducing variables only for readability?

最后都变了- 提交于 2020-01-02 07:13:12
问题 Is it a good idea to introduce variables only for the sake of readability ? Example 1: while(nameNode1.charAt(0) == nameNode2.charAt(0) && nameNode1.length() > 1 && nameNode2.length() > 1) { nameNode1 = nameNode1.substring(1, nameNode1.length()); nameNode2 = nameNode2.substring(1, nameNode2.length()); } Example 2: boolean letterFromBothNodesAreEqual_andNameHasMoreThanOneLetter = nameNode1.charAt(0) == nameNode2.charAt(0) && nameNode1.length() > 1 && nameNode2.length() > 1; while

Optimized algorithm for converting a decimal to a “pretty” fraction

旧时模样 提交于 2020-01-02 04:39:09
问题 Rather than converting an arbitrary decimal to an exact fraction (something like 323527/4362363), I am trying to convert to just common easily-discernible (in terms of human-readability) quantities like 1/2, 1/4, 1/8 etc. Other than using a series of if-then, less than/equal to etc comparisons, are there more optimized techniques to do this? Edit: In my particular case, approximations are acceptable. The idea is that 0.251243 ~ 0.25 = 1/4 - in my usage case, that's "good enough", with the

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

大憨熊 提交于 2020-01-01 03:09:12
问题 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 feasible; Are there other tips that can reduce if else statements, especially nested if-else statements? 回答1: Refactor your code into smaller work units. Too

Why is the code in most STL implementations so convoluted?

寵の児 提交于 2019-12-31 08:10:53
问题 The STL is a critical piece of the C++ world, most implementations derive from the initial efforts by Stepanov and Musser. My question is given the criticality of the code, and it being one of the primary sources for people to view examples of well written C++ for both awe and learning purposes: Why are the various implementations of the STL so disgusting to look at - convoluted and generally good examples of how not to write C++ code from an aesthetics point of view. The code examples below

What's the cleanest way to write a multiline string in JavaScript? [duplicate]

寵の児 提交于 2019-12-28 05:57:07
问题 This question already has answers here : Creating multiline strings in JavaScript (37 answers) Closed 6 years ago . It doesn't really have to add newlines, just something readable. Anything better than this? str = "line 1" + "line 2" + "line 3"; 回答1: Almost identical to NickFitz's answer: var str = ["" ,"line 1" ,"line 2" ,"line 3" ].join(""); // str will contain "line1line2line3" The difference, the code is slightly more maintainable because the lines can be re-ordered without regard to

What's the cleanest way to write a multiline string in JavaScript? [duplicate]

本秂侑毒 提交于 2019-12-28 05:57:06
问题 This question already has answers here : Creating multiline strings in JavaScript (37 answers) Closed 6 years ago . It doesn't really have to add newlines, just something readable. Anything better than this? str = "line 1" + "line 2" + "line 3"; 回答1: Almost identical to NickFitz's answer: var str = ["" ,"line 1" ,"line 2" ,"line 3" ].join(""); // str will contain "line1line2line3" The difference, the code is slightly more maintainable because the lines can be re-ordered without regard to