readability

How to self-document a callback function that is called by template library class?

本小妞迷上赌 提交于 2019-12-03 09:55:16
I have a function User::func() (callback) that would be called by a template class ( Library<T> ). In the first iteration of development, everyone know that func() serves only for that single purpose. A few months later, most members forget what func() is for. After some heavy refactoring, the func() is sometimes deleted by some coders. At first, I didn't think this is a problem at all. However, after I re-encountered this pattern several times, I think I need some counter-measure. Question How to document it elegantly? (cute && concise && no additional CPU cost) Example Here is a simplified

Flesch-Kincaid Readability: Improve PHP function

心不动则不痛 提交于 2019-12-03 08:22:02
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*$total_words/$total_sentences)-(84.6*$total_syllables/$total_words); return $score; } Do you have

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

安稳与你 提交于 2019-12-03 07:24:34
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? Refactor your code into smaller work units. Too much conditional logic is a code-smell and usually indicates that your function needs to be refactored. Try to

Splitting C++ Strings Onto Multiple Lines (Code Syntax, Not Parsing)

霸气de小男生 提交于 2019-12-03 04:04:15
问题 Not to be confused with how to split a string parsing wise, e.g.: Split a string in C++? I am a bit confused as to how to split a string onto multiple lines in c++. This sounds like a simple question, but take the following example: #include <iostream> #include <string> main() { //Gives error std::string my_val ="Hello world, this is an overly long string to have" + " on just one line"; std::cout << "My Val is : " << my_val << std::endl; //Gives error std::string my_val ="Hello world, this is

Design of an Alternative (Fluent?) Interface for Regular Expressions

主宰稳场 提交于 2019-12-03 02:42:38
问题 I've just seen a huge regex for Java that made me think a little about maintainability of regular expressions in general. I believe that most people - except some badass perl mongers - would agree that regular expressions are hardly maintainable. I was thinking about how this situation could be fixed. So far, the most promising idea I have is using a fluent interface. To give an example, instead of: Pattern pattern = Pattern.compile("a*|b{2,5}"); one could write something like this import

How “self-documenting” can code be without being annoying? [closed]

北慕城南 提交于 2019-12-02 18:49:46
I am not sure what the best practices are here, but I often see abbreviated variable names especially when the scope is small. So (to use simple Ruby examples) instead of def add_location(name, coordinates) , I see things like def add_loc(name, coord) —and I might even see something like def add_loc(n, x, y) . I imagine that longer names might tire a person out when they're accustomed to seeing abbreviations. Does verbosity help readability, or does it just hurt everyone's eyes?—Do people prefer abbreviations and shortened names over longer names? Personally, I would MUCH rather see longer

Splitting C++ Strings Onto Multiple Lines (Code Syntax, Not Parsing)

瘦欲@ 提交于 2019-12-02 17:23:10
Not to be confused with how to split a string parsing wise, e.g.: Split a string in C++? I am a bit confused as to how to split a string onto multiple lines in c++. This sounds like a simple question, but take the following example: #include <iostream> #include <string> main() { //Gives error std::string my_val ="Hello world, this is an overly long string to have" + " on just one line"; std::cout << "My Val is : " << my_val << std::endl; //Gives error std::string my_val ="Hello world, this is an overly long string to have" & " on just one line"; std::cout << "My Val is : " << my_val << std:

Python: if not val, vs if val is None

梦想与她 提交于 2019-12-02 16:59:40
I've always coded in the style of if not value , however, a few guides have brought to my attention that while this style works, it seems to have 2 potential problems: It's not completely readable; if value is None is surely more understandable. This can have implications later (and cause subtle bugs), since things like [] and 0 will evaluate to False as well. I am also starting to apply this idea to other comparisons, such as: if not value vs if value is False if not value vs if value is [] And so goes the list... The question is, how far do you go with the principle? Where to draw the line,

Design of an Alternative (Fluent?) Interface for Regular Expressions

狂风中的少年 提交于 2019-12-02 16:17:13
I've just seen a huge regex for Java that made me think a little about maintainability of regular expressions in general. I believe that most people - except some badass perl mongers - would agree that regular expressions are hardly maintainable. I was thinking about how this situation could be fixed. So far, the most promising idea I have is using a fluent interface . To give an example, instead of: Pattern pattern = Pattern.compile("a*|b{2,5}"); one could write something like this import static util.PatternBuilder.* Pattern pattern = string("a").anyTimes().or().string("b").times(2,5).compile

Why is the code in most STL implementations so convoluted?

霸气de小男生 提交于 2019-12-02 15:21:36
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 would not pass code-review at the places I've worked at for reasons varying from variable naming,