coding-style

Coding practice in R : what are the advantages and disadvantages of different styles?

老子叫甜甜 提交于 2019-12-17 15:26:08
问题 The recent questions regarding the use of require versus :: raised the question about which programming styles are used when programming in R, and what their advantages/disadvantages are. Browsing through the source code or browsing on the net, you see a lot of different styles displayed. The main trends in my code : heavy vectorization I play a lot with the indices (and nested indices), which results in rather obscure code sometimes but is generally a lot faster than other solutions. eg: x[x

Implicit conversion vs. type class

只谈情不闲聊 提交于 2019-12-17 15:04:59
问题 In Scala, we can use at least two methods to retrofit existing or new types. Suppose we want to express that something can be quantified using an Int . We can define the following trait. Implicit conversion trait Quantifiable{ def quantify: Int } And then we can use implicit conversions to quantify e.g. Strings and Lists. implicit def string2quant(s: String) = new Quantifiable{ def quantify = s.size } implicit def list2quantifiable[A](l: List[A]) = new Quantifiable{ val quantify = l.size }

What is the difference between if (NULL == pointer) vs if (pointer == NULL)?

ε祈祈猫儿з 提交于 2019-12-17 13:55:05
问题 What is the difference between using: if (NULL == pointer) and using: if (pointer == NULL) My professor says to use the former over the latter but I don't see the difference between the two. 回答1: There is no difference. What your professor prefers is called Yoda conditions also see "Yoda Conditions", "Pokémon Exception Handling" and other programming classics. It is supposed to prevent the usage of assignment( = ) by mistake over equality( == ) in a comparison, but modern compilers should

Modifier Keyword order in Java

僤鯓⒐⒋嵵緔 提交于 2019-12-17 10:26:14
问题 Every time I write method in Java with more keywords than public void , every time I write it another way. Sometimes " static public void " sometimes " public static void " etc. What is the best order (best practices) for these keywords? [ abstract/static ] [ final ] [ synchronized ] [ public/private/protected ] [ result_type ] ??? 回答1: In theory it does not matter if you say public static final or final static public, but if you follow the usual convention, other people will able to read

Static factory methods vs Instance (normal) constructors?

懵懂的女人 提交于 2019-12-17 10:24:49
问题 In a language where both are available, would you prefer to see an instance constructor or a static method that returns an instance? For example, if you're creating a String from a char[] : String.FromCharacters(chars); new String(chars); 回答1: In Effective Java, 2nd edition, Joshua Bloch certainly recommends the former. There are a few reasons I can remember, and doubtless some I can't: You can give the method a meaningful name. If you've got two ways of constructing an instance both of which

JavaScript: inline functions vs predefined functions

天大地大妈咪最大 提交于 2019-12-17 10:22:53
问题 Can any body throw me some arguments for using inline functions against passing predefined function name to some handler. I.e. which is better: (function() { setTimeout(function() { /*some code here*/ }, 5); })(); versus (function() { function invokeMe() { /*code*/ } setTimeout(invokeMe, 5); })(); Strange question, but we are almost fighting in the team about this. 回答1: Named functions There is some serious misuse of terminology in the question and answers on this page. There is nothing about

pythonic way to do something N times without an index variable?

≡放荡痞女 提交于 2019-12-17 10:18:52
问题 Every day I love python more and more. Today, I was writing some code like: for i in xrange(N): do_something() I had to do something N times. But each time didn't depend on the value of i (index variable). I realized that I was creating a variable I never used ( i ), and I thought "There surely is a more pythonic way of doing this without the need for that useless index variable." So... the question is: do you know how to do this simple task in a more (pythonic) beautiful way? 回答1: A slightly

Get rid of ugly if statements

荒凉一梦 提交于 2019-12-17 10:12:18
问题 I have this ugly code: if ( v > 10 ) size = 6; if ( v > 22 ) size = 5; if ( v > 51 ) size = 4; if ( v > 68 ) size = 3; if ( v > 117 ) size = 2; if ( v > 145 ) size = 1; return size; How can I get rid of the multiple if statements? 回答1: if ( v > 145 ) size = 1; else if ( v > 117 ) size = 2; else if ( v > 68 ) size = 3; else if ( v > 51 ) size = 4; else if ( v > 22 ) size = 5; else if ( v > 10 ) size = 6; return size; This is better for your case. Optionally you should choose Switch Case where

Should useless type qualifiers on return types be used, for clarity?

风流意气都作罢 提交于 2019-12-17 09:48:07
问题 Our static analysis tool complains about a "useless type qualifier on return type" when we have prototypes in header files such as: const int foo(); We defined it this way because the function is returning a constant that will never change, thinking that the API seemed clearer with const in place. I feel like this is similar to explicitly initializing global variables to zero for clarity, even though the C standard already states that all globals will be initialized to zero if not explicitly

Oracle Java code conventions

让人想犯罪 __ 提交于 2019-12-17 09:39:31
问题 Does someone know where I can find the document by Oracle which describes Java code conventions? This URL is not available anymore, for that reason I created new question for this topic. http://www.oracle.com/technetwork/java/codeconv-138413.html 回答1: One option is to use wayback machine, which seems to contain the document (here's direct link to pdf version). However I would also be interested to find out what Oracle did with it and do they intend to get rid of it. There's a discussion on