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.

Friday, November 11, 2011

Droj's Javascript Evaluator Tool

When learning Javascript, one of the most valuable tools you can have is a way to run snippets of code on the fly, without editing a file, then saving it, then reloading the page in your browser. You can run individual Javascript lines with browser development tools, but they're not as nice as having a Javascript "scratchpad" to try stuff out on.

How to Improve Performance of Shell Scripts

Shell scripts in Linux/Unix have the potential to do a lot of work. A lot of times they will start out as just a couple of lines written as a convenience to avoid typing in a few commands repeatedly. Then a couple more commands are added, then a couple more, and eventually you have a huge script that sets up your entire network, checks your email, makes you pancakes, and walks your dog.

One doesn't usually think too hard about the performance of shell scripts. But if you scripts do a lot of work, chances are you can make them run several time faster!

Thursday, November 10, 2011

How to Add Fonts to the Console in MS Windows

In the settings for the console in Windows, you have the option of changing the font. But the selection is fairly limited. There are usually only 2 fonts! But if there are dozens of fonts installed on the system, why are there only 2 for the console? Well, if this bothers you, there is a way to add to the list of available fonts for the command prompt on Windows.

Wednesday, November 9, 2011

How to set up a .rc file for the cmd shell on windows

In the Unix world, all command shells allow you to set up your command line environment by adding commands to 'rc' files, .bashrc, .profile, .tcshrc, .cshrc, etc... The cmd.exe shell on Windows does not have a '.cmdrc' file, so you are left with a default setup every time you start a new shell. Luckily, there is a way to source a batch file when cmd.exe starts.


Thursday, October 14, 2010

Easy way to get glibc's "free(): invalid pointer" message

I agonized over this error for a while, because there was no obvious problem:

*** glibc detected *** ./test: free(): invalid pointer: 0x0921964c ***
... [memory dump]

Friday, July 31, 2009

Perl: File::Path::mkpath results in error: "No such file or directory"

I was running a Perl script that came with ACE to generate doxygen documentation. When trying to run it on Windows via Cygwin, I got the following mysterious error:
... : No such file or directory at line …

Tuesday, July 21, 2009

How to Changing Your Reviewer Color in MS Word 2003

When you have 'track changes' enabled in MS Word 2003 (and earlier), it automatically assigns colors to each reviewer in sequence. I, for one, can't stand to have all of my changes to a document showing in bright, stark RED! It just gets hard to look at if I have a lot of updates to make. Well, if I am the first reviewer of a document, then that's what I'll get.

But you can trick MS Word into giving you a different color.

Wednesday, April 8, 2009

Using POST to Send Form Data to a Servlet

Ajax is a great way to make your pages more dynamic. When using ajax, it's common to send data to your servlet in url-encoded form using a GET request. But there can actually be issues with sending information in GET requests. It's generally better practice to send data as a POST request, but there's one subtle detail required to make it work.

Friday, March 20, 2009

How to Get Thumbnails to Show Up for More File Types in Gnome's File Explorer (Nautilus)

Gnome's file explorer was showing thumbnail images for some file types, but not others. It does so for video files by using the default installation of Totem to get the preview image. So, for example I was getting preview images for .flv files, but not for .mpg files.

Sunday, March 15, 2009

How to Swap Control and Caps Lock on Windows

A lot of programmers like to swap caps lock and control. This is especially big with Emacs users, so they can avoid "emacs pinky" from constantly holding down the control key at the bottom corner of the keyboard. The caps lock location is much easier to reach and can make a huge difference if you use a lot of control key combinations. Swapping these keys is pretty easy on Linux, but on Windows, you have to hack the registry.