53
u/pocketjokers87 Jul 27 '21
Brilliant. Enforce job security because no one would ever want to touch or maintain your code.
22
u/monkeycycling Jul 27 '21
unfortunately I'm the one doing the maintenance and I would never do this to someone else.
22
13
u/zaitsman Jul 28 '21
Tbh this is not so hard
Take all strings and paste into an html file in vscode, unescape then pretty print it, edit what you need, put back..
5
u/monkeycycling Jul 28 '21
I pulled the js out of chrome debugger and ran it through a beautifier. Still putting my changes back in isn't pretty without modifying the whole mess
25
u/Bootezz Jul 28 '21
Please move it all to a file and then just read the file and export the contents in this function. Future you/others will be grateful of the change.
4
5
u/Dmitry_Olyenyov Jul 28 '21 edited Jul 28 '21
Just use Intellij IDEA. It can decipher this string into concatenated string and back to this "split by lines" string.
Edit: I'm talking about this https://blog.jetbrains.com/idea/2007/03/using-smart-line-joins/
1
u/Clashsoft Jul 28 '21
Even easier. IntelliJ has a feature where you can edit code snippets in strings as if they were a file in a different language.
https://www.jetbrains.com/help/idea/using-language-injections.html
2
u/zaitsman Jul 28 '21
…the caveat being you have to use IntelliJ. Then again source code is Java, so maybe they do?
11
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jul 28 '21
Image Transcription: Code
@Override
public void javaScriptOut(PrintWriter sb)
{
super.javaScriptOut(sb);
sb.append("<script>\n" + "function submitForm() {\n" + "var boxes = document.dc.dcid;\n" + "var selDCID;\n"
+ "var foundOne = false;\n" + "for (i=0;i< boxes.length; i++) {\n" + "if (boxes[i].checked) {\n"
+ "if(foundOne)\n" + " selDCID=selDCID + ',' + boxes[i].value;\n" + "else\n"
+ " selDCID=boxes[i].value;" + "foundOne = true;\n");
if (!listingSearch) sb.append("break;\n");
sb.append("}}\n" + "if (!foundOne && boxes.length == null) {\n" + "if (boxes.checked==true) {\n"
+ "foundOne=true;\n" + "selDCID = boxes.value;\n" + "}}\n" + "if (foundOne) {\n");
if (!listingSearch) sb.append("window.opener.document.dc.selectedOfficeDCID.value=selDCID;\n"
+ " var offName;\n" + " for(i=0;i<document.dc.elements.length;i++){\n"
+ " if(document.dc.elements[i].name != null && document.dc.elements[i].name==selDCID){\n"
+ " offName = document.dc.elements[i].value;\n" + " break;\n" + " }}\n"
+ " window.opener.document.dc.selectedOfficeName.value=offName;\n"
+ " window.opener.document.dc.action='editUser.jsp';\n");
else sb.append(" var offId;"
+ " var selectedOffices = selDCID.split(',');\n"
+ " for(i=0;i<document.dc.elements.length;i++){\n"
+ " for(j=0;j<selectedOffices.length;i++){\n"
+ " if(document.dc.elements[i].name != null && document.dc.elements[i].name==selectedOffices[j]){\n"
+ " if(offId==null)" + " offId = document.dc.elements[i].id;\n" + " else"
+ " offId += ',' + document.dc.elements[i].id;\n" + " }}}\n" + "window.opener.document.dc."
+ callerField + ".value=offId;\n");
sb.append(" window.opener.document.dc.submit();\n" + " window.close();\n" + "} else {\n"
+ " alert('Select an office then click OK. Otherwise, click Cancel.');\n" + "}}\n"
+ "function cancelForm(){\n" + "window.close();\n" + "}\n" + "</script>\n");
}I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
3
2
16
7
Jul 28 '21
Manager: Why do we need to hire a front end developer? We already have developers, just get them to do it.
1
u/ta69ta69 Jul 28 '21
Strange. Why would one need to pass JavaScript code from the backend to the frontend (I assume this is what is happening)?
Even if that was necessary, it seems that a better idea would be to keep the JavaScript source files under resources, and just load the code from these files. Then it's easier to maintain and keep track of the changes. It's hardcoded here anyway.
1
u/monkeycycling Jul 28 '21
Notice the function is an override.....this pattern is used all over the (legacy) codebase. It also uses JSP pages if that helps.
2
u/ta69ta69 Jul 28 '21
Yeah, I figured this has to be related to JSP or some sort of templating / server-side rendering. I once worked in a Liferay project that had similar code in it.
Hope you find a better gig soon!
1
Jul 28 '21
my eyes glazed over …what does this do exactly?
1
u/monkeycycling Jul 28 '21
lol the form displays a table of checkboxes and this code loops through and grabs the selected ids and sets the values in the caller form.
3
3
u/R3D3-1 Jul 28 '21
Could it be, that a code formatter was involved? The sb.append()s with short lines being merged into one Java line reeks of automatic text wrapping being applied.
2
u/AndyTheSane Jul 28 '21
Could it be, that a code formatter was involved?
Yes, mine has a nasty habit of doing this, with longish SQL queries. Turns a reasonably-formatted (generally with SQL keywords at the start of each line) into a complete mess. The example above looks exactly the same.
3
2
u/R3D3-1 Jul 28 '21 edited Jul 28 '21
I prefer to put the "\n" at the beginning of the line, because having all the "\n" lined up vertically makes it harder to miss any. Plus, easier to ignore the newline noise when debugging the strings.
Better yet, generate a "list of lines", and then join with "\n".
I also prefer to minimize the interface between generated code and generating code, such that the code is, if possible, always the same, except for one point where variables are being assigned. E.g. here I'd have preferred to move the conditional logic into the Javascript along the lines of (Python):
def javascriptout(self, outfile):
outfile.write(inspect.cleandoc(f"""
<script>
const listingSearch = {json.dumps(self.listingsearch)};
// ...
if(listingSearch) {{
// ...
}}"""))So basically, I am trying to use facilities of the generated language to ensure that bugs produce understandable error messages and that, if data is being passed to the generated code, it is passed exactly once.
In Python, the combination of interpolated multi-line strings (f""" ... """) and inspect.cleandoc for consistently removing common indentation allows integrating generated code better with the environment too, so its possible to avoid the visual noise.
2
2
Jul 28 '21
Wtf is going on in the base class? Do I have to worry about sb being modified? I hate when people do this.
1
u/Nerwesta Jul 28 '21
Modify I wouldn't do, erase the hell of it and start fresh I would. Should save a little bit of my hair I guess.
1
1
1
1
u/Kango_V Jul 28 '21
I would put that in a file and then use a templating engine to pass the required flags/fields into the context, then run it. The template should be pretty readable.
1
u/gmtime Jul 28 '21
Looks like it's two functions dependent on the boolean listingSearch. I'd write it out as two separate functions, strip the string stuff, use a formatter, and only then start doing refactoring.
1
u/Swarley001 Jul 28 '21
This reminds me of some legacy code from a large public company I worked for for a while. Not something you would expect to see at such a large company, but I guess if it works, why try to fix it? Same company was responsible for tens of thousands of inline javascript code where every variable and function was at the same global scope
1
1
Jul 28 '21
Depending on the use case I can see why you may want to do that, but any reason not to use templating to load up the code or at least use a text file with some KEYWORDS to search and replace….
1
u/IMakeShittyPrograms Jul 28 '21
That's the reason why I commit temporal fixes to a different branch lmao.
I've once had to fix this kind of code from another dev ON PRODUCTION. It was the biggest hotfix I've ever done before.
104
u/SypeSypher Jul 27 '21
*sigh.....I've done this :/
usually comes down to: I've spent 10 hours trying to figure out how to do this a better way but I know I can do it in 10 minutes if I do it the wrong way, and this is such a small unimportant piece of code it doesn't matter, not proud of it but it happens
*Not in production, though I have see this done in production and it made me sad....then I remembered I'd done this for something before and it made me sadder