coding-style

Is it better to croak() or to die() when something bad happens in Perl?

天大地大妈咪最大 提交于 2019-12-08 14:42:13
问题 perlcritic complaints that the following code, some boilerplate DBI stuff that works perfectly fine, should croak instead of die: # Connect to database my $db_handle = DBI->connect( $url, $user, $password ) or die $DBI::errstr; All this, while die seems to work fine for me. I would think for a samurai Perl warrior, croak is less honorable than actually die when things go awry. Jokes apart Why should I croak instead of die ? What are the consequences of not heeding perlcritic's advice? 回答1:

What's the best to handle unaccepted method arguments in Java?

血红的双手。 提交于 2019-12-08 09:49:29
When writing a method, say, inside one of your DAO objects, and you dont want this method to accept certain input, for discussions sake, say it does not allow null arguments. How do you go about implementing that, taking into account this method is likely to be reused in the future by new team members. The way I do it is: In the interface, I document inside the method javadoc that arguments a, b and c cannot be null. Inside the method I check for null values first thing, and if any of a, b or c are null then I throw an IllegalArgumentException. But, what if some developer in the future just

What are some universal “clean-coding” conventions? [closed]

人盡茶涼 提交于 2019-12-08 09:46:16
问题 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 2 years ago . Aside from language-specific constructs, for someone who is programming in PHP, javascript, java, and python, what are the top 10 clean coding must-do's? I would like to keep my code as clean as possible and as readable as possible. For example, what's the take on placing the

What are the conventions for __func functions?

試著忘記壹切 提交于 2019-12-08 07:50:28
问题 I've seen a bunch of functions in Linux code named __foo. What does the double underscore stand for and when should it be used? 回答1: It means it's a reserved identifer. Both C++ 03 and C99 standard mentioned about this. C99: 7.1.3 Reserved identifiers All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use. All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both

Brace placement after init list in C++

删除回忆录丶 提交于 2019-12-08 07:50:22
问题 For anyone who places braces thus: void f() { stuff(); } How do you prefer to place braces after long initializer lists? The same way? Object::Object() : foo(1) , bar(2) { stuff(); } Or make an exception so you actually see where the init list ends? Object::Object() : foo(1) , bar(2) { stuff(); } Or leave a blank line? Object::Object() : foo(1) , bar(2) { stuff(); } Or maybe make a weird hybrid? Object::Object() : foo(1) , bar(2) { stuff(); } Or abuse indentation Object::Object() : foo(1) ,

More methods in derived classes than base class

坚强是说给别人听的谎言 提交于 2019-12-08 06:19:23
问题 I would like to ask a question about programming style in this case of derived class: class A { public: virtual foo1()=0; } class B: public A { public: virtual foo1(); virtual foo2(); } class C: public A { public: virtual foo1(); } int main() { B mB(); C mC(); mB.foo2() //OK! mC.foo2() // obviously, it is not correct return 0;} Therefore, should a derived class have less or equal public methods than the abstract base class? If the derived classes require more methods, should these be private?

JSON usage in Python

情到浓时终转凉″ 提交于 2019-12-08 05:26:56
问题 I'm looking through the json documentation and I'm trying to understand how to actually convert a Python object into JSON data, and then convert that data back into a Python object. I understand you can pass lists, dicts, and tuples of "primitives" like in the example at the top, but I tried creating a very minimal object and passing it to json.dumps() and got "object is not JSON serializable". What is the proper way to make an object JSON serializable? I'm currently imagining writing a

Benefits of using Setter and Getter methods vs. direct manipulation

偶尔善良 提交于 2019-12-08 05:15:53
问题 Sorry if this a beginner question but I was wondering what the benefits are to using setter and getter methods rather than directly manipulating them directly. I'm in obj-c and I'm wondering if there is any benefits in terms of memory/cpu usage. For instance, I'm cropping an image before I upload it and I crop it after it is taken/picked. I put all my code in the didFinishPickingMediaWithInfo method. So it would look like the following: -(void)imagePickerController:(UIImagePickerController *

Java Compare Strings

℡╲_俬逩灬. 提交于 2019-12-08 05:14:01
问题 /** * A method to compare Strings * @param arg1 * @param arg2 * @return */ public boolean myQuickCompare(String arg1, String arg2) { boolean a = arg1.length() == arg2.length(); if (a) { for (int b = 0; b > arg1.length(); b++) { if (arg1.charAt(b) != arg2.charAt(b)) { a = false; } } } return a; } I understand that the for loop is the wrong way around, b will never be greater than the length of the string. How would you correct this problem? What sensible variable names would you give for a and

Customizable DST Rules

蹲街弑〆低调 提交于 2019-12-08 02:04:24
问题 What's the task? By project specification i need to calculate DST rules for specific time zone offsets. Rules cannot be applied via standard instruments like date_default_timezone_set() because we can't rely on software configuration and can't get it updated if some DST rules will be changed. What i have? In situations when DST switch date is on the last week and the switch day of the last week is on the next month i need to decrement the number of the last week. For now i have only one ugly