Tag Info

Hot answers tagged

128

"Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live." (Took it from here) That said: there is no level of »excessive« for code reviews. I'm a newbie programmer working internships and learning a lot from experienced programmers What is more important than writing code is reading code ; read ...


70

Let me quote from Page xxii (Foreword) of "Clean Code" by Robert C. Martin from Prentice Hall. Back in my days working in the Bell Labs Software Production Research organization (Production, indeed!) we had some back-of-the-envelope findings that suggested that consistent indentation style was one of the most statistically significant indicators ...


46

By way of explanation, I'm going to borrow some code from this excellent blog post about closures. It's JavaScript, but that's the language most blog posts that talk about closures use, because closures are so important in JavaScript. Let's say you wanted to render an array as an HTML table. You could do it like this: function renderArrayAsHtmlTable ...


27

I am very much like your mentor. I treat all code reviews for developers of any experience the same way, even if I wrote the unchanged code I'm complaining about myself. It's no reflection on you or your abilities. Often, the problems with a design are not apparent until you try to modify it. Alternately, a design might have been perfect before the ...


25

In C, a function with an empty parameter list () can take anything for its arguments. Literally anything. This is usually used to implement a function which can take a variable number of arguments, though these days it's considered preferable to use the more explicit ellipsis syntax (...) for these functions. In C, a function with the parameter list (void) ...


21

To tell you a little about myself, I'm a newbie programmer working internships and learning a lot from experienced programmers [...] This sentence it seems to me is the key to the answer. I manage teams of programmers. When I do code review with programmers in general (of good and not so good levels of skill) I will get them to write code to meet or ...


19

The purpose of closures is simply to preserve state; hence the name closure - it closes over state. For the ease of further explanation, I'll use Javascript. Typically you have a function function sayHello(){ var txt="Hello"; return txt; } where the scope of the variable(s) is bound to this function. So after execution the variable txt goes out ...


15

First of all, there is nothing that is impossible without using closures. You can always replace a closure by an object implementing a specific interface. It's only a matter of brevity and reduced coupling. Second, keep in mind that closures are often used inappropriately, where a simple function reference or other construct would be more clear. You ...


14

There are two main use cases for closures: Asynchrony. Let's say you want to perform a task that will take a while, and then do something when it's done. You can either make your code wait for it to be done, which blocks further execution and can make your program unresponsive, or call your task asynchronously and say "begin this long task in the ...


14

First things first - there is very rarely a single "correct" way to do something in code. So your creativity always matters. That said, there are a whole lot of bad ways to do something in code. Ways that will cause you (or others on your team) issues in the future. Sometimes immediately. So code quality is very important, since poor code quality tends to ...


13

A couple of other examples: Sorting Most sort functions operate by comparing pairs of objects. Some comparison technique is needed. Restricting the comparison to a specific operator means a rather inflexible sort. A much better approach is to receive a comparison function as an argument to the sort function. Sometimes a stateless comparison function works ...


9

Your mentor may be an excellent coder, but... Does he have the authority to review and change all the code? Is he potentially imposing stylistic changes on functional code rather than fixing bugs/preventing dangerous behavior? Are the changes being made backed up by unit tests and/or functional tests? Was there significant unit test coverage before the ...


9

Neither foolish nor crafty I believe: MATLAB software is used for very serious engineering, science, finance, and many other fields. This being the case, MathWorks (the company that develops Matlab) are very careful not to publicly document things that they are not 100% certain will remain as-is in future Matlab versions. There are numerous examples of such ...


8

There are two issues here: The issue of your mentor disliking your solution is hard to qualify without concise examples. Maybe if you posted your code, you would find that everybody agrees with your mentor and that you are using the wrong approach (do not worry about it, the fact that mentors exist is the prove that most people need to get some actual ...


7

Closures are equivalent to objects implementing a run() method, and inversely, objects can be emulated with closures. The advantage of closures is that they can be used easily anywhere you expect a function: a.k.a. higher-order functions, simple callbacks (or Strategy Pattern). You don't need to define an interface/class to build ad-hoc closures. The ...


7

He doesn't mean little IDE utilities that create boilerplate for you, which you must modify. He's referring to more comprehensive code generation that you shouldn't have to touch. You make changes to the higher level and regenerate. The canonical Unix example would be Yacc, which uses a high-level grammar to generate complex parsing code. Other examples: ...


6

for the first time feeling like I get close to being competent. I've been programming for over twenty years, and every year I wind up saying that to myself at least once or twice. anything takes a whole lot of time because I have to find the best way to do it or else its a waste of time, it also feels like my creativity doesnt matter because there ...


6

"Done is better than perfect" I hate to break it to you, but code can always be made better, code is never perfect. If you think your code is perfect, you probably haven't looked at it enough yet, or you're no real engineer, as a real engineer can always make things better than they are. (Or, in the programmers lingo, a real hacker can always make things ...


6

Nothing that hasn't been said already, but maybe a simpler example. Here's a JavaScript example using timeouts: // Example function that logs something to the browser's console after a given delay function delayedLog(message, delay) { // this function will be called when the timer runs out var fire = function () { console.log(message); // closure ...


6

As can already be seen in the question, using such non-printable characters directly in a string-literal makes the code essentially unreadable. At first glance, you would say that the presented code should fail to compile because there are multiple identical case labels. Most languages (and that includes C#) support escape sequences to deal with troublesome ...


5

As others have said, this isn't possible, but there are a couple of easy constructs you can create yourself that achieve similar aims. Maybe<T> public struct Maybe<T> where T : class { public bool HasValue { get; private set; } private readonly T _value; public T Value { get { if(!HasValue){throw new ...


5

There is a common and dangerous myth that types like uint32_t save programmers from having to worry about the size of int. While it would be helpful if the Standards Committee were to define a means of declaring integers with machine-independent semantics, unsigned types like uint32_t have semantics which are too loose to allow code to be written in a ...


4

the easiest way to refactor that is to use a success variable, eg res = do_stuff(); if (res) res = do_more_stuff(); if (res) .... If you use a counter instead of a boolean success variable, then you can also tell if all of the steps succeeded at the end (ie. the counter will equal the number of steps that successfully ran) The one thing you ...


3

You could do the comparison of the bytes by storing the strings as constants, with decent names. So case "\0\0": would become case STATUS_CRYPTO_1: which helps remove the 'magic number' aspect of your switch statement. But the correct solution here is to add some comments. That's what comments were designed for, that's their intended purpose. Some ...


3

1.Just put all internal prototypes, typedefs, defines etc. to the top of the .c-file. I have done this in the past, but I do not like it. It does not seem consistent to pollute the .c-file with header-material. Function prototypes are not header-material. Public API function prototypes are header-material. If you had a single compilation unit, you ...


3

Nullable<T> has a constraint which won't let you use it with classes, but only structs. If T wasn't constrained, using Nullable<T> for classes would be a mistake anyway. Your intent is to simplify your code, but you're actually making it harder. Classes are nullable by their nature. By telling that the nullable entity is nullable, you're not ...


3

PHP can be used to help to show a real example in a different language. protected function registerRoutes($dic) { $router = $dic['router']; $router->map(['GET','OPTIONS'],'/api/users',function($request,$response) use ($dic) { $controller = $dic['user_api_controller']; return $controller->findAllAction($request,$response); ...


3

Placing all of the compile time switches into 1 central file makes finding and changing them easy. That said, compile time switches can be confusing, particularly if there is a significant quantity of them. I worked on an embedded project where there were a few different boards to control different hardware, but had the same MCU, communication protocols, ...


3

Maintainability trumps all. Unless you have a pressing reason not to, follow the conventions as they appear in the reference material. In this case, the first Google hit is the documentation for validations at rubyonrails.org, which shows a strong preference to the validates form. The only counter example I could find was validates_associated, which makes ...


3

I would say yes if that type has a meaning more than just the simple value. In your example, Speed and Mass might be represented by a double, but they have different meanings, and they have different calculations that can be done on them. I can tell you from personal experience that doing this (it's called micro domain btw), has made my code much more ...



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