Why is ASP.NET Core’s Startup class not an interface or abstract class?

There are several reasons why its done the way its done. One of the more obvious reasons is, because you can inject services into Configure method, such as public void Configure(IAppBuilder app, IMyService myService) { myService.DoSomething(); } Obviously, you can’t do that with interfaces, abstract classes or inheritence. The second reason why its done by … Read more

Classes to avoid (code complete)

Class names like InvoiceReader, PriceCalculator, MessageBuilder, ArticleReader, InvoiceReader are not actually verb names. They are really “noun agent-noun” class names. See agent nouns. A verb class name would be something like Validate, Operate, Manage etc. Obviously these are better used as methods and would be quite awkward as class names. The biggest problem with “noun … Read more

Why put private fields and methods at the top of class?

It stems from the days when you had to declare everything—that includes functions as well as variables—before you could use them. The internal (private) variables and functions went at the top of the file and the external (public) functions went at the bottom of the file. This meant that the public functions could reference the … Read more

Using a class’ __new__ method as a Factory: __init__ gets called twice

When you construct an object Python calls its __new__ method to create the object then calls __init__ on the object that is returned. When you create the object from inside __new__ by calling Triangle() that will result in further calls to __new__ and __init__. What you should do is: class Shape(object): def __new__(cls, desc): if … Read more

OO Javascript constructor pattern: neo-classical vs prototypal

This looks like the non-singleton version of the module pattern, whereby private variables can be simulated by taking advantage of JavaScript’s “closures”. I like it (kinda…). But I don’t really see the advantage in private variables done in this way, especially when it means that any new methods added (after initialisation) do not have access … Read more

why is java.lang.Throwable a class?

Here’s how James Gosling explained his decision: Java Developer Connection Program: Why is Throwable not an interface? The name kind of suggests it should have been. Being able to catch for types, that is, something like try {} catch (<some interface or class>), instead of only classes. That would make [the] Java [programming language] much … Read more

How would you code an efficient Circular Buffer in Java or C#?

I would use an array of T, a head and tail pointer, and add and get methods. Like: (Bug hunting is left to the user) // Hijack these for simplicity import java.nio.BufferOverflowException; import java.nio.BufferUnderflowException; public class CircularBuffer<T> { private T[] buffer; private int tail; private int head; @SuppressWarnings(“unchecked”) public CircularBuffer(int n) { buffer = (T[]) … Read more