readability

How do I write more maintainable regular expressions?

不羁的心 提交于 2019-11-28 14:37:28
问题 I have started to feel that using regular expressions decreases code maintainability. There is something evil about the terseness and power of regular expressions. Perl compounds this with side effects like default operators. I DO have a habit of documenting regular expressions with at least one sentence giving the basic intent and at least one example of what would match. Because regular expressions are built up I feel it is an absolute necessity to comment on the largest components of each

Reference equality performance difference? ((object)obj1 == (object)obj2) vs. object.ReferenceEquals( obj1, obj2 )

一笑奈何 提交于 2019-11-28 10:47:59
Is there extra overhead in using the object.ReferenceEquals method verses using ((object)obj1 == (object)obj2) ? In the first case, there would be a static method call involved, and in both cases some form of casting to an object would be involved. Even if the compiler balances out those methods, what about inequality? (object)obj != null as compared to... !object.ReferenceEquals(obj,null) I suppose that at some point, a logical negation would occur, either within the != operator, or as applied to the result of the ReferenceEquals method. What do you think? There's also the issue of

Making large constants in C source more readable?

别说谁变了你拦得住时间么 提交于 2019-11-28 03:38:18
问题 I'm working on some code for a microprocessor. It has a few large, critical constants. #define F_CPU 16000000UL In this case, this is the CPU frequency. In Hertz. As it is, it's rather hard to tell if that's 1,600,000, 160,000,000 or 16,000,000 without manually tabbing a cursor across the digits. If I put commas in the number #define F_CPU 16,000,000UL , it truncates the constant. I've worked with a few esoteric languages that have a specific digit-separator character, intended to make large

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

半腔热情 提交于 2019-11-28 02:33:07
问题 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 may vary: usually the bitwise operations (such as the logial-and here) are far more efficient than a mod (or div) operation. Of course, you may argue that some

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

你说的曾经没有我的故事 提交于 2019-11-27 21:51:04
This question already has an answer here: Creating multiline strings in JavaScript 37 answers It doesn't really have to add newlines, just something readable. Anything better than this? str = "line 1" + "line 2" + "line 3"; 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 where the commas are. No syntax errors. I like this version (different than yours just in formatting of the code): var str = "line 1"

Implicit return values in Ruby

杀马特。学长 韩版系。学妹 提交于 2019-11-27 20:37:13
I am somewhat new to Ruby and although I find it to be a very intuitive language I am having some difficulty understanding how implicit return values behave. I am working on a small program to grep Tomcat logs and generate pipe-delimited CSV files from the pertinent data. Here is a simplified example that I'm using to generate the lines from a log entry. class LineMatcher class << self def match(line, regex) output = "" line.scan(regex).each do |matched| output << matched.join("|") << "\n" end return output end end end puts LineMatcher.match("00:00:13,207 06/18 INFO stateLogger -

Why are “continue” statements bad in JavaScript?

半世苍凉 提交于 2019-11-27 19:57:33
In the book Javascript: The Good Parts by Douglas Crockford, this is all the author has to say about the continue Statement: The continue statement jumps to the top of the loop. I have never seen a piece of code that was not improved by refactoring it to remove the continue statement. This really confuses me. I know Crockford has some very opinionated views on JavaScript, but this just sounds entirely wrong to me. First of all, continue does more than just jump to the top of a loop. By default, it also progresses to the next iteration. So isn't Crockford's statement just completely false

Code to calculate “median of five” in C#

放肆的年华 提交于 2019-11-27 18:32:28
Note: Please don't interpret this as "homework question." This is just a thing I curious to know :) The median of five is sometimes used as an exercise in algorithm design and is known to be computable using only 6 comparisons . What is the best way to implement this "median of five using 6 comparisons" in C# ? All of my attempts seem to result in awkward code :( I need nice and readable code while still using only 6 comparisons. public double medianOfFive(double a, double b, double c, double d, double e){ // // return median // return c; } Note: I think I should provide the "algorithm" here

Improving Code Readability [closed]

谁说我不能喝 提交于 2019-11-27 10:21:26
问题 When it comes to code documentation, it is usually accepted that code should explain itself , and inline code documentation (excluding public API documentation) should only explain meta-code issues such as workarounds, explanations on why specific implementations were chosen, etc. How do you accomplish making your code more readable and more explaining itself ? Edit: in addition to general comments, I'm also looking for specific tips. So if you say "short but meaningful variable names", it

Boolean method naming readability

社会主义新天地 提交于 2019-11-27 09:49:56
问题 Simple question, from a readability standpoint, which method name do you prefer for a boolean method: public boolean isUserExist(...) or: public boolean doesUserExist(...) or: public boolean userExists(...) 回答1: public boolean userExists(...) Would be my prefered. As it makes your conditional checks far more like natural english: if userExists ... But I guess there is no hard and fast rule - just be consistent 回答2: I would say userExists , because 90% of the time my calling code will look