Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Code is executed second time with the wrong parameter
Message
 
 
To
24/07/2015 05:09:21
General information
Forum:
Javascript
Category:
Other
Miscellaneous
Thread ID:
01622436
Message ID:
01622460
Views:
41
Good points, Thomas. I'll re-factor.

>As you found the solution to the original problem:
>I realize that fwks sometimes generate part of the code or at least form basic templates the code is expected to follow (so do not take this personal), but the parts left standing at the bottom have so much redundancy in them, it is no wonder more time than expected for covered functionality is spent debugging.
>
>Refactoring minimally (all from the hip)
>
>without changing Routing-convention
>
>  [Route("")]
>        public ActionResult Index()
>        {            
>           =  CleanIDs();
>            return View();
>        }
>
>        [Route("Department/{departmentId}")]
>        public ActionResult DepartmentById(int departmentId)
>        {
>           =  CleanIDs();
>            ViewBag.departmentId = departmentId;
>            return View("Index");
>        }
>
>        [Route("Category/{categoryId}")]
>        public ActionResult CategoryById(int categoryId)
>        {
>           =  CleanIDs();
>            ViewBag.categoryId = categoryId;
>            return View("Index");
>        }
>
>        [Route("Item/{itemId}")]
>        public ActionResult ItemById(int itemId)
>        {
>           =  CleanIDs();
>            ViewBag.itemId = itemId;
>            return View("Index");
>        }
>
>        [Route("SubCategory/{subCategoryId}")]
>        public ActionResult SubCategoryById(int subCategoryId)
>        {
>           =  CleanIDs();
>            ViewBag.subCategoryId = subCategoryId;
>            return View("Index");
>        }
>        public CleanIDs()
>        {            
>            ViewBag.departmentId = 0;
>            ViewBag.categoryId = 0;
>            ViewBag.itemId = 0;
>            ViewBag.subCategoryId = 0;
>            return ;
>        }
>
>results in minimally more CPU instructions (calling refactored func + setting 1 property to 0 needlessly), but is much easier to optimize for JIT. LOC is not that much less, but time figuring out the relevant differences is much lower.
>
>Switching over to Dict-like access to ViewBag (speaking in C# or Python concepts) results in even less programmer code even when keeping previous routing calls
>
>
>  [Route("")]
>        public ActionResult Index()
>        {            
>           =  CleanIDs()
>            return View();
>        }
>
>        [Route("Department/{departmentId}")]
>        public ActionResult DepartmentById(int departmentId)
>        {           
>          return ByID("departmentId", departmentId);
>        }
>
>        [Route("Category/{categoryId}")]
>        public ActionResult CategoryById(int categoryId)
>        {
>           return ByID("categoryId", categoryId);
>        }
>
>        [Route("Item/{itemId}")]
>        public ActionResult ItemById(int itemId)
>        {
>           return ByID("itemId", itemId);
>        }
>
>        [Route("SubCategory/{subCategoryId}")]
>        public ActionResult SubCategoryById(int subCategoryId)
>        {
>           return ByID("subCategoryId", subCategoryId);   
>        }
>
>        public ActionResult ByID(string key, int value)
>        {
>            = CleanIDs()
>            ViewBag[key] = value;
>            return View("Index");
>        }
>
>        public CleanIDs()
>        {            
>            ViewBag["departmentId"] = 0;
>            ViewBag["categoryId"] = 0;
>            ViewBag["itemId"] = 0;
>            ViewBag["subCategoryId"] = 0;
>            return ;
>        }
>
>but it is easy to see that most of the individual routing functions can be eliminated if the call is done directly to ByID with the correct parameters.
>
>Identical strings (state example) should be at least kept in variables, fitting here is IMO a constructor function setting the static properties in code via string literals and the url via parameter.
>
>Does your code review not check for a) programming to differences b) following the rule of three?
>
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform