Hot answers tagged

54

A benefit is that the compiler can let you know if you accidentally type "ADRESS" or "FEILDSET", and letting you fix it immediately instead of behaving in a nonsensical way at runtime. While the benefit is much more useful in statically typed languages than dynamic, it is still useful even if it is a runtime error as you will get message indicating a ...


50

It's completely terrible in every possible way. At most, use a factory function to produce JButtons. You should only inherit from them if you have some serious extension needs.


37

Specifically speaking of JavaScript, you could use TypeScript instead. It offers some of the things you are referring to. Quoting the website: Types enable JavaScript developers to use highly-productive development tools and practices like static checking and code refactoring when developing JavaScript applications. And it is just a superset of JS, ...


22

This is a wonderful idea, with one caveat. Don't replace developer written tests with reviewer written tests. Have your reviewers look for corner cases and inputs that will break the code. In other words, have them try to write new tests that the original developer didn't think to write. Writing characterization tests is an absolutely wonderful way to gain ...


19

This violates the Liskov Substitution Principle because an OkayButton cannot be substituted in any place a Button is expected. For example, you can change the label of any button as you like. But doing that with an OkayButton violates its internal invariants. This is a classic misuse of inheritance for code reuse. Use a helper method instead. Another ...


19

Enums are useful for situations where you have a fixed set of values/entities that are sensible. They are self-documenting and allow the compiler validate things that would otherwise be left to run-time. They should never be used if the set of meaningful values is not known or not strictly limited. A more useful example would be something like HTTP ...


19

There are some approaches which can help: Unit testing Write unit tests where possible. Solely relying on manual testing or finding bugs in the wild is hit-and-miss. Use frameworks Rather than rolling your own and risking the introduction of bugs, use established frameworks where possible. Prefer CSS/high-level languages Where you can cede ...


18

I don't think the idea is entirely without merit - however, the main benefit of the TDD et al is that problems are found early. The developer is also best placed to spot which corner cases may require specific attention. If this is left until the code review, then there is a risk this knowledge could be lost. Writing tests during the code review would ...


13

Reusing code is not about resurrecting old unused and abandoned code and putting it to use. Reusing code is about making the same code fulfill multiple purposes (perhaps within the same project) by providing a useful abstraction that can be (re)applied in other contexts. Old abandoned code may be consulted, but should probably not be resurrected. It ...


11

This is a very non-standard way of writing Swing code. Typically, you only rarely make subclasses of Swing UI components, most commonly JFrame (in order to set up child windows and event handlers), but even that subclassing is unnecessary and discouraged by many. Providing customization of text to buttons and so-on is usually performed by extending the ...


10

Enums have nothing to do with OOP, and JavaScript doesn't have enums. Instead, enums are used whenever there is a choice between a fixed set of values. For example, a boolean is a choice between true and false, which could be implemented as enum Bool { False, True }. In a GUI library, we might have an enum for alignments: enum HAlignment { LEFT = -1, CENTER ...


10

IMHO, Best Programming Practices in Java are defined by Joshua Bloch's book, "Effective Java." It's great that your teacher is giving you OOP exercises and it's important to learn to read and write other people's styles of programming. But outside of Josh Bloch's book, opinions vary pretty widely about best practices. If you are going to extend this ...


10

Neither is generally better than the other. It's a judgment call you have to make on a case-by-case basis. But in practice, when you're in a position that you can actually make this decision, it's because you get to decide which layer in the overall program architecture should be breaking the object up into primitives, so you should be thinking about the ...


8

I'd be willing to bet that your teacher doesn't actually believe that you should always extend a Swing component to use it. I bet they're just using this as an example to force you to practice extending classes. I wouldn't worry too much about real-world best practices just yet. That being said, in the real world we favor composition over inheritance. Your ...


8

In general, yes it's ok. In general the constructor of a class is where all the setup necessary for that class to function goes. Often a portion of that setup is abstracted to other areas of the code. For example the factory pattern moves a lot of the initial setup to another class so the setup is clearly separated from the logic. In your specific example, ...


7

Wouldn't it be beneficial to write tests during code review, by the person doing the review? I have found that a good time to write tests is when you realize you need a test for a situation. Task switching for computers is expensive - even more-so for humans. At this point in time, you generally have a good understanding of the requirements and ...


6

I agree with @RobbieDee's answer but I have a bit more to add. If you really like this idea, why not have the same people write the tests before the code as executable acceptance criteria for the user story? That would do the same thing, still keep the feedback short and get everyone to have a discussion around the story ,which I think would be of greater ...


6

If your class is too big and unwieldy when it's all in one place and you can easily see it all, what makes you think scattering it across multiple files and making it harder to correlate will be an improvement? Partial classes are essentially a hack to make it easier for generated code, such as that produced by the form designer, to work together with ...


5

Bad practice generally, unless you want to be on the bleeding edge and have to manage bugs you can't control. However, a policy of continuous upgrading is a good practice, you manage the pain of upgrading if you do it regularly as changes should not be that large. One day you will have to upgrade (eg to fix a security vulnerability) and if you're many ...


5

Yes, under the right conditions. A common example is that of filter composition. Here, you build a new filter by calling a method on an older filter. Contrived example: class Filter { public function where(string field_name, object value) { filters = (clone)this->filters; filters.setitem(field_name, value); return ...


4

I'm sure there are situations where this would make sense to do, particularly if you're writing a small application, but in general I think it would be a bad idea. You could apply any argument that you could make about global variables to static variables - they're still changeable anywhere in the application. If your application (and dev team) is ...


3

Like you say, if you're running a TDD team, then this is moot since the code should already be tested. Overall I don't think this is all that great an idea, but it depends on your current approach and what works for you. Basically, the problem I see is that you lose the "short feedback loop" advantage of tests. Getting instant notification the moment you ...


3

This is not a good idea because you can introduce a race condition. In one thread you change the value to one you want but before you manage to create an instance of the class another thread changes the value, and you get an instance created with the wrong default value. Your hair will grow whiter than mine, trying to figure out why things aren't working ...


3

Even if your language doesn't require compilation, you'll probably use some kind of IDE or development tools, that can give much better support for something like an enum than just for strings. If you use an enum like object literal in javascript for example, your editor will give you code completion and your code checker like JSHint or JSLint will warn ...


3

Instead of doing it this way, I would try to implement something like a more encapsulated version of the factory pattern. Have a separate object that creates the list instead. See Factory (object-oriented programming). You are essentially already doing the factory pattern when you return a list of Person, but instead you may want to have two separate ...


3

You asked Instance variable G, why its always declared with capital? and Public interfaces with names like V, I am always taught to write descriptive names. In what conditions these names are allowed? I guess these two things have the same reason: the original algorithm description and the related sections in your text book has the form of a ...


3

You should choose integer over string if the values an integer can have and the operations an integer supports are a better fit for the data in question than the values and operations a string has. It is okay if some of the values/operations of that type don't make sense for that data, simply because there are so many different kinds of real-world data that ...


2

Refactor the big function. It probably does several things repeatedly or nearly so, if it loads several "prefabs". Then, the logic needed for repetition can be put into other functions, which could open more possibilities for refactoring (one or more new classes for loading "prefab" types, maybe?). Moving the code to a partial class will only obscure the ...


2

I would generally recommend keeping up to date. If you don't, at some stage you will have to upgrade your library (have you found a bug?), and that may cascade through your project and your dependencies, as the new library requires further updates. If you schedule regular minor upgrades, then you can manage that without having an avalanche of upgrades ...


2

One tool that hasn't been yet mentioned is simple, file-local or project-wide text search. It sounds simple, but when you include some regular expressions you can do some basic to advanced filtering, e.g. search for words located in documentation or source code. It has been an effective tool for me (besides static analyzers), and given your project size of ...



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