Tag Info

Hot answers tagged

7

Yes, a function that just raises an exception is OK. You can easily have functions like ensureWellFormed(email_address) or http_request.ensureUserLoggedIn() that raise well-documented exceptions. This de-clutters code, and many high-profile projects, e.g. Django, do use this style here and there. If you use this approach, make sure your exception objects ...


5

I personally would go with the first example, given your task. The first example more closely matches your original problem statement. The 2nd requires a little bit more mental parsing to determine if it fulfills your problem statement. Look at how you worded what is going on: "Sum the numbers from 0 to 9 skipping 5". That is exactly what the first ...


4

As it stands right now (a single condition) I don't see much reason to prefer one over the other, but I'd probably prefer the second as slightly simpler. When, however, your set of conditions are more complex, some variant of the first can be preferable, often by a wide margin. Consider something like: for (...) { if (i != 5 && (i%2!=0) ...


4

Step 1 is of course make it so your method doesn't have side effects. But let's focus on the example. Yes, you should generally abstract what a function does from how it does it, but side effects are clearly still part of what the function is doing. By making them explicit in the name, you are making the code clearer about what is going on, leading to less ...


3

Is it worth trying to avoid the conversion? It depends. This is one of those cases where the "first, measure" sort of advice comes into play. If your profiler says it's an issue, then yes. If not, then I would say no - it's readable enough with the conversion. The second is obviously safer but I am wondering if it is an overkill. Yes. If you're ...


2

But you do care about it being created, because it can throw an IOException! In fact, the only time it can throw is the first time it's called. Every other time you'll need to put a pointless try/catch block (assuming this is Java) in the calling code for something that will never happen. I'd refactor this code to have an explicit stream creation method. ...


2

Is a and Has a are reasonable guidelines, very useful when you are learning, but in practice you should never use inheritence unless it's so blatently obvious that it's needed that there is just no question. Inheritene often seems like a much neater solution, but people who have used it extensively have learned that it leads to difficult to maintain code in ...


2

My suggestion would be to extract that logic out into a method so that if you add more conditions or even change your condition it is easier to maintain in one place + it is more testable. The reason I am suggesting that is because I am assuming this is not the actual problem but it is a more simplified version of your problem that you have narrowed it down ...


2

This question mainly stems from worrying about maintenance so I will make an assumption that you are going to have many of these state-dependent functions. I would recommend having a layer in between your client and the function that it is calling which is state dependent. Then let this layer handle the state management. This will do two things for you: ...


1

I prefer no spaces before semicolon, too. But in that case it is not a semicolon. A semicolon belongs to the previous command, while the : is a language construct like in in other programming languages: for(x in list). I think it goes fine with the prefered coding-style; you also wouldn't write x= 1 but x = 1, would you?


1

So, for me, this method does one thing. It retrieves (gets) ObjectOutputStream object as its name says No. You can't retrieve something that doesn't exist. Creating an object which didn't exist before and returning that object changes program state (possibly with other significant side effects). It's not a retrieval. Retrieval should be a safe, ...


1

Yes, I think method name should describe side effects. For one thing, this example uses checked exception, which by itself reveals implementation details. Invocation of this method requires try/catch block anyway (for no reason, since it can be handled inside and throw NPE or return null, which wouldn't improve the situation, but at least make it cleaner on ...


1

Both of those code fragments are doing way too much work. You can replace them with int sum = 40; (in fact, if you gave those code fragments to a compiler, that's basically what the compiler would do). There's a famous trick for summing up the first n integers I used to do it in my head, but if you didn't know the trick and had no calculator handy, you ...


1

Maybe It can work to externalize validation logic as long as it is not externally visible and is used to enforce preconditions. Say you have an object that operates on a database connection, and most of its functions require that the connection be set up and initialized. Rather than checking the connection and conditionally throwing an exception in each ...


1

No, it would be worse functions should tell what they do. With a good naming, your check_stuff function indicates what it does. It should therefore be named e.g. checkConditionsValid depending on what it actually does. I suppose your function func also returns something or has some side effect in case that the conditions are. If not, that would be a design ...


1

Generally, it's a good idea to split something into a function of it improves the readability of your code, Or, if the part being oft repeated is somehow core to the system. In the above example, If you needed to greet your users depending on the locale, then it would make sense to have a separate greeter function.


1

or just another regular class with private methods and own fields along with action methods? Yes. It's just a class, i.e. keep methods small and refactor as needed into private sub-methods (or delegate to other classes, if appropriate). Personally, 20 lines is a good upper limit, but I try for less. Occasionally, e.g. for complex algorithms with lots ...



Only top voted, non community-wiki answers of a minimum length are eligible