2

I've had a dispute with some colleges on should I take a syntax check after every code modification as a distinct step or I'd better rely on unit tests to catch possible syntax breakage ... ( of course unit tests do that ).

This is not a question whether to use or not tests for your code ( of course yes, but see below )

My use case is pretty simple, I am newbie in Python and try to play with some web application (MVC) by modifying model file. The changes are quite simple, indeed a couple or so lines at once, however as I am pretty unfamiliar with Python syntax, some stupid errors ( like wrong indentation, so on ) easily creep into.

The real hurdle is that I deploy an application via Google Cloud Platform ( as app engine ), so it takes a while for application comes up with new version and I can hit an endpoint and finally look into its error logs to understand what's happening. So I'd like to get a shorter and simple way - at least for these types of errors - just running syntax check before deploy.

The central part of the dispute was that I hadn't been advised to take a syntax check explicitly, but better relying on higher level tests for this ( units tests for example ), on what I say that:

1) And don't have unit tests yet ( and even if I had them I'd treat them as a next step in my test chain ).

2) I only edit a certain file ( one and only one ), with a minimum modifications, and MOSTLY the types of errors I have ( as I told as I am newbie in Python ) is syntax errors, not logic or semantic ones, so as I assume I am safe to have just syntax checking and then give it a deploy. And even though I hit a semantic/logic problems with my code later ( even syntax check pass ) this is a undoubtedly question for high level tests ( unit / acceptance so on) - but for now I'm just playing with a code written in a language I am not feel quite at home.

3) I don't commit my changes outside and don't trouble anyone.

4) the last argument is controversial, because it's a bit of the scope, however I'd like to leave it here - in some cases even lightweight unit tests might be overkill if ALL you need is to get a syntax check in a single file. Consider this example. One just edits the code in place, deployed at server trying to fix immediate issues, so no unit or other tests exists and you want to be sure that your modification won't break the code syntactically.

3 Answers 3

3

If syntax problems are causing you problems and the costs and delay to find those problems are annoying enough, check the syntax first. This isn't just about syntax checks. Anything that causes an annoying enough problem deserves some sort of mitigation. Computers are really handy for boring, repetitive tasks. I wouldn't not do this because some language community thought solving problems was stupid or weird (though every community seems to have some version of it except for the Smalltalkers ;)

Other people might think it's bad practice because their cost of doing it explicitly doesn't offset the cost of finding out later. I rarely do a syntax check explicitly but then I have unit tests and run those frequently.

Perhaps your solution is to automate your deployment and throw a syntax check in there. If that fails the deployment stops. You don't have an additional step because it's already baked into the step you're doing now.

0

Personally speaking, if you drill into your head the PEP8 than most of the time syntax will come naturally i.e. like touch typing. However, most of you know multiple languages so I can imagine it get hard remembering all :)

1
-1

By default, Python completes a syntax check on the functions within your code. Additionally, this syntax check will look for any open brackets or unfinished lines of code to make sure the code will work properly. However, the syntax checker does not always highlight the problem directly. To fix this you will just have to look at the code yourself.

If you need to spell check things like variable names or strings I would advise copying the code into a spell checking program like Microsoft Word or Grammarly.

I hope this helps.

5
  • Sure, the question is not about HOW to run syntax checks ( there are a tons of ways to achieve this ), rather then it's about methodology context. I'd been advised that EXPLICIT run of syntax check for my code ( for example via python -m -B /path/to/file) is BAD/weird practice. which I don't agree with ... Nov 13, 2017 at 11:04
  • To be accurate I had been told to rely on unit tests for my code to get the job done. But see my concerns at the post. Nov 13, 2017 at 11:07
  • If the code you use works to complete the job and there is not a specific company way of doing it, use the way you are most comfortable with. However, if they have specified for you to do these unit tests you should carry them out.
    – M.Ellidge
    Nov 13, 2017 at 11:27
  • There is no requirements. I am just a python newbie trying to play with a simple python web app get deployed by Google app engine. Well I've found a code snippet to add authentication logic ( 4 lines of well understandable code, not a big deal ), so I am modifying a controller file via simple copy/paste and for some reasons adds indentation errors. It's ok. I don't want to wait a minute or so to see this indention errors in application log file after deploy via app engine, all I need fast and simple way to check this right here, before deploy. Why I need a superfluous infrastructure for this Nov 13, 2017 at 11:35
  • Since you do not have any constraints/requirements on how you carry out these syntax checks, you can do the syntax checks however you want. A simple way to check for any errors is to run it in Python before uploading to an app engine.
    – M.Ellidge
    Nov 13, 2017 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.