New answers tagged

1

Many discussions on this subject on StackOverflow are against your approach. I forward you, for instance, to this answer as well as to the post it linked to. However, your approach is the right thing to do for the simple reason that it reduces the attack surface area; and I may be it is a good idea to quote you this this real world example to highlight the ...


0

public class EventValidator implements Validator<Event> { // Validates the whole object public boolean validate(Event e){ return validatePlayers(e) && validateCourts(e) && validateMatches(e); // and so on.... } public boolean validatePlayers(Event e) { ...


0

Once you have a rough idea of what a class or function should do, you have enough information to write tests. These tests may fail, due to the thing you're testing being broken, but that is essentially a good thing. If you prefer writing the code before you write the tests for the code (sometimes, having an implementation may yield a better idea of what ...


4

You should be creating tests for all the code that you write, regardless of whether they're libraries or your application. Why ? you can assert that the code you write works the tests assert that that code continues to work as you change or add subsequent code. In short, it doesn't matter whether it's a library or an application that you're writing code ...


2

I forget about absolutely any pre-check when using my model classes' setters and similar methods to add data That's the problem. Ideally you should prevent your objects to have invalid state: Don't allow instantiation with invalid state and if you must have setters and other state-changing methods, throw an exception right there. instead I let ...


3

It is OK to delegate any logic by means of composition if that logic is going to change dynamically during the execution of a program. Complex validations like the ones you explain are as good a candidate as any to be delegated to another class via composition. Remember though that validations can occur in different moments. Instantiating a concrete ...


3

I would definitely go for the return None option. Raising an exception may increase readability (although I doubt it) on the function itself, but handling it is messier. If you return None, from the caller function you can do the following: citation = check_case_citation("Case 145/80") if citation: # Do something else: # Do something else while, if ...


0

In our Java shop, we have intentionally split web widget validation onto three separate operations. Basic formatting - numbers must be numbers; dates must be valid dates etc. Usually this validation comes for free - the web framework will do it for you when binding widget contents to the model. Single widget validation - date must be in the past; an ...



Top 50 recent answers are included