I have a method name IsQuotaAvailable. i want to get the DailyAvailableQuota data and show it in the webpage. so how can i fetch this data from this method and send to user interface
- public bool IsQuotaAvailable(Neo oNeo, string request)
- {
- EdgeQuotaCheckResponse oresponse = MakeOAuthPostQuotacheck(oNeo, GetQuotaCheckURI(), request);
- if (oresponse != null)
- {
- foreach (RemainingQuota quota in oresponse.Limits)
- {
- if (quota.limit_type.ToLower() == "daily")
- {
- DailyAvailableQuota = quota.remaining_quota;
- }
- else if (quota.limit_type.ToLower() == "monthly")
- {
- MonthlyAvailableQuota = quota.remaining_quota;
- }
- }
- if ( SettingControl.GetBoolean("bUIMonthlycheck", false) && DailyAvailableQuota > 0 && MonthlyAvailableQuota > 0)
- {
- if (!SettingControl.GetBoolean("bAllowQuotaMailCheck", false))
- {
- CheckAndSendQuotaMail(oNeo);
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- }
Abhijeet JadhavPosted Oct 1, 2019, 9:05 PM
James AxsomPosted Oct 4, 2019, 11:15 AM
Anyone can learn, write code and a computer will understand. Learn, write code and a fellow human programmer understands, now that requires mastery.
Let’s refactor your code and address code smells.
Code Smell: Ambiguous names. For example, Neo. What’s a Neo? The first thing that come to my mind as a person living in North America is the Hollywood movie, The Matrix because of my culture. Neo may mean something completely different to a person living in India, but I doubt it.
Request? What kind of request? A request to what?
Solution: rename a class to exactly what it is, no abbreviation or acronyms using a noun phrase.
Don’t name a class describing an action like “Edge Quota Check Response”, but rather something like, “Edge Quota Response” and Check may be a method in that class.
Rename your variable request somethingRequest where “something” is a abstraction of what the request is. You demystify the abstraction specifying what the request is. What is that something?
Code Smell: Too many parameters. For example Neo and request are parameters for “Is Quota Available”. Neo, a function call “Get Quota Check URI”, and request are parameters for “Make O Auth Post Quota Check”. Reduce your parameters to none or just one parameter.
Solution: Break your method down in new methods (or parts or steps) where each step may only take one parameter or no parameters.
Code Smell: Too much responsibility or work taking place inside the methods, “Is Quota Available” and “Check and Send Quota Mail”.
Solution: Break your method down in to functional steps.
You have operations taking place in the code that raise questions is to what is going on.
DailyAvailableQuota = quota.remaining_quota;
MonthlyAvailableQuota = quota.remaining_quota;
Are these two class fields or properties?
Is there an operation taking place inside them that adds quota.remaining_quota to a sum with each For Loop iteration?
Or is the value of these two being overwritten with each For Loop iteration?
Convert “Daily” and “Monthly” into an enumeration as opposed to using a string literal.
Look at your question goal. I want to get DailyAvailableQuota data and show it in the webpage.
How can you get that inside of function returning a Boolean? The only way I can think of is to raise an event inside Is Quota Available, but raising an event for such a purpose would be impractical.
What do you do?
You create a new function (or new method in a class or a new class with a new method) and return the value of “Daily Available Quota”. Write this code in your middle tier, business logic and present it to the front end or view.
Unfortunately I can’t fix your code, because I’m not on your project looking at the code holistically but I’ll provide a theoretical refactored model of your code to give an idea what your code may look like.
PinkuPosted Oct 3, 2019, 11:46 PM