Tag Info

Hot answers tagged

26

Loggers are what we call a "cross-cutting concern." They yield to techniques such as Aspect-Oriented Programming; if you have a way to decorate your classes with an attribute or perform some code weaving, then that is a good way to get logging capabilities while keeping your objects and parameter lists "pure." The only reason you might want to pass in a ...


7

We write code for other humans to read. New technologies are intended to make code easier to read, not harder. There are good and bad ways to use new technologies. If you're trying to write "impressive" code, you're doing it wrong. I suspect it's not just the use of the technologies that is concerning people. If the way you're using LINQ made a huge ...


7

In languages with function overloading, I'd argue that the more likely an argument is to be optional, the further right it should be. This creates consistency when you create overloads where they're missing: foo(mandatory); foo(mandatory, optional); foo(mandatory, optional, evenMoreOptional); In functional languages the reverse is more useful - the more ...


6

The cleanup could be in an outer function, and then return can be used instead of goto: void main_func() { /* Set-up goes here */ handle x = ...; handle y = ...; void result = inner_func(x, y); /* Clean-up goes here */ CloseHandle(x); CloseHandle(y); } void inner_func(x, y) { if (condition1) return; if (condition2) ...


6

Use a linter or some program or tool that automatically enforces style. That should alleviate the objections about it taking too much time. Most of the objections have to do with "I'm uncomfortable with that style." It doesn't matter. Coding conventions are all about uniformity. Enforcing that uniformity will make code reviews easier by eliminating ...


6

Three things come into my mind. First: 'simple' is not 'stupid'. Simplicity is the predecessor of elegance. Second: in your job, the most important thing to do is to be is a team player. Your boss is a team player. Is job is to keep the team together, and that means making your code understandable to your team mates and by that making you a part of the ...


5

I like to use present tense when the error/exception-generating code is the "root cause" of the problem, and past tense when I'm merely repackaging an error/exception from a lower layer of code. For example, when our frontend makes a request to our backend, the backend may return an error message "Cannot connect to database", and the frontend will throw ...


4

This is a difficult situation. First, do not get into a negative relationship with your boss, or with your co-workers. Above all, be positive, recognizing their valuable traits, and being helpful. Then, see what you can do to expose your co-workers to newer techniques. If necessary, include explanations in your code, so people can follow you. Appeal to ...


4

You could try saying what you mean: if (!condition1 && !condition2 && !condition3 ) { // do stuff only if all checks passed } CloseHandle(x); CloseHandle(y); // etc. As various commenters have pointed out this is only readable/maintaibable if the condition tests are fairly simple where complex conditions are involved something like this ...


3

You can definitely spend a lot of time over-engineering this problem. For languages with canonical logging implementations, just instantiate the canonical logger directly in every class. For languages without a canonical implementation, try to find a logging facade framework and stick to it. slf4j is a good choice in Java. Personally I'd rather stick to a ...


2

There are two options that one has for dealing with specific, frequently occurring immutable objects. You can either instantiate them each time, or you can use a constant that is probably stuck in a final static in some class. BigInteger for example has ZERO, ONE, and TEN. However, this is an attempt to avoid instantation of new objects. Say you're ...


2

Loggers are a bit of a special case because they have to be available literally everywhere. If you've decided that you want to pass a logger into every class' constructor, then you should definitely set a consistent convention for how you do that (eg, always the first parameter, always passed by reference, the constructor initialization list always starts ...


2

T stands for Type. just like the commonly used letter I is used for Interfaces and C for Classes. In some languages, its not used as a prefix (eg wchar_t for a C wide-character type) but the letter is still the same.


2

In C a typical way to simplify error checking and avoid deep nested if is: do { if (condition1) break; /* 1. do something... */ if (condition2) break; /* 2. do something else... */ if (condition3) break; /* 3. do something else... */ } while(0); /* Cleanup */ There are various opinion on this "idiom" (e.g. take a look at Do you consider ...


1

I agree with those suggesting that the logger should be statically accessed rather than passed into classes. However if there is a strong reason you want to pass it in (perhaps different instances want to log to different locations or something) then I would suggest you do not pass it using the constructor but rather make a separate call to do so, e.g. ...


1

It seems to me that you were given legitimate feedback from your manager. Not taking anything from it would be a lost opportunity for you. Whether or not your way is right or not your colleagues are struggling. If they're struggling the company will struggle and you will struggle providing tons of support when they're trying to debug, extend or otherwise ...


1

The answer to this is simple. Consistency is of paramount importance. but it comes with a caveat... You and you coworker are likely obsessing over the wrong sort of consistency Implementations are disposable. They can be completely overhauled with varying degrees of ease depending on the quality and comprehensiveness of the test suite. Worrying about ...


1

You're right to wonder why this symbol doesn't seem to mean anything to the rest of the file, but its spelling isn't its only attribute. Consider its positioning. The confusion lies in trying to equate the parameter name and the argument name. You are right, there is no connection between the names as such. But if you saw a Python error message due to a ...



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