readability

Is there an IDE/utility to refactor Python * imports to use standard module.member syntax?

痴心易碎 提交于 2019-12-17 23:37:22
问题 I was recently tasked with maintaining a bunch of code that uses from module import * fairly heavily. This codebase has gotten big enough that import conflicts/naming ambiguity/"where the heck did this function come from, there are like eight imported modules that have one with the same name?!"ism have become more and more common. Moving forward, I've been using explicit members (i.e. import module ... module.object.function() to make the maintenance work I do more readable. But I was

Code to calculate “median of five” in C#

泄露秘密 提交于 2019-12-17 15:32:53
问题 Note: Please don't interpret this as "homework question." This is just a thing I curious to know :) The median of five is sometimes used as an exercise in algorithm design and is known to be computable using only 6 comparisons . What is the best way to implement this "median of five using 6 comparisons" in C# ? All of my attempts seem to result in awkward code :( I need nice and readable code while still using only 6 comparisons. public double medianOfFive(double a, double b, double c, double

Java try/catch performance, is it recommended to keep what is inside the try clause to a minimum?

心已入冬 提交于 2019-12-17 06:36:07
问题 Considering you have code like this: doSomething() // this method may throw a checked a exception //do some assignements calculations doAnotherThing() //this method may also throw the same type of checked exception //more calls to methods and calculations, all throwing the same kind of exceptions. Now I know, there is in fact a performance hit when constructing the exception, specifically unwinding the stack. And I have also read several articles pointing to a slight performance hit when

null coalescing operator in accessor method

橙三吉。 提交于 2019-12-13 08:59:28
问题 i was looking around in stackoverflow whether putting null coalescing operators within an accessor method has any performance implications. Before: private Uri _Url; public Uri Url { if(_Url == null) _Url = new Uri(Utilities.GenerateUri()); return _Url; } After: private Uri _Url; public Uri Url { get { return _Url = _Url ?? new Uri(Utilities.GenerateUri()); } } I'm not even sure if the syntax is correct, but when i debug, the private object is set. Before anyone ask what's the point of doing

How to include _ in a numeric value in properties file?

折月煮酒 提交于 2019-12-10 19:05:52
问题 How can I have _ (underscore) in my numerical property while injecting it with @Value annotation in Spring? If I include _ in my value, Spring throws TypeMismatchException . .properties file: min-score=20_000 java class: @Value("${min-score}") private int minScore; 回答1: Use Spring EL in your @Value annotation to replace _ characters: @Value("#{'${min-score}'.replace('_','')}") private int minScore; 来源: https://stackoverflow.com/questions/51090283/how-to-include-in-a-numeric-value-in

Is there a neat way of doing a ToList within a LINQ query using query syntax?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 12:46:25
问题 Consider the code below: StockcheckJobs = (from job in (from stockcheckItem in MDC.StockcheckItems where distinctJobs.Contains(stockcheckItem.JobId) group stockcheckItem by new { stockcheckItem.JobId, stockcheckItem.JobData.EngineerId } into jobs select jobs).ToList() let date = MJM.GetOrCreateJobData(job.Key.JobId).CompletedJob.Value orderby date descending select new StockcheckJobsModel.StockcheckJob() { JobId = job.Key.JobId, Date = date, Engineer = (EngineerModel)job.Key.EngineerId,

foreach(… in …) or .ForEach(); that is the question [duplicate]

纵饮孤独 提交于 2019-12-10 01:10:16
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: C# foreach vs functional each This is a question about coding for readability. I have an XDocument and a List<string> of the names of the elements that contain sensitive information that I need to mask (replace with underscores in this example). XDocument xDoc; List<string> propertiesToMask; This can be written in two ways, using traditional foreach loops, or using .ForEach methods with lamba syntax. foreach

Colorize negative/positive numbers (jQuery)

一笑奈何 提交于 2019-12-09 22:55:26
问题 I'd like to color numbers in a table for better readability: green for positive (+00.00); red for negative (-00.00) and; black for default case (no sign) 回答1: Here ya go: $(document).ready( function() { // the following will select all 'td' elements with class "of_number_to_be_evaluated" // if the TD element has a '-', it will assign a 'red' class, and do the same for green. $("td.of_number_to_be_evaluated:contains('-')").addClass('red'); $("td.of_number_to_be_evaluated:contains('+')")

How to self-document a callback function that is called by template library class?

六眼飞鱼酱① 提交于 2019-12-09 07:57:14
问题 I have a function User::func() (callback) that would be called by a template class ( Library<T> ). In the first iteration of development, everyone know that func() serves only for that single purpose. A few months later, most members forget what func() is for. After some heavy refactoring, the func() is sometimes deleted by some coders. At first, I didn't think this is a problem at all. However, after I re-encountered this pattern several times, I think I need some counter-measure. Question

Compare rotated lists in python

天大地大妈咪最大 提交于 2019-12-08 02:06:14
问题 I'm trying to compare two lists to determine if one is a rotation (cyclic permutation) of the other, e.g.: a = [1, 2, 3] b = [1, 2, 3] or [2, 3, 1] or [3, 1, 2] are all matches, whereas: b = [3, 2, 1] is not To do this I've got the following code: def _matching_lists(a, b): return not [i for i, j in zip(a,b) if i != j] def _compare_rotated_lists(a, b): rotations = [b[i:] + b[:i] for i in range(len(b))] matches = [i for i in range(len(rotations)) if _matching_lists(a, rotations[i])] return