8. PMD for Apex Static Code Analysis | Robert Söesemann


pmd
Robert Söesemann

Robert Söesemann ported PMD, a static code analyzer, to the Salesforce world. He teaches me what this is all about. This talk is a continuation from the last episode.


Main Points

  1. How PMD works
  2. What is PMD for Apex
  3. Out-of-the-box rules
  4. Code formatting check in PMD for Apex
  5. Integration of PMD into various IDEs and CI-platforms
  6. How to get started
  7. Ways to customize and create new rules
  8. Contribute to the PMD open source repo
  9. Clean code with PMD as a Trailhead topic

Links


Contact Me

If you enjoy this podcast, please reach me out to let me know your thoughts or simply say Hello. My (Xi’s) contact info:

Take care, until next time :)!

Transcript

AI-generated, unedited.

[00:00] Do do do! You're tuning to Salesforce Way podcast, episode eight, PMD Apex. In

[00:16] the last episode, Robert and I discussed about the clean code. In this episode, our talk continues. Here, Robert teaches me what PMD Apex is and how it automatically validates our Apex code for the bad code quality. If you enjoy our talk, don't forget to thank Robert on Twitter and advocate his Dreamforce proposals. His Twitter handle is linked in the show notes, and our website SalesforceWay dot com. Okay,

[00:57] so you just mentioned the PMD and the PMD Apex, right? So we want to dive into that part as well. So you said it's like a code analyzer tool, right? The PMD. So how do you port it into Apex, and what does it really do? Could you give some simple examples or elaborate a little more? So as I said, it's the the the type of software is called static code analysis. The static is the important part. It doesn't. Okay. Have you ever heard about the tool checkmarks? No. From Salesforce. This is not a static code analyzer. It really checks code during runtime. You know when. When code runs, um, you really know which method calls which other method, and this is used by Salesforce for doing security review. You know when you for the ISV the managed packages. Yeah, for for security issues when you put some stuff on the App Exchange, and they check your code if if there's not a security leak. But this is really you. You basically need to run the software. But now Salesforce is run on their servers, so. Static code and analyze is analysis something which is also very helpful. It just takes the classes as they are with the source code and parses. Them, and basically apply some rules. Just imagine, um, you, you. I'm sure

[02:27] even after three months, you heard about the rule that you shouldn't have SOQL inside of loops. Yeah, because when there are some limits about how many. Suckle queries you can do, and if it's in a big loop, it it will basically crawl your break your stuff. So um, and and a static code analyzer is perfectly able to do that because he knows where is a loop, and if inside the loop there's a suckle statement, he will basically. Tell its system there's an issue, and, and with PMD, PMD is written in Java, okay, and which parses the language and then basically looks for a bunch of rules.

[03:11] Notifies of errors. It gives out a report. It's basically a big Java program which runs on the command line, and yeah, and it exists for Java for years, for a century. I don't know. Okay. And I I I came from a Java background, and a few years ago I was thinking about how we can somewhat guarantee code quality across the teams and across the the skill level, and I thought. I mean, Apex is is is Java under the hood. It's basically less than Java, so it should be really easy to take something that is for Java and make it work for Apex. Was it that easy? I thought. That's what I thought. Okay. Then I learned about how complicated parsing languages is, and how nifty the details are. And um, I did this together with a student in a student project. He helped me a lot with that, and we. But we we basically um couldn't make it. We weren't smart enough to to write our own Apex parses. Others did around that time. So there was already a commercial product around at that time. But then I thought there must be an open source parser in a way because Eclipse, the Force.com IDE, it seems to parse my code. I I just tried my Eclipse without internet connection,

[04:42] and it did it. It knew what methods were and. And then I thought, yeah, maybe I should somewhat reverse engineer the Force IDE, and I find out there was a parser, but no code. And then, yeah, what I did, I basically contacted the guys who made the Force IDE, Force.com IDE, and asked them, hey, could I just use your parser? And they said yes. And then. And they were very helpful. We struggled because we didn't have source code, but it it it worked out. And so after half a year, I I I was able to parse Apex, and then I I took. There are hundreds of Java rules, but I it took me more time to decide which rule to keep than which not to keep, and I I ported like. A set of twenty rules, wrote a bunch of rules which doesn't exist in the Salesforce world, like don't do cycle in inside of loops, and then I published it, and then I went to Dreamforce and talked about it, and yeah, and then then people get interested. It would the room was full of people. The the the great guys got wealth from the IntelliJ extension Illuminated Cloud basically programmed the PMD extension um for his IDE during the Dreamforce session and then others jumped on and um basically and and all the coding tools around VS Code or

[06:12] so we have um there there is a PMD extension so yeah. PMD was adopted by the tooling providers. I'm, I'm not sure if if there are really many developers already using it, but I hope. Okay, our our our talk will take part in changing that. Okay, I I love this story. So was the Force dot com IDE using the PMD under the hood for the parser? No, no, no, no. So it's another one. No PMD is. It needs a parser. You you basically for a static code analyzer, um, it needs a parser for the language is parsed. And and PMD had a Java parser which we could use. So we we took the the parser from the Force. dot com IDE and made it work with PMD. Ah, okay, okay. So, um, it PMD is is is a framework because PMD can also check C sharp code or JavaScript. And for every language, it has its own parser and its own set of rules. Okay. So sometimes people really say PMD is my baby, but it's not. I just wrote a language extension and contributed some rules. Other people also contributed rules, but but this is basically it. PMD takes the source file, breaks it down into what's in there,

[07:42] and then there are some rules which basically say. Do you have loops? And then the parser says yes. I have two of them. And then isn't then the rule says um. Do they have circle loops inside of them? And then the person says yes, and then PMD says okay, rule issue. Okay, how it it it basically applies all the rules it knows about Apex and Visual Force to all the Apex and Visual Forces. Stuff your directory and then it outputs a list. Okay. Now, now I get it. So PMD is like a abstract framework that we need to feed in like a parser for different languages and feeding extra rules that the parser can help. Right, okay. Okay. If you check the PMD project page, you see you can. But but Java is the biggest tool there. For for C sharp and C plus plus, there were other tools. So it's it it cannot PMD cannot deny that it comes from Java background. But um it it it is used by some other languages. And PMD is is basically used by many plugins and tools. So. When I did it, there was already an Eclipse plugin for PMD, which then could immediately be used in the Force.com IDE. So when I did this Dreamforce talk two years ago, I also could show how wonderfully it works in the IDE. So this means the developer writes a class, runs PMD,

[09:13] and sees some bad stuff. Okay. And back then I didn't like that. I didn't want to to make code quality like a private hobby of developers. I wanted to have it in my continuous integration. So I, I. There are some some companies that basically sit on top of your GitHub, and when you commit or pull request, they they can do something like Circle CI. And I found a company which which which had such thing, and they allowed me to build a PMD engine. And after that, I was able to. Whenever somebody checks in, PMD is run on the GitHub commit, and then you get an email that you you have some issues and. Right. Okay. Okay. I I got your point. So um, it's not only about some local fun you have as a developer. No, it's really it's it's building quality. It's a required tool. And up to go when when you are done with your user story, you check it in. You see what PMD tells you. You fix the the naive problems. When I say naive, I mean the trivial stuff that that other developers don't want to. Take care of, and then fix them, and then you can go into a human code review. Okay. So it's like a one extra automated code quality check by PMD. Yeah. Okay. Absolutely. Okay. What kind of out

[10:44] of box rules does PMD Apex, especially for the Apex language, nowadays already offer? What do you mean out of the box? Like, if I install PMD, what kind of rules can I already run? Yeah, as an PMD has, I think around thirty, forty rules. I don't know some Java stuff like styling. Okay. People they they fight about how the the braces should be and there should be a space between four and the the yeah the brace and and how methods should be called methods should be uppercase or lowercase and global variables stuff like that um that's that was important to me it's it's not super configurable so the the the style rules in in in PMD they basically check that the the Apex code adheres to. To Java Sun standards, so that that's one part. There are other rules which check. the the the complexity of code. This is really actually my favorite. It's it's a rule which, which does some some scientific stuff. It counts how many different path, um, the compiler can go through your method or code. Just imagine if you have a class which just calls two methods. There's basically one path, but if you have an if else, you have two paths. Right, right,

[12:15] stuff like this, and and it's called cyclomatic complexity, and it basically tells you how many. Different tests. Do you need to really semantically cover this method? So, um, if if a method has a has a cyclomatic complexity above ten, you basically know it's a mess. It's not understandable. It's not testable. So it it also tells you about the complexity of your code. Then there are many security rules which check stuff that are important for the the the security review by Salesforce. If you used escaping in the right place, if you do some field level security checks and crud checks before you do DML. They were contributed by people from Salesforce. So this is really great work. Those security rules are really complicated to write.

[13:13] And there was a guy back then who wrote many of those things. What else do we have? Yeah, stuff like class too long, method too long, all the stuff that makes classes really look like spaghetti code. At the beginning, you mentioned the syntax check, because I understand in Apex. No matter which IDE you are using, the syntax wasn't so important, and the Apex language is not a case-sensitive language either. I see code with all different styles, no matter where the bracket is, so it always goes well. Right. No, no language does. No, it Java also doesn't care. Okay. I mean, Salesforce is is even more special. I think. When they when they invented Apex, they thought the language should work for Java developers and C Sharp .NET developers. That's why. They got loose on this uppercase lowercase thing, but I don't know. I I absolutely know that in Teams it's crucial. It's not it's not important if where the brace is, but it should always be in the same spot. You're right. Because people can really go religious, and I this is just such a waste of of thinking time of smart people. Yeah. It's it. There is no true or false. There's

[14:43] I mean, yeah. Just make one at the beginning of the project. Stick to it's it right and and yeah. It's I didn't mean syntax. Syntax is the stuff the compiler checks. I meant formatting. Yeah, right, right. But but also like method names and yeah, the the simple stuff. If if a method is If a global variable is not a public final, is not is not written full uppercase, you don't recognize it as such a such a such a property, and then you might misuse it. And this is just stuff that, it's it's boring in the Java world, but in Salesforce world, it's really a great new tool. Okay, and when you look at the up to go code base. Everything looks equal. So, so from if you don't read the stuff, you you cannot recognize who who made it, because it looks absolutely the same. And yeah, just just to make it clear, those formatting rules were from the Java language, and we can just use it in in the Apex. No, no, I, I, I had to port every single. Okay, but I just decided that the Java rules are cool. It was totally egoistic. I, I never wanted to make a product. I, I said, at this, my, my student,

[16:14] he said, shouldn't we also add stuff for others? I said, I don't care. It's an open source project. We make it like we use it. PMD lets you really configure how severe stuff is, and. And I said, "Yeah, stuff that we think is important, we make it important. And if anyone hates it, he can just make a pull request, and we discuss it, and then." We do it, yeah. So if anybody downloads P and D and thinks the rules are too strict, that's perfectly okay. I'm very strict on code quality, but it's it's highly configurable. Okay, I I need to ask you a question because I'm kind of junior in Salesforce here. Is there any way the IDE that we're using can auto format the Apex code? Yes, there is. I don't want to do advertisement, but I really love Illuminated Cloud. It's the IDE of my choice. Okay. You know this plugin for IntelliJ. Yeah, I know that. Do it. It can do it. It it does auto formatting. Really? Okay. It can do a lot of stuff that PMD does even better. I mean, D and D will tell you that your formatting is wrong after you checked in, and you can configure Illuminated Cloud to do it instantly when you save. And it's the same with like unused code

[17:44] or whatever. So. I think tooling providers that build the IDE can do stuff like PMD much cooler, much fast, much more often. Why should people care about formatting at all? Everybody can write how ugly he wants. Yeah. And before you save or yeah, I I don't like it. I really I'm really. I'm really very serious about how I write code. Even if I just want to try out just some stuff, I formatted right. But but yes, IDEs can do it. Okay, but I think yeah, I know this illuminated cloud. I know it's a paid service, couple hundred bucks a year, right? On top of uh, IntelliJ Lightens. Yeah, IntelliJ is free. Uh, you have to check it out. I think it's it's cheaper for all sorts, but yeah, it's um. Cause it's cool. It's it's a good price, and there are other players out there. Yeah, because you know the we all have PMD and. There is this this great new IDE for young hip developers. I never I never liked those editor-like IDEs because they're IDEs. But Visual Studio Code seems to be a really cool tool. Salesforce decided to to quit this Eclipse business, and I know there's a very cool PMD plugin for it. I know of the Welkin Suite as a very cool. IDE based

[19:14] on top of the real the real Visual Studio and yeah yeah it has of course has a Microsoft supporting on the back and I actually I have done a show. In my another podcast with the core support in in the team, and they seems to have a really good team and really active supporting fixing all the bugs and all the issues over there. But the thing is that the the Salesforce plugging, the official Salesforce plugging in Visual Studio Code, is still kind of catching up. It doesn't have the auto formatting. It doesn't does the syntax highlighting that well. Sometimes it just does it in the wrong way. Um. Yeah, I think there's a lot of work. That's why I I was so happy with Illuminated Cloud because I basically I was basically one of those who who thought the the Force .com IDE would ever catch up with a decent Eclipse Java IDE, and then other players came around and died, and then. And then Salesforce DX came up, and they totally switched gears. No, we don't want IDEs anymore. We we just want want this Emacs style, hip developers style. They they basically from one day to the other, CLI became cool. Yeah, I I'm not working at Salesforce, so I can say this. I hated CLIs for the rest of my life. I I don't memorize commands. Even I I hate black backgrounds. Whatever, I really want shiny

[20:45] IDEs with many buttons where I can use my mouse. I don't want to hack in the CLI, so I I never made I I did they they lost me the minute they they said CLI is cool, but I I know I'm I might might be too old for that. Yeah, but it's it's like a people's choice. I think everybody has a that everybody choose his tool. That's the good good good craftsmen do. To sometimes discuss which hammer is better, but in the end they are building cool cool roofs and right right. Um. Okay, where we are, we have talked about the IDEs, and I think we were in the outer box rules for the PMD Apex, right? The what? The auto box and the the the auto box the by default rules right that that were what we were discussing and then we go off topic. The box always sounds like a sounds like a product. It's. Let me just—I don't want to show it, but I can. Using D is really that simple. You you download the binary package, unpack it. It um. Go to your command line. Put in like PMD. Give them the source of your classes. Say this is for Apex, and then say please output the

[22:15] report to the command line or to to HTML, and then run it. And then it runs for a minute, and then you open a file, and it says, okay, in your whatever in your opportunity controller, you have three problems. Yeah, that's it's that simple, and and those tools basically take this output and show it in a nice fashion. Yeah. To be honest, I have already tried PMD both in my spare time and in an official customer project. Uh, just the custom project. It's already like an ongoing project. By the time we start to try PMD, it has already like a two hundred, three hundred warnings. So, it was kind of ah, what has happened and what we really need to fix. And we we dive into details, and then we realize the things are given, the warnings, the informations are important. Actually, are super like precise, and but we just didn't have time during the kind of project to fix all of them. It's good. Yeah, we all we also get a lot of false positives. So okay. Um. I mean, there there are like regularly every month there are some like many issues in the the GitHub repository of PMD where people say ah this rule doesn't work for

[23:45] whatever for communities or with this with the new switch statement whatever so there are there's still a lot of room for improvement so I'm really happy to hear that you didn't get back. Wrong issues. Yeah, many of them. We we haven't gone through all, but many of them are definitely really providing values already. Yeah, that's good. You don't have to fix them all. It's really that's what I say. Exactly. Um. Yeah, it's like you don't have to switch from not knowing about what it is to perfectionism. You can just leave it as it is, but knowing about what issues you have in existing code. Will make thing harder next time, and so I don't put a lot of pressure when I do reviews to developers. I really, when when somebody commit, we do code reviews, and when people commit to me, I tend to really comment on basically every line. Okay, and then people are really scared to death. Say you don't have to, just fix one. And read all the others, and next time you won't do it. So, it's it's not really. I mean, really, we as developers, we we have to. It's our it's our responsibility to do our job good, but also have to respect that we don't get paid

[25:15] for fixing. The stuff from last year and refactoring won't pay our salaries. So pragmatism is really important in that. Indeed, indeed. One last question I want to talk about the PMD is how easy or how difficult it is to create the custom rules. Very good question. Because this is where I'm a bit disappointed. Okay. I expected I expected many companies to jump on the topic, use it like big ISVs, and then say, oh, those rules are not sufficient, or we need more. And then I I really expected to MVPs and some famous bloggers and developers to jump on it and write it. And I contacted many of them and and. It's it's not that hard. There are two ways how you can build custom rules. There's one way which is super dead simple, and it's XPath. Have you heard about that? Yeah, yeah, that's so on the web. Basically, an XML-based description of a rule, and you don't have to go this this full open source circle. If you the okay, the other part is a Java-based rule. You have to. Basically, go to GitHub, fork the repository, check it out,

[26:46] add your rule, run all the tests, make a pull request, have it accepted by the PMD maintainers. This takes a while. This takes a while. And this might really scare the shit out of people, but actually it's easy. But um, yeah, but but XPath routes are easy because you don't have to do anything. You you can just do it in your local configuration. For example, my plan is to to show in this in my my second Dreamforce talk. I hope to to get it in how to write a simple. I always see system debug statements in developer code when they commit stuff, and there's no rule that will warn you that you still have a, you know, system debug statements are really time-consuming. If there's a developer console watching it, it will really slow down your code, and so. You should remove it, and I will show that you need like a five-line XPath rule to to get rid of them, and then some smart developers in the audience might. Say yeah, but this is not true. I need sometimes to leave them in because they are there to debug problems, and then I will basically say, okay, then you need a Java rule and show how to make it a little bit more complicated in Java, but it's.

[28:11] I mean, everybody who's listening to us and wants to know it can just go to GitHub, look into the PMD rules, and look how long they are. And I could never have written this framework on my own. I was really just copying over Java stuff and trying to to make sense out of it. But the most rules are super easy, like to suckle in. In in loops, it's really a method which says I'm a PMD rule, and then you you have a method which basically stops when it finds a loop. And then check all the children. Is there a circle? And then you say add issue. So it's a little bit more of concepts, but yeah, I would say it's too much for junior developers. But but all the architects and CTOs out there that have top notch. Apps out there and make millions on the Salesforce platform. They it should be easy for them to contribute rules. So okay, yeah, I also really wish people. And I would I would I would do some some some training. I would yeah I think we need code with with important people if they could just write some PMD rules. I found this part kind of interesting because. When by the time I joined Salesforce for one month, I already started thinking. Okay, from the other mature

[29:41] local compiling languages like C sharp, so those things already have a really good static code analyzer, and and here we have the PMD. Why people are not so Interested in using it, and then I searched by Google, and I found there's a PMD, and I had this Apex contribution that you made. That's the I mean, it's not that popular. People are not. I don't know. Maybe you know more than. Yeah, I have my. I have my theory. Okay, theory, and my theory goes that way. Ten years ago or so, Salesforce basically marketed that they had, which is no software logo. Can you remember that? Right, right. The logo was the word software, which was marked out, so like a sign, no software.

[30:38] So software and writing code—that's what what the CEOs and CTOs hated out there. They knew this this developer guys are complicated and and it will cost millions and we we will be dependent for the next years. So Salesforce is not. Like an engineering company, they are not like Google. They they are not super attractive for developers. If you when you look out to all the marketing material and what MVPs say, they they basically tell you you don't need to code, you can just be an awesome admin and do some clicks in the process builder, and then you have some. Business value and um, they need. I mean, this is really just my theory. I hope any Salesforce person out there don't hate me for that. But um, if if you have a platform like Salesforce, you want people to come in. You don't want people to be afraid of your platform because they say it's complex or I can't do. And and they they are really good at showing people that were teachers and now they are trailblazers. Um, if you have a platform and you do this marketing thing, you don't tell people that coding is hard. That you read have to read like ten meters of books and learn about efficiency and about static code analysis. It's it's unsexy. It's it's it's not something. I really one time in in in the last four year, I contacted the Trailhead people and said.

[32:08] Shouldn't we do a clean code stuff on Trailhead and I don't know. That didn't answer. Trying into a desert. Yeah, it's it's it's different. It's like yeah. That's interesting. I mean, by the time we publish our episode. We should have seen another episode I have done with Sarah from actually from Trailhead team. She was really experienced and she oh the the inventor the no no she she's like um joining in Salesforce for a couple months only. Okay. Yeah, she but she's kind of experienced. She was in Microsoft stack and she has written a lot of technical books and she has done a lot of self kind of self-employed projects as well. And from her word, I understand the Trailhead platform has expanded to different topics. For example, the the new JavaScript feature, the ES two thousand fifteen, those stuff. I was catching up in the platform, so I think clean code at least. Yeah, you could cover it, but then you would have to decide who who should do it. Absolutely, they talk about agile. They talk about gender bias, and it's a cool platform. But it it it gets it it really gets quite advanced soon. You know, and and you always have to fight this

[33:39] mentality of getting things done fast. I mean, all the blocks and trailheads are about that you can basically change the world in in a minute. Yeah, some AI, but. They don't tell us that AI is super hard. Yeah, that's that's something I also hate. Yeah, basically starved to death. And no, it's about change the world. And and and as as a yeah as a German engineer, I just say sometimes reality doesn't fit what marketing needs. And I would say PMD and clean code and static code analysis. Falls into this unsexy bucket. Yeah, I understand. Well, yeah, well, that's why we have this Salesforce Way podcast. We try to advocate the developer related topics, even if Salesforce doesn't like it. They like it. They like it. All right. I think that's all we want to discuss for today. So before we are closing this session, do you, Robert, still have something else you want to say? No, thanks for invitation. I really, I was a pleasure talking to you. I liked the question. I, I hope there are many people interested in it. And I, I just can, not restate. I didn't state it. Yeah, people should get be free. Feel free to get in contact with me on Twitter, wherever you will find me on Google. And

[35:09] if you are interested in one of those topics, I'm. I'm not a coach. I'm not an. I I cannot consult your company, but I I will sure help you as a single person to to get in this into this area and um yeah yeah the more people. Think about this. The more people do it, the more we will all benefit. So, yeah. Thanks again, thanks Robert for coming to the show. I will add all those information, Twitter information, PMD, the GitHub repository, the URLs to our show notes, so our listeners could could find it out. Okay. Nice evening. Thanks, Robert. Talk to you later then. Bye. Bye

[35:52] bye. Hey, thanks for listening to the end of the show. I really appreciate you spent your time with me, and I'd love to connect to you, my listeners. If you have any advice, idea, opinions, or simply a greetings to each other, my Twitter, LinkedIn, email information can be all found at the website SalesforceWay.com. Oh, by the way, it occurs to me some of you are not yet subscribed. If you're one of those people who are not subscribed to the show, go to SalesforceWay.com, hit the RSS feed or the link to iTunes to subscribe, so you won't miss any of our shows in the future. Okay, so with all this out of the way, I'd say having a great day and see you the next time.

Leave a Reply