2
\$\begingroup\$

I am working on a web page where I am creating a JS object using {} and []. This object is passed into a handler

I wanted to see if there is any more way to write this method. Its a lot of code and just wanted to make sure there is no better way to do this?

My logic

Is to build the object is JS and then pass it into the Handler to be Deserialize and then saved into the db.

function saveLongAnswerText() {
 console.log('%c saveLongAnswerText ', 'background: #222; color: #bada55');
 if ($("#form1").valid()) {
 MeetingPollingQuestion = {};
 MeetingPollingQuestion.MeetingPollingId = $("#hfMeetingPollingId").val();
 MeetingPollingQuestion.MeetingPollingQuestionType = "LongAnswerText";
 MeetingPollingQuestion.SequenceOrder = 2;
 MeetingPollingQuestion.MeetingPollingParts = [];
 MeetingPollingParts = {};
 MeetingPollingParts.Type = "Question";
 MeetingPollingParts.MeetingPollingPartsValues = [];
 MeetingPollingPartsValues = {};
 MeetingPollingPartsValues.Type = "Question";
 MeetingPollingPartsValues.QuestionValue = $("#editorLongAnswerText").data("kendoEditor").value();
 MeetingPollingParts.MeetingPollingPartsValues.push(MeetingPollingPartsValues);
 MeetingPollingQuestion.MeetingPollingParts.push(MeetingPollingParts);
 console.log(MeetingPollingQuestion);
 Metronic.blockUI({ boxed: true, message: "Saving Question.." });
 $.ajax({
 type: 'POST',
 url: 'ManagePolling.ashx',
 data: { "PollingQuestion": JSON.stringify(MeetingPollingQuestion), "Action": "SaveQuestion" },
 datatype: "JSON",
 success: function (data) {
 if (data.resultStatus.ResultCode == "1") {
 toastr.success("Saved successfully", "Success");
 }
 if (data.resultStatus.ResultCode == "2")
 toastr.warning(data.resultStatus.Message, "Warning");
 if (data.resultStatus.ResultCode == "3")
 toastr.warning(data.resultStatus.Message, "Error");
 Metronic.unblockUI();
 },
 error: function (data) {
 toastr.error('An error has occured! \n' + data.resultStatus.Message);
 Metronic.unblockUI();
 }
 });
 }
}

Handler

 string jsonData = context.Request.Form["PollingQuestion"];
 var MeetingPollingQuestion = new Model.MeetingPollingQuestion();
 var MeetingPollingParts = new List<Model.MeetingPollingParts>();
 var MeetingPollingPartsValues = new List<Model.MeetingPollingPartsValues>();
 MeetingPollingQuestion.MeetingPollingParts = MeetingPollingParts;
 MeetingPollingQuestion = Deserialize<Model.MeetingPollingQuestion>(jsonData);
asked Oct 3, 2022 at 16:20
\$\endgroup\$
5
  • \$\begingroup\$ Is this correct: MeetingPollingPartsValues.Type = "Question";? \$\endgroup\$ Commented Oct 4, 2022 at 10:48
  • \$\begingroup\$ I think that will be changed at some point. \$\endgroup\$ Commented Oct 4, 2022 at 11:58
  • \$\begingroup\$ From what perspective are you expecting a review? \$\endgroup\$ Commented Oct 5, 2022 at 18:51
  • 1
    \$\begingroup\$ How I could possibly improve the code quality? \$\endgroup\$ Commented Oct 5, 2022 at 19:49
  • 1
    \$\begingroup\$ @Jefferson Thank you for the bounty! :) \$\endgroup\$ Commented Oct 13, 2022 at 18:55

1 Answer 1

1
+50
\$\begingroup\$

Client-side

Prefer fail fast

  • Rather than guarding the whole object creation and the ajax communication logic with a condition if ($("#form1").valid()) please prefer early exit
    • if (!$("#form1").valid()) return;

Prefer object and collection initializer

  • Rather than using value assignment you can use object initializer to avoid repetitive code
const MeetingPollingQuestion = {
 MeetingPollingId: $("#hfMeetingPollingId").val(),
 MeetingPollingQuestionType: "LongAnswerText",
 SequenceOrder: 2,
 MeetingPollingParts: [
 {
 Type: "Question",
 MeetingPollingPartsValues: [
 {
 Type: "Question",
 QuestionValue: $("#editorLongAnswerText").data("kendoEditor").value()
 }
 ]
 }
 ]
};

Try to avoid mixing stringify and object initializer

  • This line seems really odd to me
data: { "PollingQuestion": JSON.stringify(MeetingPollingQuestion), "Action": "SaveQuestion" }
  • I would suggest to use object initializer first and then stringify it
data: JSON.stringify({ PollingQuestion: MeetingPollingQuestion, Action: "SaveQuestion" })

Naming

  • I would suggest to consolidate your naming strategy because even in a simple line you have Pascal and camel Casing as well
data.resultStatus.ResultCode
  • It is minor but please also try to consolidate the usage of ' and "

Please prefer switch

  • Rather than having 3 if branches (.ResultCode == "1" ... .ResultCode == "3") please prefer to use switch
switch (data.resultStatus.ResultCode) {
 case "1": toastr.success("Saved successfully", "Success"); break;
 case "2": toastr.warning(data.resultStatus.Message, "Warning"); break;
 case "3": toastr.warning(data.resultStatus.Message, "Error"); break;
}

Server-side

  • The MeetingPollingPartsValues variable is not used
  • I think the MeetingPollingParts variable is not needed because you overwrite the entire MeetingPollingQuestion object
  • So, the entire method could be simplified for the following line
var meetingPollingQuestion = Deserialize<Model.MeetingPollingQuestion>(context.Request.Form["PollingQuestion"]);
answered Oct 6, 2022 at 8:16
\$\endgroup\$
5
  • \$\begingroup\$ can I do a loop in the object initializer? \$\endgroup\$ Commented Oct 11, 2022 at 17:27
  • \$\begingroup\$ @Jefferson As far as I know, you can not \$\endgroup\$ Commented Oct 11, 2022 at 17:41
  • \$\begingroup\$ I added that Addition object would I not be able to create the const ? \$\endgroup\$ Commented Oct 11, 2022 at 18:02
  • 3
    \$\begingroup\$ @Jefferson Here at code review we don't modify / extend the original question after a review has been placed. We encourage people to create a follow up question and link them. \$\endgroup\$ Commented Oct 11, 2022 at 18:20
  • \$\begingroup\$ I created a new post with that method codereview.stackexchange.com/questions/280364/… \$\endgroup\$ Commented Oct 11, 2022 at 18:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.