Tag Info

Hot answers tagged

42

Rule of thumb: variables should always be in - or as close as possible to - the scope where they are needed. Another way to phrase it is that variables should be enclosed inside the context in which they make sense and are actually useful. Most often you will want to declare your incrementing variable along with the for statement. Sometimes you will declare ...


12

If getNumberFromMordor and makeSomeOperation are public, they should be tested by themselves, so your unit test should not doublecheck that getNumberFromMordor works correctly. Only test what SomeNonatomicMethod does. Don't re-test public methods it relies on. If getNumberFromMordor is private, you should test SomeNonatomicMethod against the possibility ...


11

Use UTF-8. string.size() won't equal the amount of code points, but that is mostly a useless metric anyway. In almost all cases, you should either worry about the number of user-perceived characters/glyphs (and for that, UTF-32 fails just as badly), or about the number of bytes of storage used (for this, UTF-32 is offers no advantage and uses more bytes to ...


4

Xml Parsers are not something you should be building yourself, unless you want to for learning purposes or to make something specific for your needs. XML parsers are complicated enough that other folks already know how to write one better than you or I could (within a reasonable time frame). One of the reasons for using XML is that libraries to read and ...


4

It depends. If your Account class shall be mapped to a relational database, then its not just a good idea, but proven practice, to use technical IDs for every table as PKs (and FKs, referencing those PKs). To my experience, separating the technical PK of all tables from the "domain keys" (like the "bank account number") works very well and helps you to avoid ...


4

I think the answer lies in between the two of you. Changes are to be expected as software evolves. No matter how competent you are, you can't predict the future with 100% accuracy. And even if you could, it may not match your customer's predictions or taste. Since he's the one who pays he (most unfortunately :) ) has a say in it. So unless it's dead simple, ...


4

It is perfectly fine to reuse an object like this as long as it is written correctly for that purpose. Invoking behavior on the object should not be affected by any previous calls to the object. Consider the following code: class Example { private int x = 1; public int multiply(int y) { return x *= y; } } Calling the multiply() function alters ...


4

They are not equivalent in their execution order. foo("hi", bar("bye", function() { // do something cool })); This must first evaluate bar("bye", function() { … }), then calls foo("hi", …) with the result of that invocation. As the return value of bar is undefined, you'll get an error when you call that as a function. If you want the callback example ...


3

It's atypical. It's verbose. It can make methods more difficult to find by name. Inconsistency is likely to crawl in. It is redundant, because the distinction is already sort of built-in in OOP. When dealing with classes, "a" is implicit and when dealing with particular instances, we mean "the". We don't need to resort to natural language grammar to help ...


3

This has the potential to end up as a bike shed argument. Nevertheless, what I found are the biggest discrepancies between academia and "the real world" are: Don't expect to make a good living only by knowing the tools. Learn a domain instead, and apply programming to that. To quote this website: People who can code in the world of technology ...


3

My personal preference is to allow multiple returns at the very beginning and the very end, but not in the middle (where they could be easily missed). Of course, one might argue that if a subroutine is so long that it has a "beginning", "middle" and "end", it is too long anyway. I am specifically fond of using the trailing Perl-style conditionals as ...


2

I've seen this pattern many times in various C-syntax languages such as C, C++, Java, C#, and even JavaScript. The extra asterisks at the beginning of each line have no syntactic significance. They are there purely to aid in readability by catching your eye and making obvious that you are looking at a multi-line comment. The use of this template (comment ...


2

After watching this amazing talk "Ian Cooper: TDD, where did it all go wrong", I'm going to disagree with @pdr. I think you should only keep the original tests. Your ability to refactor your system under test without breaking, writing or changing any tests is the whole purpose of writing tests in the first place. If I were to test the extracted class, ...


2

1: Some wording: We don't instantiate objects at all, we instantiate classes, and the product of a class instantiation is an object (also called an instance). Of course we can instantiate a class as many times as required to create a new and different object with each instantiation. 2: Whatever, you cannot have the code you suggest: Test test = new Test(); ...


2

One of the principles of layered structure is that every layer call only the layer below and receive callbacks. So if the reporting layer does not provide access to this data, it is fair to assume that the data is not accessible from any other layer above. I don't think adding globals is a good idea. It makes the architecture inconsistent and adds very ...


2

TDD is not design - it is a design process. A main artifact a TDD process eventually give you is the unit test suites, which should (to some degree) attest to the actual code's scope and capabilities. Good test suites give some assurances that the code works, and added security that future code changes can be done without breaking past code, as failing ...


2

It's entirely possible to build libraries using bare code. In fact, I'll go out on a limb and say that it's not uncommon to do so. However... The history of software is laying abstractions on top of older abstractions. Once you've figured out how to do a thing, it is customary to wrap that thing in a function, module or library and call it, rather than ...


2

An example of iteration being more useful than recursion: in a paper by Dijkstra (the relevant part is the first two out of the last three paragraphs), a graph theory problem is discussed. Somebody had found a recursive solution and published it; and it seemed very impressive and hard to understand. Dijkstra had been messing around with a mini-language ...


2

I often use recursion when I can't achieve immutability via iteration. There is something to be said about trying to avoid immutability all the time- it's not always the correct approach, but sometimes it can be. Let's take a trivial example that doesn't show the efficacy of recursion to erase mutability: assume that I want to program Conway's Game of ...


2

Embrace refactoring. Embrace the fact that you can create code that works, and you have tests that make it work, and then you can change the internals having confidence that you are not breaking anything. And as you mention, KISS. About the view of not having written/designed correctly from the start, for me the answer is that I have not worked in a project ...


2

It depends primarily of what platforms you're aiming at. If you target only PC platforms (which none of is very exotic), then you have many choices regarding language (C++, Java, Python, Go, etc.) and cross-platform GUI framework (Qt, GTK, SWT, Swing, wxWidgets, etc etc). Portability shouldn't be main concern when choosing a language for PC-only program ...


2

One could try to apply this to programming languages. There is something to be said for the dynamic nature of languages. For example, while I've been using C# since 2002, there is something to be said for what new features have been introduced, what has been deprecated so that it isn't always the same language. While there is the similarity of some English ...


1

Each OS uses its own file formats for executable files and shared libraries and each OS also has its own API for applications to interact with it. This means that generally, binaries built for OS A can not be used on OS B. There are two ways to write applications that can be used on multiple platforms: You use a virtual machine (VM) that hides all/most of ...


1

The first thing I can suggest you is to take out the Logic from Mark-up or basically avoid to write in-line logic in your mark-up. So you can re-write your first example like this: if ( in_array('name', $error) ) { $class = 'form-error'; } else { $class = null; } // or ... $class = in_array( 'name', $error ) ? 'form-error' : null; if ( isset($name) ...


1

It all come down to who writes better code relative to the amount of time. Your company/team should define what "better" means. Customers have something to say about this as well. Make sure you have a solid definition of Refactoring. It sounds like you do, but you mention it along with code behavioral changes very close together in your question. Be ...


1

I don't think that id is a database artefact. How do you know that account you are working with is correct? You know it's a correct account because it probably has a unique identifier. In your case the unique identifier is called "Id", so naturally you think it's a database artefact. Because of this I think that it's perfectly fine to have Id property in ...


1

Before deciding, lets look at the similarities and differences. Iteration is suited to problems where there are a fixed number of partial results. An example is to sum the elements of an array; we only need one partial result - the sum calculated so far. Implementation: In iteration we have a loop. Think of it as having 4 parts: A decision to continue ...


1

Recursion is in many cases much simpler and much more easier to understand than iteration. Often you can solve problem that normally would take ~50 lines of code in just 10 lines by using recursion. Of corse every problem that can be solved with recursion can also be solved with iteration and you can get some better performance by that, but in many cases ...


1

I would say that libraries should only depend on the libraries it absolutely needs to get the job done. They shouldn't need to depend on libraries that unrelated parts of the system depend upon. Sometimes you will see that LibraryA depends on LibraryB and LibraryC. Then if LibraryZ needs to depend on LibraryA, then it also has to depend on LibraryB and C. ...


1

The idea behind that advice is that dependency management sucks on many platforms (keyword: “DLL hell”, although the problem exists in various degrees for all platforms and all languages). This is not just a problem of having compatible libraries for dynamic linking, but also for fetching the correct version of every dependency before compilation. If these ...



Only top voted, non community-wiki answers of a minimum length are eligible