2,936 reputation
513
bio website
location Illinois
age 44
visits member for 2 years, 7 months
seen 10 hours ago

Jan
15
comment Is it always a best practice to write a function for anything that needs to repeat twice?
@RubberDuck: I think far too little attention is generally given to the question of what things should or should not be "aliased"--in many regards, and would expect that the two most common causes of bugs are (1) believing things are aliased/attached when they aren't, or (2) changing something without realizing that other things are attached to it. Such issues arise in the design of both code and data structures, but I don't recall having ever seen much attention given to the concept. Aliasing doesn't generally matter when there's only one of something, or if it will never change, but...
Jan
13
comment Disadvantages of scoped-based memory management
@JonHarrop: Memory and resource leaks are not always fatal, and finalizers can often manage to make things work most of the time. If clicking a button on a form will cause 100 bytes of memory to be allocated and uselessly retained until the form is closed, that would not be "correct", but may not cause major trouble unless the button gets clicked a million times. I would posit that correctness frequently requires that objects be notified when their services are no longer needed, but is not always a requirement for programs to be usable.
Jan
13
comment Should I return from a function early or use an if statement?
@MichaelK: A method should an exception if the post-condition can't be met. In some cases, a method should exit early because the post-condition has been achieved even before the function started. For example, if one invokes a "Set control label" method to change a control's label to Fred, the window's label is already Fred, and setting the control's name to its present state would force a redraw (which, while potentially useful in some cases, would be annoying in the one at hand), it would be perfectly reasonable to have the set-name method early-exit if the old and new names match.
Jan
13
comment Why Garbage Collection if smart pointers are there
@KonradRudolph: Each class layer could be expected to use its "main" constructor to put itself into a state where it was ready to receive virtual calls from the base, but refrain from doing anything that would trigger any virtual calls until the LifetimeNotification method was invoked. Adding such a thing to .NET at this point probably wouldn't be feasible, but there's no reason the next popular OO framework couldn't have one.
Jan
13
comment Why Garbage Collection if smart pointers are there
@KonradRudolph: Still, there wouldn't have been any technical obstacle to having Object contain a protected virtual LifetimeNotification method with parameters indicating what phase of its lifetime it was entering. The .NET framework could then, among other things, call that method whenever a constructor either completed normally or via an exception (with parameters indicating which). Such a design would assist with many things, including resolving problems with having a base class sign up for and start receiving events before derived-class construction was complete.
Jan
13
comment Why Garbage Collection if smart pointers are there
@KonradRudolph: C++/CLI tries somewhat to combine the approaches, though .NET isn't really set up for it. For example, if a type in C++ is declared as "owning" a field of an IDisposable, an auto-generated Dispose method will call Dispose on it; further, if a constructor throws the compiler will automatically generate a Dispose on the partially-constructed object. Unfortunately, if another language like C# is used to code a class derived from one coded in C++/CLI, and the derived class constructor throws, the base-class Dispose won't get called.
Jan
13
answered Is it always a best practice to write a function for anything that needs to repeat twice?
Jan
13
comment Disadvantages of scoped-based memory management
The problem in languages isn't that they support GC, but that they abandon RAII. There's no reason a language/framework shouldn't be able to support both.
Jan
13
comment Why Garbage Collection if smart pointers are there
@KonradRudolph: There's no reason it shouldn't be able to coexist beautifully with GC. In a GC-based system, using a reference to an object which has been abandoned and replaced with a new one won't trap but won't have the desired effect either. In a RAII-based system, using a reference to a destroyed object will cause Undefined Behavior. Combining the two approaches would make it possible to have both situations deterministically trap.
Jan
13
comment Disadvantages of scoped-based memory management
...suppose objects Fred and Barney sign up for notification whenever anything in a certain directory is modified. Fred's handler does nothing but increment a counter whose value it can report on request, but for which it has no other use. Barney's handler will pop up a new window if a certain file is modified. For correctness, Fred should be subscribed with a weak event, but Barney's should be strong, but the timer object will have no way of knowing that.
Jan
13
comment Disadvantages of scoped-based memory management
@JonHarrop: I was using the term "scope" in the same sense as a C++ "scoped pointer" [the lifetime of the object should be that of the container holding it], since that's the usage implied by the original question. My point is that objects create potentially-long-lived references to themselves for purposes such as receiving events may not be composable in a purely-GC system. For correctness, certain references need to be strong and certain references need to be weak, and which references need to be which will depend upon how an object is used. For example...
Jan
3
comment Do any notable C extensions include integer types whose behavior is independent of machine word size
@chux: Some compilers provide command-line or #pragma options to control char signedness. An assertion would prevent code from compiling without the proper settings, but couldn't relieve the programmer from the requirement of manually applying the proper setting. A standardized directive could eliminate the need for such things. Further, it would be possible to make a nicely-implemented int-promotion directive be applicable within a narrow scope. The fact that a particular method requires that the product of two uint32_t be uint32_t doesn't mean that all code should work that way.
Jan
3
comment Do any notable C extensions include integer types whose behavior is independent of machine word size
...also provide a good way to make code smoothly portable among machines with different register sizes.
Jan
3
comment Do any notable C extensions include integer types whose behavior is independent of machine word size
@chux: Well, thanks for suggesting that I fix the title. Very useful comment. The biggest problems arise with types that are half the size of int, though other problems can exist with other types. The char type is also problematic. I really dislike the existence of distinct dialects of a language without a standard means of requesting one. IMHO, there should be a standard ways of saying "make char signed or refuse compilation if unable to do so" or "make char signed or refuse compilation if unable to do so". Similar directives for the minimum-promotion size could...
Jan
3
comment What is different between the internal design of Java and C++ that lets C++ have multiple inheritance?
...and a type which did so might behave very oddly, but such an approach could be pretty powerful otherwise.
Jan
3
comment What is different between the internal design of Java and C++ that lets C++ have multiple inheritance?
@James_pic: I'll have to look into those; I would think that a language which compiles to JVM bytecode (I think Scala does, right) would be limited by what the JVM can do, but a language could go beyond if it had a few conventions which types should follow, and limited use of certain features to types obeying such conventions. For example, one could define an interface UseableAs<T> with a member T useAs() which would in all "legitimate" implementations return this. There would be no mechanism in the JVM to prevent a type from implementing that interface to do something else...
Jan
3
comment Do ALL your variables need to be declared private?
@back2dos: An aggregate should be designed such that atomically reading out the visible state of an instance whose identity-hash value has never been read, copying that state to a new instance, and replacing all references to the original with references to the new instance, would not alter the state of the universe in any observable fashion. There's nothing wrong with aggregates offering "utility" methods, but such methods should simply provide convenient way of doing things which could be done just as well without them. Any abstractly-defined "behavior" should be in client code.
Jan
3
comment Do ALL your variables need to be declared private?
@back2dos: The issue isn't just one of performance. Semantically, I think "update a minmax-aggregate to take additional data into account" is cleaner than "assign to this minmax-tuple variable a tuple which reflects a pre-existing tuple along with some additional data". Further, using accessors creates ambiguity about what mm.setMin(12.0); mm.setMax(10.0); will do. By contrast, the effect of mm.min=12.0; mm.max=10.0; is unambiguous.
Jan
3
comment Do any notable C extensions include integer types whose behavior is independent of machine word size
@chux: Is the new title better? While uint16_t is supposed to behave as a 16-bit storage location, the behavior of type uint16_t in expressions will vary depending upon the underlying integer types.
Jan
3
revised Do any notable C extensions include integer types whose behavior is independent of machine word size
Fixed title question