New IntelliJ Tricks I Learnt Part 2

Apparently, I’m all about Continuous Learning - after I wrote the last post about IntelliJ Tips, I carried on through my trawl of the documentation and, of course, discovered a load more things I didn’t know. And this is all just in the Quick Start section!

So here’s the next batch (although once again I’m not sure how many are only applicable to IntelliJ 14). All shortcuts are for Windows unless otherwise stated, and Mac users should be able to replace the Ctrl key with Cmd in all these cases.

Navigate between methods

Now I knew you could use the structure thingie (Ctrl+F12) to let you navigate to methods in your class (although I never actually use it, it somehow never sticks in my head). What I did not know is that you can jump between methods using Alt+Up/Down arrows. This strikes me as being super useful, especially to browse tests.

Refactoring String Fragments

I’ve wanted this feature so many times, but could never find it or get it to work - did you know you can refactor out parts of a hardcoded string into variable/field/constant? Select the section of the String you want to extract, and use the normal “Extract…” shortcuts - e.g. Ctrl+Alt+V to extract it into a variable.

I’m sure I’ve tried this before and it didn’t work. But I’ve tested it and it does!

Invert Boolean

There doesn’t seem to be a keyboard shortcut mapped by default, but using “Refactor -> Invert Boolean…” (or using our newly discovered “Navigate to Action” shortcut of Shift+Ctrl+A and typing “Invert”) you can tell IntelliJ you want a boolean field to mean the opposite thing, and it will flip the meaning everywhere. For example, you might have an saved flag, and you now want it to mean notSaved - IntelliJ will make sure the isSaved method returns !notSaved.

Magic Refactor Suggestions

You don’t always know exactly what refactoring operation you want to perform, and IntelliJ is ready to help - it can suggest refactorings available in the current context using the catchy magic incantation Ctrl+Shift+Alt+T. Seems useful, but I’m pretty sure I’m not going to remember that one.

Highlight implemented/overridden methods

I love everything about IntelliJ’s ability to find usages of things - finding where methods are called, finding implementations of a method, highlighting usages of a field, method or variable… What I didn’t know, is that you can highlight the methods that implement or override a specific interface or superclass. For some people, this might not add much, as your little “I override something” icon

"Overrides

combined with the @Override annotation, might tell you all you need to know. But that only really works if you’re implementing a a single interface. If you’ve correctly separated your concerns, you might implement several interfaces, and want to know which are the methods from a particular one. That’s where Shift+Ctrl+F7 on the “extends” keyword comes in handy.

Run individual inspection by name

I use inspections quite a lot. They’re especially useful if you have a codebase that doesn’t quite conform to your ideal standards, so you can’t yet turn on automated checks like checkstyle to break the build when the code doesn’t conform - there’s an initial step where first you need to clean up the code. Inspections are great, because you can have IntelliJ give you little yellow marks to show where things are suboptimal, and aim to check in only files without little yellow marks.

What I didn’t know, is that you can run a single inspection on files in any scope. So if, for example, you wanted to make sure that the code you’ve touched in this change has no unused bits, like unused parameters or unused/untested methods, you could press Shift+Alt+Ctrl+I and type “unused”. Select the “Unused Declaration” inspection and choose to run this on all uncommitted files. This will tell you where you can clean up your code, and every commit should make your codebase that little bit better.

Saving Power With Highlighting

I did not know you could easily alter the active errors/warnings, and use that to save power. Quite Interesting.

Auto indent your lines

I almost obsessively reformat my code as I go, pressing Ctrl+Alt+L almost like a muscle twitch. What I’ve just discovered, though, is that there’s a shortcut which will just fix the line indentation, which is probably 80% of what I need anyway - Ctrl+Alt+I.

Fix Evil String Concatenation

I can’t believe I didn’t notice this one earlier - do you know how many times I’ve gone around and manually changed:

System.out.println("This thing " + something + " is " + somethingElse); 

to

System.out.printf("This thing %s is %s%n", something, somethingElse); 

Do you??? Anyway you don’t need to - Alt+Enter inside the params will suggest the replacement and do it for you. Nice.

VCS Quick Access

One of the areas where I definitely don’t use IntelliJ to the best of its abilities is the Git integration. I’ve been using SourceTree combined with IntelliJ to do most of my VCS management (I’m so sorry, I’m just not a command line person, I need to actually see it). With the new laptop, I haven’t installed SourceTree yet and I’m going to try and do everything via the IDE. Previously I used the right-click menu mostly for managing VCS operations, occasionally the VCS menu, and did most stuff in the “Changes” window. But now I’ve discovered Alt+"Back Quote” (Ctrl+V on the mac) to bring up the menu of git-shaped operations for this context. To stop you having to hunt all over the keyboard to find what on earth a “Back Quote” is, it’s `, which on both my UK and US (Windows) keyboards is in the top left corner of your keyboard (on the Mac, it’s on the bottom left next to the shift, but you shouldn’t need it since your shortcut is different anyway).

Amend Commits

I’ve just discovered that you can commit a change which smushes up into your last commit and fixes it. I’m assuming it commits the new changes and (potentially) an updated commit message into the last change. I really wish I’d known about this earlier, on a number of occasions in the last few months I’ve done this either manually or by doing a rather dangerous soft reset. I will definitely be using this in the future.

"Amend

The Cursor as a Breakpoint

Did you know that you can use the cursor as a temporary breakpoint? I did not. Well, now you know.

Labelling Changes

I hope by now none of us developers are doing that icky thing of having “Copy of…” and “Backup of…” filenames and directories to keep track of our versions. We’re all using a VCS of course. But if you’re being strict about making sure that every commit leads to a working build (we all do that, right…?) then sometimes even distributed file systems like git, with its local branches, are too heavyweight for experiments and debugging.

IntelliJ has Local History, which I personally think you should never rely on, but has occasionally saved my bacon in times of strife. It also does this really nice thing (automatically) where it labels versions where the tests passed, so you’ve got some level of confidence that at that point in time, at that revision, things weren’t hideously broken. At the very least, they must have compiled. What I did not know is that you can add your own labels, so you could label some local, non-committed version of the file as “feature x works but y doesn’t” (or probably something much shorter).

I think it looks useful. As I say though, not a replacement for VCS, but a suppliment.

So, that's the end of Week One at JetBrains, and already I've learnt a whole heap of things. I'm sure there's more to come, so stay tuned!

New IntelliJ Tricks I Learnt Today

Day three of my brand new job - woohoo! One of my first tasks is checking the documentation of IntelliJ, since I’m already a “Power User”, to see if there’s stuff missing that someone like me might want to see. Firstly, a confession - I’m not sure I’ve actually read the IntelliJ documentation before. I mean, really read it. Of course, I’ve dipped into it when I needed to know something, I’ve searched for help with specific items, but like many developers, I tend to use something first rather than read the documentation first.

But I’ll tell you what, today I’ve learnt a whole bunch of useful things IntelliJ can do that I hadn’t previously known, and they’re the sorts of things you would only stumble over if a) you read the docs (pah!) or b) you watched someone else using the tool and saw them doing something you didn’t know was possible.

So here’s today’s list, and feel free to laugh at me and be smug in my direction if you already knew these things (note: I have upgraded to IntelliJ 14, and at the time of writing I don’t know which of these features are new and which are simply previously undiscovered):

Navigate directly to an action or option

Press Shift+Ctrl+A (Windows) or Shift+Cmd+A (Mac) and start typing the name of the thing you want to do, or the setting you want to change. I’ve found this especially useful now I’m back on Windows and I don’t have that nice helpful typing box on the help menu that will take me straight to the menu item I want. But what’s most useful about this for me today, as I’m setting up a new profile on a new laptop (more on the new laptop later, if it doesn’t defeat me utterly) is that from here, you can turn various settings on and off without having to navigate through the settings screen:

<img src="https://trishagee.github.io/static/images/stuff_i_learnt_about_intellij/action.png&#34; alt="Navigate to Action" title="Navigate to Action">

Statement completion

I never knew there was a shortcut for finishing off a statement, all that Java boilerplate you need to keep the compiler happy - semi colons and curly brackets and everything. But there is, and I can see it being super useful, especially if you’re using other bits of IntelliJ code completion to create the core of your line of code.

Issue tracker integration

I’m sure loads of people are already using integration with their favourite issue tracker, but I hadn’t really thought about this as something that I really cared about. But I was watching the intro videos for YouTrack (not surprisingly I’ll be using this instead of Jira during my time at JetBrains) and when I saw how the IDE integration worked, I suddenly realised how this small thing can add up to save thousands of keystrokes over time, not to mention saving precious navigational seconds and window-switching.

For me, just the ability to generate at least the start of the commit message is a saving in time, and adds to project consistency if all your developers are doing the same thing. On my last project we only just realised we all quoted the Jira issues in subtly different ways.

Settings shortcut keys

When I initially started using IntelliJ in anger back in 2008, I was very much a point-and-click type user. Over time, pair programming with keyboard-super-users and giving live demos with very limited time available, I’ve worked harder to become a keyboard-shortcuts person. With the latest version of IntelliJ, I can’t find the tiny icons I use to get to the settings window, something that I’ve been using a lot as I set up the new IDE instance on the new laptop. So I bit the bullet and learnt the keyboard shortcuts, and it’s already saving me a bunch of time (on the rare occasions when the aforementioned “Navigate to Action” fails me).

Ctrl+Alt+S for the settings dialog (appears to be Cmd + , on the Mac) Shift+Ctrl+Alt+S for the project structure dialog (appears to be Cmd + ; on the Mac)

<img src="https://trishagee.github.io/static/images/stuff_i_learnt_about_intellij/settings.png&#34; alt="Settings Shortcuts" title="Settings Shortcuts">

Yeah. I can’t believe I wasn’t already using those either.

Selecting the negative

I use completion/suggestions all the time from IntelliJ, I’m far too lazy to type everything. What I didn’t realise, is that you don’t just have to accept the default they give you. If, for example, IntelliJ suggests a boolean value, you can get it to negate that value by selecting it with “!” instead of enter or tab. Yes, I know I’ve done a horrible job of explaining that, but take a look at the documentation and try it, it’s quite cool.

Exclude from Autocomplete

Speaking of code completion, sometimes IntelliJ is sometimes just a bit too keen to suggest something, and you really will never want that value. In the past, I’ve always used Settings -> Editor -> General -> Auto Import to manage the suggestions (the most common one for me is I will never want java.awt.List, I will almost always want java.util.List). Well, you don’t have to go into settings to do this, you can do it from the suggestions themselves - navigate to the item you don’t want (in my case, java.awt.List) and press Alt+Enter, this will give you the option of excluding this from the suggestions in the future.

That’s what I found from just one day of browsing the documentation and setting up my IDE from scratch yet again. Maybe I’ll promptly forget all these, or maybe they’ll be new weapons in my armoury of IDE-fu. Either way, maybe one or two people will find this useful.

Why Java developers hate .NET

I have been struggling with .NET.  Actually, I have been fighting pitched battles with it.

All I want to do is take our existing Java client example code and write an equivalent in C#.  Easy, right?

Trisha’s Guide to Converting Java to C#

Turns out writing the actual C# is relatively straightforward.  Putting to one side the question of writing optimal code (these are very basic samples after all), to get the examples to compile and run was a simple process:

1. Find-and-replace the following (only you can’t use Ctrl+R like I expect.  Sigh.)

final = readonly (but remove from method params)
System.out.printf = Console.WriteLine
Map = Dictionary
BigDecimal = decimal
Set… oh.  I have no idea.

2. When using callbacks, replace anonymous inner classes with delegates

Java

something.doSomething(new SomethingRequest(),
new SomethingCallBack()
{
public void onSuccess()
{
System.out.println(“Action successful”);
}

public void onFailure()
{
System.err.println(“Action failed”);
}
});

C#

private void foo ()
{
_something.DoSomething(new SomethingRequest(),
SomethingSuccess,
SomethingFailure);
}

private void SomethingSuccess()
{
Console.WriteLine(“Action successful”);
}

private void SomethingFailure()
{
Console.Error.WriteLine(“Action failed”);
}

I rather like this pattern actually. You can’t really tell in the noddy example above, but the C# code is generally shorter and more reusable.

3. Replace getters and setters with properties

Java

private class MyClass
{
private BigDecimal myField = new BigDecimal("-1.0”);

public BigDecimal getMyField()
{
return myField;
}

public void setMyField(BigDecimal instructionId)
{
this.myField = myField
}
}

C#

internal class MyClass
{
public decimal MyField{ get; set; }
}

What the… where did all my code go??

My Thoughts

I was pleasantly surprised with the language. In general, for what I was doing, the equivalent C# was a lot less code. The fact that the syntax is not wildly different from Java made the transition relatively easy, even if I don’t get all the nuances.

I didn’t really like the enums - I can see what purpose they serve, but I quite like the way the Java ones are pretty much classes in their own right with properties of their own - it allows you to encapsulate some of your simplest domain objects. But it’s a minor point, not a deal-breaker.

The C# capitalisation makes me queasy though. I just can’t get my head around it. In Java, if I say:

com.mechanitis.foo.Bar

I know the class is Bar and the rest is the package (or namespace, or whatever). This is more useful when you’re using static methods and so forth:

com.mechanitis.foo.Bar.thatThingIWantToDo()

In C#, I know the thing at the end is a method and the thing before that is a class, but it doesn’t jump out at me:

Com.Mechanitis.Foo.Bar.ThatThingIWantToDo();

And if you’re using a property:

Com.Mechanitis.Foo.Bar.MyProperty;

The whole thing makes me dizzy.

You could argue that all this is redundant with modern tools and IDEs doing all the heavy lifting for you - nice colourisation etc.

Which brings me to The Rant.

Oh My Dear God What Is Wrong With Visual Studio?

C# needs to be a shorter, more succinct language because it takes three billion times longer to do anything in Visual bloody Studio.

I’m coming at this from a Java, IntelliJ point of view, so there’s always the possibility it might be lack of familiarity with the tool, rather than the tool itself, which is the problem.  It’s a long time since I used VS, and that was back in the 90s when I was doing ASP and COM (shhh, don’t tell anyone).

But things shouldn’t be this hard.  I was ready to accept, due to my newbie status, the IDE not helping me. I didn’t expect it to actively hinder me.

For example: it took hours of my life that I will never get back to discover that you can’t simply run a class that has a main method by right-clicking and selecting “run” (note: I’m not even trying Ctrl+F10).  No.  I have to select, at a Solution level, which Projects are runnable.  Then I have to select at the Project level the class with the main method I actually want to run.  Then, it opens up a terminal window and runs it in there, which promptly disappears when the program errors or finishes.  What’s wrong with outputting in the output window of the IDE? Is that not what it’s for?

Finally I worked out how to run the cursed program (seriously, like that’s not the first thing everyone wants to do?  How do people write “Hello World”?).  Now, where are the command line arguments?  Of course, they’re at the project properties level too, because each project only has one entry point.  I seriously had to Google that too because I couldn’t work it out from the IDE alone.

The next day, my ReSharper licence had expired.  I decided I should attempt to limp on without it, after all hundreds of developers must be surviving with just Visual Studio.  How bad could it be?

Very bad, it turns out.

All those helpful little squiggles I was leaning heavily on to convert my Java to C#?

  • The red to tell you you’re utterly wrong.
  • The orange to tell you could be using less code.
  • The blue to remind you to stop thinking in Java and helpfully offer corrected naming.
  • The green to suggest stuff that C# could do differently.

Yeah, all gone.

How do people code like this?  Do they really just do a build to check if it’s all wrong or not?

Next, I try to find a class.  I actually have no idea how to do this, because I can’t use Ctrl+N.  So I Ctrl

I realise this is a waste of time anyway, because one thing that really annoys me about Visual Studio is that I can’t find a way to sync the project tree to the class file I’m looking at.  I can’t get it to jump to highlight where I am.  When I’m using IntelliJ, I find this dead useful when I want to see other stuff in the same package.

Less than ten minutes after attempting to use Visual Studio without ReSharper, I’ve abandoned the fight and tracked down a licence and installed it.

Documentation isn’t a standard function of .NET?

What sort of message does this give developers?  Documentation isn’t important?

I always thought Javadoc was pretty ugly and clunky. In addition, now our IDEs generate so much, it’s frequently meaningless.  But it is generated by standard Java tools, and HTML is a standard format that can be read on pretty much any computer with any operating system.

I could not believe how hard it was to get the XML comments out of the C# into something the user can actually read.  Thank goodness, some enterprising member of the team had already done that for us.  All I needed to do was hack/crowbar the tutorial I’d been working on into the generated documentation, so it ended up in the Windows help files in some fashion.

I know there’s a way to get HTML/XML files into the end result using Sandcastle, but hours of Googling only told me it was possible, not how to do it.  I still have no idea what the correct question is to ask to find the solution.

Right now, this is an unsolved mystery.  Our .NET client users will have to read the plain HTML I’m afraid.

In Conclusion

Are we lowly Java developers spoilt with our shiny IDEs?

Or is there such a fundamentally different approach to development for .NET people that all the functionality is there, I just can’t find it?

I’m disappointed if I’m honest.  I’m sure the .NET camp used to tout their tools as their superiority.  I ended up feeling sorry for the poor NET people.  Is there anything they can use that isn’t Visual Studio?

In Conclusion: Despite the nasty capitalisation I found myself surprisingly taken with C#.  But until they can give me a proper development environment, I won’t be tempted by the dark side any time soon.