Tag Info

Hot answers tagged

7

Object.create is defined in newer versions of browsers (can't say exactly since when). You can see its description on the Mozilla developer network. This is just a polyfill (quite similar to the one on that page, with a few less checks) to be able to use the same function on older browsers. On newer browser it is already defined as a function and will thus ...


7

Yes, so the function can change thing. In general, it is best to put the behavior on the object itself. In other words, prefer this: class Thing { public: void do_stuff() { ... } }; ...to this: void do_stuff( Thing & thing ) { ... } However, both examples are perfectly valid and usable. One good example of a non-member non-const ...


5

There are reasons why you might want to change things in place. One reason might be performance: it's expensive to make all those new objects for output. There are reasons why you might write things in a more functional, immutable style. One reason is referential transparency. Another is easier and more reliable concurrency and function composition. ...


5

As far as I can see, when you write: I made some changes which affected the expected array element position The advantage that you're focusing on is that in Method #1, the start position is only found in one place (the initial value of i) whereas in Method #2, that information is implicitly contained in all four array indexes. Seen this way, the ...


5

Your first method has 4 side effects (the pre-increments) while the second has none. In general you should avoid side-effects if you can. Furthermore, the second one is more clear about your intent. If I would see the first one in someone else's code I would ask myself if there was any reason for performing the pre-increments. So, stick with the second ...


4

Does it really help to have a very specific rule set? No. Quite frankly, no. Or, to put it more constructively - take a look at any source code off github or sourceforge, can you read tit? If the answer is "yes" then you've just demonstrated that a single style is not important for readability. What does help readability is readable code, which may ...


4

Use operator overloading if it improves code clarity and maintainability. Sometimes it helps clarity and maintainability: a + b is shorter and clearer than a.addTo(b), it makes it easier to write generic algorithms (e.g., templates) and change data types, etc. Sometimes it hurts clarity and maintainability: overloaded operators can be surprising ...


4

Names should be expressive in direct proportion to their scope. A loop variable should have a really small scope, so it's perfectly alright to just call it s. If this makes the code unreadable, you should refactor the loop code into a method, not find a longer variable name.


4

I'd say if your Java devs regularly program in C# and vice versa, they might have some argument for making them look the same (slightly easier to read). But even then, differences can be useful enough to trump reading ease. I for one like to use same-line-open-brackets in JavaScript to remind my brain at all times that the code I'm looking at is not C#. ...


3

There is a case for different styles for each language - it helps you remember that you're writing something different, and thus will help to prevent some subtle errors due to language similarities (ie where you are still thinking Java techniques when writing C# code and vice-versa). It can slow you down a little when switching, but frankly - this is a ...


3

The First method is horrible on my vision and it does not prevent "Exception". The same feelings I have when I see "magic numbers"! XD If possible, you need to change the architecture, something is wrong! Or You need to check it before to use it in the method! Or be right that those params always come with their values filled and in the same order! Did You ...


2

I've had this exact situation come up with a co-worker and when reviewing code from contractors -- the code should always convey your intent and expectations as clearly as possible given the constraints of performance and size. Performance with SQL relies upon the query optimizer -- while it probably won't make any difference, if it does, more likely than ...


2

I think its fairly clear that a consistent programming style can help some things especially if you are publishing your code. Prevents unusual style Limited code difference between versions on check in to source control Consistent style across large code base eases understanding However, I would say its definitely an expensive thing to have. You have ...


2

Unfortunately, I don't think there is any way to achieve your goal. Your workplace has made Java the first-class language and culture, and made C# second-class. Trying to raise this issue will possibly make you an unfavorable person among the coworkers. That particular coding style may also have the backing of some senior staff in the company. Given that ...


2

Yes, some times it is very convenient to be able to locally modify a pass-by-value argument to a function. For example, the argument might be a reference to a node of a linked list, and within the function you may want to traverse the list, so you will want to be doing node = *(node.next);. So, ill-advised? definitely not, in my opinion. There exist ...



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