Questions tagged [defensive-programming]

Defensive programming is a form of defensive design intended to ensure the continuing function of a piece of software in spite of unforeseeable usage of said software. Defensive programming techniques are used especially when a piece of software could be misused mischievously or inadvertently to catastrophic effect.

148 questions
1
vote
1 answer

How to check for NoClassDefFoundError?

I am getting few NoClassDefFoundError in my Android mobile app only from specific devices of Samsung and QMobile. Will adding the below code detect the error? //Check for Class not found error for Samsung 4.2.2 devices case try { …
brainless
  • 5,698
  • 16
  • 59
  • 82
1
vote
0 answers

defensive programming, if-then-else should be a switch-case?

I was coding an if-then-else, when it occurred to me, that this was a bad programming: state = "published" # state could be "published" or "draft" ... if state == "published" display_production_copy else display_draft_copy end However, as…
Daniel
  • 7,006
  • 7
  • 43
  • 49
1
vote
3 answers

How can I guard against "Object cannot be cast from DBNull to other types"?

I've got this code, where ISTM I'm defensively coding against assigning nulls: foreach (DataRow priceAndUsageVarianceRow in _dtUsage.Rows) { //var pauv = new PriceAndUsageVariance //{ // Description =…
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
1
vote
3 answers

Error checking in business methods a.k.a doing defensive programming

I am starting to add tests to a large java code base. I often see the following in the session beans I am testing: public OrderDTO getOrderDTO(Long id) { Order o = (Order)entityManager.find(Order.class, id); OrderDTO dto = new…
1
vote
0 answers

Calling an Android method which may not be available in the current API

I am creating an Android app for blind users. One usability issue is that their fingers tend to touch the Back button in the Navigation Bar, and they suddenly find themselves out of the app. In Android 4.4 KitKat and later, I can use... …
James Newton
  • 6,623
  • 8
  • 49
  • 113
1
vote
7 answers

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

Such as deleting the output file during run, directing two instances of the sw to the same IO etc ?
gil
  • 2,248
  • 6
  • 25
  • 31
1
vote
1 answer

Detecting unknown XML tags in Scala

I'm building an XML parser in Scala and want to be defensive against user mistakes. If the user gives a tag that I don't support (e.g., rather than ) or, more generally, puts a tag in the wrong place, I want to detect it and throw an…
Dylan Knowles
  • 2,726
  • 1
  • 26
  • 52
1
vote
4 answers

Java Defensive Copies

I've seen defensive copies coded like this void someMethod(Date d) { myDate = new Date( d.getTime() ); } But that doesn't make sense to me, isn't there a way in Java to create an identical copy in memory of that object? I've read the clone()…
walnutmon
  • 5,873
  • 7
  • 42
  • 57
1
vote
2 answers

Is having multiple layers of data validation in a program defense in depth against malfunction, or code-cluttering paranoia?

For example, when the data is initially input, I might just spit it back out if it doesn't match my validation regex. Then when it comes time to use it, I often feel the impulse to do something like: function useData(data) { if(data.match(/some…
user2958725
  • 1,355
  • 3
  • 12
  • 16
1
vote
4 answers

What is good practice? A copy constructor or a defensive copy method

I have a class Graph and I need a copy of that graph. I am going to modify the internals of graph object ( eg: delete an edge etc. ). I have 2 ways to implement a graph. A copy constructor A method called 'getGraph() { return new Graph(this)}'.…
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
1
vote
2 answers

Secure-by-default django ORM layer---how?

I'm running a Django shop where we serve each our clients an object graph which is completely separate from the graphs of all the other clients. The data is moderately sensitive, so I don't want any of it to leak from one client to another, nor for…
Jonas Kölker
  • 7,680
  • 3
  • 44
  • 51
1
vote
2 answers

CLR: If a constructor fails will it always throw an exception?

In Delphi, if there was an exception during construction of an object: any allocated memory would be released and an exception would be thrown. For example, the following was guaranteed to either return a valid Camera object, or throw an…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
0
votes
3 answers

using try/except & while loop for calculator in python

I am trying to create a calculator and use defensive programming to avoid unexpected events and user inputs. I included a try/except block for user input & for zero division. When I input the division operator to calculate a zero division, my loop…
0
votes
0 answers

Advice on refactoring lots of consecutive if checks

I have some code like so: export async function handleRefresh() { if (!existsSync('postr.toml')) fail('not a postr directory'); const posts = expandGlob('posts/*'); for await (const post of posts) { if (!post.isDirectory) { …
0
votes
3 answers

Custom copy constructor and adding a field

Duplicate of this. In C++ you sometimes have to implement the copy constructor yourself (when you have pointer as a member usually). Over compiler generated copy constructor this has the disadvantage that when you add a member field and forget to…
Roman Plášil
  • 1,941
  • 2
  • 17
  • 27