defensive-programming

Kotlin: Specify input-constraints in interface

孤街浪徒 提交于 2020-05-15 06:22:04
问题 Lets say I have the following interface: interface MathThing { fun mathFunction(x : Int) } Let's say the constraint I want to put onto this function is that x cannot be negative. How can I make sure that every time this (or any other arbitrary) condition isn't met on a object of type MathThing, a (custom) exception is thrown? 回答1: One way is to use a wrapper class for your function parameters. You can make an extension function so it's a little easier to pass values to the function. data

How to detect QObject::moveToThread() failure in Qt5?

强颜欢笑 提交于 2020-01-23 01:10:06
问题 The documentation on QObject::moveToThread() for Qt5.3 explains that the moveToThread() method can fail if the object has a parent. How would I detect this failure in my code? I realize that simply making sure that my object does not have a parent first is probably good enough, but as a defensive programming practice I would like to test the return value from all calls that may fail. EDIT: I want to stress here after some answers that I am fully aware that I can test if parent is 0 before

Why is the MVC paradigm best suited for web applications?

为君一笑 提交于 2019-12-22 09:57:17
问题 I'm fairly certain my professor will ask me why I chose to use MVC for my web application. Truth be told, I'm new to MVC. I read about it, I'm building a blog application using it, I think it's very logical to approach a problem this way. But why? O_O I draw a blank. How is better suited than say, building an N-tier application? 回答1: Well, basically: separation of concerns on the usage level, not the physical level. Quoting PoEAA on MVC MVC splits user interface interaction into three

What's the most defensive way to loop through lines in a file with Perl?

£可爱£侵袭症+ 提交于 2019-12-18 14:45:10
问题 I usually loop through lines in a file using the following code: open my $fh, '<', $file or die "Could not open file $file for reading: $!\n"; while ( my $line = <$fh> ) { ... } However, in answering another question, Evan Carroll edited my answer, changing my while statement to: while ( defined( my $line = <$fh> ) ) { ... } His rationale was that if you have a line that's 0 (it'd have to be the last line, else it would have a carriage return) then your while would exit prematurely if you

When should I use Debug.Assert()?

爱⌒轻易说出口 提交于 2019-12-17 07:58:10
问题 I've been a professional software engineer for about a year now, having graduated with a CS degree. I've known about assertions for a while in C++ and C, but had no idea they existed in C# and .NET at all until recently. Our production code contains no asserts whatsoever and my question is this... Should I begin using Asserts in our production code? And if so, When is its use most appropriate? Would it make more sense to do Debug.Assert(val != null); or if ( val == null ) throw new exception(

How can I prevent my desktop application from breaking horribly when the user messes with its files at run-time?

谁说我不能喝 提交于 2019-12-13 18:15:08
问题 Such as deleting the output file during run, directing two instances of the sw to the same IO etc ? 回答1: There isn't much you can do to prevent the user from doing that, but what you can do is to apply defensive programming. In your two examples you'd have to, every time you access a resource, detect failure conditions and react appropriately either throwing exceptions and dying or continuing without access to that resource. Never assume a resource allocation will succeed, always check what

Inefficiency of defensive copy in Java [closed]

青春壹個敷衍的年華 提交于 2019-12-13 13:27:55
问题 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 6 years ago . I'm a longtime C/C++ programmer who's learning Java. I've read about the problem of breaking encapsulation by having an accessor method that returns a reference to a private field. The standard Java solution seems to be defensive copying - calling the copy constructor or

Is there nothing wrong with digits in identifier name?

纵饮孤独 提交于 2019-12-13 08:59:47
问题 Since in C\C++\Java - int 2a ; //invalid suffix "a" on integer constant Is there nothing wrong with digits in the rest of variant name although it's valid syntax ? Like - int num1 ; int num_1st ; int num_2nd ; 回答1: I've never heard of anybody (e.g., any coding standards/guidelines) that had a problem with digits in an identifier. Nonetheless, too many of them can indicate that a vector or array might be preferable -- even with only two, your num_1st and num_2nd might be better as numbers[2] .

Is there / should there be a way to track the validity of a raw pointer taken from a smart pointer for debug/QA purposes?

自作多情 提交于 2019-12-11 17:34:25
问题 As I understand C++ Core Guidelines by Bjarne Stroustrup & Herb Sutter, it is a good idea to use pointers this way: unique_ptr for the good old clear one ownership shared_ptr for truly shared ownership by design weak_ptr when by design the pointer might not be valid anymore raw pointer whenever the pointer is valid by design, and ownership is unchanged Thus theoretically it still may be a common bug that a raw pointer becomes invalid (see R37). I wonder if there is a way for debug/QA purposes

Basic defensive programming [duplicate]

蹲街弑〆低调 提交于 2019-12-11 02:47:26
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Favorite (Clever) Defensive Programming Best Practices I am always advised by some programmers to pay concentration to easy debugging . What is defensive programming and to which extend should it be considered while practicing? And one more important question: is there any key things to consider while coding and what are they? 回答1: Have a look at Defensive programming Case Study – Defensive Programming The art