fiveworlds Posted October 19, 2016 Posted October 19, 2016 Looking for a function which takes as input java code and outputs same code with all variable names formatted as camel case.
John Cuthber Posted October 19, 2016 Posted October 19, 2016 Good luck. How will it know if a variable is TheRapistsPhonenumber or TherapistsPhonenumber ? 1
fiveworlds Posted October 19, 2016 Author Posted October 19, 2016 Perhaps I don't use the word rapist/therapist that often I could put it in a set of excluded words. Alternatively I can get the program to allow me to select an appropriate camel case variable name from a list of possible variable names. Really I'm looking for a premade function downloading the oxford english dictionary would take a while.
John Cuthber Posted October 20, 2016 Posted October 20, 2016 Perhaps I don't use the word rapist/therapist that often I could put it in a set of excluded words. Alternatively I can get the program to allow me to select an appropriate camel case variable name from a list of possible variable names. Really I'm looking for a premade function downloading the oxford english dictionary would take a while. Downloading the dictionary wouldn't help. "The", "rapist", and "therapist" are all in the dictionary. Using a word processor's S+R function might be the easiest way.
ydoaPs Posted October 20, 2016 Posted October 20, 2016 Good luck. How will it know if a variable is TheRapistsPhonenumber or TherapistsPhonenumber ? Actually, you could probably do some topic modelling and pull up some probability distributions based on the other words/phrases in the code. Alternatively, you could have your function not only take a source code as input but also a word bank. Now, if you're dealing with therapists of rapists, you'll still have that issue, but not with anywhere near the frequency.
Sensei Posted October 20, 2016 Posted October 20, 2016 (edited) You need a ready made script to convert strings to camelcase It would not be easy to write such tool. He wanted to process not strings but variables. It would require entire C/C++/Java language parser, which will identify whether string is variable. Leave alone functions, methods and everything what is not variable. Built-in types would be the easiest to identify, but what if variable is class object? It requires making class database. Either declared in project that's processed, and the all external standard and included headers. In C++ also support for namespaces is needed (std::string is not the same as xxx::string). Probably weeks needed to write it, without damaging source code, in the all possible cases.. Edited October 20, 2016 by Sensei
Strange Posted October 20, 2016 Posted October 20, 2016 It would not be easy to write such tool. He wanted to process not strings but variables. It would require entire C/C++/Java language parser, which will identify whether string is variable. Leave alone functions, methods and everything what is not variable. Built-in types would be the easiest to identify, but what if variable is class object? It requires making class database. Either declared in project that's processed, and the all external standard and included headers. In C++ also support for namespaces is needed (std::string is not the same as xxx::string). Probably weeks needed to write it, without damaging source code, in the all possible cases.. Wrong thread. But most (all?) modern IDEs include refactoring tools that take care of all of this. For Java, I would recommend Netbeans (or Eclipse if you have already climbed the mountainous learning curve).
ydoaPs Posted October 20, 2016 Posted October 20, 2016 It would not be easy to write such tool. He wanted to process not strings but variables. It would require entire C/C++/Java language parser, which will identify whether string is variable. The variables would be strings if it is taking the program as an input in a mention modality rather than a use modality. The camelcase function wouldn't be trying to run the code, so it would want to parse the source file as a text file rather than code to interpret/compile. It doesn't require an entire language parser either. Since Java is a strongly typed language, you can use the datatypes as flags to let you know the part of the string that is a variable name. Then it's a matter of converting the variable name string into camelcase, find/replacing it, then either saving the original file or making a new one. It's a relatively easy project, especially for someone who is supposedly advancing machine vision. Though, this is really off topic, and another mod should merge the tangent with the other thread.
John Cuthber Posted October 20, 2016 Posted October 20, 2016 Never forget the penismightierthanthesword. It's nice when they are out to get her out together. I will stop before I drive you all to MansLaughter There are lots of these ambiguities. The real solution is to use CamelCase when you type the things in the first place.
Strange Posted October 20, 2016 Posted October 20, 2016 He might want to convert from pen_is to PenIs ...
Sensei Posted October 20, 2016 Posted October 20, 2016 (edited) The variables would be strings if it is taking the program as an input in a mention modality rather than a use modality. The camelcase function wouldn't be trying to run the code, so it would want to parse the source file as a text file rather than code to interpret/compile. It doesn't require an entire language parser either. Since Java is a strongly typed language, you can use the datatypes as flags to let you know the part of the string that is a variable name. Then it's a matter of converting the variable name string into camelcase, find/replacing it, then either saving the original file or making a new one. C'mon, ydoaPs! Fiveworlds wanted function which is taking as argument JUST path to source file, without any variable names. Variable names he wanted to be identified by parser, automatically! Suppose so you have 100+ variables, not camelCase. Your way it requires 100 entering each variable name as argument, original name, and renamed. Then what is difference from regular Refactor > Refactor This.. (Rename) ? It doesn't require an entire language parser either. Absolutely nonsense. Suppose so you have tool which is taking path to .java file, variable name prior, and after rename, as arguments. Now we have code which has variable name as string in quotes for example. "[blahblah]" It cannot be processed by parser. It's not variable name. Suppose so we have it as method of some external class object method: Object.BlahBlah() you cannot change it to Object.blahBlah() (camelCase version of BlahBlah) because it was defined in these external headers as Object.BlahBlah() after changing name, code won't be compile-able anymore! So parser has to find "Object", check it has "BlahBlah" method, and ignore rename.. Here is example mine function in Java (Android Studio project for Android v4+ OS), I made couple days ago: AlertDialog.Builder builder = new AlertDialog.Builder( this ); builder.setTitle( R.string.quit_title ); builder.setMessage(R.string.quit_message); builder.setPositiveButton(R.string.quit_yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // finishAndRemoveTask() truly removed application! But v21+ required! if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { finishAndRemoveTask(); } else { // TODO: this application quitting does not want to work on Android <v21 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { finishAffinity(); } System.exit(0); } } }); builder.setNegativeButton(R.string.quit_no, null); builder.show(); I clicked on "builder" (that's true variable name, and the only variable in above source code),refactored it to "BUILDER", and it still compiled without any single problem. If I would run code which is simply searching for "builder" (case-insensitive), it would find also AlertDialog.Builder as matching string, after changing it to AlertDialog.builder or alertDialog.builder it would not compile anymore... There is no alertDialog class, there is no builder() method in AlertDialog, nor builder() in alertDialog either... Android's classes, methods, functions: AlertDialog.Builder, DialogInterface.OnClickListener do not obey camelCase.. It's a relatively easy project, especially for someone who is supposedly advancing machine vision. Easy to write, does not automatically mean, fast to write in minutes, or hours, to handle the all cases possible. Though, this is really off topic, and another mod should merge the tangent with the other thread. You started attacking fiveworlds in this thread, not me.. I agree completely with your second example, about factorial, but first example, was miss. Edited October 20, 2016 by Sensei
ydoaPs Posted October 20, 2016 Posted October 20, 2016 C'mon, ydoaPs! Fiveworlds wanted function which is taking as argument JUST path to source file, without any variable names. Variable names he wanted to be identified by parser, automatically! Suppose so you have 100+ variables, not camelCase. Your way it requires 100 entering each variable name as argument, original name, and renamed. Then what is difference from regular Refactor > Refactor This.. (Rename) ? No, my way just requires the program to be able to flag the strings for Java data types (e.g. "float"). It uses that to pick out the next string (separated by whitespace, of course) and then camelcase that (and the string following it, should it end in a comma). Once the variable names are identified in the declarations, they can be find/replaced no problem with a camelcase version. Code that can recognize datatypes and references to other files need not be a complete language parser.
John Cuthber Posted October 20, 2016 Posted October 20, 2016 (edited) He might want to convert from pen_is to PenIs ... One reasonable option would be to use search and replace in just about any text editor. Let's face it, humans sometimes struggle to parse these things- that's why we use camel case. Anyone from South Yorkshire will probably read alongpenistoneroad differently from most other folk. Expecting a machine to do it is unreasonable. Also, beware of this http://xkcd.com/974/ Edited October 20, 2016 by John Cuthber
Sensei Posted October 20, 2016 Posted October 20, 2016 (edited) No, my way just requires the program to be able to flag the strings for Java data types (e.g. "float"). That's what I said "Built-in types would be the easiest to identify," #7 post.. But if float is put as member of other object class RGBA { float mRed; float mGreen; float mBlue; float mAlpha; public RGBA( float r, float g, float b, float a ) { mRed = r; mGreen = g; mBlue = b; mAlpha = a; } }; And then used: RGBA colorred = new RGBA( 1.0, 0.0, 0.0, 0.0 ); We have no idea what is "RGBA", therefor there is need to search for it. Analyze its every member. Code that can recognize datatypes and references to other files need not be a complete language parser. But it's very close to it. In complex code you would have to do it recursively.. Edited October 20, 2016 by Sensei
Strange Posted October 20, 2016 Posted October 20, 2016 One reasonable option would be to use search and replace in just about any text editor. See post #4
fiveworlds Posted October 20, 2016 Author Posted October 20, 2016 It would not be easy to write such tool. He wanted to process not strings but variables. It would require entire C/C++/Java language parser, which will identify whether string is variable. Leave alone functions, methods and everything what is not variable. Built-in types would be the easiest to identify, but what if variable is class object? It requires making class database. Either declared in project that's processed, and the all external standard and included headers. In C++ also support for namespaces is needed (std::string is not the same as xxx::string). Probably weeks needed to write it, without damaging source code, in the all possible cases.. That's the idea eclipse already has a built in parser to allow for variable refactoring so it is possible to parse out all the variables. What I am looking for is just a script that will refactor the variable names to camelcase for me. Say my boss wants all my variables to be in camelcase and all constant variables to be capitalized. Sure it is easy enough for me to type them like that but most of the time I could be half asleep and just forget. Therefore I want to make a parser which will refactor my variables to camelcase or underscore case or whatever naming convention I have to follow. That way programmers can write variables in whatever form they like and the variables are just parsed to camelcase or whatever before being sent to the boss. Say for instance I like writing if statements like if(condition){ //do some stuff } But the company wants if statements with only one line without the brackets like. if (condition) sysout("10"); It is annoying to remember to omit the brackets all the time but if there was a parser to do it for me then I would never have to think about it.
Strange Posted October 20, 2016 Posted October 20, 2016 Say my boss wants all my variables to be in camelcase and all constant variables to be capitalized. Sure it is easy enough for me to type them like that but most of the time I could be half asleep and just forget. Then you don't deserve your job. Let someone competent have it. 1
John Cuthber Posted October 20, 2016 Posted October 20, 2016 See post #4 Well it would work... If the only tool you have is a hammer, you see all problems as nails. I suspect that the OP has a text editor of some sort which he knows how to use. If he's running on a minimalist Linux box without any sort of word processor then I guess SED's the way to go. Perhaps we should set up some sort of bench-marking challenge to find out which approach is best for a variety of input data and then see what type of data he's using, then we could design a system based on optimising some combination of the characteristics whichever options work best. Did you like the XKCD cartoon?
fiveworlds Posted October 20, 2016 Author Posted October 20, 2016 Then you don't deserve your job. Let someone competent have it. Yes because you don't sleep haha. The easiest way would be probably be a mod to the text editor to just throw an error if I forgot a capital letter or an underscore.
John Cuthber Posted October 20, 2016 Posted October 20, 2016 Then you don't deserve your job. Let someone competent have it. Actually, I think what he needs is a boss who recognises what's important. stringwithclientsnameinit doesn't really need camel case AlongPenistoneRoad does.
Strange Posted October 20, 2016 Posted October 20, 2016 Well it would work... If the only tool you have is a hammer, you see all problems as nails. I suspect that the OP has a text editor of some sort which he knows how to use. If he's running on a minimalist Linux box without any sort of word processor then I guess SED's the way to go. I think that to process a batch of files, sed is still the easiest way. None of the editors I commonly use make it quite as easy. Did you like the XKCD cartoon? Of course. Reminded me of Larry Wall's three great virtues of a programmer: laziness, impatience and hubris. Actually, I think what he needs is a boss who recognises what's important. stringwithclientsnameinit doesn't really need camel case AlongPenistoneRoad does. I am of the view that coding standards are important. When I was one of the people running a small design team with a bunch of new graduates, we would go in and review their work after hours. If they had abused the coding standards too much we would just delete all their work and explain why the next day. (Sounds unbelievable, when I say it now. But it worked!) This was in hardware design where standards are much, much more important (because there are millions of pounds of manufacturing costs at stake). People who do software are just much sloppier - I think this is why there are always so many bugs in software. The "we can fix it later" ability and attitude breeds contempt for specifications, standards and testing. If they had to get it right first time, then I suspect things would be a bit different.
Sensei Posted October 21, 2016 Posted October 21, 2016 (edited) People who do software are just much sloppier - I think this is why there are always so many bugs in software. The "we can fix it later" ability and attitude breeds contempt for specifications, standards and testing. If they had to get it right first time, then I suspect things would be a bit different. You are missing one key important thing: paid upgrades. If thing is done absolutely perfect since the beginning, there is no room for improvement, so no 2nd, nor 3rd time, paid upgrade. Actually hardware manufacturers do the same. CPU makers have 2 generations higher technology available, but push older architecture to retail customers, sell it for astonishing prices, fill market, and then 2-4 years later they release what they had at hand couple years ago. When they have at hand completely new architecture.. And delay release of it, to not cut branch they're sitting on. Edited October 21, 2016 by Sensei
Strange Posted October 21, 2016 Posted October 21, 2016 You are missing one key important thing: paid upgrades. If thing is done absolutely perfect since the beginning, there is no room for improvement, so no 2nd, nor 3rd time, paid upgrade. I doubt that is the reason. I have never worked for a company that thought they could charge people for bug-fixed versions of a product. Paid upgrades should be (and are) based on added functionality, improved performance, etc. Actually hardware manufacturers do the same. CPU makers have 2 generations higher technology available, but push older architecture to retail customers, sell it for astonishing prices, fill market, and then 2-4 years later they release what they had at hand couple years ago. When they have at hand completely new architecture.. And delay release of it, to not cut branch they're sitting on. Having spent most of my career designing microprocessors for some of the largest semiconductor companies, I can assure you that is not true either.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now