神刀安全网

Not Yet Implemented

18 June 2016

When writing Rust code, I often stub out methods (or parts of them) that I intend to implement later with unimplemented!() . As I work primarily in Java, I recently thought about what keeps me from doing the same in Java. My IDE of choice is Eclipse (don’t argue, the choice was actually made by my boss, though I am completely OK with it), which allows me to create new methods that contain the following (say, for a method returning a String ):

String myAwesomeMethod() {     //TODO Auto-generated method stub     return null; } 

The nice thing about it is that it compiles. The not-so-nice thing is that if I forget to implement the method, but use it somewhere, the returned null may or may not alert me to the fact that I just ran unimplemented code. The NullPointerException I will inevitably get may or may not be near the unimplemented method. At least I get a stack trace.

Unless I create a method that returns a primitive type, say int :

int myEvenMoreAwesomeMethdo() {     //TODO Auto-generated method stub     return 0; } 

Again, this will happily compile, but unlike the null the returned zero may not even be distinguishable from a valid value (unless whatever code calls this function explicitly rules out zero, which is rare).

Much worse than unimplemented!() , don’t you think? Luckily, there’s a way to emulate this one. Click on WindowPreferences , expand JavaCode StyleCode Templates , in the upper area select CodeMethod body and click the Edit... button.

Then change the pattern to the following:

// ${todo} Auto-generated method stub throw new Error("not yet implemented"); 

Now my unimplemented methods will automatically throw an Error that alerts me to the actual position I have to step in and code. Much better!

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Not Yet Implemented

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址