New answers tagged

2

The Question The question is So why public fields considered evil due to the encapsulation violation while auto-implemented properties are so great? The answer to that question is covered in one of the technical points you note Changing a variable to a property is a breaking change If you publish any class to anyone (even your future self), ...


12

Properties do not violate encapsulation, because they hide the underlying implementation. When you access a property, you don't need to know how exactly it is implemented: whether it's an auto-property, whether it accesses some other object, or anything else. It also means that the implementation can be easily changed and all users of such code will still ...


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 ...


2

The point of such enum might be to provide Js Api (goog) a set/bundle of allowed tags. Which ones? The ones defined by W3C HTML 4.01 (check out enum's documentation). So it's settings boundaries. Might or might not be this the real goal, however it would works fine for such purpose. If you know how Javascript works, what code is doing is defining an array ...


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 ...


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 ...


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 ...


1

I don't think so because there are a fixed number. You may have different boards. If you write different code for each board (i.e. in the style of board.RelayTen.), you'll more code to maintain. One way to contain this expansion of configurations is to use a common program with external configuration or maybe an internal configuration that is ...


3

Given a variable which needs to have a scope beyond a single function call, yet be accessible by only one function, I believe it's okay to not introduce a new scope just for that one function as long as you don't make the variable global. In other words, I'd put this variable at module scope. You should already have some kind of module system which prevents ...


0

If your goal is to limit variable scoping, you are making the issue more difficult than necessary. Consider this function that achieves the same goal with a much more limited scope, both for the internal variables and the function complexity: function doIt(x) { var theAnswer = 42; return x * 42; } It does the same thing yours does, but without the ...


0

Your method GetHousesWithinSameDistrict makes use of a house repository, and an address repository, and has its own logic to select amongst the entities returned by these repositories. As such I would suggest the simple solution is to make use of mocking, and mock those repositories to return entities of the appropriate type, such that you can test your ...


0

The simple answer is: use reflection. For example, the following code creates an Address object and sets District: var address = new Address(); typeof(Address).GetProperty("District").SetValue(address, "A district", null); How you do that in your test code depends on what _addressRepository is (eg, is it declared as an interface?) and how easily it is ...



Top 50 recent answers are included