We’ve all heard of the benefits of pairing when it comes to coding, many of us have done it in our jobs and reaped the rewards, but have you ever paired to write English?
We’ve all heard of the benefits of pairing when it comes to coding, many of us have done it in our jobs and reaped the rewards, but have you ever paired to write English?
One of the super cool things about IntelliJ IDEA is how much code you can generate with minimum effort. Yes, it’s not the 1990s anymore, we’re no longer measured on how many lines of code we generate (thankfully), but you also know that Java has its fair share of boilerplate code.
Well, there’s a shortcut in IntelliJ IDEA that generates a lot of code for you:
⌘N on macOS
Alt+Ins on Windows and Linux
These shortcuts load the Generate menu! Here’s a quick tour of where you can use it in Java projects in IntelliJ IDEA. It’s not a complete list; let me know where else we can use it please!
In the Project Window, you can use this shortcut to create a whole host of things which are project and folder specific. If you use the shortcut on your directory that is marked as your sources root (usually src) in a Java project you get the option to create a new Java file (among other things). You’re then asked to select between a Class, Interface, Record (Preview), Enum or Annotation. It’s a speedy way of creating new classes for your project.
Before we move on, a closely related shortcut is the one we use for a new Scratch File. It’s ⌘⇧N on macOS, or Ctrl+Shift+Alt+Ins on Windows/Linux. You can select to create a new Scratch file using ⌘N on macOS, or Alt+Ins on Windows and Linux in the Project Tool window, but it’s worth committing the scratch file shortcut to memory too as it’s handy to be able to dump some code or notes in an area outside your project and share it across IntelliJ IDEA projects.
Now that you’ve got your class, you may want to generate a constructor or two. However, before we do that, let’s add a couple of variables to our class:
public class GenerateCode {
private final String name = "Helen";
private int age;
private String mood;
}
We can use the same shortcut to make ourselves a constructor. We get some options here because we’ve got some fields in our class:
IntelliJ IDEA is asking us if we want to pass our fields into our Constructor.
If we select both and click OK we have our Constructor with the parameters passed in.
public class GenerateCode {
private final String name = "Helen";
private int age;
private String mood;
public GenerateCode(int age, String mood) {
this.age = age;
this.mood = mood;
}
}
We don’t need to stop there either. There’s a whole host of code that IntelliJ IDEA can generate for us at this stage including:
Getter
Setter
Getter and Setter
equals() and hashCode()
toString()
Override Methods
Delegate Methods
Test
While we’re here, Java Records are coming and IntelliJ IDEA is ready. Another way you could generate code if you’re not ready to move to Java Records is to use the Generate shortcut to create a new Java record, and then you can convert the Java record to a normal Java class with ⌥⏎ on macOS, or Alt+Enter on Windows and Linux with your caret on the class name.
When our Java class implements an interface, we need to ensure that we implement that interface’s methods. The Generate menu helps us here too. Let’s say that our code looks like this, and we’re implementing NewInterface:
public class GenerateCode implements NewInterface {
private final String name = "Helen";
private int age;
private String mood;
}
When we use ⌘N on macOS, or Alt+Ins on Windows and Linux this time, we see select a new option call Implement Methods:
Now IntelliJ IDEA has generated that code for us:
public class GenerateCode implements NewInterface {
private final String name = "Helen";
private int age;
private String mood;
@Override
public void doSomething() {
}
@Override
public void goSomewhere() {
}
}
This also works for overriding methods from superclasses/super abstract classes.
Another useful trick you so is to use ⌘N on macOS, or Alt+Ins on Windows and Linux when you’re in a dialogue, and you need to add more rows or data. For example, we added a default constructor to our class, but we now want to refactor it to change the signature. Our code currently reflects the default constructor:
public class GenerateCode{
private final String name = "Helen";
private int age;
private String mood;
public GenerateCode() {
}
}
Let’s refactor the Constructor with ⌘F6 on macOS, or Ctrl+F6 on Windows/Linux. In the Change Signature dialogue, you can use ⌘N on macOS, or Alt+Ins on Windows/Linux to add a new parameter. This saves you using your mouse to click the little + icon.
This trick works in all the dialogue boxes that require additional lines to be added that I’ve found so far.
Finally, everyone loves a good test and rightly so. We’ve already mentioned that you can use the Generate menu from a Java method to generate a corresponding test class. However, once you’re in the test class, you can use ⌘N on macOS, or Alt+Ins on Windows and Linux again to create much of the boilerplate code you might need, including (for JUnit5 at least):
Test Method
SetUp Method
TearDown Method
BeforeClass Method
AfterClass Method
If you are working with a different testing framework, your Generate menu will give you other relevant options.
Java may be a little clunky on the boilerplate side of things, but IntelliJ IDEA takes the heavy lifting out of that to a large extent so along with the shorcut for intention actions, it’s a compelling combination.
I’ve never given the word normal that much thought before, but 2020 has made me challenge my perceptions of normal. Normal is just a word used to describe the current social status quo. Normal right now is facemasks, excessive soap, and social distancing. If you’d have mentioned phrases like lockdown, covidiot, or keyworker in 2019, most people would have tilted their head to one side, given you a quizzical look and wondered just how large the wine was you had at lunchtime.
Equally, if you’d have said they’d be a fundraiser to buy Chris Whitty his own ‘next slide’ clicker, that Jonathan Van-Tam would become famous for comparing yoghurts to vaccines, or that Brexit would not be the biggest story of 2020 (in the UK), people would have likely nodded slowly and pretended that they had to leave to take an urgent call.
However, all of this is now entirely normal for me. Along with teachers giving kids grades because exams are cancelled, hugging loved ones through transparent shower curtains, and me waving manically at my webcam several times a day. Normal is a word that describes a constant state of flux. I’ll be using it more thoughtfully in the future. I’ll also be checking if I’m on mute.
These are my top three learnings from 2020.
I pivoted my career from Technical Writing to Developer Advocacy in 2020 (via Product Owning). I did Java at university, it was a long time ago, but as it turns out, I still rather like it. Unlike the more prepared of my peers, I turned up at Sussex University to study Computer Science with virtually no knowledge beyond how to plug a computer in, and a bet to get a degree that I was determined to win. Retrospectively, I probably could have drunk a little less, and applied myself more, but I was 19 and somewhat lacking in life experience and foresight.
Fast-forward 20+ years, I’m no longer 19, I have some life experience, and occasionally I have foresight. This has meant that as my career comes full circle and back to Java (which, much like me, has changed a lot), I’m finding it much easier to work with the language and apply myself. It’s made me realise how much I love the language; although I think a good portion of that love is also attributed to the invention of IDEs such as IntelliJ IDEA. Wow do they make life easier when it comes to learning to code; I wish they’d been around in the ’90s!
I guess age isn’t all that bad after all (aside from pulling muscles doing mundane tasks around the house which seems to be part of the package). Coming back after a period of evolution is really enjoyable, especially in a role where I can learn and share with others who are on the same journey.
Honestly, before 2020 this terrified me. However, 2020 brought this new normal, which involved speaking from the safety of your dining room table/box room/kitchen/sofa to an audience. I’d never done a conference talk of any sort before except internally at places I worked. I joined the London Java Community, and I started with a 5-minute lightning talk. I got the bug; I got it really bad actually. I was offered the chance to moderate a YouTube panel through work and took it, and this week I’m giving a longer presentation to code nation, and I can’t wait. Oh, I’m also doing a podcast episode in a couple of weeks!
I really enjoy speaking, and I’m looking forward to being able to do it in person, so I can better learn the craft and meet more people. I’m sure that’s a completely different skill-set, but one I look forward to working on.
2020 gave me the gift of time. It gave me an hour of commuting time back a day (at least), it gave me all my social commitments back (I’m still in mourning for these, but I hope they’ll be back one day), and it meant that the phrase “set the alarm” was a fairly pointless one. The 2020 commute to work could be managed in about 7 minutes providing I didn’t have bed hair.
There was no requirement to go anywhere, in fact, it was mostly against the law to go anywhere except the supermarket, and the one thing that needed to happen (work), felt oddly more manageable as a result. Of course, this is just my experience, for many people the 2020 experience robbed them of time, especially for those with children which I recognise.
Initially, I was a little bit confused with what I should do with all the time that I wasn’t used to having, but gradually I became rather accustomed to it. I have read more books this year than in the past five years combined, I have created myself a platform (this site), I have embarked on learning new things both for fun and in my job, I have crafted house-based gym routines that even the most sadistic gym-goer would be proud of, I have made quilts for friends and colleagues, I have contributed to open source projects, and I’ve started writing regular blogs on a variety of subjects too.
I know for many that 2020 has been an incredibly tough year. I have definitely had many low points as well, but these are the learnings that I’m taking from the year for myself, and I hope I can build on them in 2021; whatever that looks like.