1

Apologies if the question isn't too good, this is my first post.

I know that in Java defensive programming is a good thing to do, as it stops values of variables being changed if they are not supposed to be. However, if a program is compiled is this needed?

An example of what I mean is the following:

For my university coursework, I had to create a web browser using Swing and AWT in Java. This web browser was created solely by me, and when it was submitted I had to send an executable JAR file, and the .java files (only so that they can be marked, they will not be edited).

From my part, programming wise, I knew that I would not be doing any of the things that defensive programming is supposed to prevent. The source code will also never go public or be edited (whether this was published for public use or not). Therefore, why would defensive programming be needed seeing as you can't get the source code from an executable file?

I feel like I would only use this as it is the accepted standards for programming.

I hope I've explained this well.

Thanks.

  • a JAR is reversible – ollaw May 19 '17 at 13:58
  • But my question still stands for any programming language, not just Java. A .exe file would still have the same point. – mikesimbo123 May 19 '17 at 13:59
  • https://en.wikipedia.org/wiki/Kerckhoffs%27s_principle – SLaks May 19 '17 at 14:01
  • 3
    Once it's been compiled, it's not really an issue. It's to prevent you from messing up your own source code when you forget some aspect of it down the road. If you work on the same code long enough, even you as the author begin to forget things eventually. Do yourself a favor and make it as hard as possible for future-you to break things. – Carcigenicate May 19 '17 at 14:01
  • 4
    You seem to think that defensive programming is about copy-protection or something like that. It's not. [Defensive programming](https://en.wikipedia.org/wiki/Defensive_programming) (Wikipedia): *Defensive programming is a form of defensive design intended to ensure the **continuing function** of a piece of software **under unforeseen circumstances**.* It's about error handling, to make the code protect itself against bad data, or even coding errors. – Andreas May 19 '17 at 14:01
  • I'm voting to close this question as off-topic because it is based on a total misunderstanding of the concept being questioned. As such, the question is meaningless. – Andreas May 19 '17 at 14:03
  • The fact I was working on it non-stop for two weeks probably didn't make me appreciate that @Carcigenicate – mikesimbo123 May 19 '17 at 14:04
  • I think I get it now, my lecture notes used an example of using a set and get method one after another to get a value and then change it, but in reality it is far more stretching. Thanks – mikesimbo123 May 19 '17 at 14:05
  • @joshsp98 I worked on a Neural Net for a month straight once. I was dreaming in that code, and there was still points where I'd look back at some of the first code I wrote for the project, and be confused as to exactly what was going on, then got scared to change things. Try working on a different project for a week, then go back. How much of the web browser design was as intuitive and easy to understand as you originally thought it was? – Carcigenicate May 19 '17 at 14:08
  • @joshsp98 Defensive programming is seen as a good practice. You should follow good practice, you should practice good practice - that's how you get good at it. ", I knew that I would not be doing any of the things that defensive programming is supposed to prevent" is also a statement of inexperience. You make more mistakes than you think you do. Will you remember all the details that supposedly did not need defensive programming when you get back to this code base in the future? Also, If your course requires you to perform defensive programming, that is what you should do to get the grades. – nos May 19 '17 at 14:09
  • 1
    @nos So it basically protects you from yourself, and if applicable any other developers working on the same project? I started Java in September, and I'm not really working on projects concurrently, so you're right that I'm inexperienced! – mikesimbo123 May 19 '17 at 14:11

1 Answers1

0

Question answered brilliantly by numerous comments. Best put by an excerpt of a comment by @Carcigenicate

Once it's been compiled, it's not really an issue. It's to prevent you from messing up your own source code when you forget some aspect of it down the road.

SO won't let me mark this as answered yet.