readability

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

巧了我就是萌 提交于 2019-12-05 11:06:22
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 latter more preferable for human readability in terms of a quick indicator (not used for calculation, just

How to write readable Javascript

若如初见. 提交于 2019-12-05 09:42:15
In JavaScript, the standard rules for code formatting don't seem to cut it. You still end up with messes of });}); all over the place and I don't believe I even know of established rules for the correct indention of anonymous functions declared as arguments to other functions. In short, I have trouble reading my own JavaScript, and I bet I'm not alone. I think the idea that I am coming around to is to just not define functions within other functions. Write every function with a name and as a child to the script tag. When you need to pass functions around, use their names. Of course, this makes

Access member variables directly or pass as parameter?

谁说我不能喝 提交于 2019-12-05 01:26:20
I noticed that even when paying respect to the single responsibility principle of OOD, sometimes classes still grow large. Sometimes accessing member variables directly in methods feels like having global state, and a lot of stuff exists in the current scope. Just by looking at the method currently working in, it is not possible anymore to determine where invidiual variables accessible in the current scope come from. When working together with a friend lately, I realized I write much more verbose code than him, because I pass member variables still as parameters into every single method. Is

Colorize negative/positive numbers (jQuery)

吃可爱长大的小学妹 提交于 2019-12-04 15:12:36
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) Here ya go: $(document).ready( function() { // the following will select all 'td' elements with class "of_number_to_be_evaluated" // if the TD element has a '-', it will assign a 'red' class, and do the same for green. $("td.of_number_to_be_evaluated:contains('-')").addClass('red'); $("td.of_number_to_be_evaluated:contains('+')").addClass('green'); } Then use CSS to style the input elements: td.red { color: red; } td.green { color: green; }

Flesch-Kincaid Readability: Improve PHP function

随声附和 提交于 2019-12-04 11:56:15
问题 I wrote this PHP code to implement the Flesch-Kincaid Readability Score as a function: function readability($text) { $total_sentences = 1; // one full stop = two sentences => start with 1 $punctuation_marks = array('.', '?', '!', ':'); foreach ($punctuation_marks as $punctuation_mark) { $total_sentences += substr_count($text, $punctuation_mark); } $total_words = str_word_count($text); $total_syllable = 3; // assuming this value since I don't know how to count them $score = 206.835-(1.015*

Flesch-Kincaid readability test

北城余情 提交于 2019-12-04 05:26:23
问题 Are there any opensource .Net libraries that handle Flesch-Kincaid readability calculations? Wiki: http://en.wikipedia.org/wiki/Flesch-Kincaid_readability_test 回答1: Not open source, but you could delegate to Word using the ReadabilityStatistic interface. Even if your document isn't in Word to begin with, you could open Word (invisibly to the user), dump your text into Word, and then use ReadabilityStatistic to calculate the statistics. 回答2: As described in the Flesch-Kincaid grade level

What is the prefered style for single decision and action statements?

会有一股神秘感。 提交于 2019-12-04 04:11:25
问题 In the case of languages that support single decision and action without brackets, such as the following example: if (var == true) doSomething(); What is the preferred way of writing this? Should brackets always be used, or should their usage be left as a preference of the individual developer? Additionally, does this practice depend on the size of the code block, such as in the following example: if (var == 1) doSomething(1); else if (var > 1 && var < 10) doSomething(2); else { validate(var)

How to cleanly keep below 80-char width with long strings?

扶醉桌前 提交于 2019-12-04 02:47:02
问题 I'm attempting to keep my code to 80 chars or less nowadays as I think it looks more aesthetically pleasing, for the most part. Sometimes, though, the code ends up looking worse if I have to put line breaks in weird places. One thing I haven't figured out how to handle very nicely yet is long strings. For example: #0.........1........2........3........4.........5.........6.........7.........8xxxxxxxxx9xxxxxx def foo(): if conditional(): logger.info("<Conditional's meaning> happened, so we're

Should one use < or <= in a for loop [closed]

天大地大妈咪最大 提交于 2019-12-03 18:26:32
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 months ago . If you had to iterate through a loop 7 times, would you use: for (int i = 0; i < 7; i++) or: for (int i = 0; i <= 6; i++) There are two considerations: performance readability For performance I'm assuming Java or C#. Does it matter if "less than" or "less than or equal to"

Is there a concise opposite of “is empty”?

筅森魡賤 提交于 2019-12-03 16:18:41
问题 Interfaces to string classes typically have of method named IsEmpty (VCL) or empty (STL). That's absolutely reasonable because it's a special case, but the code that uses these methods often has to negate this predicate, which leads to a "optical (and even psychological) overhead" (the exclamation mark is not very obvious, especially after an opening parenthesis). See for instance this (simplified) code: /// format an optional time specification for output std::string fmtTime(const std: