Wednesday, March 6, 2019

What is emailed....?

Before the section....

Friday, February 10, 2017

Repeated keys in Cygwin over Remote Desktop

When connected to a Windows PC over a Remote Desktop connection (aka RDP), and the connection has any slowness, the delays can trigger the key repeat timeout, resulting in extra keys being sent to Cygwin applications.

For example, I use cygwin at work, and everything works great. But when I'm at home, connected to my work PC via Remote Desktop, I was constantly having to correct repeated keys when the connection wasn't perfect. The xset command can fix this:

xset r rate 1000 250

This sets the initial repeat delay to 1000 ms (1 second), and the repeate at 250ms.

Thursday, June 23, 2016

Is C# Better than Java?

Ways it's better:


  1. Default argument values for methods - Java doesn't have them, which means you always have to "fake it" by creating numerous overloads to achieve the same effect.
  2. Support for unsigned integer types - Java doesn't have them, which means you have to play games with signed values of different sizes to achieve the same effect.
  3. Support for multiple languages in .NET. In C#, you can create and interact with objects defined in other programming languages. There is already support for several built in, like VB and C++, and others freely available, like Python.
  4. Lambdas. Java says "Wuh?". Actually, lambdas are a lot like Java anonymous inner classes, except they implement a delegate instead of a class or interface. The same semantics apply WRT closures and scope.
  5. Documentation is excellent in MSDN for C# and all aspects of .NET. There is developer reference as well as programming guides. The primary source of (vendor-supplied) documentation for Java is the Javadoc, which really is just an API reference; if I need guidance, I usually need to google it.
  6. Anonymous methods. Java ain't got 'em.


Shortcomings:

  1. Visual Studio is quite expensive for the average coder for the Professional edition or above, and the free "Express" edition sucks in comparison. Java development in Eclipse provides a free, full-featured IDE.

Similarities:


  1. Static constructors == Java static blocks
  2. You can specify a multi-level namespace in a single statement (instead of multiple namespace statements). This is basically the same as Java's package declaration.


Differences:


  1. A static class in C# == final Java class with only static members and private constructor
  2. Anonymous types are not Java anonymous classes. C# anonymous types are anonymous objects with no defined type at all. Java anonymous classes are subclasses of existing classes or interfaces that are assigned to a variable of that type.



Wednesday, June 15, 2016

Are Unit Test Frameworks Really That Great?


Unit test frameworks seem great. Modern test utilities like Mokito and PowerMokito give you the ability to test any part of your code, no matter how hard they are to get to, and no matter how inflexible and poorly designed the code is.

I can test my private methods! This is great!!

Or is it?

The public interface for that class changed. The functionality is only a little different. But the implementation is bunk, and it has to be redone!

Which would be more work?

A -- update unit tests that verify only the functionality of the public interface (which only has minor changes)?

B -- rewrite the unit tests almost from scratch, because they are tied inextricably to the private implementation and will all completely fail after the production code is rewritten?

The answer should be obvious, but if not...

A complete rewrite is more work, even for test code.

Tuesday, February 25, 2014

Printing Binary Numbers in Powershell

When working with numeric values and bit-flags, it is helpful to be able to see the binary representation of a value, especially when trying to understand what's going on.

Doing a search for printing a binary number in C# will get you results for functions and blocks of code that iterate through the bits. But that is completely unnecessary for this simple task.

The solution lies in the System.Converter class, which has 'toString' methods that take the base as the second argument.

Example:

PS> [convert]::ToString(0xfe, 2)
11111110

Saturday, July 28, 2012

PowerShell: How to Manipulate Application Windows

So, I wanted to write a script to raise a window. Simple, right? As far as I can find, there is no way to manipulate external application windows in pure C#.

Ideally, there would be a method somewhere that would give me a list of System.Windows.Window objects, which would then allow me to manipulate them in various ways. But I can’t find one.

The best I could come up with in my searches was reference for Windows API functions for manipulating windows based on their handle:

http://msdn.microsoft.com/en-us/library/windows/desktop/ff468919(v=vs.85).aspx

This is actually pretty good, since maybe not all applications are PresentationFoundation windows and wouldn’t be associated with a Window object anyway.

So, the next problem was to call these functions from PowerShell.

In C#, calling a native function is done by attaching the [DllImport] attribute to an ‘extern’ function declaration. This actually makes things much easier, because PowerShell v2 can compile C# declarations on the fly using the add-type cmdlet.

So, here’s how to make the Win API methods available:

> $t = '[DllImport("user32.dll")] public static extern bool SetForegroundWindow(int handle);'
> add-type -name win -member $t -namespace native

Here’s how to get a window handle:

> ([diagnostics.process]::GetProcesses() | ?{ $_.ProcessName -eq "chrome" -and $_.MainWindowHandle -ne 0 }).MainWindowHandle
133226

And finally, here’s how to call the method using the window handle:

> [native.win]::SetForegroundWindow(133226)
True

Saturday, November 26, 2011

Hungarian Notation - You Should Reconsider It

I used to think that Hungarian Notation was silly. I had a hard time seeing the point of it, and it just seemed like visual clutter when reading code. Eventually, I ended up working on a team that used it, and it really grew on me! The real problem with Hungarian Notation is that it comes in so many different forms. So, its effectiveness can vary, and the perception of it can vary as well, from highly useful to friggin’ annoying. But the benefits of a consistent naming convention shouldn’t be written off just because there are some less-than-ideal examples out there. The truth is, it’s pretty useful, and it can save you a lot of grief.

Wednesday, November 16, 2011

Add to the Java Classpath at Runtime

When writing an application, in any programming language, one often needs to load data from an external resource. This is typically a file on the filesystem, in which case one writes code to open the file using a filesystem path, and read the contents. The Java programming language lets you do this in a more abstract way, where instead of loading a file, or a device, you simply load a “resource”.

Monday, November 14, 2011

Java: getResource() vs. getResource() ...er, huh?

The 'getResource' method in Java allows applications to get files and other resources generically, without hard-coded knowledge of the type of resource, as long as it's in your class path. The only caveat is that you make sure you understand the difference between the getResource method and the other method used for the same purpose called 'getResource'.

Er... wait. Huh? Is that right?

Unfortunately, yes.