New answers tagged

1

Based on your sparse description the two errors can be internally identical with the difference being made by the locus of catching. To be more concrete: def write_photo(photo, path): try: open(path, 'w') # can throw OSError ... except OSError: logging.error("cannot write path", path) raise def save_kitten(kitten): ...


0

I almost never use the collection literals for empty collections in my Python code and prefer the named list, set, dict and tuple constructors instead. The reason for this is that the literals can be quite confusing. For example, {} is not the empty set but an empty dictionary which is contrary to what anybody with a background in mathematics would ...


2

The difference between my_list = list() and my_list = [] is that list requires a namespace lookup, first in the module level globals, then in the builtins. On the other hand, [] is a list literal and is parsed as creating a new list from the language, which doesn't require any name lookups. So the literal is faster on object creation. Otherwise, ...


-1

It is not necessary to always start with 'is' or 'has'. In some situations, other words may be applicable, but should still have a clear indication of what True / False responses mean. e.g. canMarry - True if over the age of consent and either (is not married or in a culture that allows polygamy) wasMarried - False if never married or (is divorced / ...


5

You could create your custom method for extracting Map values as an Either instance and use them in a for comprehension: implicit final class MapOps[A, B](self: Map[A, B]) { def getAsEither(key: A): Either[String, B] = { self.get(key) match { case Some(value) => Right(value) case _ => Left(s"Key not found: $key") } } } case ...


0

This dfs algorithm looks for a cycle in the graph G starting at v. The "extra parameter" u is the "previous" or "parent" node of v in the search tree, it is passed here because it enables the algorithm to avoid to take a sequence like "u - v - u" for a cycle. Understanding code is sometimes not easy, it takes practice, especially recursive code. There are ...


1

Wouldn’t it make sense to have an interface (Java speaking) OrderedSet and the classes BinaryTree and RedBlackTree implement that interface? If you are implementing a library and introduce the interface later, this will require a new major version. In this new major version, it could also be possible to rename the class OrderedSet to BinaryTree and ...


1

Addressing the question from a different perspective, I'd say that providing a domain-oriented abstraction may be the best answer. Rather than describing the properties (abstract or concrete) of the implementation, define an abstraction that is directly domain-relevant to its users, and limited to the specific capabilities they need. This means you provide ...


5

Something meaningful? Nesting lots of meaningless loop variables quickly becomes unreadable. Imagine seeing something like this embedded inside four levels of loops: do_stuff(i,j,k+4, l*i) and trying to understand what it does vs something like: do_stuff(row, col, height + 4, function_value * row) This is a simple example. A lot of this sort of logic ...


4

If there are four or more dimensions, then they represent something more than just cartesian (or other) co-ordinates, so just use a (short) word that signifies what each level actually is.


-1

Actual Solution (not just philosophy) Allow code comments to override linting, and compile lists of those comments if you like (as is done with todo comments often) Give your developers the ability to work outside the convention explicitly and with reason - and during code reviews by humans it can be scrutinized as needed.


-1

Best seems to use the automated code formatter that is a built-in feature of almost any descent IDE and should exist for almost any more or less widespread programming language. This quietly eliminates lots of unnecessary work and lots of friction during code reviews. It is fully reasonable to require from all developers to develop a habit to apply the ...


5

Putting the default statement first will work technically, but is a very unusual idiom. IMHO that's because what you scetched above is not the typical "use case" for a switch/default block. It is much more common to use the default branch similar to an else statement, like switch (value) { case 1: potential_case1() case 2: potential_case2() case 3: ...


2

Based on your edits, your aiming for the right goal. There are many benefits to using a style guide, but the two most important in my opinion, are readability of code between team members, and lack of "silly" commits (like white space only, or extra lines and the like). To achieve your goal, your chosen (or created) style guide should be simple and easy ...


1

I've grappled with code style guidelines for years, as many others have on this forum. This includes both fighting style guides I find abhorred, and trying to encourage others to use style guides to rein in their style so it can be more readable as a whole. The corporation benefits from a common coding standard. There are many important things to consider ...


1

It doesn't matter too much what coding style people have, as long as they have a coding style. If a company insists on a coding style, they step heavily on the toes of about 49% of their developers. The problem is that a large number of developers don't mind much adapting their coding style to some prevalent standard in a company, but they mind very much ...


9

As far as I can tell, the statement that confused you is a pragmatic compromise made in order for the guidelines to serve as wide an audience as possible. Depending on your specific context (more on that below) you may have an option to adjust it and make more efficient use of the guidelines. You see, guidelines refer to "strong personal objections" as a ...


6

Is coding style in organizations an optional thing? Organizations opt to have coding styles - there is no requirement to have the in the first place. So the quote you are reading addresses the biggest issue that I see regarding coding "style" and real superstar "hackers" - you bring a new guy on board and he writes code that will drop zombies and makes ...


0

Your question centers around reconciling the two quotes. I think what treecoder wrote answers your question generally. It represents your guiding principle in writing style guidelines. More specifically, since you are responsible for setting the coding style guidelines (this is my assumption since you are the document writer), you get to decide what is ...


2

This more of a psychological ploy instead of some literal interpretation of how best to manage coding styles. It makes the team/company/manager/leader less authoritarian. I would focus more on situational exceptions instead of personal. Regardless of the coding document, the goal is to make things easy to read. Confusing code should be addressed and altered ...


2

So, am I misunderstanding something from this document and the quote at the top of this question? Can people really just ignore coding style? It depends. The place I'm at currently has no style guide, and it's not a big deal. There are some slight variations between programmers, but not so much as to impact readability, discoverability, or consistency ...


52

Allowing people to ignore coding styles because of personal preference is a bad idea. The quote in your question seems to allow any developer to simply say "I'm not going to use this style because I don't like it." This goes against the whole point, which is getting everyone on the team to do things the same way for consistency and readability. I agree ...


0

I think the mood music here is that if the accepted wisdom is that the coding standards are incorrect or incomplete, then it is the lesser of two evils to press on if the developers are in general agreement rather than go through the arduous process of getting the document changed and re-reviewed. It should also be noted that the code standard enforcement ...


13

Coding style, and style guides exist to help make code more readable. And readability exists to help with complexity. Hence ultimately whether or not to violate (I'd call it adapt) the coding style to match your organizational needs comes down to how much it helps make things understandable. Remember, everything, and I stress, EVERYTHING, from OO ...


1

You have two options. Create seperate mywebsite.api and a mywebsite.app projects in your solution. Advantages Proper seperation of concerns. You can delploy updates to your api and your front end independently. Architecture of sites can be changed independently (i.e. you can update your api to run on asp.net 5 without affecting the website) Cleaner ...


2

Not much of coding in Python and its long time since I coded in C. For C programs, I used to prefer top-down, because that is how I design my programs. If a calls b, b is listed after a. I also used to have a header for each .c file that lists all functions in it and included in the .c file. I follow the same model in Java class methods now.


-2

The point of writing a method is to do one thing in an obviously correct way. This principle is usually applied to explain why over-long methods are bad; a method should fit on your monitor in one piece. But it also cuts the other way: a method should not be so small that several fit on your monitor at the same time! This means that it is quite irrelevant ...


1

Wappalyser will not be able to see the backend code running on a remote server. If you are running your Django site locally, then it will be able to inspect and report on everything. or to put it another way, you do not have access to Youtube's backend server so you cannot tell anything about it or the technology it uses. You do have full access to your ...


2

Refactor the big function. It probably does several things repeatedly or nearly so, if it loads several "prefabs". Then, the logic needed for repetition can be put into other functions, which could open more possibilities for refactoring (one or more new classes for loading "prefab" types, maybe?). Moving the code to a partial class will only obscure the ...


-1

Use which ever convention you prefer and live with the consequences. Most readers of your code will know how to interpret "is" and "has" prefixes. For this reason I recommend using the convention. It is helpful to others. In addition, consider what "makes sense grammatically" actually means in a coding context. Don't confuse the grammar of your spoken ...


2

It is conventional in recursive functions to write the base case first. That means your first option. Your second option is testing for the inverse of the base case, which is confusing to read. Writing the base case first makes little difference in this particular case, but in the general case if you were to use pattern matching, or a switch statement, or ...


5

In terms of speed of execution they are the same. The compiled/interpreted code is going to do a test if nodetype == 3. If yes it executes a return statement next. If no it executes the other return statement next. Both cases are going to execute a return statement next. In terms of stack usage, they are the same. In both examples shown, it only drops ...


6

If your class is too big and unwieldy when it's all in one place and you can easily see it all, what makes you think scattering it across multiple files and making it harder to correlate will be an improvement? Partial classes are essentially a hack to make it easier for generated code, such as that produced by the form designer, to work together with ...



Top 50 recent answers are included