coding-style

When should checked exception/unchecked exception be chosen?

拟墨画扇 提交于 2019-12-13 13:33:05
问题 I have learned from various tutorial that "If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception." I really want to see the effectiveness of previous statement through some code examples. e.g. try { br.readLine(); } catch (IOException e) { e.printStackTrace(); } Here, IOException is checked Exception.So, how I'm supposed to recover when this Exception occurs

Warning: Assignment in condition

非 Y 不嫁゛ 提交于 2019-12-13 13:24:31
问题 One thing that has always bugged me is that when checking my php scripts for problems I get the warning "bool-assign : Assignment in condition" and I get them a lot. e.g.: $guests = array(); $sql = "SELECT * FROM `guestlist`"; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) $guests[] = $row['name']; Is there a different way to get multiple or all rows into an object or array? Or is there nothing wrong with this method? 回答1: try doing this instead: $guests = array(); $sql

JSLint says new keyword is missing [duplicate]

久未见 提交于 2019-12-13 12:28:12
问题 This question already has answers here : How to fix JSLint “missing new” error (2 answers) Closed 6 years ago . Let's say I have some JavaScript that looks like this: function A() { var MyVar, SomeParameter; // do work MyVar = FunctionB(SomeParameter); } JsLint says I'm Missing 'new'. at the line MyVar = FunctionB(SomeParameter); Why should I rewrite as MyVar = new FunctionB(SomeParameter); Is there going to be any benefit? 回答1: It is the convention that constructors (eg: Array , Object ) are

GCC optimization trick, does it really work?

笑着哭i 提交于 2019-12-13 12:06:34
问题 While looking at some questions on optimization, this accepted answer for the question on coding practices for most effective use of the optimizer piqued my curiosity. The assertion is that local variables should be used for computations in a function, not output arguments. It was suggested this would allow the compiler to make additional optimizations otherwise not possible. So, writing a simple bit of code for the example Foo class and compiling the code fragments with g++ v4.4 and -O2 gave

What is the advantage of the 'src/main/java'' convention?

匆匆过客 提交于 2019-12-13 11:32:27
问题 I've noticed that a lot of projects have the following structure: Project-A bin lib src main java RootLevelPackageClass.java I currently use the following convention (as my projects are 100% java): Project-A bin lib src RootLevelPackageClass.java I'm not currently using Maven but am wondering if this is a Maven convention or not or if there is another reason. Can someone explain why the first version is so popular these days and if I should adopt this new convention or not? Chris 回答1: Main

c# is it better to use a switch or if/else when comparing an enum [duplicate]

浪尽此生 提交于 2019-12-13 11:01:47
问题 This question already has answers here : If vs. Switch Speed (5 answers) Closed 6 years ago . I have an enum with 4 values and I am just curious as to whether it would be better to use for if/else statements to check what the value is, or use a switch statement. When I say better I mean in terms of performance, as both are about as readable as one another. Thanks 回答1: I think that in terms of performance you won't feel any difference if you have only 4 values to check. I figured out, that

Big picture or guide on programming related topics?

…衆ロ難τιáo~ 提交于 2019-12-13 05:40:27
问题 I was wondering if someone could give a big picture of how the following topics and others not mentioned are related: "code reuse" "refactor" "design pattern" "coding/programming standard/style" ... I have heard of these from people's talk and books' names, but got overwhelmed by these names. May I get some organization and big picture from here? Thanks and regards! 回答1: I think you need to buy a copy of Dave Thomas and Andy Hunt's "The Pragmatic Programmer". There are plenty of other good

Ajax or JavaScript: Changing Style According to Server Response

浪子不回头ぞ 提交于 2019-12-13 03:37:37
问题 Hey, I'd like to change the font color or the responseText based on the result. For example, if the responseText is NOT FOUND, I'd like to font color to be red. Otherwise, it will be black. It's currently displaying the correct responseText; I just want to change the color when necessary. Here is my current Ajax: function newXMLHttpRequest() { var xmlreq = false; if (window.XMLHttpRequest) { xmlreq = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { xmlreq = new ActiveXObject(

Jenkins - How to concatenate ${WORKSPACE} with a string literal?

社会主义新天地 提交于 2019-12-13 03:19:28
问题 In the below scripted pipeline code(groovy syntax): ws('/app/jenkins/workspace/abc/def/ghi'){ sh './mvnw clean install } ws('/app/jenkins/workspace/abc/def/xyz'){ sh './mvnw clean install } where /app/jenkins/workspace/abc/def is ${WORKSPACE} ghi & xyz are sub-folders within ${WORKSPACE} How to concatenate ${WORKSPACE} with sub-folder ghi and store the value in someVar ? to use the syntax ws(someVar){} 回答1: You can try ws("${WORKSPACE}/ghi"){ .... } Pay attention to use gstring substitution

What are the pros and cons to assigning the end point of a for loop?

被刻印的时光 ゝ 提交于 2019-12-13 02:58:40
问题 In various languages (I'm going to use JavaScript here, but I've seen it done in PHP and C++ and probably elsewhere), there seem to be a couple of ways of constructing a simple for loop. Version 1 is like: var top = document.getElementsByTagName("p"); for (var i = 0; i < top.length; i++) { ...do something } Whereas elsewhere I've seen people construct the for loop like: for (var i = 0, ii = top.length; i < ii; i++) It seems like people do it without rhyme or reason - the same programmer will