code-readability

Would VS2008 c++ compiler optimize the following if statement?

谁说胖子不能爱 提交于 2019-12-23 06:04:15
问题 if (false == x) { ...} as opposed to: if (!x) { ... } and if (false == f1()) { ...} as opposed to: if (!f1()) { ... } I think the if(false == ... version is more readable. Do you agree, or have another trick you can propose? Will it be just as fast? Thanks. This is why I do not like !x: if (25 == a->function1(12345, 6789) && 45 == b->function1(12345, 6789) && !c->someOtherFunction(123)) { ... } The following seems better: if (25 == a->function1(12345, 6789) && 45 == b->function1(12345, 6789)

Method call overhead

我只是一个虾纸丫 提交于 2019-12-22 12:58:29
问题 What's the overhead of calling a method in .NET which returns immediately (because a condition which is the first code inside is not met)? I'd argue that no matter how real-time your application is, that overhead is negligible and that in any case profiling should show the facts, readability being more important, but some co-workers do not agree with me and say that you should always avoid those calls... Any other opinion? 回答1: I dare say there are some edge cases where it could be

How can I implement NotOfType<T> in LINQ that has a nice calling syntax?

风流意气都作罢 提交于 2019-12-17 20:04:43
问题 I'm trying to come up with an implementation for NotOfType , which has a readable call syntax. NotOfType should be the complement to OfType<T> and would consequently yield all elements that are not of type T My goal was to implement a method which would be called just like OfType<T> , like in the last line of this snippet: public abstract class Animal {} public class Monkey : Animal {} public class Giraffe : Animal {} public class Lion : Animal {} var monkey = new Monkey(); var giraffe = new

Too many if statements

吃可爱长大的小学妹 提交于 2019-12-17 16:28:30
问题 I have some topic to discuss. I have a fragment of code with 24 if s/ elif s. Operation is my own class that represents functionality similar to Enum. Here is a fragment of code: if operation == Operation.START: strategy = strategy_objects.StartObject() elif operation == Operation.STOP: strategy = strategy_objects.StopObject() elif operation == Operation.STATUS: strategy = strategy_objects.StatusObject() (...) I have concerns from readability point of view. Is is better to change it into 24

Is using java Map.containsKey() redundant when using map.get()

孤街浪徒 提交于 2019-12-17 15:34:16
问题 I have been wondering for some time whether it is allowable within best practice to refrain from using the containsKey() method on java.util.Map and instead do a null check on the result from get() . My rationale is that it seems redundant to do the lookup of the value twice - first for the containsKey() and then again for get() . On the other hand it may be that most standard implementations of Map cache the last lookup or that the compiler can otherwise do away with the redundancy, and that

Is Code For Computers or for People? [closed]

烂漫一生 提交于 2019-12-17 05:50:21
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Ultimately, code compiles down (eventually) into instructions for a CPU. Code, however, (in my humble opinion) is for human beings to

A better way to test the value of an Option?

守給你的承諾、 提交于 2019-12-13 12:53:16
问题 I often find myself with an Option[T] for some type T and wish to test the value of the option against some value. For example: val opt = Some("oxbow") if (opt.isDefined && opt.get == "lakes") //do something The following code is equivalent and removes the requirement to test the existence of the value of the option if (opt.map(_ == "lakes").getOrElse(false)) //do something However this seems less readable to me. Other possibilities are: if (opt.filter(_ == "lakes").isDefined) if (opt.find(_

Ternary Operators & Maintenance [closed]

元气小坏坏 提交于 2019-12-08 13:28:58
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . Programmer A just loves some ternary operators, especially nested ones. Better yet he claims that they make the code easier to read

Return vs. Not Return of functions?

断了今生、忘了曾经 提交于 2019-12-07 07:51:04
问题 Return or not return, it's a question for functions! Or, does it really matter? Here goes the story : I used to write code like the following: Type3 myFunc(Type1 input1, Type2 input2){} But recently my project colleges told me that I should try, as mush as possible, to avoid writing function like this, and suggest the following way by putting the returned value in the input parameters. void myFunc(Type1 input1, Type2 input2, Type3 &output){} They convinced me that this is better and faster

How Do I Avoid Line-Break Padding?

≡放荡痞女 提交于 2019-12-05 22:23:00
问题 My biggest gripe with HTML is that line breaks add a tiny bit of space between elements. (jsFiddle.) This can screw up layouts where child elements are sized to exactly fit their parents. I read somewhere that you can remove this implicit padding - while still keeping the code somewhat legible - by using comments like this: <!-- --><div>Foo</div><!-- --><div>Bar</div><!-- --><div>And so on...</div><!-- --> This works, but I feel like there has to be a better solution. What other ways are