coding-style

Java Main Method, Good Coding Style [closed]

随声附和 提交于 2020-01-27 10:06:08
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . I've had quite a long discussion with a friend of mine about the correct and good use of the main method in Java. Basically we have a class like this: public class AnImporter implements Runnable { // some methods, attributes, etc. } But where to put the main method? I

In Python, is it better to use list comprehensions or for-each loops?

孤街浪徒 提交于 2020-01-26 11:47:54
问题 Which of the following is better to use and why? Method 1: for k, v in os.environ.items(): print "%s=%s" % (k, v) Method 2: print "\n".join(["%s=%s" % (k, v) for k,v in os.environ.items()]) I tend to lead towards the first as more understandable, but that might just be because I'm new to Python and list comprehensions are still somewhat foreign to me. Is the second way considered more Pythonic? I'm assuming there's no performance difference, but I may be wrong. What would be the advantages

Max line count of one file? [closed]

流过昼夜 提交于 2020-01-24 22:13:32
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed yesterday . How many max number of lines of code one .cs file should hold. Are there any industry standards/best practices? 回答1: I don't know how much, but there is a number which is considered a best practice. It's all about maintainability, and there's much more to it than the number of

Naming convention for property [closed]

本小妞迷上赌 提交于 2020-01-24 10:58:00
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . Which one is preferable or more clear? public int FrozenRegionWidth { get; set; } Or... public int WidthOfFrozenRegion { get; set; } 回答1: I'd say FrozenRegionWidth, otherwise you'll end up with a whole bunch of properties starting with 'WidthOf..'. Having said that, shouldn't

Naming convention for property [closed]

╄→尐↘猪︶ㄣ 提交于 2020-01-24 10:57:15
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . Which one is preferable or more clear? public int FrozenRegionWidth { get; set; } Or... public int WidthOfFrozenRegion { get; set; } 回答1: I'd say FrozenRegionWidth, otherwise you'll end up with a whole bunch of properties starting with 'WidthOf..'. Having said that, shouldn't

efficient way to count the element in a dictionary in Python using a loop

那年仲夏 提交于 2020-01-24 03:22:07
问题 I have a list of values. I wish to count during a loop the number of element for each class (i.e. 1,2,3,4,5) mylist = [1,1,1,1,1,1,2,3,2,2,2,2,3,3,4,5,5,5,5] mydict = dict() for index in mylist: mydict[index] = +1 mydict Out[344]: {1: 1, 2: 1, 3: 1, 4: 1, 5: 1} I wish to get this result Out[344]: {1: 6, 2: 5, 3: 3, 4: 1, 5: 4} 回答1: For your smaller example, with a limited diversity of elements, you can use a set and a dict comprehension: >>> mylist = [1,1,1,1,1,1,2,3,2,2,2,2,3,3,4,5,5,5,5] >>

Should 'using' be inside the namespace or outside? [duplicate]

孤者浪人 提交于 2020-01-24 03:21:26
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Should Usings be inside or outside the namespace Are there any technical reasons for preferring this namespace Foo { using System; using System.IO; instead of the default using System; using System.IO; namespace Foo { 回答1: Eric Lippert explains this. In general, they're identical. However, using statements in the namespace can see namespaces and aliases included outside the namespace. 回答2: Almost* the only

Why is the use of a constant considered better programming style than the use of a literal?

假装没事ソ 提交于 2020-01-23 07:56:28
问题 Why is the use of a constant considered better programming style than the use of a literal? What is the exact advantage of the prior over the latter? 回答1: One reason is to assist in maintenance. Let's say you're stuck in the dark ages and your graphic manipulation program works only with 1.4M floppy disks. You get a request to handle the new-fangled 2.8M floppy disks and you think to yourself "Ha, I just need to search through the code looking for 1440 and replacing that with 2880. Simple, eh

Why is the use of a constant considered better programming style than the use of a literal?

浪尽此生 提交于 2020-01-23 07:56:06
问题 Why is the use of a constant considered better programming style than the use of a literal? What is the exact advantage of the prior over the latter? 回答1: One reason is to assist in maintenance. Let's say you're stuck in the dark ages and your graphic manipulation program works only with 1.4M floppy disks. You get a request to handle the new-fangled 2.8M floppy disks and you think to yourself "Ha, I just need to search through the code looking for 1440 and replacing that with 2880. Simple, eh

Throw an exception or return null

丶灬走出姿态 提交于 2020-01-22 21:29:50
问题 If I've got the function below, with two choices private MyObject findBlank() { for (int i = 0; i < pieces.length; i++) { if(pieces[i].isBlank()){ return pieces[i]; } } return null; } private MyObject findBlank() { for (int i = 0; i < pieces.length; i++) { if(pieces[i].isBlank()){ return pieces[i]; } } throw new NoSuchFieldError("No blank piece found!"); } From this method I know that it should always return an object one of the 'pieces' always is isBlank() == true , the return null at the