1

For example, I know in c++, I can use

myString=="abc"

to check if 2 strings are equal. However, in Java, it is comparing if 2 objects are the same object. Also in other language, for example, javascript, I should use myString==="abc" instead.

In my current environment, I may switch between c++ and Java frequently, and I can't choose the language to use (eg:Java for Android, c++ for iOS). I afraid I would write stringA == stringB accidentally when switching from c++ to Java (or other languages).

So my question is, is avoiding misusing "==" in other languages a valid reason to avoid writing

myString=="abc" 

in c++ (ie:only use myString.compare("abc") ==0 )?

I' not dropping unique features of a language, there may some related questions looks similar to my question:

Should we avoid language features that C++ has but Java doesn't?

Why is it bad to write something in language X as if you're writing a program in language Y in terms of using a shared coding paradigm

,but I believe my intent is different in my view : those questions seems try to drop unique features of a language, but I would keep c++ unique features (i.e.:I would use multiple inheritance if necessary), oppositely I'm trying to drop similar syntax but with different effects among other languages.

wcminipgasker2023
  • 721
  • 1
  • 3
  • 10
  • 8
    You have identified a real problem: you might accidentally use `==` to compare strings in Java. I think the appropriate solution for that problem would be to use a linter that warns you if this bug occurs. For example, Findbugs has a rule that checks for this. – amon Jul 28 '23 at 06:22
  • For instance for the == vs === you could use syntax highlighting where you give == in C++ the same color as === in Java and == in Java a distinct different color. That way you can write idiomatic code that your colleagues understand but your ide helps you avoid bugs. – Pieter B Jul 28 '23 at 10:21
  • @amon That should be the answer. Not to try to sound like Yogi Beara but if programming languages weren't different, they would all be the same language. For a professional programmer, being able to keep track of those differences is part of the work. – JimmyJames Jul 28 '23 at 17:04
  • Just wondering: How else would you compare whether two strings are equal in C++? – gnasher729 Jul 29 '23 at 18:20

3 Answers3

11

By the reasoning that similar syntax behaves differently in C++ and Java, you should also forbid the use of new in both your C++ and your Java code. Because you might just call new in C++ and forget the corresponding call to delete.

But if you forbid the use of new on both sides, it becomes effectively impossible to write a Java program of any size. And there are so many subtle differences between the languages that regulating them all means that you just can't really write any programs any more, or at the very least not in an idiomatic way. For example, did you also consider the subtle differences in integer sizes and overflow rules?


When writing a coding guide, don't look at how other languages work but try to codify idiomatic usage and best practices of the language for which you write the coding guide.

Only when it comes to guidelines for the logical structure (use of classes, nested classes, number of classes per file, order of variables, functions, members, etc.) of the code does it make sense to look at other languages used by the teams, to see if there is an advantage in giving the code in different languages a similar structure.

Bart van Ingen Schenau
  • 71,712
  • 20
  • 110
  • 179
2

If you always work alone for yourself, it does not matter - and you definitely don't have to ask strangers from the internet about such stylistic issues.

But if you work in a team, maybe in a team where you expect some staff changes from time to time, better try to write your code in a way your C++ specialists and your Java specialists will not fire tons of WTFs at you at the next code review. And code like myString.compare("abc") ==0 will make a C++ guy think, "WTF, did the author not understand that string comparison in C++ works differently than in Java?".

Even if you don't work in a larger team for now, you are not working in an ivory tower. You will have to read, include or discuss code from other C++ and Java devs (for example, certain open source snippets). Hence, it is best getting used to their "standard" way of doing things. You don't want to isolate yourself by sticking to some convention which will definitely look weird for people trained in either of those lanaguages.

Doc Brown
  • 199,015
  • 33
  • 367
  • 565
0

I'll suggest a Frame Challenge. If you do make that mistake of using "==" in Java, then surely your testing will catch it?

There are many different ways that you can make mistakes while coding that include things like what you are worried about as well as other subtle things (like using the wrong variable etc).

The point being that testing your code is supposed to help identify such mistakes by questioning the assumptions you have made about how your code behaves. If you make a gross error in Java by mixing up string comparisons, then that is the sort of thing that testing should be helping you find.

Peter M
  • 2,029
  • 2
  • 17
  • 22
  • It may be hard to find through testing. Strings with different characters are handled correctly. If strings with the same characters are usually the same object, but not always, then you can have a hard to find error. – gnasher729 Jul 29 '23 at 11:32