Jul 10
Fasten your seatbelt
Using your IDE effeciently is an integral part of being a proficient developer, Intellij offers a multitude of
options to turn your coding experience into a fast mouseless one.
While most developers memorize the key bindings they tend to less explore other options of cutting key strokes away,
in this post ill cover couple of those, while the examples ill be showing are in Java land the ideas should be
useful in other languages as well.
@Test
public void someTest(){
// ..
}
You type the public void someTest proceeded by the curely braces, then you head up and add the @Test annotation, quite repetative isn't it? it would be nicer to be able to do something like:
test <Tab>
expands to ->
@Test
public void <cursor>(){
}
// now you enter the test name
@Test
public void someTest(){
}
In order to enable this head on to the settings (using Alt+Ctrl+S) and search "Live Templates", add a new template called test with the following content:
@Test
public void $NAME$ {
}
Make sure to set the context as Java Code and your set to go, other templates that I like are iter (foreach loop), psvm (public void main), and thru (throw runtime).
Another optimization helps us to find out early when we forget to implement an interface method:
public interface Bar {
void someMethod();
}
public class Foo implements Bar {
// a long list of other methods
public void someMethod(){// oops
// intellij default comment
}
// yet some more code
}
Discovering it couple of minutes later during (hopfully) a test is quite annoying, wouldnt it be nice to get warned if you forgot to implement it?
public class Foo implements Bar {
// a long list of other methods
public void someMethod(){
throw new RuntimeException("not implemented yet");
}
// yet some more code
}
Head on to settings and look for "File Templates", select "implemented method body" and replace it with "throw new RuntimeException("not implemented yet");",
this will make un-implemented methods fail loud and clear.
Our last optimization involves finals, mutating local variables and method parameters is limiting readability and make it harder to write thread safe code:
public class NotReadable{
public void wacky(int i) {
//.. post some logic with j
i = j;
// multiple lines after, do we really know what i is now?
return i + 4;
}
}
Making them final solves this however going past each one manualy is really tidiouse so most devs just avoid it, well there is an easy way to automate it,
head on settings and search for inspections, look for "decleration can have final modifier" and enable it:
public class NotReadable{
public void wacky(int i) {// intellij highlights i, alt + enter and select "fix all"
// ...
}
}
In post iv introduced three quick methods of saving key strokes, our goal is not count how many keys we press but rather to remove distractions in order to concetrate better on the problem we are trying to solve.
Follow me online