coding-style

Refactoring / layout of functional Scala

非 Y 不嫁゛ 提交于 2019-12-05 13:57:01
问题 This one liner... Console.println(io.Source.fromFile("names.txt").getLines.mkString.split(",").map{x:String => x.slice(1, x.length -1)}.sortBy { x => x}.zipWithIndex.map{t =>{ (t._2 +1)*(t._1.map{_.toChar - "A"(0).toChar + 1}.sum)}}.sum); ... is my solution to Project Euler problem 22. It seems to work, and it's written in (my attempt at) functional style. This example is a bit extreme, but my question is a bit more general - how do you prefer to write/format/comment functional style code?

Should you declare enums inside or outside a class? [closed]

旧时模样 提交于 2019-12-05 13:38:46
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . Should you declare enums inside or outside a class if the said enums are only used in the class member functions? namespace nspace { /

mysql should i use apostrophe in mysql queries?

大城市里の小女人 提交于 2019-12-05 13:25:28
What is the correct way of writing a query to a MySQL database on numeric data-types: SELECT * FROM accounts WHERE id = 5; or SELECT * FROM accounts WHERE id = '5'; Mainly I prefer the last one, using ' because it is more consistent with text data-types. Does it effect the performance? Quotes are for strings, MySQL is going to read those quotes and then cast it to an integer, this is slower then just handing it an int to begin with. Honestly the performance difference is minor, but it is just like writing a program that stores numbers in strings and then casts to int when it needs to do some

x or y: acceptable idiom, or obfuscation?

纵饮孤独 提交于 2019-12-05 13:13:41
I have to extract values from a variable that may be None, with some defaults in mind. I first wrote this code: if self.maxTiles is None: maxX, maxY = 2, 2 else: maxX, maxY = self.maxTiles Then I realized I could shorten it to: maxX, maxY = self.maxTiles if self.maxTiles is not None else (2, 2) But then I realized this might be the most succinct and easily readable: maxX, maxY = self.maxTiles or (2, 2) Is the latter acceptable, or too hackish? About, specifically, self.maxTiles if self.maxTiles is not None else (2, 2) I've found that "double negatives" of the general form if not A: B else: C

proper way to construct HTML based on AJAX input

我的梦境 提交于 2019-12-05 13:12:54
So I'm developing this web application in Django. The exact web framework doesn't matter, but the point is: We have a nice separation between code, data and the actual HTML. The further we go however, the more we find we'd like to keep on a single web page and make the interface respond to user actions via AJAX requests. Now I find myself writing all these handler functions which expect a particular input from the AJAX request and construct large parts of the page by basically concatenating strings and data. Suddenly it's 1999 again and I'm manually creating HTML strings. This can't be it? So

if (condition) continue; OR if (!condition) { … }? (style preference)

﹥>﹥吖頭↗ 提交于 2019-12-05 12:52:39
I know this is a matter of style, hence the subjective tag. I have a small piece of code, with two nested conditions. I could code it in two ways, and I'd like to see how more experienced developers think it should look like. Style 1 : while (!String.IsNullOrEmpty(msg = reader.readMsg())) { RaiseMessageReceived(); if (parseMsg) { ParsedMsg parsedMsg = parser.parseMsg(msg); RaiseMessageParsed(); if (processMsg) { process(parsedMsg); RaiseMessageProcessed(); } } } Style 2: while (!String.IsNullOrEmpty(msg = reader.readMsg())) { RaiseMessageReceived(); if (!parseMsg) continue; ParsedMsg parsedMsg

Early return/golden path in Swift

こ雲淡風輕ζ 提交于 2019-12-05 12:47:17
I'm used to write code with early return/golden path in Objective-C. I tried this approach in Swift, and noticed that early return comes at the expense of using the forced unwrapping operator ( ! ) when optionals are involved. Take a method that calculates the size of a directory. First, the golden path version: private func calculateSize_GoldenPath(directory:String) -> UInt64 { let fileManager = NSFileManager.defaultManager() var error : NSError? var contents = fileManager.contentsOfDirectoryAtPath(directory, error: &error) as [String]? if contents == nil { NSLog("Failed to list directory

Better to use “and” or “in” when chaining “let” statements?

≯℡__Kan透↙ 提交于 2019-12-05 12:44:41
问题 I realize this is probably a silly question, but... If I'm chaining a bunch of let statements which do not need to know each other's values, is it better to use and or in ? For example, which of these is preferable, if any: let a = "foo" and b = "bar" and c = "baz" in (* etc. *) or let a = "foo" in let b = "bar" in let c = "baz" in (* etc. *) My intuition tells me the former ought to be "better" (by a very petty definition of "better") because it creates the minimum number of scopes necessary

Clean code - Where should @Autowired be applied?

六月ゝ 毕业季﹏ 提交于 2019-12-05 12:19:30
问题 I'll start with a simple example. You have a Spring boot application that runs a CommandLineRunner class on initialization. // MyCommandLineRunner.java public class MyCommandLineRunner implements CommandLineRunner { private final Log logger = LogFactory.getLog(getClass()); @Autowired //IntelliJ Warning private DataSource ds; @Override public void run(String... args) throws Exception { logger.info("DataSource: " + ds.toString()); } } // Application.java @SpringBootApplication public class

What is the perfect way to write “return” in a method

风格不统一 提交于 2019-12-05 12:18:30
I don't like methods have several return lines. So I created a return value with string result - and in every condition I write result = something... But when I write "try-catch" mechanism, I have to set public string result . Because, if I return a result in try, compiler will launch error, and says not all codes have return value. If I write result = string.Empty to end of the method, resharper says, it's not reachable code. So, here an example, and here is my question; "What is the perfect way to write "return" in a method ?" public static string PingThatAddress(string hostAddress) { try {