There was a book I read before, about how to code in a generic way. People always regards themselves as a Java programmer, c# programmer or etc but at the end of the day, we are all just programmers.
I believe in atom programming. This told us another way of thinking, to know how the code is composed but not how it’s written. To understand the logic itself but not trap ourselves in the endless debugging caused by syntax issues.
I will write the same code in different languages and explain what the atom programming means in the day to day work.
1 | // take converting the cases of characters in a string as an example |
Pascal:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15program exString;
var
str: packed array [1..10] of char;
i: integer;
begin
writeln('Input the string');
readln(str);
for i := 0 to 10 do:
begin
str[i] = (str[i] >= 'A' ? str[i] <<= 1 : (str[i] >= 'a' ? str[i] >>=1 : str[i]));
end;
writeln(str);
end.
C++:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using namespace std;
int ChangeCase(char s[]){
int i=0;
char c;
while (s[i]){
//Change Case (Similar as it's in Pascal)
i++;
}
return 0;
}
int main(){
char s[10];
cout<<"Input the string"<<endl;
cin>>s[];
ChangeCase(s[]);
cout<<s<<endl;
return 0;
}
Java:1
2
3
4// Very Simple
// Use String.substring(a,b).toUpperCase()
// Or .toLowerCase()
// Then concatenate them together
And here is why it should be done in these ways:
Pascal / C are the typical procedural languages with have great ability of memory manipulation. Using the bit operators can efficiently change the case of a character as it’s defined in ASCII.
C++ have reserved some procedural programming function and has an object oriented feature. This gives it the ability to read the memory and to use the I/O stream in the same time. C++ has another great feature - operator override, it can reduce the amount of code by some customized overriding functions.
String and other complex types in Java are dealt as an object. And the String object in Java, unlike the equivalent class in C++, are encapsulated so completely that it provides a lot of native function to call including case converting.
One word in short
It’s not a question that most languages can have the same work done. To think in a logical and structural way, then pick up a most suitable language can relieve a lot of pain afterward.