22

I don't do C# so pardon the syntax, but you should be able to reduce the amount of code by using a for loop over a string array with your column names. Again I don't do C# but in Java you could do something like this: for(String col : new String[]{"article", "account", "dim_1", ...}) { if (e.SourceCol == _detailSpread.GetColNumber(col)){ ...


20

Other answers are great, but this is really re-inventing the wheel. What you are looking for is the observer pattern. .NET has great in-built capabilities with observer through System.Observer<T> and System.Observable<T>. You can also get the library Rx-Main through NuGet to gain the ability to compose these, LINQ-style, into operations that can ...


16

The things to consider when it comes to async event handlers are: Exceptions thrown for the handler might be rethrown on the UI SynchronizationContext, which usually crashes the application. After you raise the event, the handlers won't be completed yet. The execution of a handler might be interleaved with the execution of the code after the raising and ...


15

A shorter version would be to create an anonymous array in the if statement combined with a collection initializer where you will add all of the column names and call the extension method .Any to check if any item matches the predicate: if (new[] {"article", "account", "dim_1", ...}.Any(item => e.SourceCol == _detailSpread.GetColNumber(item))) { ...


13

The short code wins hands down. I understood it immediately, and more importantly, I can trivially verify that the code is reasonably error free. This is much harder with the longer code. You say that the longer code is easier to understand but I claim that this is objectively wrong. Case in point, the long code uses lots of magic numbers: 1, 0, -1, … what ...


13

For me this whole idea smells like a bad design. I don't like having this logic hidden in the event sender. If I see an event and I subscribe to it I really expect to be called each time that actions happens. Would be really weird, at some point, to simply have my delegate removed from the list. And this is because events shouldn't really have so much ...


12

Layout Calling .setSize() with fixed dimensions isn't portable. For example, this is what happens on Mac OS X: Instead, call frame.pack() to automatically set the frame size according to its contents. If you want the 2 × 2 layout, then use a suitable LayoutManager — for example, p.setLayout(new GridLayout(2, 0, 10, 6)); Don't Repeat Yourself The code ...


12

I've got a few small points about this code and then I have some other points regarding your design. I'll try to discuss your design in perspective to game-development (where I am completely inexperienced) and in perspective to (the more common Java area) business applications. Names and Design choices Your class (and interface) names seem to come from a C#...


11

You're right. There is a simpler way. First of all, instead of giving every interactive element its own method, let's give them all the same method. - (IBAction)buttonPressed:(id)sender; If they're not all buttons, a different method name is in line, but for this example, I'll assume them all to be buttons. Now then, whether you're creating these UI ...


11

Use MIDI Why play a WAV file, when you could synthesize the tones using MIDI? The tones can be played with indefinite length. You wouldn't need to distribute supplementary files with the program. You can also easily modify the program to generate other pitches without having to obtain new WAV recordings. import java.awt.BorderLayout; import java.awt....


11

Create a new Actionlistener class and add it to all Buttons: b1.addActionListener(new MyActionListener(1)) b2.addActionListener(new MyActionListener(2)) b3.addActionListener(new MyActionListener(3)) ... -- class MyActionListener implements ActionListener { private int guess ; public MyActionListener(int guess){ this.guess = guess; } ...


11

You can actually do this entirely in CSS with no need for JavaScript. You just have to switch to using background-image instead of an image within the anchor and apply a unique class (or id) to each element to target them in your CSS. .button { display: inline-block; width: 40px; height: 40px; } .star { background-image: url(https://docs.google....


11

The code is damn long, so just a few notes: Use initializer expressions like private JRadioButton setRed = new JRadioButton("Red", true); Use static factory methods to save even more like private JRadioButton setRed = makeColorButton("Red", true, Color.RED); private static JRadioButton makeColorButton( String name, boolean isSet, Color color) { ...


11

C# events are type-safe. The common (and recommended) pattern is to use EventHandler<T>, where T is a custom type deriving from EventArgs, containing the data of the event. You should use this pattern if possible, especially if you're writing a library that will be used by third parties. The problems that I can see in this pattern are: The sender ...


10

You can clean up the repetition by moving the ActionListener's code into a custom class, like this: static class KeyButtonListener implements ActionListener { private final JButton button; private final String filename; KeyButtonListener(JButton button, String filename) { this.button = button; this.filename = filename; } ...


10

You shouldn't be using images for text, nor should you be using JavaScript at all. The images aren't semantically relevant; they are just for styling. By using text, you'll make your site workable on text-only devices, as well as screen readers. Furthermore, it's actually easier to maintain your website if it's HTML text, rather than text that has been ...


8

You could make it a little bit more useful if you added some supporting factory methods to create the arguments. That way you don't have to use the constructor and its somewhat awkward syntax (having to supply the types). public static class EventHandlerExtensions { public static EventArgs<T> CreateArgs<T>( this EventHandler<...


8

I'm wondering if I should first get rid of all the irrelevant parts Yes. Especially since source control will keep any code you may want in the future. If it is not being used it is just taking up resources to maintain it. (e.g. everytime you read this section of code you have to remember what is and isn't relevant.) If these are Constants I would suggest ...


8

Being particularly pedantic, but I have the following observations: Annotation @Event You go to great lengths to document @Event but not to document the priority() setting .... just saying... The priority is the sort of thing that needs documentation... is priority == 80 more or less important than priority == 0? EventExecutor There is nothing in here ...


8

I was expecting this to be hard as, from what I've read, most people don't create their own Actually, there are more people who create event emitters than you think. I'm one of them, and here's my implementation. Aside from Event Emitter, it's also called "pub-sub", and here's one of the same nature under that label. It's a basic practice of the concept of ...


8

The trick here is to create an inner class. Something I've started doing recently is to create a method that returns the interface you want! private View.OnClickListener createClickListener(final int value) { return new View.OnClickListener() { buttonValue = value; // the rest of your code goes here }; } Thanks to marking the int ...


8

Indent everything one level inside of the Sub...End Sub block. Go one more inside of If blocks and loops. Private Sub Worksheet_Change(ByVal Target As Range) ' all code starts at this level of indentation If condition Then ' one more level End If End Sub There shouldn't be more than one blank line between instructions. Private Sub ...


8

IMO your events should accommodate the C# standard for events: public interface IFoo<T> { event EventHandler<T> ChangeValue; event EventHandler<object> ChangeObject; event EventHandler ChangeEmpty; void InvokeChange(T value); } The EventHandler delegate takes a source object as argument, which may be useful when ...


7

The standard signature for an event-handler delegate is void HandlerName(object sender, HandlerArgs args) Where HandlerArgs inherits from System.EventArgs So to follow this convention, you should create a new class: class OrderSubmittedEventArgs : EventArgs { OrderDetails { get; set; } } And then you could theoretically define the event as: /...


7

I prefer a simple cast over as when I don't expect an object to be of any other type. If you do expect this, use as and be sure to check for null. var comboBox = (ComboBox)sender; When it makes sense to group code together do so. if (!comboBox.IsDropDownOpen || !combobox.IsEditable || !combobox.IsReadOnly) Try to use LINQ where possible. Place variables ...


7

Well first you should think about refactoring your code. The following if (enabled1) { sectionButton1.setAttribute("class", "sectionButtonEnabled"); } is repeated a few times with just 2 differences. The enabled flag and the button. So this could easily go into a new function function SetClassIfEnabled(enabled, btn) { if (enabled) { ...


7

You could represent the mouse state using a struct: struct MouseSettings { Signal btnLeft_, btnMiddle_, btnRight_, btnX1_, btnX2_; int32_t absX_, absY_, relX_, relY_, scrollX_, scrollY_; } class MouseInputHandler : public InputHandler { public: MouseInputHandler(ResourceContext &context) : mouseSettings(mouseSettings_) {} ...


7

As mentioned in the comments, this is usually called "debouncing." A simpler and easier-to-use approach would be something like this: function debounce(fn, delay) { var timeout; return function () { var context = this, args = arguments; clearTimeout(timeout); timeout = setTimeout(function () { fn....


7

You can easily make the code more readable by breaking the large method into three smaller ones. My suggestion: void MainGame::ProcessScoreContainerChildren(const Touch * touch) { Vector<Node *> ScoreContainerChildren = pScoreContainer->getChildren(); for (auto childNode : ScoreContainerChildren) { if(childNode->getTag() == ...


7

A few things: You are overusing public static, all those variables should instead be private and not static. Whenever you can, avoid static. Also whenever you can, use private. The variable guess is not needed on the class, it can be a local variable inside each method. But if you use the answer by Jens you can just remove it from where it is now. You ...


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