Writing equality tests in an if statement - if-statement

I wanted to see if someone could shed some light on this for me. When I was using the Checkstyle plugin for Eclipse, I got a warning message on the following statement (not exactly this, but this form):
if (x != y)
{
do();
}
Why is this a problem?
Edit: I apologize, I should have been more clear with my question. Thanks very much for your answers; they actually helped me with a completely different problem.
The warning that I got recommended that I not use a test of the form "x != y", but instead use a test of the form "x = y". I'm wondering why one would be better than the other.

The default setting for the LeftCurly checker is "eol". This means that you are enforcing the rule that if statements (and similar blocks) should look like this:
if (x != y) {
do();
}
If you prefer the style you showed in the question, use the "nl" setting, described in the lcurly policy section of the Checkstyle manual.
You can modify the setting in Eclipse like this:
From Window, choose Preferences.
If needed, create a new check configuration (usually by copying one of the unmodifiable originals) and make it the default.
Double-click the new configuration, click Blocks, then double-click Left Curly Brace Placement. Modify and save.

See the Checkstyle's check description + rationale: Standard Checks

Is there a "else"?
I think that if you wrote something like that
if (x != y)
{
do();
}
else
{
doSomethingElse();
}
I'd recomend too
if (x == y)
{
doSomethingElse();
}
else
{
do();
}

Related

When i extract a method in eclipse it changes "and" and "or" to "&&" and "||". Is this changeable?

I strictly use "and", "or" and "xor" instead of "&&", "||" and "^". But when i then use the extract method, functionality in eclipse it doesn't use "and" etc. regardless of it being used in the original method.
void MethodName() {
if (foo and bar) {
// Do stuff
}
}
Turns into:
void NewMethodName() {
if (foo && bar) {
// Do stuff
}
}
I have looked in every c/c++ setting in eclipse and not a single one i have found has had anything relating to this, and when looking online i haven't found anyone else with this problem.
Settings i've looked into:
Code Style
Formatter
Appearance
Autotools
Editor
Code Analysis
Template Default Values
As well as any underlying settings in the before-mentioned ones
Does anyone know of a setting or anything to fix this?

How to type a close brace `}` when clion doesn't understand your code and reformats it wrong?

I have some code that compiles fine but I type the closing brace } for the else, it moves all the code from the else { all the way to the left and throws away all indentation.
if (some_condition) {
some_real_code();
} else {
obj.some(stuff);
obj(some,other(stuff));
and when I type the final } I get:
if (some_condition) {
some_real_code();
} else {
obj.
some(stuff);
obj(
some,
other(stuff));
}
The only way I've found to deal with this when it happens is to select a brace in my code, copy it to my clipboard, then do a right-click "paste simple" in clion, which doesn't do any reformatting.
Is there any better way? For example, an a phone, if it autocorrects you and you delete the autocorrected word and retype the same word again, it won't re-autocorrect you because it figures you actually knew what you meant when you do it the second time.
Thank you.
edit: I'm not saying clion is bad or wrong for not understanding my code because in my real code I use language features that it doesn't claim to have support for. I'm just looking for how to work around it's rather aggressive lack of support.
Please, switch off "Reformat block on typing '}'":
Seems that you would be interested in for-IDE-stub implementation in guarded block (Per-ide variable: in CLion it’s CLION_IDE , in AppCode – APPCODE_IDE , in Android Studio – STUDIO_IDE)
I would not turn autoformatting off, because in the majority of cases it is useful. But when this undesired autoformatting happens, I just do the following workaround:
Cancel the autoformatting (Ctrl+Z). The curly bracket is cancelled too.
Instead of typing bare }, I type it commented: //}.
Then just uncomment this line (Ctrl+/ or remove the slashes).
Profit! :)

SLOC in cppcheck

I want to write checker that can be added to other checkers in CppCheck. This checker must check SLOC of all member function, for example the function should contain no more than 200 significant lines of code. But in CppCheck I only found method that checks the existence of a body hasBody(), but not a count of lines.
I am a cppcheck developer. I am no expert in this topic. I think it depends on exactly what you want to count. how many lines is this:
void f() { int x=3; int y=x+2; dostuff(x+y+4); }
I would guess that you want to go through the tokens and count semicolons or something:
for (tok = functionScope->classStart; tok != functionScope->classEnd; tok = tok->next()) {
if (tok->str() == ";")
++lines;
}
I think this checker you suggest is interesting but it does not fit well in the core cppcheck tool. I would suggest that you write an addon. I will be happy to add it in our addons folder and show it in the GUI etc.
By the way.. I have thought that it would be nice to integrate (execute and read results) ohcount, cccc, or whatever in the GUI so extended statistics can be shown.

Bukkit - Referring to a custom item in an IF statement doesn't work

Apologies for terrible use of words but I'm not into the lingo yet.
I've been creating a spell book for a simple bukkit plugin which opens into an inventory on the right click of a certain custom item that is crafted. Here is the code
#EventHandler
public void onBookInteract(PlayerInteractEvent e){
if(e.getAction() == Action.RIGHT_CLICK_AIR){
if(e.getItem() == SpellBook){
openGUI(e.getPlayer());
When I try to do this ingame nothing happens. I've removed if(e.getItem() == SpellBook){ and it works as well as if I change the statement to:
if(e.getMaterial() == Material.BLAZE_POWDER){
It works as well. Probably a simple error but I only started coding a couple of days ago. Thanks for any and all helpful feedback ^_^
There are a couple things that could be wrong.
1) Make sure that the class that this is in implements Listener, and that in your Main class, (the one that extends JavaPlugin) in the onEnable() method, you have:
this.getServer().getPluginManager().registerEvents(new <class that implements Listener>(), this);
so if the class that your code with #EventHandler is in is called Handler, then you would use:
this.getServer().getPluginManager().registerEvents(new Handler(), this);
2) Try using .equals() instead of ==:
if(e.getAction().equals(Action.RIGHT_CLICK_AIR)){
if(e.getItem().equals(SpellBook)){
3) Make sure that SpellBook is actually an ItemStack. If it is, then you may want to try doing this if it has no ItemMeta (display name, lore, etc.)
if(e.getItem().getType().equals(SpellBook.getType())){
Otherwise, if it does have ItemMeta, you could use this:
if(e.getItem().getType().equals(SpellBook.getType()) && e.getItem().hasItemMeta()){
if(e.getItem().getItemMeta().getDisplayName().equals(SpellBook.getItemMeta().getDisplayName(){
if(e.getItem().getItemMeta().getLore().equals(SpellBook.getItemMeta().getLore(){
So, your final code should probably look something like this:
#EventHandler
public void onBookInteract(PlayerInteractEvent e){
if(e.getAction().equals(Action.RIGHT_CLICK_AIR)){
if(e.getItem().getType().equals(SpellBook.getType()) && e.getItem().hasItemMeta()){
if(e.getItem().getItemMeta().getDisplayName().equals(SpellBook.getItemMeta().getDisplayName()){
if(e.getItem().getItemMeta().getLore().equals(SpellBook.getItemMeta().getLore()){
openGUI(e.getPlayer());
}
}
}
}
}
If SpellBook is a type (i.e. a Java class) then that's your problem, e.getItem() returns an instance of a class. Again, if SpellBook is a type (I can't tell with the brief code you gave), then try using e.getItem() instanceof SpellBook instead. Sorry if I'm way off.

omnicppcomplete not supporting all forms of const

I have omnicppcomplete working fine except once in a while it won't complete some of the variables methods/members. I finally got annoyed enough to dig into why and I believe the reason is that omnicppcomplete does support the syntax "Foo const & foo" in function arguments.
For example, if I have a function defined as:
int foo( Bar const & b ){
}
I won't be able to get completion information when I later type "b.". However if I change the signature to:
int foo( const Bar & b ){
}
I will be able to get completion information when I type "b.". It seems to only be in function argument lists because I tried simply defining a variable within the function with the signature "Bar const & bref" and I was able to get completion information for bref.
I would be surprised if this is an actual limitation of omnicppcomplete; anyone have any thoughts on whether or not this is a bug and/or if there is a workaround for it? Changing the coding style does not seem like a reasonable solution.
Seems like a limitation in omnicppcomplete, but I pulled up the vim debugger and found it.
Open up autoload/omni/cpp/utils.vim, go to line 518, should look like this:
for token in tokens
if state==0
if token.value=='>'
let parenGroup = token.group
let state=1
elseif token.kind == 'cppWord'
let szResult = token.value.szResult
let state=2
elseif index(['*', '&'], token.value)<0 "This is line 518
break
endif
And change that line to:
elseif token.value != 'const' && index(['*', '&'], token.value)<0
Or, here's the vim commands to do it =):
/index(\['\*', '&'],<CR>itoken.value != 'const' &&<ESC>:w
I'll try submitting this to the maintainer of omnicppcomplete, but it's kind of hackish, dunno if it'll get in. Might've been able to check if token.kind == 'cppKeyword', but I figured I'd err on the side of changing the least.
Having experienced issues with omnicppcomplete, I searched for an alternative and found clang complete which uses clang's metadata output (that is intended for such purposes). I works extremely well and provided your code compiles, it will understand everything.