Tag Info

Hot answers tagged

80

Code that works for you and is easy to maintain is by definition "good". You should never change things just for the sake of obeying someone's idea of "good practice" if that person cannot point out what the problem with your code is. In this case, the most obvious problem is that resources are hard-coded into your application - even if they're selected ...


35

Follow the standard - the end is the iterator past the one you want. This allows you to use all the standard algorithms and containers without problem. It also means your users will be able to write the code they always have (eg for (x=startIt; x != endIt; x++) and this will work as expected. If you change this behaviour and set the last iterator to the ...


28

This rethrow(e); function violates the general principle which says that under normal circumstances, a function will at some point return, while under exceptional circumstances a function will throw an exception. This function violates this principle by throwing an exception under normal circumstances. That's the source of all of the confusion. What ...


13

You are absolutely right in thinking this is a bad practice. I've seen this in production code, and it always comes back to bite you. What happens when you want to add another environment? Or change your development server? Or you need to fail over to a different location? You can't because your configuration is directly tied to code. Configuration should ...


12

Errors are values I don't know what rethrow does, but let's imagine that you are logging something and building a wrapper exception like in Konrad's answer. Let's call it logAndWrap. Instead of throwing the exception as a side-effect of logAndWrap, you could let it do its work as a side-effect and make it return an exception (at least, the one given in ...


9

With your convention: every function in the algorithm library should be used changing the upper bound of the range and it can be quite error prone it isn't easy to represent empty sequences (this was Dijkstra's argument in Why Numbering Should Start At Zero). you can easily incur in off-by-one errors (e.g. when you take a partition of a collection). You ...


6

The throw was probably added to get around the "method must return a value" error that would otherwise occur - the Java data flow analyser is smart enough to understand that no return is necessary after a throw, but not after your custom rethrow() method, and there is no @NoReturn annotation that you could use to fix this. Nevertheless, creating a new ...


6

How often should RAII be used? As often as it makes sense to use (that is, whenever you have an operation that will need to be inverted/undone/closed/finalized/committed/etc. you should probably use RAII). However, I also know that you are supposed to create as few objects as possible, as to save RAM. No; This is a form of premature optimization ...


4

For one, (as others have mentioned) this is a bad idea because you're tying implementation details into your code. This makes it difficult to change things. As mentioned in this answer, if you want to add a new environment now you have to update your code everywhere, instead of just adding your program to a new environment. There is another serious flaw ...


4

In the examples you provided there is most probably no difference, since all the necessary conversions will most probably be performed at compile time. The following will incur a slight performance penalty: if( floatVariable > intVariable ) The penalty will be of the order of an additional clockcycle. (Maybe two? three clock cycles at most? that's ...


4

You wrote: The std convention is to have the last iterator point beyond the last element I think I can help your mental model by giving you two little replies (one section each). Don't think of it as beyond-last indexing, think of it as edge-based indexing Why edge-based indexing (right-open interval indexing) is nice Don't think of it as ...


4

To answer your question: Are modern IDE's a 'crutch'? No. They are a tool. Here are a few other questions that might be relevant: "Can tools be used as crutches?" Sure. "Are all IDEs always used as crutches?" Nope. "Do some people use IDEs as a crutch?" Probably. "Will starting on crutches prevent somebody from reaching their full potential?" ...


3

It depends on what you call coding. If you consider coding to be an art, then if the computer is doing a part, that part wasn't the art. Personally, I have had maybe a cumulative total of 1 or 2 weeks worth of work in my programing career where my understanding of the underlying mechanics obscured by IDEs made me produce a better product. By that logic, ...


2

IDEs are a tool that programmers use to streamline common tasks. Even though yes, a programmer can and possible some do, avoid using them that would be analogous to avoiding using a wire stripper because I could just use my teeth. When software engineering was young most programs were relatively small and could easily be made in simple text editors. This ...


2

I would suggest implementing command line parameters and command line help using argparse. The different functions could then be selected by command line options. If others without python installed need to use your program, then I would package it as an executable using pyinstaller. Pyinstaller can be used to build 3 possilbe executables: onefile, onedir ...


2

Have the 8 function calls wrapped in if __name__ == '__main__': Then you can call the script by running python NAME_OF_YOUR_SCRIPT.py If you need to pass any variables in when running it, use the argparse module


2

I don't know if there's a convention. Anyhow, another trick would be to do like so: private <T> T rethrow(Exception exception) { // or whatever it actually does Log.e("Ouch! " + exception.getMessage()); throw new CustomWrapperException(exception); } Allowing for this: try { return ...


1

If you remove the try block completely then you don't need the rethrow or the throw. This code does exactly the same thing as the original: public Configuration retrieveUserMailConfiguration(Long id) throws MailException { return translate(mailManagementService.retrieveUserMailConfiguration(id)); } Don't let the fact that it comes from more seasoned ...


1

The throw new RuntimeException("cannot reach here"); makes it clear to a PERSON reading the code what is going on, so is a lot better then returning null for example. It also make it easyer to debug if the code is changed in an unexpected way. However rethrow(e) just seems wrong! So in your case I think refactering the code is a better option. ...


1

The main problem with IDEs isn't their crutchiness. Many people overestimate their advantages. Any decent programmer can learn to do quite well without one in a matter of weeks if necessary. Their main problem is their lack of portability, as you've discovered. Visual Studio is great if you happen to be developing on a Microsoft platform for a ...


1

The Principle of Least Privilege is about security, not software engineering and should not be confused with Encapsulation. They are two very different things. The point of the Principle of Least Privilege is to give a user (or application) the bare minimum of permissions it needs. So user A may get a different set of permissions than user B. This is to ...


1

What if I want to run the backend on my own machine but not on port 55793, for example if I were running multiple versions at the same time to compare them? What if I want to run the application backend on one machine, but access it from another? What if I want to add a fourth environment? As others have pointed out, you have to recompile just to change the ...


1

First and foremost, determine what is more common in your system - to list groups of a specific player or to list players of a specific group. Depending on which case is more common, implement it accordingly. If you try to implement both equally, you will take a performance hit for both. So, assumption goes Player belongs to a group, so: public class ...


1

It's not wrong. It might be a hint that your class is too big for its shoes in the first place and you should refactor it, but that has nothing to do with programming against interfaces. If you follow the principle that complex functionality should deal with interface types rather than concrete types, then your concrete class must wear all those interface ...


1

Is this good for the project? No. You have pointed out, yourself, that you have observed that it results in low-quality reports that are not targeted at required functionality, and that the testers end up, to compound the problem, scrambling to complete the work that they are actually "supposed" to be doing. If not, how can I (as a software ...


1

One possible solution: Upon execution, create a temporary file and store the status in it. Useful to keep logs as well. Delete the file every time new execution starts and you no longer need it.



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